Settings (gb.settings)
This class manages global configuration files.
This class can be used like an object by creating a hidden instance on demand.
This class is .
This class acts like a / array.
Static properties
|
Static methods
|
|
|
|
If you use this class statically, then the default project configuration file will be used.
The path of the default configuration file is:
User.Home &/ ".config/gambas3" &/ Application.Name & ".conf"
For example, the development environment configuration file is stored in
~/.config/gambas3/gambas3.conf
This example loads a window's position from a settings file when a form is opened. If the settings file does not exist, then the form's default position would be used. The example also saves the forms position when the form closes. The default settings file for the application is used.
Examples
PUBLIC SUB Form_Open()
' Update window position from settings file
' If the settings file is not found then
' use default position.
ME.Top = Settings["Window/Top", ME.Top]
ME.Left = Settings["Window/Left", ME.Left]
ME.Height = Settings["Window/Height", ME.Height]
ME.Width = Settings["Window/Width", ME.Width]
END
PUBLIC SUB Form_Close()
' Save window settings when application closes
Settings["Window/Top"] = ME.Top
Settings["Window/Left"] = ME.Left
Settings["Window/Height"] = ME.Height
Settings["Window/Width"] = ME.Width
END
The saved settings file would look something like the following settings in the Window group.
[Window]
Top=13
Left=18
Height=154
Width=235
PUBLIC SUB ShowASpecialSetting()
DIM prhOtherConfFile as Settings
DIM prhLangUsed as String
' Read a setting from a file used by another application
' located in /home/user/.config/otherapp/
' and named "international.conf"
'First create a new instance of Settings class
'with the full path to desired file
prhOtherConfFile = NEW Settings("/home/user/.config/otherapp/international.conf")
'next read the "Lang1" value from the "UserPreferences" section
prhLangUsed = prhOtherConfFile["UserPreferences/Lang1", NULL]
IF IsNull(prhLangUsed) THEN
Message.Info("No Lang1 found in conf file (or no Section named UserPreferences)")
ELSE
Message.Info("The value of Lang1 that found in conf file is:\n" & prhLangUsed)
END IF
END
Note that you can directly use the
Write method to store the settings of a window, and the
Read method to recall them.
Warning.
When you quit the program the settings are automatically saved but if your program is running in the background and your computer is shutdown it will
hard exit and
not save the settings. so if your program is likely to exit because of a system shutdown and not manually then you should use the
Save function to save the settings as they are changed and not leave it for when the program exits normally as it won't exit normally.