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