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