]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: config.h | |
e54c96f1 | 3 | // Purpose: interface of wxConfigBase |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxConfigBase | |
7c913512 | 11 | |
bd0812fe BP |
12 | wxConfigBase defines the basic interface of all config classes. It can not |
13 | be used by itself (it is an abstract base class) and you will always use | |
14 | one of its derivations: wxFileConfig, wxRegConfig or any other. | |
15 | ||
16 | However, usually you don't even need to know the precise nature of the | |
17 | class you're working with but you would just use the wxConfigBase methods. | |
18 | This allows you to write the same code regardless of whether you're working | |
19 | with the registry under Win32 or text-based config files under Unix (or | |
20 | even Windows 3.1 .INI files if you're really unlucky). To make writing the | |
21 | portable code even easier, wxWidgets provides a typedef wxConfig which is | |
22 | mapped onto the native wxConfigBase implementation on the given platform: | |
23 | i.e. wxRegConfig under Win32 and wxFileConfig otherwise. | |
24 | ||
25 | See @ref overview_config for a description of all features of this class. | |
26 | ||
27 | It is highly recommended to use static functions Get() and/or Set(), so | |
28 | please have a look at them. | |
29 | ||
30 | Related Include Files: | |
31 | ||
32 | @li @c <wx/config.h> - Let wxWidgets choose a wxConfig class for your | |
33 | platform. | |
34 | @li @c <wx/confbase.h> - Base config class. | |
35 | @li @c <wx/fileconf.h> - wxFileConfig class. | |
bbc5b7f8 | 36 | @li @c <wx/msw/regconf.h> - wxRegConfig class, see also wxRegKey. |
bd0812fe BP |
37 | |
38 | ||
39 | @section configbase_example Example | |
40 | ||
41 | Here is how you would typically use this class: | |
42 | ||
43 | @code | |
44 | // using wxConfig instead of writing wxFileConfig or wxRegConfig enhances | |
45 | // portability of the code | |
46 | wxConfig *config = new wxConfig("MyAppName"); | |
47 | ||
48 | wxString str; | |
49 | if ( config->Read("LastPrompt", &str) ) { | |
50 | // last prompt was found in the config file/registry and its value is | |
51 | // now in str | |
52 | // ... | |
53 | } | |
54 | else { | |
55 | // no last prompt... | |
56 | } | |
57 | ||
58 | // another example: using default values and the full path instead of just | |
59 | // key name: if the key is not found , the value 17 is returned | |
60 | long value = config->ReadLong("/LastRun/CalculatedValues/MaxValue", 17); | |
61 | ||
62 | // at the end of the program we would save everything back | |
63 | config->Write("LastPrompt", str); | |
64 | config->Write("/LastRun/CalculatedValues/MaxValue", value); | |
65 | ||
66 | // the changes will be written back automatically | |
67 | delete config; | |
68 | @endcode | |
69 | ||
70 | This basic example, of course, doesn't show all wxConfig features, such as | |
71 | enumerating, testing for existence and deleting the entries and groups of | |
72 | entries in the config file, its abilities to automatically store the | |
73 | default values or expand the environment variables on the fly. However, the | |
74 | main idea is that using this class is easy and that it should normally do | |
75 | what you expect it to. | |
76 | ||
77 | @note In the documentation of this class, the words "config file" also mean | |
78 | "registry hive" for wxRegConfig and, generally speaking, might mean | |
79 | any physical storage where a wxConfigBase-derived class stores its | |
80 | data. | |
81 | ||
82 | ||
83 | @section configbase_static Static Functions | |
84 | ||
85 | The static functions provided deal with the "default" config object. | |
86 | Although its usage is not at all mandatory it may be convenient to use a | |
87 | global config object instead of creating and deleting the local config | |
88 | objects each time you need one (especially because creating a wxFileConfig | |
89 | object might be a time consuming operation). In this case, you may create | |
90 | this global config object in the very start of the program and Set() it as | |
91 | the default. Then, from anywhere in your program, you may access it using | |
92 | the Get() function. This global wxConfig object will be deleted by | |
93 | wxWidgets automatically if it exists. Note that this implies that if you do | |
94 | delete this object yourself (usually in wxApp::OnExit()) you must use | |
95 | Set(@NULL) to prevent wxWidgets from deleting it the second time. | |
96 | ||
97 | As it happens, you may even further simplify the procedure described above: | |
98 | you may forget about calling Set(). When Get() is called and there is no | |
99 | current object, it will create one using Create() function. To disable this | |
100 | behaviour DontCreateOnDemand() is provided. | |
101 | ||
102 | @note You should use either Set() or Get() because wxWidgets library itself | |
103 | would take advantage of it and could save various information in it. | |
104 | For example wxFontMapper or Unix version of wxFileDialog have the | |
105 | ability to use wxConfig class. | |
106 | ||
107 | ||
108 | @section configbase_paths Path Management | |
109 | ||
110 | As explained in the @ref overview_config "config overview", the config | |
111 | classes support a file system-like hierarchy of keys (files) and groups | |
112 | (directories). As in the file system case, to specify a key in the config | |
113 | class you must use a path to it. Config classes also support the notion of | |
114 | the current group, which makes it possible to use the relative paths. To | |
115 | clarify all this, here is an example (it is only for the sake of | |
116 | demonstration, it doesn't do anything sensible!): | |
117 | ||
118 | @code | |
119 | wxConfig *config = new wxConfig("FooBarApp"); | |
120 | ||
121 | // right now the current path is '/' | |
122 | conf->Write("RootEntry", 1); | |
123 | ||
124 | // go to some other place: if the group(s) don't exist, they will be created | |
125 | conf->SetPath("/Group/Subgroup"); | |
126 | ||
127 | // create an entry in subgroup | |
128 | conf->Write("SubgroupEntry", 3); | |
129 | ||
130 | // '..' is understood | |
131 | conf->Write("../GroupEntry", 2); | |
132 | conf->SetPath(".."); | |
133 | ||
134 | wxASSERT( conf->ReadLong("Subgroup/SubgroupEntry", 0) == 3 ); | |
135 | ||
136 | // use absolute path: it is allowed, too | |
137 | wxASSERT( conf->ReadLong("/RootEntry", 0) == 1 ); | |
138 | @endcode | |
139 | ||
140 | It is highly recommended that you restore the path to its old value on | |
141 | function exit: | |
142 | ||
143 | @code | |
144 | void foo(wxConfigBase *config) | |
145 | { | |
146 | wxString strOldPath = config->GetPath(); | |
147 | ||
148 | config->SetPath("/Foo/Data"); | |
149 | // ... | |
150 | ||
151 | config->SetPath(strOldPath); | |
152 | } | |
153 | @endcode | |
154 | ||
155 | Otherwise the assert in the following example will surely fail (we suppose | |
156 | here that the foo() function is the same as above except that it doesn’t | |
157 | save and restore the path): | |
158 | ||
159 | @code | |
160 | void bar(wxConfigBase *config) | |
161 | { | |
162 | config->Write("Test", 17); | |
163 | ||
164 | foo(config); | |
165 | ||
166 | // we're reading "/Foo/Data/Test" here! -1 will probably be returned... | |
167 | wxASSERT( config->ReadLong("Test", -1) == 17 ); | |
168 | } | |
169 | @endcode | |
170 | ||
171 | Finally, the path separator in wxConfigBase and derived classes is always | |
172 | "/", regardless of the platform (i.e. it is not "\\" under Windows). | |
173 | ||
174 | ||
175 | @section configbase_enumeration Enumeration | |
176 | ||
177 | The enumeration functions allow you to enumerate all entries and groups in | |
178 | the config file. All functions here return @false when there are no more | |
179 | items. | |
180 | ||
181 | You must pass the same index to GetNext() and GetFirst() (don't modify it). | |
182 | Please note that it is not the index of the current item (you will have | |
183 | some great surprises with wxRegConfig if you assume this) and you shouldn't | |
184 | even look at it: it is just a "cookie" which stores the state of the | |
185 | enumeration. It can't be stored inside the class because it would prevent | |
186 | you from running several enumerations simultaneously, that's why you must | |
187 | pass it explicitly. | |
188 | ||
189 | Having said all this, enumerating the config entries/groups is very simple: | |
190 | ||
191 | @code | |
192 | wxConfigBase *config = ...; | |
193 | wxArrayString aNames; | |
194 | ||
195 | // enumeration variables | |
196 | wxString str; | |
197 | long dummy; | |
198 | ||
199 | // first enum all entries | |
200 | bool bCont = config->GetFirstEntry(str, dummy); | |
201 | while ( bCont ) { | |
202 | aNames.Add(str); | |
203 | ||
204 | bCont = GetConfig()->GetNextEntry(str, dummy); | |
205 | } | |
206 | ||
207 | // ... we have all entry names in aNames... | |
208 | ||
209 | // now all groups... | |
210 | bCont = GetConfig()->GetFirstGroup(str, dummy); | |
211 | while ( bCont ) { | |
212 | aNames.Add(str); | |
213 | ||
214 | bCont = GetConfig()->GetNextGroup(str, dummy); | |
215 | } | |
216 | ||
217 | // ... we have all group (and entry) names in aNames... | |
218 | @endcode | |
219 | ||
220 | There are also functions to get the number of entries/subgroups without | |
221 | actually enumerating them, but you will probably never need them. | |
222 | ||
223 | ||
224 | @section configbase_keyaccess Key Access | |
225 | ||
226 | The key access functions are the core of wxConfigBase class: they allow you | |
227 | to read and write config file data. All Read() functions take a default | |
228 | value which will be returned if the specified key is not found in the | |
229 | config file. | |
230 | ||
231 | Currently, supported types of data are: wxString, @c long, @c double, | |
232 | @c bool, wxColour and any other types for which the functions | |
233 | wxToString() and wxFromString() are defined. | |
234 | ||
235 | Try not to read long values into string variables and vice versa: | |
236 | although it just might work with wxFileConfig, you will get a system | |
237 | error with wxRegConfig because in the Windows registry the different | |
238 | types of entries are indeed used. | |
239 | ||
240 | Final remark: the @a szKey parameter for all these functions can | |
241 | contain an arbitrary path (either relative or absolute), not just the | |
242 | key name. | |
243 | ||
244 | @beginWxPythonOnly | |
245 | In place of a single overloaded method name, wxPython implements the | |
246 | following methods: | |
247 | - Read(key, default="") - Returns a string. | |
248 | - ReadInt(key, default=0) - Returns an integer. | |
249 | - ReadFloat(key, default=0.0) - Returns a floating point number. | |
250 | - ReadBool(key, default=0) - Returns a boolean. | |
251 | - Write(key, value) - Writes a string. | |
252 | - WriteInt(key, value) - Writes an int. | |
253 | - WriteFloat(key, value) - Writes a floating point number. | |
254 | @endWxPythonOnly | |
255 | ||
7c913512 | 256 | |
23324ae1 FM |
257 | @library{wxbase} |
258 | @category{misc} | |
259 | */ | |
260 | class wxConfigBase : public wxObject | |
261 | { | |
262 | public: | |
263 | /** | |
23324ae1 FM |
264 | This is the default and only constructor of the wxConfigBase class, and |
265 | derived classes. | |
3c4f71cc | 266 | |
7c913512 | 267 | @param appName |
bd0812fe BP |
268 | The application name. If this is empty, the class will normally use |
269 | wxApp::GetAppName() to set it. The application name is used in the | |
270 | registry key on Windows, and can be used to deduce the local | |
271 | filename parameter if that is missing. | |
7c913512 | 272 | @param vendorName |
bd0812fe BP |
273 | The vendor name. If this is empty, it is assumed that no vendor |
274 | name is wanted, if this is optional for the current config class. | |
275 | The vendor name is appended to the application name for | |
276 | wxRegConfig. | |
7c913512 | 277 | @param localFilename |
bd0812fe BP |
278 | Some config classes require a local filename. If this is not |
279 | present, but required, the application name will be used instead. | |
7c913512 | 280 | @param globalFilename |
bd0812fe BP |
281 | Some config classes require a global filename. If this is not |
282 | present, but required, the application name will be used instead. | |
7c913512 | 283 | @param style |
bd0812fe BP |
284 | Can be one of wxCONFIG_USE_LOCAL_FILE and wxCONFIG_USE_GLOBAL_FILE. |
285 | The style interpretation depends on the config class and is ignored | |
286 | by some implementations. For wxFileConfig, these styles determine | |
287 | whether a local or global config file is created or used: if | |
288 | wxCONFIG_USE_GLOBAL_FILE is used, then settings are read from the | |
289 | global config file and if wxCONFIG_USE_LOCAL_FILE is used, settings | |
290 | are read from and written to local config file (if they are both | |
291 | set, global file is read first, then local file, overwriting global | |
292 | settings). If the flag is present but the parameter is empty, the | |
293 | parameter will be set to a default. If the parameter is present but | |
294 | the style flag not, the relevant flag will be added to the style. | |
295 | For wxRegConfig, thie GLOBAL flag refers to HKLM key while LOCAL | |
296 | one is for the usual HKCU one. | |
297 | @n For wxFileConfig you can also add wxCONFIG_USE_RELATIVE_PATH by | |
298 | logically or'ing it to either of the _FILE options to tell | |
299 | wxFileConfig to use relative instead of absolute paths. | |
300 | @n On non-VMS Unix systems, the default local configuration file is | |
301 | "~/.appname". However, this path may be also used as user data | |
302 | directory (see wxStandardPaths::GetUserDataDir()) if the | |
303 | application has several data files. In this case | |
304 | wxCONFIG_USE_SUBDIR flag, which changes the default local | |
305 | configuration file to "~/.appname/appname" should be used. Notice | |
306 | that this flag is ignored if localFilename is provided. | |
307 | wxCONFIG_USE_SUBDIR is new since wxWidgets version 2.8.2. | |
308 | @n For wxFileConfig, you can also add | |
309 | wxCONFIG_USE_NO_ESCAPE_CHARACTERS which will turn off character | |
310 | escaping for the values of entries stored in the config file: for | |
311 | example a foo key with some backslash characters will be stored as | |
312 | "foo=C:\mydir" instead of the usual storage of "foo=C:\\mydir". | |
313 | @n The wxCONFIG_USE_NO_ESCAPE_CHARACTERS style can be helpful if your | |
314 | config file must be read or written to by a non-wxWidgets program | |
315 | (which might not understand the escape characters). Note, however, | |
316 | that if wxCONFIG_USE_NO_ESCAPE_CHARACTERS style is used, it is is | |
317 | now your application's responsibility to ensure that there is no | |
318 | newline or other illegal characters in a value, before writing that | |
319 | value to the file. | |
7c913512 | 320 | @param conv |
bd0812fe BP |
321 | This parameter is only used by wxFileConfig when compiled in |
322 | Unicode mode. It specifies the encoding in which the configuration | |
323 | file is written. | |
3c4f71cc | 324 | |
23324ae1 | 325 | @remarks By default, environment variable expansion is on and recording |
4cc4bfaf | 326 | defaults is off. |
23324ae1 FM |
327 | */ |
328 | wxConfigBase(const wxString& appName = wxEmptyString, | |
329 | const wxString& vendorName = wxEmptyString, | |
330 | const wxString& localFilename = wxEmptyString, | |
331 | const wxString& globalFilename = wxEmptyString, | |
bd0812fe BP |
332 | long style = 0, |
333 | const wxMBConv& conv = wxConvAuto()); | |
23324ae1 FM |
334 | |
335 | /** | |
336 | Empty but ensures that dtor of all derived classes is virtual. | |
337 | */ | |
338 | ~wxConfigBase(); | |
339 | ||
23324ae1 FM |
340 | |
341 | /** | |
bd0812fe | 342 | @name Path Management |
23324ae1 | 343 | |
bd0812fe | 344 | See @ref configbase_paths |
23324ae1 | 345 | */ |
bd0812fe | 346 | //@{ |
23324ae1 FM |
347 | |
348 | /** | |
bd0812fe | 349 | Retrieve the current path (always as absolute path). |
23324ae1 | 350 | */ |
bd0812fe | 351 | const wxString GetPath() const; |
23324ae1 FM |
352 | |
353 | /** | |
bd0812fe BP |
354 | Set current path: if the first character is '/', it is the absolute |
355 | path, otherwise it is a relative path. '..' is supported. If @a strPath | |
356 | doesn't exist it is created. | |
23324ae1 | 357 | */ |
bd0812fe | 358 | void SetPath(const wxString& strPath); |
23324ae1 | 359 | |
bd0812fe | 360 | //@} |
23324ae1 | 361 | |
23324ae1 FM |
362 | |
363 | /** | |
bd0812fe | 364 | @name Enumeration |
23324ae1 | 365 | |
bd0812fe | 366 | See @ref configbase_enumeration |
23324ae1 | 367 | */ |
bd0812fe | 368 | //@{ |
23324ae1 FM |
369 | |
370 | /** | |
371 | Gets the first entry. | |
bd0812fe BP |
372 | |
373 | @beginWxPythonOnly | |
374 | The wxPython version of this method returns a 3-tuple consisting of the | |
375 | continue flag, the value string, and the index for the next call. | |
376 | @endWxPythonOnly | |
23324ae1 | 377 | */ |
328f5751 | 378 | bool GetFirstEntry(wxString& str, long& index) const; |
23324ae1 FM |
379 | |
380 | /** | |
381 | Gets the first group. | |
bd0812fe BP |
382 | |
383 | @beginWxPythonOnly | |
384 | The wxPython version of this method returns a 3-tuple consisting of the | |
385 | continue flag, the value string, and the index for the next call. | |
386 | @endWxPythonOnly | |
23324ae1 | 387 | */ |
328f5751 | 388 | bool GetFirstGroup(wxString& str, long& index) const; |
23324ae1 FM |
389 | |
390 | /** | |
391 | Gets the next entry. | |
bd0812fe BP |
392 | |
393 | @beginWxPythonOnly | |
394 | The wxPython version of this method returns a 3-tuple consisting of the | |
395 | continue flag, the value string, and the index for the next call. | |
396 | @endWxPythonOnly | |
23324ae1 | 397 | */ |
328f5751 | 398 | bool GetNextEntry(wxString& str, long& index) const; |
23324ae1 FM |
399 | |
400 | /** | |
401 | Gets the next group. | |
bd0812fe BP |
402 | |
403 | @beginWxPythonOnly | |
404 | The wxPython version of this method returns a 3-tuple consisting of the | |
405 | continue flag, the value string, and the index for the next call. | |
406 | @endWxPythonOnly | |
23324ae1 | 407 | */ |
328f5751 | 408 | bool GetNextGroup(wxString& str, long& index) const; |
23324ae1 FM |
409 | |
410 | /** | |
bd0812fe | 411 | Get number of entries in the current group. |
23324ae1 | 412 | */ |
328f5751 | 413 | uint GetNumberOfEntries(bool bRecursive = false) const; |
23324ae1 FM |
414 | |
415 | /** | |
bd0812fe BP |
416 | Get number of entries/subgroups in the current group, with or without |
417 | its subgroups. | |
23324ae1 | 418 | */ |
328f5751 | 419 | uint GetNumberOfGroups(bool bRecursive = false) const; |
23324ae1 | 420 | |
bd0812fe BP |
421 | //@} |
422 | ||
423 | ||
424 | enum EntryType | |
425 | { | |
426 | Type_Unknown, | |
427 | Type_String, | |
428 | Type_Boolean, | |
429 | Type_Integer, | |
430 | Type_Float | |
431 | }; | |
23324ae1 FM |
432 | |
433 | /** | |
bd0812fe | 434 | @name Tests of Existence |
23324ae1 | 435 | */ |
bd0812fe | 436 | //@{ |
23324ae1 FM |
437 | |
438 | /** | |
d29a9a8a | 439 | @return @true if either a group or an entry with a given name exists. |
23324ae1 | 440 | */ |
bd0812fe | 441 | bool Exists(wxString& strName) const; |
23324ae1 FM |
442 | |
443 | /** | |
bd0812fe BP |
444 | Returns the type of the given entry or @e Unknown if the entry doesn't |
445 | exist. This function should be used to decide which version of Read() | |
446 | should be used because some of wxConfig implementations will complain | |
447 | about type mismatch otherwise: e.g., an attempt to read a string value | |
448 | from an integer key with wxRegConfig will fail. | |
23324ae1 | 449 | */ |
bd0812fe | 450 | wxConfigBase::EntryType GetEntryType(const wxString& name) const; |
23324ae1 FM |
451 | |
452 | /** | |
d29a9a8a | 453 | @return @true if the entry by this name exists. |
23324ae1 | 454 | */ |
bd0812fe | 455 | bool HasEntry(wxString& strName) const; |
23324ae1 FM |
456 | |
457 | /** | |
d29a9a8a | 458 | @return @true if the group by this name exists. |
23324ae1 | 459 | */ |
bd0812fe | 460 | bool HasGroup(const wxString& strName) const; |
23324ae1 | 461 | |
bd0812fe | 462 | //@} |
3c4f71cc | 463 | |
3c4f71cc | 464 | |
bd0812fe BP |
465 | /** |
466 | @name Miscellaneous Functions | |
23324ae1 | 467 | */ |
bd0812fe | 468 | //@{ |
23324ae1 FM |
469 | |
470 | /** | |
bd0812fe | 471 | Returns the application name. |
23324ae1 | 472 | */ |
bd0812fe | 473 | wxString GetAppName() const; |
23324ae1 FM |
474 | |
475 | /** | |
bd0812fe BP |
476 | Returns the vendor name. |
477 | */ | |
478 | wxString GetVendorName() const; | |
3c4f71cc | 479 | |
bd0812fe | 480 | //@} |
3c4f71cc | 481 | |
3c4f71cc | 482 | |
bd0812fe BP |
483 | /** |
484 | @name Key Access | |
3c4f71cc | 485 | |
bd0812fe | 486 | See @ref configbase_keyaccess |
23324ae1 | 487 | */ |
bd0812fe | 488 | //@{ |
23324ae1 FM |
489 | |
490 | /** | |
bd0812fe BP |
491 | Permanently writes all changes (otherwise, they're only written from |
492 | object's destructor). | |
23324ae1 | 493 | */ |
bd0812fe | 494 | bool Flush(bool bCurrentOnly = false); |
23324ae1 | 495 | |
23324ae1 | 496 | /** |
bd0812fe BP |
497 | Read a string from the key, returning @true if the value was read. If |
498 | the key was not found, @a str is not changed. | |
23324ae1 | 499 | */ |
328f5751 | 500 | bool Read(const wxString& key, wxString* str) const; |
bd0812fe BP |
501 | /** |
502 | Read a string from the key. The default value is returned if the key | |
503 | was not found. | |
504 | ||
d29a9a8a | 505 | @return @true if value was really read, @false if the default was used. |
bd0812fe | 506 | */ |
328f5751 | 507 | const bool Read(const wxString& key, wxString* str, |
bd0812fe BP |
508 | const wxString& defaultVal) const; |
509 | /** | |
510 | Another version of Read(), returning the string value directly. | |
511 | */ | |
512 | const wxString Read(const wxString& key, | |
513 | const wxString& defaultVal) const; | |
514 | /** | |
515 | Reads a long value, returning @true if the value was found. If the | |
516 | value was not found, @a l is not changed. | |
517 | */ | |
328f5751 | 518 | const bool Read(const wxString& key, long* l) const; |
bd0812fe BP |
519 | /** |
520 | Reads a long value, returning @true if the value was found. If the | |
521 | value was not found, @a defaultVal is used instead. | |
522 | */ | |
328f5751 FM |
523 | const bool Read(const wxString& key, long* l, |
524 | long defaultVal) const; | |
bd0812fe BP |
525 | /** |
526 | Reads a double value, returning @true if the value was found. If the | |
527 | value was not found, @a d is not changed. | |
528 | */ | |
328f5751 | 529 | const bool Read(const wxString& key, double* d) const; |
bd0812fe BP |
530 | /** |
531 | Reads a double value, returning @true if the value was found. If the | |
532 | value was not found, @a defaultVal is used instead. | |
533 | */ | |
328f5751 | 534 | const bool Read(const wxString& key, double* d, |
bd0812fe BP |
535 | double defaultVal) const; |
536 | /** | |
537 | Reads a bool value, returning @true if the value was found. If the | |
538 | value was not found, @a b is not changed. | |
539 | */ | |
328f5751 | 540 | const bool Read(const wxString& key, bool* b) const; |
bd0812fe BP |
541 | /** |
542 | Reads a bool value, returning @true if the value was found. If the | |
543 | value was not found, @a defaultVal is used instead. | |
544 | */ | |
328f5751 | 545 | const bool Read(const wxString& key, bool* d, |
bd0812fe BP |
546 | bool defaultVal) const; |
547 | /** | |
548 | Reads a binary block, returning @true if the value was found. If the | |
549 | value was not found, @a buf is not changed. | |
550 | */ | |
328f5751 | 551 | const bool Read(const wxString& key, wxMemoryBuffer* buf) const; |
bd0812fe BP |
552 | /** |
553 | Reads a value of type T, for which function wxFromString() is defined, | |
554 | returning @true if the value was found. If the value was not found, | |
555 | @a value is not changed. | |
556 | */ | |
328f5751 | 557 | const bool Read(const wxString& key, T* value) const; |
bd0812fe BP |
558 | /** |
559 | Reads a value of type T, for which function wxFromString() is defined, | |
560 | returning @true if the value was found. If the value was not found, | |
561 | @a defaultVal is used instead. | |
562 | */ | |
328f5751 | 563 | const bool Read(const wxString& key, T* value, |
bd0812fe | 564 | const T& defaultVal) const; |
23324ae1 FM |
565 | |
566 | /** | |
bd0812fe BP |
567 | Reads a bool value from the key and returns it. @a defaultVal is |
568 | returned if the key is not found. | |
23324ae1 | 569 | */ |
328f5751 | 570 | long ReadBool(const wxString& key, bool defaultVal) const; |
23324ae1 FM |
571 | |
572 | /** | |
bd0812fe BP |
573 | Reads a double value from the key and returns it. @a defaultVal is |
574 | returned if the key is not found. | |
23324ae1 | 575 | */ |
328f5751 | 576 | long ReadDouble(const wxString& key, double defaultVal) const; |
23324ae1 FM |
577 | |
578 | /** | |
bd0812fe BP |
579 | Reads a long value from the key and returns it. @a defaultVal is |
580 | returned if the key is not found. | |
23324ae1 | 581 | */ |
328f5751 | 582 | long ReadLong(const wxString& key, long defaultVal) const; |
23324ae1 FM |
583 | |
584 | /** | |
bd0812fe BP |
585 | Reads a value of type T (for which the function wxFromString() must be |
586 | defined) from the key and returns it. @a defaultVal is returned if the | |
587 | key is not found. | |
23324ae1 | 588 | */ |
328f5751 | 589 | T ReadObject(const wxString& key, T const& defaultVal) const; |
23324ae1 FM |
590 | |
591 | /** | |
bd0812fe BP |
592 | Writes the wxString value to the config file and returns @true on |
593 | success. | |
594 | */ | |
595 | bool Write(const wxString& key, const wxString& value); | |
596 | /** | |
597 | Writes the long value to the config file and returns @true on success. | |
598 | */ | |
599 | bool Write(const wxString& key, long value); | |
600 | /** | |
601 | Writes the double value to the config file and returns @true on | |
602 | success. | |
603 | */ | |
604 | bool Write(const wxString& key, double value); | |
605 | /** | |
606 | Writes the bool value to the config file and returns @true on success. | |
23324ae1 | 607 | */ |
bd0812fe BP |
608 | bool Write(const wxString& key, bool value); |
609 | /** | |
610 | Writes the wxMemoryBuffer value to the config file and returns @true on | |
611 | success. | |
612 | */ | |
613 | bool Write(const wxString& key, const wxMemoryBuffer& buf); | |
614 | /** | |
615 | Writes the specified value to the config file and returns @true on | |
616 | success. The function wxToString() must be defined for type @e T. | |
617 | */ | |
618 | bool Write(const wxString& key, T const& buf); | |
619 | ||
620 | //@} | |
23324ae1 FM |
621 | |
622 | ||
623 | /** | |
bd0812fe BP |
624 | @name Rename Entries/Groups |
625 | ||
626 | These functions allow renaming entries or subgroups of the current | |
627 | group. They will return @false on error, typically because either the | |
628 | entry/group with the original name doesn't exist, because the | |
629 | entry/group with the new name already exists or because the function is | |
630 | not supported in this wxConfig implementation. | |
23324ae1 | 631 | */ |
bd0812fe | 632 | //@{ |
23324ae1 FM |
633 | |
634 | /** | |
bd0812fe BP |
635 | Renames an entry in the current group. The entries names (both the old |
636 | and the new one) shouldn't contain backslashes, i.e. only simple names | |
637 | and not arbitrary paths are accepted by this function. | |
638 | ||
d29a9a8a BP |
639 | @return @false if @a oldName doesn't exist or if @a newName already |
640 | exists. | |
23324ae1 | 641 | */ |
bd0812fe | 642 | bool RenameEntry(const wxString& oldName, const wxString& newName); |
23324ae1 FM |
643 | |
644 | /** | |
bd0812fe BP |
645 | Renames a subgroup of the current group. The subgroup names (both the |
646 | old and the new one) shouldn't contain backslashes, i.e. only simple | |
647 | names and not arbitrary paths are accepted by this function. | |
648 | ||
d29a9a8a BP |
649 | @return @false if @a oldName doesn't exist or if @a newName already |
650 | exists. | |
23324ae1 | 651 | */ |
bd0812fe BP |
652 | bool RenameGroup(const wxString& oldName, const wxString& newName); |
653 | ||
654 | //@} | |
655 | ||
23324ae1 FM |
656 | |
657 | /** | |
bd0812fe BP |
658 | @name Delete Entries/Groups |
659 | ||
660 | These functions delete entries and/or groups of entries from the config | |
661 | file. DeleteAll() is especially useful if you want to erase all traces | |
662 | of your program presence: for example, when you uninstall it. | |
23324ae1 | 663 | */ |
bd0812fe | 664 | //@{ |
23324ae1 FM |
665 | |
666 | /** | |
bd0812fe BP |
667 | Delete the whole underlying object (disk file, registry key, ...). |
668 | Primarly for use by uninstallation routine. | |
23324ae1 | 669 | */ |
bd0812fe | 670 | bool DeleteAll(); |
23324ae1 FM |
671 | |
672 | /** | |
bd0812fe BP |
673 | Deletes the specified entry and the group it belongs to if it was the |
674 | last key in it and the second parameter is @true. | |
23324ae1 | 675 | */ |
bd0812fe BP |
676 | bool DeleteEntry(const wxString& key, |
677 | bool bDeleteGroupIfEmpty = true); | |
23324ae1 FM |
678 | |
679 | /** | |
bd0812fe BP |
680 | Delete the group (with all subgroups). If the current path is under the |
681 | group being deleted it is changed to its deepest still existing | |
682 | component. E.g. if the current path is @c "/A/B/C/D" and the group @c C | |
683 | is deleted, the path becomes @c "/A/B". | |
684 | */ | |
685 | bool DeleteGroup(const wxString& key); | |
3c4f71cc | 686 | |
bd0812fe | 687 | //@} |
3c4f71cc | 688 | |
3c4f71cc | 689 | |
bd0812fe BP |
690 | /** |
691 | @name Options | |
23324ae1 | 692 | |
bd0812fe BP |
693 | Some aspects of wxConfigBase behaviour can be changed during run-time. |
694 | The first of them is the expansion of environment variables in the | |
695 | string values read from the config file: for example, if you have the | |
696 | following in your config file: | |
23324ae1 | 697 | |
bd0812fe BP |
698 | @code |
699 | # config file for my program | |
700 | UserData = $HOME/data | |
3c4f71cc | 701 | |
bd0812fe BP |
702 | # the following syntax is valud only under Windows |
703 | UserData = %windir%\\data.dat | |
704 | @endcode | |
3c4f71cc | 705 | |
bd0812fe BP |
706 | The call to Read("UserData") will return something like |
707 | @c "/home/zeitlin/data" on linux for example. | |
3c4f71cc | 708 | |
bd0812fe BP |
709 | Although this feature is very useful, it may be annoying if you read a |
710 | value which containts '$' or '%' symbols (% is used for environment | |
711 | variables expansion under Windows) which are not used for environment | |
712 | variable expansion. In this situation you may call | |
713 | SetExpandEnvVars(@false) just before reading this value and | |
714 | SetExpandEnvVars(@true) just after. Another solution would be to prefix | |
715 | the offending symbols with a backslash. | |
23324ae1 | 716 | */ |
bd0812fe | 717 | //@{ |
23324ae1 | 718 | |
bd0812fe BP |
719 | /** |
720 | Returns @true if we are expanding environment variables in key values. | |
721 | */ | |
722 | bool IsExpandingEnvVars() const; | |
23324ae1 | 723 | |
23324ae1 | 724 | /** |
bd0812fe BP |
725 | Returns @true if we are writing defaults back to the config file. |
726 | */ | |
727 | bool IsRecordingDefaults() const; | |
3c4f71cc | 728 | |
bd0812fe BP |
729 | /** |
730 | Determine whether we wish to expand environment variables in key | |
731 | values. | |
732 | */ | |
733 | void SetExpandEnvVars(bool bDoIt = true); | |
3c4f71cc | 734 | |
bd0812fe BP |
735 | /** |
736 | Sets whether defaults are recorded to the config file whenever an | |
737 | attempt to read the value which is not present in it is done. | |
3c4f71cc | 738 | |
bd0812fe BP |
739 | If on (default is off) all default values for the settings used by the |
740 | program are written back to the config file. This allows the user to | |
741 | see what config options may be changed and is probably useful only for | |
742 | wxFileConfig. | |
743 | */ | |
744 | void SetRecordDefaults(bool bDoIt = true); | |
3c4f71cc | 745 | |
bd0812fe | 746 | //@} |
3c4f71cc | 747 | |
3c4f71cc | 748 | |
bd0812fe BP |
749 | /** |
750 | Create a new config object: this function will create the "best" | |
751 | implementation of wxConfig available for the current platform, see | |
752 | comments near the definition of wxCONFIG_WIN32_NATIVE for details. It | |
753 | returns the created object and also sets it as the current one. | |
754 | */ | |
755 | static wxConfigBase* Create(); | |
3c4f71cc | 756 | |
bd0812fe BP |
757 | /** |
758 | Calling this function will prevent @e Get() from automatically creating | |
759 | a new config object if the current one is @NULL. It might be useful to | |
760 | call it near the program end to prevent "accidental" creation of a new | |
761 | config object. | |
762 | */ | |
763 | static void DontCreateOnDemand(); | |
3c4f71cc | 764 | |
bd0812fe BP |
765 | /** |
766 | Get the current config object. If there is no current object and | |
767 | @a CreateOnDemand is @true, this creates one (using Create()) unless | |
768 | DontCreateOnDemand() was called previously. | |
769 | */ | |
770 | static wxConfigBase* Get(bool CreateOnDemand = true); | |
3c4f71cc | 771 | |
bd0812fe BP |
772 | /** |
773 | Sets the config object as the current one, returns the pointer to the | |
774 | previous current object (both the parameter and returned value may be | |
775 | @NULL). | |
23324ae1 | 776 | */ |
bd0812fe | 777 | static wxConfigBase* Set(wxConfigBase* pConfig); |
23324ae1 | 778 | }; |
e54c96f1 | 779 |