7. Preferences
Fnd_Pref stores named values for you and for Foundation itself: window positions, "open at startup" switches, the user's list layouts, anything your application wants remembered. You read and write them by name and type; where they live is a matter of scope.
Reading and writing
If (Fnd_Pref_GetBoolean("Display Invoices at Startup"; True))
Fnd_IO_DisplayTable(->[Invoices])
End if
Fnd_Pref_SetBoolean("Display Invoices at Startup"; False)
One getter and one setter per type: Fnd_Pref_GetText, GetBoolean, GetLongInt, GetReal, GetObject and their Set partners. A getter takes an optional default, returned when the item does not exist, and stored at that moment so the next read finds it. Fnd_Pref_Remove(name) deletes an item.
Window positions have their own pair: Fnd_Pref_SetWindow saves the current window's coordinates under the process name, or a name you give, and Fnd_Pref_GetWindow(name; ->left; ->top; ->right; ->bottom) reads them back. Foundation's own windows use these through Fnd_Wnd_*, so a window you open through Fnd_Wnd_OpenWindow is remembered without any call of yours.
Prefix your names with something of your own ("ACME: Default Branch"); Foundation's are prefixed with the module ("Fnd_Nav: Open at Startup").
Scope
The optional last parameter of every getter and setter says where an item lives:
| Scope | Where | Follows |
|---|---|---|
Fnd_Pref_Local (the default) | a JSON file on this machine, <DatabaseName>.json in the user's 4D preferences folder | the machine and OS user |
Fnd_Pref_Server | the [Fnd_Pref] table in the data file, under the user's name | the user, from machine to machine |
Fnd_Pref_Shared | the [Fnd_Pref] table, under the shared owner | everyone |
| a text label | the [Fnd_Pref] table, under that owner | whatever you mean by the label |
The file is named after the DatabaseName you set in Fnd_Hook_Shell_Setup, which is why every database needs its own name. Earlier versions wrote a property list (.plist on macOS, .xml on Windows); the first run of Foundation 6 reads it once and writes the JSON file beside it.
Server and shared items need the [Fnd_Pref] table (ID, Owner, Name, Type, Value). Fnd_Pref_Init builds it the first time preferences are used where the host allows a table to be created, which means an interpreted project; a compiled host must carry the table in its structure. Foundation Product Sales has it, so copying the structure from there is the quickest route.
The user name that server scope files under is Fnd_Pref_UserName, which you may set at login if your application has its own users.
The Preferences window
Fnd_Pref_Display("Preferences") opens your own Preferences form in a new process, with the menu bar and the Window menu handled. FPS has a Preferences project form: three checkboxes bound to interprocess variables, loaded in On Load from Fnd_Pref_GetBoolean and written back from the object methods with Fnd_Pref_SetBoolean. Copy the form and replace the checkboxes.
The Prefs Explorer
Fnd_Pref_Editor opens a developer's view of the store: every local item, its type and value, editable in place and deletable, with a checkbox to show the server-scoped items too. FPS puts it on the navigation palette as "Prefs Explorer" through Fnd_Nav_AddButtonMethod. Useful while developing; leave it off the palette for users.