]>
git.saurik.com Git - wxWidgets.git/blob - interface/config.h
0f9bf1a69df55b49c51db0c6c8347900f233e683
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxConfigBase class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 wxConfigBase class defines the basic interface of all config classes. It can
14 not be used by itself (it is an abstract base class) and you will always use one
15 of its derivations: wxFileConfig,
16 wxRegConfig or any other.
18 However, usually you don't even need to know the precise nature of the class
19 you're working with but you would just use the wxConfigBase methods. This
20 allows you to write the same code regardless of whether you're working with
21 the registry under Win32 or text-based config files under Unix (or even
22 Windows 3.1 .INI files if you're really unlucky). To make writing the portable
23 code even easier, wxWidgets provides a typedef wxConfig
24 which is mapped onto the native wxConfigBase implementation on the given
25 platform: i.e. wxRegConfig under Win32 and
26 wxFileConfig otherwise.
28 See @ref overview_wxconfigoverview "config overview" for the descriptions of all
29 features of this class.
31 It is highly recommended to use static functions @e Get() and/or @e Set(),
32 so please have a @ref overview_wxconfigstaticfunctions "look at them."
37 class wxConfigBase
: public wxObject
42 This is the default and only constructor of the wxConfigBase class, and
46 The application name. If this is empty, the class will
47 normally use wxApp::GetAppName to set it. The
48 application name is used in the registry key on Windows, and can be used to
49 deduce the local filename parameter if that is missing.
51 The vendor name. If this is empty, it is assumed that
52 no vendor name is wanted, if this is optional for the current config class.
53 The vendor name is appended to the application name for wxRegConfig.
55 Some config classes require a local filename. If this
56 is not present, but required, the application name will be used instead.
58 Some config classes require a global filename. If
59 this is not present, but required, the application name will be used
62 Can be one of wxCONFIG_USE_LOCAL_FILE and
63 wxCONFIG_USE_GLOBAL_FILE. The style interpretation depends on the config
64 class and is ignored by some implementations. For wxFileConfig, these styles
65 determine whether a local or global config file is created or used: if
66 wxCONFIG_USE_GLOBAL_FILE is used, then settings are read from the global
67 config file and if wxCONFIG_USE_LOCAL_FILE is used, settings are read from
68 and written to local config file (if they are both set, global file is read
69 first, then local file, overwriting global settings). If the
70 flag is present but the parameter is empty, the parameter will be set to a
71 default. If the parameter is present but the style flag not, the relevant
73 will be added to the style. For wxRegConfig, thie GLOBAL flag refers to HKLM
74 key while LOCAL one is for the usual HKCU one.
75 For wxFileConfig you can also add wxCONFIG_USE_RELATIVE_PATH by logically
76 or'ing it to either of the _FILE options to tell wxFileConfig to use
78 instead of absolute paths.
79 On non-VMS Unix systems, the default local configuration file is ~/.appname.
80 However, this path may be also used as user data directory
81 (see wxStandardPaths::GetUserDataDir) if
82 the application has several data files. In this case wxCONFIG_USE_SUBDIR
83 flag, which changes the default local configuration file to
85 should be used. Notice that this flag is ignored if localFilename is
86 provided. This function is new since wxWidgets version 2.8.2
87 For wxFileConfig, you can also add wxCONFIG_USE_NO_ESCAPE_CHARACTERS which
88 will turn off character escaping for the values of entries stored in the
90 file: for example a foo key with some backslash characters will be stored
91 as foo=C:\mydir instead of the usual storage of
93 The wxCONFIG_USE_NO_ESCAPE_CHARACTERS style can be helpful if your config
94 file must be read or written to by a non-wxWidgets program (which might not
95 understand the escape characters). Note, however, that if
96 wxCONFIG_USE_NO_ESCAPE_CHARACTERS style is used, it is is now
97 your application's responsibility to ensure that there is no newline or
98 other illegal characters in a value, before writing that value to the file.
100 This parameter is only used by wxFileConfig when compiled
101 in Unicode mode. It specifies the encoding in which the configuration file
104 @remarks By default, environment variable expansion is on and recording
107 wxConfigBase(const wxString
& appName
= wxEmptyString
,
108 const wxString
& vendorName
= wxEmptyString
,
109 const wxString
& localFilename
= wxEmptyString
,
110 const wxString
& globalFilename
= wxEmptyString
,
114 Empty but ensures that dtor of all derived classes is virtual.
119 @ref ctor() wxConfigBase
121 @ref dtor() ~wxConfigBase
126 Create a new config object: this function will create the "best"
127 implementation of wxConfig available for the current platform, see comments
128 near the definition of wxCONFIG_WIN32_NATIVE for details. It returns the
129 created object and also sets it as the current one.
131 static wxConfigBase
* Create();
134 The functions in this section delete entries and/or groups of entries from the
135 config file. @e DeleteAll() is especially useful if you want to erase all
136 traces of your program presence: for example, when you uninstall it.
146 Delete the whole underlying object (disk file, registry key, ...). Primarly
147 for use by uninstallation routine.
152 Deletes the specified entry and the group it belongs to if it was the last key
153 in it and the second parameter is @true.
155 bool DeleteEntry(const wxString
& key
,
156 bool bDeleteGroupIfEmpty
= true);
159 Delete the group (with all subgroups). If the current path is under the group
160 being deleted it is changed to its deepest still existing component. E.g. if
161 the current path is @c /A/B/C/D and the group @c C is deleted the
162 path becomes @c /A/B.
164 bool DeleteGroup(const wxString
& key
);
167 Calling this function will prevent @e Get() from automatically creating a
168 new config object if the current one is @NULL. It might be useful to call it
169 near the program end to prevent "accidental" creation of a new config object.
171 void DontCreateOnDemand();
174 The functions in this section allow to enumerate all entries and groups in the
175 config file. All functions here return @false when there are no more items.
176 You must pass the same index to GetNext and GetFirst (don't modify it).
177 Please note that it is @b not the index of the current item (you will have
178 some great surprises with wxRegConfig if you assume this) and you shouldn't
179 even look at it: it is just a "cookie" which stores the state of the
180 enumeration. It can't be stored inside the class because it would prevent you
181 from running several enumerations simultaneously, that's why you must pass it
183 Having said all this, enumerating the config entries/groups is very simple:
185 There are also functions to get the number of entries/subgroups without
186 actually enumerating them, but you will probably never need them.
202 returns @true if either a group or an entry with a given name exists
204 bool Exists(wxString
& strName
);
207 permanently writes all changes (otherwise, they're only written from object's
210 bool Flush(bool bCurrentOnly
= false);
213 Get the current config object. If there is no current object and
214 @a CreateOnDemand is @true, creates one
215 (using @e Create) unless DontCreateOnDemand was called previously.
217 static wxConfigBase
* Get(bool CreateOnDemand
= true);
220 Returns the application name.
222 wxString
GetAppName();
225 Returns the type of the given entry or @e Unknown if the entry doesn't
226 exist. This function should be used to decide which version of Read() should
227 be used because some of wxConfig implementations will complain about type
228 mismatch otherwise: e.g., an attempt to read a string value from an integer
229 key with wxRegConfig will fail.
230 The result is an element of enum EntryType:
232 enum wxConfigBase::EntryType
GetEntryType(const wxString
& name
);
235 Gets the first entry.
237 bool GetFirstEntry(wxString
& str
, long& index
);
240 Gets the first group.
242 bool GetFirstGroup(wxString
& str
, long& index
);
247 bool GetNextEntry(wxString
& str
, long& index
);
252 bool GetNextGroup(wxString
& str
, long& index
);
257 uint
GetNumberOfEntries(bool bRecursive
= false);
260 Get number of entries/subgroups in the current group, with or without its
263 uint
GetNumberOfGroups(bool bRecursive
= false);
266 Retrieve the current path (always as absolute path).
268 const wxString
GetPath();
271 Returns the vendor name.
273 wxString
GetVendorName();
276 returns @true if the entry by this name exists
278 bool HasEntry(wxString
& strName
);
281 returns @true if the group by this name exists
283 bool HasGroup(const wxString
& strName
);
286 Returns @true if we are expanding environment variables in key values.
288 bool IsExpandingEnvVars();
291 Returns @true if we are writing defaults back to the config file.
293 bool IsRecordingDefaults();
296 These function are the core of wxConfigBase class: they allow you to read and
297 write config file data. All @e Read function take a default value which
298 will be returned if the specified key is not found in the config file.
299 Currently, supported types of data are:
300 wxString, @e long, @e double, @e bool,
301 wxColour and any other types,
302 for which functions wxToString
303 and wxFromString are defined.
304 Try not to read long values into string variables and vice versa: although it
305 just might work with wxFileConfig, you will get a system error with
306 wxRegConfig because in the Windows registry the different types of entries are
308 Final remark: the @e szKey parameter for all these functions can contain an
309 arbitrary path (either relative or absolute), not just the key name.
323 wxFileConfig::SetUmask
328 Some aspects of wxConfigBase behaviour can be changed during run-time. The
329 first of them is the expansion of environment variables in the string values
330 read from the config file: for example, if you have the following in your
333 the call to @c config-Read("UserData") will return something like
334 @c "/home/zeitlin/data" if you're lucky enough to run a Linux system ;-)
335 Although this feature is very useful, it may be annoying if you read a value
336 which containts '$' or '%' symbols (% is used for environment variables
337 expansion under Windows) which are not used for environment variable
338 expansion. In this situation you may call SetExpandEnvVars(@false) just before
339 reading this value and SetExpandEnvVars(@true) just after. Another solution
340 would be to prefix the offending symbols with a backslash.
341 The following functions control this option:
348 IsRecordingDefaults()
353 As explained in @ref overview_wxconfigoverview "config overview", the config
355 support a file system-like hierarchy of keys (files) and groups (directories).
356 As in the file system case, to specify a key in the config class you must use
357 a path to it. Config classes also support the notion of the current group,
358 which makes it possible to use the relative paths. To clarify all this, here
359 is an example (it is only for the sake of demonstration, it doesn't do anything
362 @e Warning: it is probably a good idea to always restore the path to its
363 old value on function exit:
365 because otherwise the assert in the following example will surely fail
366 (we suppose here that @e foo() function is the same as above except that it
367 doesn't save and restore the path):
369 Finally, the path separator in wxConfigBase and derived classes is always '/',
370 regardless of the platform (i.e. it is @b not '\\' under Windows).
379 Reads a value of type T, for which function
380 wxFromString is defined,
381 returning @true if the value was found.
382 If the value was not found, @a defaultVal is used instead.
383 bool Read(const wxStringkey, T* value) const;
386 @b Read(key, default="")
390 @b ReadInt(key, default=0)
394 @b ReadFloat(key, default=0.0)
396 Returns a floating point number
398 @b ReadBool(key, default=0)
402 bool Read(const wxString
& key
, wxString
* str
);
403 bool Read(const wxString
& key
, wxString
* str
,
404 const wxString
& defaultVal
);
405 wxString
Read(const wxString
& key
,
407 wxString
& defaultVal
);
408 bool Read(const wxString
& key
, long* l
);
409 bool Read(const wxString
& key
, long* l
, long defaultVal
);
410 bool Read(const wxString
& key
, double* d
);
411 bool Read(const wxString
& key
, double* d
, double defaultVal
);
412 bool Read(const wxString
& key
, bool* b
);
413 bool Read(const wxString
& key
, bool* d
, bool defaultVal
);
414 bool Read(const wxString
& key
, wxMemoryBuffer
* buf
);
415 bool Read(const wxString
& key
, T
* value
);
416 bool Read(const wxString
& key
, T
* value
,
417 T
const& defaultVal
);
421 Reads a bool value from the key and returns it. @a defaultVal is returned
422 if the key is not found.
424 long ReadBool(const wxString
& key
, bool defaultVal
);
427 Reads a double value from the key and returns it. @a defaultVal is returned
428 if the key is not found.
430 long ReadDouble(const wxString
& key
, double defaultVal
);
433 Reads a long value from the key and returns it. @a defaultVal is returned
434 if the key is not found.
436 long ReadLong(const wxString
& key
, long defaultVal
);
439 Reads a value of type T, for which function
440 wxFromString is defined, from the key and returns it.
441 @a defaultVal is returned if the key is not found.
443 T
ReadObject(const wxString
& key
, T
const& defaultVal
);
446 The functions in this section allow to rename entries or subgroups of the
447 current group. They will return @false on error. typically because either the
448 entry/group with the original name doesn't exist, because the entry/group with
449 the new name already exists or because the function is not supported in this
450 wxConfig implementation.
458 Renames an entry in the current group. The entries names (both the old and
459 the new one) shouldn't contain backslashes, i.e. only simple names and not
460 arbitrary paths are accepted by this function.
461 Returns @false if @a oldName doesn't exist or if @a newName already
464 bool RenameEntry(const wxString
& oldName
,
465 const wxString
& newName
);
468 Renames a subgroup of the current group. The subgroup names (both the old and
469 the new one) shouldn't contain backslashes, i.e. only simple names and not
470 arbitrary paths are accepted by this function.
471 Returns @false if @a oldName doesn't exist or if @a newName already
474 bool RenameGroup(const wxString
& oldName
,
475 const wxString
& newName
);
478 Sets the config object as the current one, returns the pointer to the previous
479 current object (both the parameter and returned value may be @NULL)
481 static wxConfigBase
* Set(wxConfigBase
* pConfig
);
484 Determine whether we wish to expand environment variables in key values.
486 void SetExpandEnvVars(bool bDoIt
= true);
489 Set current path: if the first character is '/', it is the absolute path,
490 otherwise it is a relative path. '..' is supported. If strPath doesn't
493 void SetPath(const wxString
& strPath
);
496 Sets whether defaults are recorded to the config file whenever an attempt to
497 read the value which is not present in it is done.
498 If on (default is off) all default values for the settings used by the program
499 are written back to the config file. This allows the user to see what config
500 options may be changed and is probably useful only for wxFileConfig.
502 void SetRecordDefaults(bool bDoIt
= true);
505 These functions deal with the "default" config object. Although its usage is
506 not at all mandatory it may be convenient to use a global config object
507 instead of creating and deleting the local config objects each time you need
508 one (especially because creating a wxFileConfig object might be a time
509 consuming operation). In this case, you may create this global config object
510 in the very start of the program and @e Set() it as the default. Then, from
511 anywhere in your program, you may access it using the @e Get() function.
512 This global wxConfig object will be deleted by wxWidgets automatically if it
513 exists. Note that this implies that if you do delete this object yourself
514 (usually in wxApp::OnExit) you must use @e Set(@NULL)
515 to prevent wxWidgets from deleting it the second time.
516 As it happens, you may even further simplify the procedure described above:
517 you may forget about calling @e Set(). When @e Get() is called and there
518 is no current object, it will create one using @e Create() function. To
519 disable this behaviour @e DontCreateOnDemand() is provided.
520 @b Note: You should use either @e Set() or @e Get() because wxWidgets
521 library itself would take advantage of it and could save various information
522 in it. For example wxFontMapper or Unix version
523 of wxFileDialog have the ability to use wxConfig class.
547 These functions write the specified value to the config file and return @true
548 on success. In the last one, function wxToString must be
549 defined for type @e T.
556 @b WriteInt(key, value)
560 @b WriteFloat(key, value)
562 Writes a floating point number
564 @b WriteBool(key, value)
568 bool Write(const wxString
& key
, const wxString
& value
);
569 bool Write(const wxString
& key
, long value
);
570 bool Write(const wxString
& key
, double value
);
571 bool Write(const wxString
& key
, bool value
);
572 bool Write(const wxString
& key
, const wxMemoryBuffer
& buf
);
573 bool Write(const wxString
& key
, const T
& buf
);