config.h 34 KB

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