1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: SWIG interface for wxConfig, wxFileConfig, etc.
7 // Created: 25-Nov-1998
9 // Copyright: (c) 2003 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 //---------------------------------------------------------------------------
22 //---------------------------------------------------------------------------
26 static PyObject* __EnumerationHelper(bool flag, wxString& str, long index) {
27 PyObject* ret = PyTuple_New(3);
29 PyTuple_SET_ITEM(ret, 0, PyInt_FromLong(flag));
30 PyTuple_SET_ITEM(ret, 1, wx2PyString(str));
31 PyTuple_SET_ITEM(ret, 2, PyInt_FromLong(index));
40 wxCONFIG_USE_LOCAL_FILE,
41 wxCONFIG_USE_GLOBAL_FILE,
42 wxCONFIG_USE_RELATIVE_PATH,
43 wxCONFIG_USE_NO_ESCAPE_CHARACTERS
48 // abstract base class wxConfigBase which defines the interface for derived
51 // wxConfig organizes the items in a tree-like structure (modeled after the
52 // Unix/Dos filesystem). There are groups (directories) and keys (files).
53 // There is always one current group given by the current path.
55 // Keys are pairs "key_name = value" where value may be of string or integer
56 // (long) type (TODO doubles and other types such as wxDate coming soon).
59 // wxConfigBase(const wxString& appName = wxPyEmptyString, **** An ABC
60 // const wxString& vendorName = wxPyEmptyString,
61 // const wxString& localFilename = wxPyEmptyString,
62 // const wxString& globalFilename = wxPyEmptyString,
71 Type_Integer, // use Read(long *)
72 Type_Float // use Read(double *)
76 // sets the config object, returns the previous pointer
77 static wxConfigBase *Set(wxConfigBase *pConfig);
79 // get the config object, creates it on demand unless DontCreateOnDemand
81 static wxConfigBase *Get(bool createOnDemand = True);
83 // create a new config object: this function will create the "best"
84 // implementation of wxConfig available for the current platform, see
85 // comments near definition wxUSE_CONFIG_NATIVE for details. It returns
86 // the created object and also sets it as ms_pConfig.
87 static wxConfigBase *Create();
89 // should Get() try to create a new log object if the current one is NULL?
90 static void DontCreateOnDemand();
94 // set current path: if the first character is '/', it's the absolute path,
95 // otherwise it's a relative path. '..' is supported. If the strPath
96 // doesn't exist it is created.
97 virtual void SetPath(const wxString& strPath);
99 // retrieve the current path (always as absolute path)
100 virtual const wxString& GetPath() const;
105 // Each of these enumeration methods return a 3-tuple consisting of
106 // the continue flag, the value string, and the index for the next call.
108 // enumerate subgroups
109 PyObject* GetFirstGroup() {
114 cont = self->GetFirstGroup(value, index);
115 return __EnumerationHelper(cont, value, index);
117 PyObject* GetNextGroup(long index) {
121 cont = self->GetNextGroup(value, index);
122 return __EnumerationHelper(cont, value, index);
126 PyObject* GetFirstEntry() {
131 cont = self->GetFirstEntry(value, index);
132 return __EnumerationHelper(cont, value, index);
134 PyObject* GetNextEntry(long index) {
138 cont = self->GetNextEntry(value, index);
139 return __EnumerationHelper(cont, value, index);
145 // get number of entries/subgroups in the current group, with or without
147 virtual size_t GetNumberOfEntries(bool bRecursive = False) const;
148 virtual size_t GetNumberOfGroups(bool bRecursive = False) const;
150 // returns True if the group by this name exists
151 virtual bool HasGroup(const wxString& strName) const;
153 // same as above, but for an entry
154 virtual bool HasEntry(const wxString& strName) const;
156 // returns True if either a group or an entry with a given name exist
157 bool Exists(const wxString& strName) const;
159 // get the entry type
160 virtual EntryType GetEntryType(const wxString& name) const;
163 // Key access. Returns the value of key if it exists, defaultVal otherwise
164 wxString Read(const wxString& key, const wxString& defaultVal = wxPyEmptyString);
167 long ReadInt(const wxString& key, long defaultVal = 0) {
169 self->Read(key, &rv, defaultVal);
172 double ReadFloat(const wxString& key, double defaultVal = 0.0) {
174 self->Read(key, &rv, defaultVal);
177 bool ReadBool(const wxString& key, bool defaultVal = False) {
179 self->Read(key, &rv, defaultVal);
185 // write the value (return True on success)
186 bool Write(const wxString& key, const wxString& value);
187 %name(WriteInt)bool Write(const wxString& key, long value);
188 %name(WriteFloat)bool Write(const wxString& key, double value);
189 %name(WriteBool)bool Write(const wxString& key, bool value);
192 // permanently writes all changes
193 virtual bool Flush(bool bCurrentOnly = False);
195 // renaming, all functions return False on failure (probably because the new
196 // name is already taken by an existing entry)
198 virtual bool RenameEntry(const wxString& oldName,
199 const wxString& newName);
201 virtual bool RenameGroup(const wxString& oldName,
202 const wxString& newName);
204 // deletes the specified entry and the group it belongs to if
205 // it was the last key in it and the second parameter is True
206 virtual bool DeleteEntry(const wxString& key,
207 bool bDeleteGroupIfEmpty = True);
209 // delete the group (with all subgroups)
210 virtual bool DeleteGroup(const wxString& key);
212 // delete the whole underlying object (disk file, registry key, ...)
213 // primarly for use by desinstallation routine.
214 virtual bool DeleteAll();
217 // we can automatically expand environment variables in the config entries
218 // (this option is on by default, you can turn it on/off at any time)
219 bool IsExpandingEnvVars() const;
220 void SetExpandEnvVars(bool bDoIt = True);
222 // recording of default values
223 void SetRecordDefaults(bool bDoIt = True);
224 bool IsRecordingDefaults() const;
226 // does expansion only if needed
227 wxString ExpandEnvVars(const wxString& str) const;
230 wxString GetAppName() const;
231 wxString GetVendorName() const;
233 // Used wxIniConfig to set members in constructor
234 void SetAppName(const wxString& appName);
235 void SetVendorName(const wxString& vendorName);
237 void SetStyle(long style);
238 long GetStyle() const;
242 //---------------------------------------------------------------------------
244 // a handy little class which changes current path to the path of given entry
245 // and restores it in dtor: so if you declare a local variable of this type,
246 // you work in the entry directory and the path is automatically restored
247 // when the function returns
248 // Taken out of wxConfig since not all compilers can cope with nested classes.
249 class wxConfigPathChanger
252 // ctor/dtor do path changing/restorin
253 wxConfigPathChanger(const wxConfigBase *pContainer, const wxString& strEntry);
254 ~wxConfigPathChanger();
257 const wxString& Name() const { return m_strName; }
261 //---------------------------------------------------------------------------
263 // This will be a wxRegConfig on Win32 and wxFileConfig otherwise.
264 class wxConfig : public wxConfigBase {
266 wxConfig(const wxString& appName = wxPyEmptyString,
267 const wxString& vendorName = wxPyEmptyString,
268 const wxString& localFilename = wxPyEmptyString,
269 const wxString& globalFilename = wxPyEmptyString,
275 // Sometimes it's nice to explicitly have a wxFileConfig too.
276 class wxFileConfig : public wxConfigBase {
278 wxFileConfig(const wxString& appName = wxPyEmptyString,
279 const wxString& vendorName = wxPyEmptyString,
280 const wxString& localFilename = wxPyEmptyString,
281 const wxString& globalFilename = wxPyEmptyString,
287 //---------------------------------------------------------------------------
290 // Replace environment variables ($SOMETHING) with their values. The format is
291 // $VARNAME or ${VARNAME} where VARNAME contains alphanumeric characters and
292 // '_' only. '$' must be escaped ('\$') in order to be taken literally.
293 wxString wxExpandEnvVars(const wxString &sz);
296 //---------------------------------------------------------------------------