]>
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$ | |
526954c5 | 6 | // Licence: wxWindows licence |
23324ae1 FM |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
123919a9 RD |
9 | |
10 | // Flags for constructor style parameter | |
11 | enum | |
12 | { | |
13 | wxCONFIG_USE_LOCAL_FILE = 1, | |
14 | wxCONFIG_USE_GLOBAL_FILE = 2, | |
15 | wxCONFIG_USE_RELATIVE_PATH = 4, | |
16 | wxCONFIG_USE_NO_ESCAPE_CHARACTERS = 8, | |
17 | wxCONFIG_USE_SUBDIR = 16 | |
18 | }; | |
19 | ||
20 | ||
23324ae1 FM |
21 | /** |
22 | @class wxConfigBase | |
7c913512 | 23 | |
4c51a665 | 24 | wxConfigBase defines the basic interface of all config classes. It cannot |
bd0812fe BP |
25 | be used by itself (it is an abstract base class) and you will always use |
26 | one of its derivations: wxFileConfig, wxRegConfig or any other. | |
27 | ||
28 | However, usually you don't even need to know the precise nature of the | |
29 | class you're working with but you would just use the wxConfigBase methods. | |
30 | This allows you to write the same code regardless of whether you're working | |
df693ed6 FM |
31 | with the registry under Windows or text-based config files under Unix. |
32 | To make writing the portable code even easier, wxWidgets provides a typedef | |
33 | wxConfig which is mapped onto the native wxConfigBase implementation on the | |
34 | given platform: i.e. wxRegConfig under Windows and wxFileConfig otherwise. | |
bd0812fe BP |
35 | |
36 | See @ref overview_config for a description of all features of this class. | |
37 | ||
38 | It is highly recommended to use static functions Get() and/or Set(), so | |
39 | please have a look at them. | |
40 | ||
41 | Related Include Files: | |
42 | ||
43 | @li @c <wx/config.h> - Let wxWidgets choose a wxConfig class for your | |
44 | platform. | |
45 | @li @c <wx/confbase.h> - Base config class. | |
46 | @li @c <wx/fileconf.h> - wxFileConfig class. | |
bbc5b7f8 | 47 | @li @c <wx/msw/regconf.h> - wxRegConfig class, see also wxRegKey. |
bd0812fe BP |
48 | |
49 | ||
50 | @section configbase_example Example | |
51 | ||
52 | Here is how you would typically use this class: | |
53 | ||
54 | @code | |
55 | // using wxConfig instead of writing wxFileConfig or wxRegConfig enhances | |
56 | // portability of the code | |
57 | wxConfig *config = new wxConfig("MyAppName"); | |
58 | ||
59 | wxString str; | |
60 | if ( config->Read("LastPrompt", &str) ) { | |
61 | // last prompt was found in the config file/registry and its value is | |
62 | // now in str | |
63 | // ... | |
64 | } | |
65 | else { | |
66 | // no last prompt... | |
67 | } | |
68 | ||
69 | // another example: using default values and the full path instead of just | |
70 | // key name: if the key is not found , the value 17 is returned | |
71 | long value = config->ReadLong("/LastRun/CalculatedValues/MaxValue", 17); | |
72 | ||
73 | // at the end of the program we would save everything back | |
74 | config->Write("LastPrompt", str); | |
75 | config->Write("/LastRun/CalculatedValues/MaxValue", value); | |
76 | ||
77 | // the changes will be written back automatically | |
78 | delete config; | |
79 | @endcode | |
80 | ||
81 | This basic example, of course, doesn't show all wxConfig features, such as | |
82 | enumerating, testing for existence and deleting the entries and groups of | |
83 | entries in the config file, its abilities to automatically store the | |
84 | default values or expand the environment variables on the fly. However, the | |
85 | main idea is that using this class is easy and that it should normally do | |
86 | what you expect it to. | |
87 | ||
88 | @note In the documentation of this class, the words "config file" also mean | |
89 | "registry hive" for wxRegConfig and, generally speaking, might mean | |
90 | any physical storage where a wxConfigBase-derived class stores its | |
91 | data. | |
92 | ||
93 | ||
94 | @section configbase_static Static Functions | |
95 | ||
96 | The static functions provided deal with the "default" config object. | |
97 | Although its usage is not at all mandatory it may be convenient to use a | |
98 | global config object instead of creating and deleting the local config | |
99 | objects each time you need one (especially because creating a wxFileConfig | |
100 | object might be a time consuming operation). In this case, you may create | |
101 | this global config object in the very start of the program and Set() it as | |
102 | the default. Then, from anywhere in your program, you may access it using | |
103 | the Get() function. This global wxConfig object will be deleted by | |
104 | wxWidgets automatically if it exists. Note that this implies that if you do | |
105 | delete this object yourself (usually in wxApp::OnExit()) you must use | |
106 | Set(@NULL) to prevent wxWidgets from deleting it the second time. | |
107 | ||
108 | As it happens, you may even further simplify the procedure described above: | |
109 | you may forget about calling Set(). When Get() is called and there is no | |
110 | current object, it will create one using Create() function. To disable this | |
111 | behaviour DontCreateOnDemand() is provided. | |
112 | ||
113 | @note You should use either Set() or Get() because wxWidgets library itself | |
114 | would take advantage of it and could save various information in it. | |
115 | For example wxFontMapper or Unix version of wxFileDialog have the | |
116 | ability to use wxConfig class. | |
117 | ||
118 | ||
119 | @section configbase_paths Path Management | |
120 | ||
121 | As explained in the @ref overview_config "config overview", the config | |
122 | classes support a file system-like hierarchy of keys (files) and groups | |
123 | (directories). As in the file system case, to specify a key in the config | |
124 | class you must use a path to it. Config classes also support the notion of | |
125 | the current group, which makes it possible to use the relative paths. To | |
126 | clarify all this, here is an example (it is only for the sake of | |
127 | demonstration, it doesn't do anything sensible!): | |
128 | ||
129 | @code | |
130 | wxConfig *config = new wxConfig("FooBarApp"); | |
131 | ||
132 | // right now the current path is '/' | |
133 | conf->Write("RootEntry", 1); | |
134 | ||
135 | // go to some other place: if the group(s) don't exist, they will be created | |
136 | conf->SetPath("/Group/Subgroup"); | |
137 | ||
138 | // create an entry in subgroup | |
139 | conf->Write("SubgroupEntry", 3); | |
140 | ||
141 | // '..' is understood | |
142 | conf->Write("../GroupEntry", 2); | |
143 | conf->SetPath(".."); | |
144 | ||
145 | wxASSERT( conf->ReadLong("Subgroup/SubgroupEntry", 0) == 3 ); | |
146 | ||
147 | // use absolute path: it is allowed, too | |
148 | wxASSERT( conf->ReadLong("/RootEntry", 0) == 1 ); | |
149 | @endcode | |
150 | ||
151 | It is highly recommended that you restore the path to its old value on | |
152 | function exit: | |
153 | ||
154 | @code | |
155 | void foo(wxConfigBase *config) | |
156 | { | |
157 | wxString strOldPath = config->GetPath(); | |
158 | ||
159 | config->SetPath("/Foo/Data"); | |
160 | // ... | |
161 | ||
162 | config->SetPath(strOldPath); | |
163 | } | |
164 | @endcode | |
165 | ||
166 | Otherwise the assert in the following example will surely fail (we suppose | |
167 | here that the foo() function is the same as above except that it doesn’t | |
168 | save and restore the path): | |
169 | ||
170 | @code | |
171 | void bar(wxConfigBase *config) | |
172 | { | |
173 | config->Write("Test", 17); | |
174 | ||
175 | foo(config); | |
176 | ||
177 | // we're reading "/Foo/Data/Test" here! -1 will probably be returned... | |
178 | wxASSERT( config->ReadLong("Test", -1) == 17 ); | |
179 | } | |
180 | @endcode | |
181 | ||
182 | Finally, the path separator in wxConfigBase and derived classes is always | |
183 | "/", regardless of the platform (i.e. it is not "\\" under Windows). | |
184 | ||
185 | ||
186 | @section configbase_enumeration Enumeration | |
187 | ||
188 | The enumeration functions allow you to enumerate all entries and groups in | |
189 | the config file. All functions here return @false when there are no more | |
190 | items. | |
191 | ||
192 | You must pass the same index to GetNext() and GetFirst() (don't modify it). | |
193 | Please note that it is not the index of the current item (you will have | |
194 | some great surprises with wxRegConfig if you assume this) and you shouldn't | |
195 | even look at it: it is just a "cookie" which stores the state of the | |
196 | enumeration. It can't be stored inside the class because it would prevent | |
197 | you from running several enumerations simultaneously, that's why you must | |
198 | pass it explicitly. | |
199 | ||
200 | Having said all this, enumerating the config entries/groups is very simple: | |
201 | ||
202 | @code | |
203 | wxConfigBase *config = ...; | |
204 | wxArrayString aNames; | |
205 | ||
206 | // enumeration variables | |
207 | wxString str; | |
208 | long dummy; | |
209 | ||
210 | // first enum all entries | |
211 | bool bCont = config->GetFirstEntry(str, dummy); | |
212 | while ( bCont ) { | |
213 | aNames.Add(str); | |
214 | ||
215 | bCont = GetConfig()->GetNextEntry(str, dummy); | |
216 | } | |
217 | ||
218 | // ... we have all entry names in aNames... | |
219 | ||
220 | // now all groups... | |
221 | bCont = GetConfig()->GetFirstGroup(str, dummy); | |
222 | while ( bCont ) { | |
223 | aNames.Add(str); | |
224 | ||
225 | bCont = GetConfig()->GetNextGroup(str, dummy); | |
226 | } | |
227 | ||
228 | // ... we have all group (and entry) names in aNames... | |
229 | @endcode | |
230 | ||
231 | There are also functions to get the number of entries/subgroups without | |
232 | actually enumerating them, but you will probably never need them. | |
233 | ||
234 | ||
235 | @section configbase_keyaccess Key Access | |
236 | ||
237 | The key access functions are the core of wxConfigBase class: they allow you | |
238 | to read and write config file data. All Read() functions take a default | |
239 | value which will be returned if the specified key is not found in the | |
240 | config file. | |
241 | ||
242 | Currently, supported types of data are: wxString, @c long, @c double, | |
243 | @c bool, wxColour and any other types for which the functions | |
244 | wxToString() and wxFromString() are defined. | |
245 | ||
246 | Try not to read long values into string variables and vice versa: | |
247 | although it just might work with wxFileConfig, you will get a system | |
248 | error with wxRegConfig because in the Windows registry the different | |
249 | types of entries are indeed used. | |
250 | ||
251 | Final remark: the @a szKey parameter for all these functions can | |
252 | contain an arbitrary path (either relative or absolute), not just the | |
253 | key name. | |
254 | ||
23324ae1 | 255 | @library{wxbase} |
3c99e2fd | 256 | @category{cfg} |
b2fc9f0e FM |
257 | |
258 | @see wxConfigPathChanger | |
23324ae1 FM |
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 |
df693ed6 FM |
284 | Can be one of @c wxCONFIG_USE_LOCAL_FILE and @c wxCONFIG_USE_GLOBAL_FILE. |
285 | @n The style interpretation depends on the config class and is ignored | |
bd0812fe BP |
286 | by some implementations. For wxFileConfig, these styles determine |
287 | whether a local or global config file is created or used: if | |
df693ed6 FM |
288 | @c wxCONFIG_USE_GLOBAL_FILE is used, then settings are read from the |
289 | global config file and if @c wxCONFIG_USE_LOCAL_FILE is used, settings | |
bd0812fe BP |
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. | |
df693ed6 FM |
295 | For wxRegConfig, the GLOBAL flag refers to the @c HKLM key while LOCAL |
296 | one is for the usual @c HKCU one. | |
297 | @n For wxFileConfig you can also add @c wxCONFIG_USE_RELATIVE_PATH by | |
bd0812fe BP |
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 | |
df693ed6 | 304 | @c wxCONFIG_USE_SUBDIR flag, which changes the default local |
bd0812fe | 305 | configuration file to "~/.appname/appname" should be used. Notice |
df693ed6 FM |
306 | that this flag is ignored if @a localFilename is provided. |
307 | @c wxCONFIG_USE_SUBDIR is new since wxWidgets version 2.8.2. | |
bd0812fe | 308 | @n For wxFileConfig, you can also add |
df693ed6 | 309 | @c wxCONFIG_USE_NO_ESCAPE_CHARACTERS which will turn off character |
bd0812fe BP |
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". | |
df693ed6 | 313 | @n The @c wxCONFIG_USE_NO_ESCAPE_CHARACTERS style can be helpful if your |
bd0812fe BP |
314 | config file must be read or written to by a non-wxWidgets program |
315 | (which might not understand the escape characters). Note, however, | |
d13b34d3 | 316 | that if @c wxCONFIG_USE_NO_ESCAPE_CHARACTERS style is used, it is |
bd0812fe BP |
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, | |
76e9224e FM |
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 | */ | |
b7e94bd7 | 338 | virtual ~wxConfigBase(); |
23324ae1 | 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 | */ |
382f12e4 | 351 | virtual const wxString& GetPath() const = 0; |
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 | |
b2fc9f0e FM |
356 | doesn't exist, it is created. |
357 | ||
358 | @see wxConfigPathChanger | |
23324ae1 | 359 | */ |
382f12e4 | 360 | virtual void SetPath(const wxString& strPath) = 0; |
23324ae1 | 361 | |
bd0812fe | 362 | //@} |
23324ae1 | 363 | |
23324ae1 FM |
364 | |
365 | /** | |
bd0812fe | 366 | @name Enumeration |
23324ae1 | 367 | |
bd0812fe | 368 | See @ref configbase_enumeration |
23324ae1 | 369 | */ |
bd0812fe | 370 | //@{ |
23324ae1 FM |
371 | |
372 | /** | |
373 | Gets the first entry. | |
bd0812fe | 374 | |
1058f652 MB |
375 | @beginWxPerlOnly |
376 | In wxPerl this method takes no parameters and returns a 3-element | |
377 | list (continue_flag, string, index_for_getnextentry). | |
378 | @endWxPerlOnly | |
23324ae1 | 379 | */ |
382f12e4 | 380 | virtual bool GetFirstEntry(wxString& str, long& index) const = 0; |
23324ae1 FM |
381 | |
382 | /** | |
383 | Gets the first group. | |
bd0812fe | 384 | |
1058f652 MB |
385 | @beginWxPerlOnly |
386 | In wxPerl this method takes no parameters and returns a 3-element | |
387 | list (continue_flag, string, index_for_getnextentry). | |
388 | @endWxPerlOnly | |
23324ae1 | 389 | */ |
382f12e4 | 390 | virtual bool GetFirstGroup(wxString& str, long& index) const = 0; |
23324ae1 FM |
391 | |
392 | /** | |
393 | Gets the next entry. | |
bd0812fe | 394 | |
1058f652 MB |
395 | @beginWxPerlOnly |
396 | In wxPerl this method only takes the @a index parameter and | |
397 | returns a 3-element list (continue_flag, string, | |
398 | index_for_getnextentry). | |
399 | @endWxPerlOnly | |
23324ae1 | 400 | */ |
382f12e4 | 401 | virtual bool GetNextEntry(wxString& str, long& index) const = 0; |
23324ae1 FM |
402 | |
403 | /** | |
404 | Gets the next group. | |
bd0812fe | 405 | |
1058f652 MB |
406 | @beginWxPerlOnly |
407 | In wxPerl this method only takes the @a index parameter and | |
408 | returns a 3-element list (continue_flag, string, | |
409 | index_for_getnextentry). | |
410 | @endWxPerlOnly | |
23324ae1 | 411 | */ |
382f12e4 | 412 | virtual bool GetNextGroup(wxString& str, long& index) const = 0; |
23324ae1 FM |
413 | |
414 | /** | |
bd0812fe | 415 | Get number of entries in the current group. |
23324ae1 | 416 | */ |
382f12e4 | 417 | virtual size_t GetNumberOfEntries(bool bRecursive = false) const = 0; |
23324ae1 FM |
418 | |
419 | /** | |
bd0812fe BP |
420 | Get number of entries/subgroups in the current group, with or without |
421 | its subgroups. | |
23324ae1 | 422 | */ |
382f12e4 | 423 | virtual size_t GetNumberOfGroups(bool bRecursive = false) const = 0; |
23324ae1 | 424 | |
bd0812fe BP |
425 | //@} |
426 | ||
427 | ||
428 | enum EntryType | |
429 | { | |
430 | Type_Unknown, | |
431 | Type_String, | |
432 | Type_Boolean, | |
433 | Type_Integer, | |
434 | Type_Float | |
435 | }; | |
23324ae1 FM |
436 | |
437 | /** | |
bd0812fe | 438 | @name Tests of Existence |
23324ae1 | 439 | */ |
bd0812fe | 440 | //@{ |
23324ae1 FM |
441 | |
442 | /** | |
d29a9a8a | 443 | @return @true if either a group or an entry with a given name exists. |
23324ae1 | 444 | */ |
382f12e4 | 445 | bool Exists(const wxString& strName) const; |
23324ae1 FM |
446 | |
447 | /** | |
bd0812fe BP |
448 | Returns the type of the given entry or @e Unknown if the entry doesn't |
449 | exist. This function should be used to decide which version of Read() | |
450 | should be used because some of wxConfig implementations will complain | |
451 | about type mismatch otherwise: e.g., an attempt to read a string value | |
452 | from an integer key with wxRegConfig will fail. | |
23324ae1 | 453 | */ |
382f12e4 | 454 | virtual wxConfigBase::EntryType GetEntryType(const wxString& name) const; |
23324ae1 FM |
455 | |
456 | /** | |
d29a9a8a | 457 | @return @true if the entry by this name exists. |
23324ae1 | 458 | */ |
382f12e4 | 459 | virtual bool HasEntry(const wxString& strName) const = 0; |
23324ae1 FM |
460 | |
461 | /** | |
d29a9a8a | 462 | @return @true if the group by this name exists. |
23324ae1 | 463 | */ |
382f12e4 | 464 | virtual bool HasGroup(const wxString& strName) const = 0; |
23324ae1 | 465 | |
bd0812fe | 466 | //@} |
3c4f71cc | 467 | |
3c4f71cc | 468 | |
bd0812fe BP |
469 | /** |
470 | @name Miscellaneous Functions | |
23324ae1 | 471 | */ |
bd0812fe | 472 | //@{ |
23324ae1 FM |
473 | |
474 | /** | |
bd0812fe | 475 | Returns the application name. |
23324ae1 | 476 | */ |
bd0812fe | 477 | wxString GetAppName() const; |
23324ae1 FM |
478 | |
479 | /** | |
bd0812fe BP |
480 | Returns the vendor name. |
481 | */ | |
482 | wxString GetVendorName() const; | |
3c4f71cc | 483 | |
bd0812fe | 484 | //@} |
3c4f71cc | 485 | |
3c4f71cc | 486 | |
bd0812fe BP |
487 | /** |
488 | @name Key Access | |
3c4f71cc | 489 | |
bd0812fe | 490 | See @ref configbase_keyaccess |
23324ae1 | 491 | */ |
bd0812fe | 492 | //@{ |
23324ae1 FM |
493 | |
494 | /** | |
bd0812fe BP |
495 | Permanently writes all changes (otherwise, they're only written from |
496 | object's destructor). | |
23324ae1 | 497 | */ |
382f12e4 | 498 | virtual bool Flush(bool bCurrentOnly = false) = 0; |
23324ae1 | 499 | |
23324ae1 | 500 | /** |
bd0812fe BP |
501 | Read a string from the key, returning @true if the value was read. If |
502 | the key was not found, @a str is not changed. | |
1058f652 MB |
503 | |
504 | @beginWxPerlOnly | |
505 | Not supported by wxPerl. | |
506 | @endWxPerlOnly | |
23324ae1 | 507 | */ |
328f5751 | 508 | bool Read(const wxString& key, wxString* str) const; |
bd0812fe BP |
509 | /** |
510 | Read a string from the key. The default value is returned if the key | |
511 | was not found. | |
512 | ||
d29a9a8a | 513 | @return @true if value was really read, @false if the default was used. |
1058f652 MB |
514 | |
515 | @beginWxPerlOnly | |
516 | Not supported by wxPerl. | |
517 | @endWxPerlOnly | |
bd0812fe | 518 | */ |
11e3af6e FM |
519 | bool Read(const wxString& key, wxString* str, |
520 | const wxString& defaultVal) const; | |
bd0812fe BP |
521 | /** |
522 | Another version of Read(), returning the string value directly. | |
1058f652 MB |
523 | |
524 | @beginWxPerlOnly | |
525 | In wxPerl, this can be called as: | |
526 | - Read(key): returns the empty string if no key is found | |
527 | - Read(key, default): returns the default value if no key is found | |
528 | @endWxPerlOnly | |
bd0812fe BP |
529 | */ |
530 | const wxString Read(const wxString& key, | |
11e3af6e | 531 | const wxString& defaultVal) const; |
bd0812fe BP |
532 | /** |
533 | Reads a long value, returning @true if the value was found. If the | |
534 | value was not found, @a l is not changed. | |
1058f652 MB |
535 | |
536 | @beginWxPerlOnly | |
537 | Not supported by wxPerl. | |
538 | @endWxPerlOnly | |
bd0812fe | 539 | */ |
11e3af6e | 540 | bool Read(const wxString& key, long* l) const; |
bd0812fe BP |
541 | /** |
542 | Reads a long value, returning @true if the value was found. If the | |
543 | value was not found, @a defaultVal is used instead. | |
1058f652 MB |
544 | |
545 | @beginWxPerlOnly | |
546 | In wxPerl, this can be called as: | |
547 | - ReadInt(key): returns the 0 if no key is found | |
548 | - ReadInt(key, default): returns the default value if no key is found | |
549 | @endWxPerlOnly | |
bd0812fe | 550 | */ |
11e3af6e FM |
551 | bool Read(const wxString& key, long* l, |
552 | long defaultVal) const; | |
bd0812fe BP |
553 | /** |
554 | Reads a double value, returning @true if the value was found. If the | |
555 | value was not found, @a d is not changed. | |
1058f652 MB |
556 | |
557 | @beginWxPerlOnly | |
558 | Not supported by wxPerl. | |
559 | @endWxPerlOnly | |
bd0812fe | 560 | */ |
11e3af6e | 561 | bool Read(const wxString& key, double* d) const; |
bd0812fe BP |
562 | /** |
563 | Reads a double value, returning @true if the value was found. If the | |
564 | value was not found, @a defaultVal is used instead. | |
1058f652 MB |
565 | |
566 | @beginWxPerlOnly | |
567 | In wxPerl, this can be called as: | |
568 | - ReadFloat(key): returns the 0.0 if no key is found | |
569 | - ReadFloat(key, default): returns the default value if no key is found | |
570 | @endWxPerlOnly | |
bd0812fe | 571 | */ |
11e3af6e | 572 | bool Read(const wxString& key, double* d, |
bd0812fe | 573 | double defaultVal) const; |
384859f8 | 574 | |
bd0812fe | 575 | /** |
384859f8 VZ |
576 | Reads a float value, returning @true if the value was found. |
577 | ||
0c99d1fb | 578 | If the value was not found, @a f is not changed. |
384859f8 VZ |
579 | |
580 | Notice that the value is read as a double but must be in a valid range | |
581 | for floats for the function to return @true. | |
582 | ||
583 | @since 2.9.1 | |
584 | ||
585 | @beginWxPerlOnly | |
586 | Not supported by wxPerl. | |
587 | @endWxPerlOnly | |
588 | */ | |
384859f8 | 589 | bool Read(const wxString& key, float* f) const; |
0c99d1fb VZ |
590 | /** |
591 | Reads a float value, returning @true if the value was found. | |
592 | ||
593 | If the value was not found, @a defaultVal is used instead. | |
594 | ||
595 | Notice that the value is read as a double but must be in a valid range | |
596 | for floats for the function to return @true. | |
597 | ||
598 | @since 2.9.1 | |
599 | ||
600 | @beginWxPerlOnly | |
601 | Not supported by wxPerl. | |
602 | @endWxPerlOnly | |
603 | */ | |
384859f8 | 604 | bool Read(const wxString& key, float* f, float defaultVal) const; |
384859f8 VZ |
605 | |
606 | /** | |
0c99d1fb | 607 | Reads a boolean value, returning @true if the value was found. If the |
bd0812fe | 608 | value was not found, @a b is not changed. |
1058f652 | 609 | |
384859f8 VZ |
610 | @since 2.9.1 |
611 | ||
1058f652 MB |
612 | @beginWxPerlOnly |
613 | Not supported by wxPerl. | |
614 | @endWxPerlOnly | |
bd0812fe | 615 | */ |
11e3af6e | 616 | bool Read(const wxString& key, bool* b) const; |
bd0812fe | 617 | /** |
0c99d1fb | 618 | Reads a boolean value, returning @true if the value was found. If the |
bd0812fe | 619 | value was not found, @a defaultVal is used instead. |
1058f652 MB |
620 | |
621 | @beginWxPerlOnly | |
622 | In wxPerl, this can be called as: | |
623 | - ReadBool(key): returns false if no key is found | |
624 | - ReadBool(key, default): returns the default value if no key is found | |
625 | @endWxPerlOnly | |
bd0812fe | 626 | */ |
11e3af6e FM |
627 | bool Read(const wxString& key, bool* d, |
628 | bool defaultVal) const; | |
bd0812fe BP |
629 | /** |
630 | Reads a binary block, returning @true if the value was found. If the | |
631 | value was not found, @a buf is not changed. | |
632 | */ | |
11e3af6e | 633 | bool Read(const wxString& key, wxMemoryBuffer* buf) const; |
bd0812fe BP |
634 | /** |
635 | Reads a value of type T, for which function wxFromString() is defined, | |
636 | returning @true if the value was found. If the value was not found, | |
637 | @a value is not changed. | |
638 | */ | |
11e3af6e | 639 | bool Read(const wxString& key, T* value) const; |
bd0812fe BP |
640 | /** |
641 | Reads a value of type T, for which function wxFromString() is defined, | |
642 | returning @true if the value was found. If the value was not found, | |
643 | @a defaultVal is used instead. | |
644 | */ | |
11e3af6e FM |
645 | bool Read(const wxString& key, T* value, |
646 | const T& defaultVal) const; | |
23324ae1 FM |
647 | |
648 | /** | |
bd0812fe BP |
649 | Reads a bool value from the key and returns it. @a defaultVal is |
650 | returned if the key is not found. | |
23324ae1 | 651 | */ |
382f12e4 | 652 | bool ReadBool(const wxString& key, bool defaultVal) const; |
23324ae1 FM |
653 | |
654 | /** | |
bd0812fe BP |
655 | Reads a double value from the key and returns it. @a defaultVal is |
656 | returned if the key is not found. | |
23324ae1 | 657 | */ |
382f12e4 | 658 | double ReadDouble(const wxString& key, double defaultVal) const; |
23324ae1 FM |
659 | |
660 | /** | |
bd0812fe BP |
661 | Reads a long value from the key and returns it. @a defaultVal is |
662 | returned if the key is not found. | |
23324ae1 | 663 | */ |
328f5751 | 664 | long ReadLong(const wxString& key, long defaultVal) const; |
23324ae1 FM |
665 | |
666 | /** | |
bd0812fe BP |
667 | Reads a value of type T (for which the function wxFromString() must be |
668 | defined) from the key and returns it. @a defaultVal is returned if the | |
669 | key is not found. | |
23324ae1 | 670 | */ |
328f5751 | 671 | T ReadObject(const wxString& key, T const& defaultVal) const; |
23324ae1 FM |
672 | |
673 | /** | |
bd0812fe BP |
674 | Writes the wxString value to the config file and returns @true on |
675 | success. | |
676 | */ | |
677 | bool Write(const wxString& key, const wxString& value); | |
678 | /** | |
679 | Writes the long value to the config file and returns @true on success. | |
680 | */ | |
681 | bool Write(const wxString& key, long value); | |
682 | /** | |
683 | Writes the double value to the config file and returns @true on | |
684 | success. | |
7e22c2bd VZ |
685 | |
686 | Notice that if floating point numbers are saved as strings (as is the | |
687 | case with the configuration files used by wxFileConfig), this function | |
688 | uses the C locale for writing out the number, i.e. it will always use a | |
689 | period as the decimal separator, irrespectively of the current locale. | |
690 | This behaviour is new since wxWidgets 2.9.1 as the current locale was | |
691 | used before, but the change should be transparent because both C and | |
692 | current locales are tried when reading the numbers back. | |
bd0812fe BP |
693 | */ |
694 | bool Write(const wxString& key, double value); | |
695 | /** | |
696 | Writes the bool value to the config file and returns @true on success. | |
23324ae1 | 697 | */ |
bd0812fe BP |
698 | bool Write(const wxString& key, bool value); |
699 | /** | |
700 | Writes the wxMemoryBuffer value to the config file and returns @true on | |
701 | success. | |
702 | */ | |
703 | bool Write(const wxString& key, const wxMemoryBuffer& buf); | |
704 | /** | |
705 | Writes the specified value to the config file and returns @true on | |
706 | success. The function wxToString() must be defined for type @e T. | |
707 | */ | |
708 | bool Write(const wxString& key, T const& buf); | |
709 | ||
710 | //@} | |
23324ae1 FM |
711 | |
712 | ||
713 | /** | |
bd0812fe BP |
714 | @name Rename Entries/Groups |
715 | ||
716 | These functions allow renaming entries or subgroups of the current | |
717 | group. They will return @false on error, typically because either the | |
718 | entry/group with the original name doesn't exist, because the | |
719 | entry/group with the new name already exists or because the function is | |
720 | not supported in this wxConfig implementation. | |
23324ae1 | 721 | */ |
bd0812fe | 722 | //@{ |
23324ae1 FM |
723 | |
724 | /** | |
bd0812fe BP |
725 | Renames an entry in the current group. The entries names (both the old |
726 | and the new one) shouldn't contain backslashes, i.e. only simple names | |
727 | and not arbitrary paths are accepted by this function. | |
728 | ||
d29a9a8a BP |
729 | @return @false if @a oldName doesn't exist or if @a newName already |
730 | exists. | |
23324ae1 | 731 | */ |
382f12e4 FM |
732 | virtual bool RenameEntry(const wxString& oldName, |
733 | const wxString& newName) = 0; | |
23324ae1 FM |
734 | |
735 | /** | |
bd0812fe BP |
736 | Renames a subgroup of the current group. The subgroup names (both the |
737 | old and the new one) shouldn't contain backslashes, i.e. only simple | |
738 | names and not arbitrary paths are accepted by this function. | |
739 | ||
d29a9a8a BP |
740 | @return @false if @a oldName doesn't exist or if @a newName already |
741 | exists. | |
23324ae1 | 742 | */ |
382f12e4 FM |
743 | virtual bool RenameGroup(const wxString& oldName, |
744 | const wxString& newName) = 0; | |
bd0812fe BP |
745 | |
746 | //@} | |
747 | ||
23324ae1 FM |
748 | |
749 | /** | |
bd0812fe BP |
750 | @name Delete Entries/Groups |
751 | ||
752 | These functions delete entries and/or groups of entries from the config | |
753 | file. DeleteAll() is especially useful if you want to erase all traces | |
754 | of your program presence: for example, when you uninstall it. | |
23324ae1 | 755 | */ |
bd0812fe | 756 | //@{ |
23324ae1 FM |
757 | |
758 | /** | |
bd0812fe | 759 | Delete the whole underlying object (disk file, registry key, ...). |
d13b34d3 | 760 | Primarily for use by uninstallation routine. |
23324ae1 | 761 | */ |
382f12e4 | 762 | virtual bool DeleteAll() = 0; |
23324ae1 FM |
763 | |
764 | /** | |
bd0812fe BP |
765 | Deletes the specified entry and the group it belongs to if it was the |
766 | last key in it and the second parameter is @true. | |
23324ae1 | 767 | */ |
382f12e4 FM |
768 | virtual bool DeleteEntry(const wxString& key, |
769 | bool bDeleteGroupIfEmpty = true) = 0; | |
23324ae1 FM |
770 | |
771 | /** | |
bd0812fe BP |
772 | Delete the group (with all subgroups). If the current path is under the |
773 | group being deleted it is changed to its deepest still existing | |
774 | component. E.g. if the current path is @c "/A/B/C/D" and the group @c C | |
775 | is deleted, the path becomes @c "/A/B". | |
776 | */ | |
382f12e4 | 777 | virtual bool DeleteGroup(const wxString& key) = 0; |
3c4f71cc | 778 | |
bd0812fe | 779 | //@} |
3c4f71cc | 780 | |
3c4f71cc | 781 | |
bd0812fe BP |
782 | /** |
783 | @name Options | |
23324ae1 | 784 | |
bd0812fe BP |
785 | Some aspects of wxConfigBase behaviour can be changed during run-time. |
786 | The first of them is the expansion of environment variables in the | |
787 | string values read from the config file: for example, if you have the | |
788 | following in your config file: | |
23324ae1 | 789 | |
bd0812fe BP |
790 | @code |
791 | # config file for my program | |
792 | UserData = $HOME/data | |
3c4f71cc | 793 | |
bd0812fe BP |
794 | # the following syntax is valud only under Windows |
795 | UserData = %windir%\\data.dat | |
796 | @endcode | |
3c4f71cc | 797 | |
bd0812fe BP |
798 | The call to Read("UserData") will return something like |
799 | @c "/home/zeitlin/data" on linux for example. | |
3c4f71cc | 800 | |
bd0812fe BP |
801 | Although this feature is very useful, it may be annoying if you read a |
802 | value which containts '$' or '%' symbols (% is used for environment | |
803 | variables expansion under Windows) which are not used for environment | |
804 | variable expansion. In this situation you may call | |
805 | SetExpandEnvVars(@false) just before reading this value and | |
806 | SetExpandEnvVars(@true) just after. Another solution would be to prefix | |
807 | the offending symbols with a backslash. | |
23324ae1 | 808 | */ |
bd0812fe | 809 | //@{ |
23324ae1 | 810 | |
bd0812fe BP |
811 | /** |
812 | Returns @true if we are expanding environment variables in key values. | |
813 | */ | |
814 | bool IsExpandingEnvVars() const; | |
23324ae1 | 815 | |
23324ae1 | 816 | /** |
bd0812fe BP |
817 | Returns @true if we are writing defaults back to the config file. |
818 | */ | |
819 | bool IsRecordingDefaults() const; | |
3c4f71cc | 820 | |
bd0812fe BP |
821 | /** |
822 | Determine whether we wish to expand environment variables in key | |
823 | values. | |
824 | */ | |
825 | void SetExpandEnvVars(bool bDoIt = true); | |
3c4f71cc | 826 | |
bd0812fe BP |
827 | /** |
828 | Sets whether defaults are recorded to the config file whenever an | |
829 | attempt to read the value which is not present in it is done. | |
3c4f71cc | 830 | |
bd0812fe BP |
831 | If on (default is off) all default values for the settings used by the |
832 | program are written back to the config file. This allows the user to | |
833 | see what config options may be changed and is probably useful only for | |
834 | wxFileConfig. | |
835 | */ | |
836 | void SetRecordDefaults(bool bDoIt = true); | |
3c4f71cc | 837 | |
bd0812fe | 838 | //@} |
3c4f71cc | 839 | |
3c4f71cc | 840 | |
bd0812fe | 841 | /** |
ba67d647 VZ |
842 | Create a new config object and sets it as the current one. |
843 | ||
844 | This function will create the most appropriate implementation of | |
845 | wxConfig available for the current platform. By default this means that | |
846 | the system registry will be used for storing the configuration | |
847 | information under MSW and a file under the user home directory (see | |
848 | wxStandardPaths::GetUserConfigDir()) elsewhere. | |
849 | ||
850 | If you prefer to use the configuration files everywhere, you can define | |
851 | @c wxUSE_CONFIG_NATIVE to 0 when compiling wxWidgets. Or you can simply | |
852 | always create wxFileConfig explicitly. | |
853 | ||
854 | Finally, if you want to create a custom wxConfig subclass you may | |
855 | change this function behaviour by overriding wxAppTraits::CreateConfig() | |
856 | to create it. An example when this could be useful could be an | |
857 | application which could be installed either normally (in which case the | |
858 | default behaviour of using wxRegConfig is appropriate) or in a | |
859 | "portable" way in which case a wxFileConfig with a file in the program | |
860 | directory would be used and the choice would be done in CreateConfig() | |
861 | at run-time. | |
bd0812fe BP |
862 | */ |
863 | static wxConfigBase* Create(); | |
3c4f71cc | 864 | |
bd0812fe BP |
865 | /** |
866 | Calling this function will prevent @e Get() from automatically creating | |
867 | a new config object if the current one is @NULL. It might be useful to | |
868 | call it near the program end to prevent "accidental" creation of a new | |
869 | config object. | |
870 | */ | |
871 | static void DontCreateOnDemand(); | |
3c4f71cc | 872 | |
bd0812fe BP |
873 | /** |
874 | Get the current config object. If there is no current object and | |
875 | @a CreateOnDemand is @true, this creates one (using Create()) unless | |
876 | DontCreateOnDemand() was called previously. | |
877 | */ | |
878 | static wxConfigBase* Get(bool CreateOnDemand = true); | |
3c4f71cc | 879 | |
bd0812fe BP |
880 | /** |
881 | Sets the config object as the current one, returns the pointer to the | |
882 | previous current object (both the parameter and returned value may be | |
883 | @NULL). | |
23324ae1 | 884 | */ |
bd0812fe | 885 | static wxConfigBase* Set(wxConfigBase* pConfig); |
23324ae1 | 886 | }; |
e54c96f1 | 887 | |
b2fc9f0e FM |
888 | |
889 | /** | |
890 | @class wxConfigPathChanger | |
891 | ||
892 | A handy little class which changes the current path in a wxConfig object and restores it in dtor. | |
893 | Declaring a local variable of this type, it's possible to work in a specific directory | |
894 | and ensure that the path is automatically restored when the function returns. | |
895 | ||
896 | For example: | |
897 | @code | |
898 | // this function loads somes settings from the given wxConfig object; | |
899 | // the path selected inside it is left unchanged | |
900 | bool LoadMySettings(wxConfigBase* cfg) | |
901 | { | |
902 | wxConfigPathChanger changer(cfg, "/Foo/Data/SomeString"); | |
903 | wxString str; | |
904 | if ( !config->Read("SomeString", &str) ) { | |
905 | wxLogError("Couldn't read SomeString!"); | |
906 | return false; | |
907 | // NOTE: without wxConfigPathChanger it would be easy to forget to | |
908 | // set the old path back into the wxConfig object before this return! | |
909 | } | |
910 | ||
911 | // do something useful with SomeString... | |
912 | ||
913 | return true; // again: wxConfigPathChanger dtor will restore the original wxConfig path | |
914 | } | |
915 | @endcode | |
916 | ||
917 | @library{wxbase} | |
918 | @category{cfg} | |
919 | */ | |
123919a9 | 920 | class wxConfigPathChanger |
b2fc9f0e FM |
921 | { |
922 | public: | |
923 | ||
924 | /** | |
925 | Changes the path of the given wxConfigBase object so that the key @a strEntry is accessible | |
926 | (for read or write). | |
927 | ||
928 | In other words, the ctor uses wxConfigBase::SetPath() with everything which precedes the | |
929 | last slash of @a strEntry, so that: | |
930 | @code | |
931 | wxConfigPathChanger(wxConfigBase::Get(), "/MyProgram/SomeKeyName"); | |
932 | @endcode | |
933 | has the same effect of: | |
934 | @code | |
935 | wxConfigPathChanger(wxConfigBase::Get(), "/MyProgram/"); | |
936 | @endcode | |
937 | */ | |
938 | wxConfigPathChanger(const wxConfigBase *pContainer, const wxString& strEntry); | |
939 | ||
940 | /** | |
941 | Restores the path selected, inside the wxConfig object passed to the ctor, to the path which was | |
942 | selected when the wxConfigPathChanger ctor was called. | |
943 | */ | |
944 | ~wxConfigPathChanger(); | |
945 | ||
946 | /** | |
947 | Returns the name of the key which was passed to the ctor. | |
948 | The "name" is just anything which follows the last slash of the string given to the ctor. | |
949 | */ | |
29a3f654 | 950 | const wxString& Name() const; |
b2fc9f0e FM |
951 | |
952 | /** | |
953 | This method must be called if the original path inside the wxConfig object | |
954 | (i.e. the current path at the moment of creation of this wxConfigPathChanger object) | |
955 | could have been deleted, thus preventing wxConfigPathChanger from restoring the not | |
956 | existing (any more) path. | |
957 | ||
958 | If the original path doesn't exist any more, the path will be restored to | |
959 | the deepest still existing component of the old path. | |
960 | */ | |
961 | void UpdateIfDeleted(); | |
962 | }; | |
963 |