]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/registry.h
Oups...
[wxWidgets.git] / include / wx / msw / registry.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/registry.h
3 // Purpose: Registry classes and functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 03.04.1998
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_MSW_REGISTRY_H_
13 #define _WX_MSW_REGISTRY_H_
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "registry.h"
17 #endif
18
19 class WXDLLIMPEXP_BASE wxOutputStream;
20
21 // ----------------------------------------------------------------------------
22 // class wxRegKey encapsulates window HKEY handle
23 // ----------------------------------------------------------------------------
24
25 class WXDLLIMPEXP_BASE wxRegKey
26 {
27 public:
28 // NB: do _not_ change the values of elements in these enumerations!
29
30 // registry value types (with comments from winnt.h)
31 enum ValueType
32 {
33 Type_None, // No value type
34 Type_String, // Unicode nul terminated string
35 Type_Expand_String, // Unicode nul terminated string
36 // (with environment variable references)
37 Type_Binary, // Free form binary
38 Type_Dword, // 32-bit number
39 Type_Dword_little_endian // 32-bit number
40 = Type_Dword, // (same as Type_DWORD)
41 Type_Dword_big_endian, // 32-bit number
42 Type_Link, // Symbolic Link (unicode)
43 Type_Multi_String, // Multiple Unicode strings
44 Type_Resource_list, // Resource list in the resource map
45 Type_Full_resource_descriptor, // Resource list in the hardware description
46 Type_Resource_requirements_list // ???
47 };
48
49 // predefined registry keys
50 enum StdKey
51 {
52 HKCR, // classes root
53 HKCU, // current user
54 HKLM, // local machine
55 HKUSR // users
56 #ifndef __WXWINCE__
57 ,
58 HKPD // performance data (WinNT/2K only)
59 #endif
60 #if WINVER >= 0x0400
61 ,
62 HKCC, // current config (starting from Win95/NT 4.0)
63 HKDD // dynamic data (Win95/98 only)
64 #endif // Winver
65 };
66
67 // access mode for the key
68 enum AccessMode
69 {
70 Read, // read-only
71 Write // read and write
72 };
73
74 // information about standard (predefined) registry keys
75 // number of standard keys
76 static const size_t nStdKeys;
77 // get the name of a standard key
78 static const wxChar *GetStdKeyName(size_t key);
79 // get the short name of a standard key
80 static const wxChar *GetStdKeyShortName(size_t key);
81 // get StdKey from root HKEY
82 static StdKey GetStdKeyFromHkey(WXHKEY hkey);
83
84 // extacts the std key prefix from the string (return value) and
85 // leaves only the part after it (i.e. modifies the string passed!)
86 static StdKey ExtractKeyName(wxString& str);
87
88 // ctors
89 // root key is set to HKCR (the only root key under Win16)
90 wxRegKey();
91 // strKey is the full name of the key (i.e. starting with HKEY_xxx...)
92 wxRegKey(const wxString& strKey);
93 // strKey is the name of key under (standard key) keyParent
94 wxRegKey(StdKey keyParent, const wxString& strKey);
95 // strKey is the name of key under (previously created) keyParent
96 wxRegKey(const wxRegKey& keyParent, const wxString& strKey);
97 // dtor closes the key
98 ~wxRegKey();
99
100 // change key (closes the previously opened key if any)
101 // the name is absolute, i.e. should start with HKEY_xxx
102 void SetName(const wxString& strKey);
103 // the name is relative to the parent key
104 void SetName(StdKey keyParent, const wxString& strKey);
105 // the name is relative to the parent key
106 void SetName(const wxRegKey& keyParent, const wxString& strKey);
107 // hKey should be opened and will be closed in wxRegKey dtor
108 void SetHkey(WXHKEY hKey);
109
110 // get infomation about the key
111 // get the (full) key name. Abbreviate std root keys if bShortPrefix.
112 wxString GetName(bool bShortPrefix = true) const;
113 // return true if the key exists
114 bool Exists() const;
115 // get the info about key (any number of these pointers may be NULL)
116 bool GetKeyInfo(size_t *pnSubKeys, // number of subkeys
117 size_t *pnMaxKeyLen, // max len of subkey name
118 size_t *pnValues, // number of values
119 size_t *pnMaxValueLen) const;
120 // return true if the key is opened
121 bool IsOpened() const { return m_hKey != 0; }
122 // for "if ( !key ) wxLogError(...)" kind of expressions
123 operator bool() const { return m_dwLastError == 0; }
124
125 // operations on the key itself
126 // explicitly open the key (will be automatically done by all functions
127 // which need the key to be opened if the key is not opened yet)
128 bool Open(AccessMode mode = Write);
129 // create the key: will fail if the key already exists and !bOkIfExists
130 bool Create(bool bOkIfExists = true);
131 // rename a value from old name to new one
132 bool RenameValue(const wxChar *szValueOld, const wxChar *szValueNew);
133 // rename the key
134 bool Rename(const wxChar *szNewName);
135 // copy value to another key possibly changing its name (by default it will
136 // remain the same)
137 bool CopyValue(const wxChar *szValue, wxRegKey& keyDst,
138 const wxChar *szNewName = NULL);
139 // copy the entire contents of the key recursively to another location
140 bool Copy(const wxChar *szNewName);
141 // same as Copy() but using a key and not the name
142 bool Copy(wxRegKey& keyDst);
143 // close the key (will be automatically done in dtor)
144 bool Close();
145
146 // deleting keys/values
147 // deletes this key and all of it's subkeys/values
148 bool DeleteSelf();
149 // deletes the subkey with all of it's subkeys/values recursively
150 bool DeleteKey(const wxChar *szKey);
151 // deletes the named value (may be NULL to remove the default value)
152 bool DeleteValue(const wxChar *szValue);
153
154 // access to values and subkeys
155 // get value type
156 ValueType GetValueType(const wxChar *szValue) const;
157 // returns true if the value contains a number (else it's some string)
158 bool IsNumericValue(const wxChar *szValue) const;
159
160 // assignment operators set the default value of the key
161 wxRegKey& operator=(const wxString& strValue)
162 { SetValue(NULL, strValue); return *this; }
163 wxRegKey& operator=(long lValue)
164 { SetValue(NULL, lValue); return *this; }
165
166 // query the default value of the key: implicitly or explicitly
167 wxString QueryDefaultValue() const;
168 operator wxString() const { return QueryDefaultValue(); }
169
170 // named values
171
172 // set the string value
173 bool SetValue(const wxChar *szValue, const wxString& strValue);
174 // retrieve the string value
175 bool QueryValue(const wxChar *szValue, wxString& strValue) const
176 { return QueryValue(szValue, strValue, false); }
177 // retrieve raw string value
178 bool QueryRawValue(const wxChar *szValue, wxString& strValue) const
179 { return QueryValue(szValue, strValue, true); }
180 // retrieve either raw or expanded string value
181 bool QueryValue(const wxChar *szValue, wxString& strValue, bool raw) const;
182
183 // set the numeric value
184 bool SetValue(const wxChar *szValue, long lValue);
185 // return the numeric value
186 bool QueryValue(const wxChar *szValue, long *plValue) const;
187 // set the binary value
188 bool SetValue(const wxChar *szValue, const wxMemoryBuffer& buf);
189 // return the binary value
190 bool QueryValue(const wxChar *szValue, wxMemoryBuffer& buf) const;
191
192 // query existence of a key/value
193 // return true if value exists
194 bool HasValue(const wxChar *szKey) const;
195 // return true if given subkey exists
196 bool HasSubKey(const wxChar *szKey) const;
197 // return true if any subkeys exist
198 bool HasSubkeys() const;
199 // return true if any values exist
200 bool HasValues() const;
201 // return true if the key is empty (nothing under this key)
202 bool IsEmpty() const { return !HasSubkeys() && !HasValues(); }
203
204 // enumerate values and subkeys
205 bool GetFirstValue(wxString& strValueName, long& lIndex);
206 bool GetNextValue (wxString& strValueName, long& lIndex) const;
207
208 bool GetFirstKey (wxString& strKeyName , long& lIndex);
209 bool GetNextKey (wxString& strKeyName , long& lIndex) const;
210
211 // export the contents of this key and all its subkeys to the given file
212 // (which won't be overwritten, it's an error if it already exists)
213 //
214 // note that we export the key in REGEDIT4 format, not RegSaveKey() binary
215 // format nor newer REGEDIT5 one
216 bool Export(const wxString& filename) const;
217
218 // same as above but write to the given (opened) stream
219 bool Export(wxOutputStream& ostr) const;
220
221
222 // for wxRegConfig usage only: preallocate some memory for the name
223 void ReserveMemoryForName(size_t bytes) { m_strKey.reserve(bytes); }
224
225 private:
226 // common part of all ctors
227 void Init()
228 {
229 m_hKey = (WXHKEY) NULL;
230 m_dwLastError = 0;
231 }
232
233 // recursive helper for Export()
234 bool DoExport(wxOutputStream& ostr) const;
235
236 // export a single value
237 bool DoExportValue(wxOutputStream& ostr, const wxString& name) const;
238
239 // return the text representation (in REGEDIT4 format) of the value with the
240 // given name
241 wxString FormatValue(const wxString& name) const;
242
243
244 WXHKEY m_hKey, // our handle
245 m_hRootKey; // handle of the top key (i.e. StdKey)
246 wxString m_strKey; // key name (relative to m_hRootKey)
247
248 long m_dwLastError; // last error (0 if none)
249
250
251 DECLARE_NO_COPY_CLASS(wxRegKey)
252 };
253
254 #endif // _WX_MSW_REGISTRY_H_
255