]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/registry.h
fixes for __WINCE_NET__ build (slightly modified patch 1892654)
[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 #include "wx/defs.h"
16
17 #if wxUSE_REGKEY
18
19 class WXDLLIMPEXP_FWD_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 HKPD, // performance data (WinNT/2K only)
57 HKCC, // current config
58 HKDD, // dynamic data (Win95/98 only)
59 HKMAX
60 };
61
62 // access mode for the key
63 enum AccessMode
64 {
65 Read, // read-only
66 Write // read and write
67 };
68
69 // information about standard (predefined) registry keys
70 // number of standard keys
71 static const size_t nStdKeys;
72 // get the name of a standard key
73 static const wxChar *GetStdKeyName(size_t key);
74 // get the short name of a standard key
75 static const wxChar *GetStdKeyShortName(size_t key);
76 // get StdKey from root HKEY
77 static StdKey GetStdKeyFromHkey(WXHKEY hkey);
78
79 // extacts the std key prefix from the string (return value) and
80 // leaves only the part after it (i.e. modifies the string passed!)
81 static StdKey ExtractKeyName(wxString& str);
82
83 // ctors
84 // root key is set to HKCR (the only root key under Win16)
85 wxRegKey();
86 // strKey is the full name of the key (i.e. starting with HKEY_xxx...)
87 wxRegKey(const wxString& strKey);
88 // strKey is the name of key under (standard key) keyParent
89 wxRegKey(StdKey keyParent, const wxString& strKey);
90 // strKey is the name of key under (previously created) keyParent
91 wxRegKey(const wxRegKey& keyParent, const wxString& strKey);
92 // dtor closes the key
93 ~wxRegKey();
94
95 // change key (closes the previously opened key if any)
96 // the name is absolute, i.e. should start with HKEY_xxx
97 void SetName(const wxString& strKey);
98 // the name is relative to the parent key
99 void SetName(StdKey keyParent, const wxString& strKey);
100 // the name is relative to the parent key
101 void SetName(const wxRegKey& keyParent, const wxString& strKey);
102 // hKey should be opened and will be closed in wxRegKey dtor
103 void SetHkey(WXHKEY hKey);
104
105 // get infomation about the key
106 // get the (full) key name. Abbreviate std root keys if bShortPrefix.
107 wxString GetName(bool bShortPrefix = true) const;
108 // return true if the key exists
109 bool Exists() const;
110 // get the info about key (any number of these pointers may be NULL)
111 bool GetKeyInfo(size_t *pnSubKeys, // number of subkeys
112 size_t *pnMaxKeyLen, // max len of subkey name
113 size_t *pnValues, // number of values
114 size_t *pnMaxValueLen) const;
115 // return true if the key is opened
116 bool IsOpened() const { return m_hKey != 0; }
117 // for "if ( !key ) wxLogError(...)" kind of expressions
118 operator bool() const { return m_dwLastError == 0; }
119
120 // operations on the key itself
121 // explicitly open the key (will be automatically done by all functions
122 // which need the key to be opened if the key is not opened yet)
123 bool Open(AccessMode mode = Write);
124 // create the key: will fail if the key already exists and !bOkIfExists
125 bool Create(bool bOkIfExists = true);
126 // rename a value from old name to new one
127 bool RenameValue(const wxString& szValueOld, const wxString& szValueNew);
128 // rename the key
129 bool Rename(const wxString& szNewName);
130 // copy value to another key possibly changing its name (by default it will
131 // remain the same)
132 bool CopyValue(const wxString& szValue, wxRegKey& keyDst,
133 const wxString& szNewName = wxEmptyString);
134
135 // copy the entire contents of the key recursively to another location
136 bool Copy(const wxString& 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 wxString& szKey);
147 // deletes the named value (may be empty string to remove the default value)
148 bool DeleteValue(const wxString& szValue);
149
150 // access to values and subkeys
151 // get value type
152 ValueType GetValueType(const wxString& szValue) const;
153 // returns true if the value contains a number (else it's some string)
154 bool IsNumericValue(const wxString& szValue) const;
155
156 // assignment operators set the default value of the key
157 wxRegKey& operator=(const wxString& strValue)
158 { SetValue(wxEmptyString, strValue); return *this; }
159
160 // query the default value of the key: implicitly or explicitly
161 wxString QueryDefaultValue() const;
162 operator wxString() const { return QueryDefaultValue(); }
163
164 // named values
165
166 // set the string value
167 bool SetValue(const wxString& szValue, const wxString& strValue);
168 // retrieve the string value
169 bool QueryValue(const wxString& szValue, wxString& strValue) const
170 { return QueryValue(szValue, strValue, false); }
171 // retrieve raw string value
172 bool QueryRawValue(const wxString& szValue, wxString& strValue) const
173 { return QueryValue(szValue, strValue, true); }
174 // retrieve either raw or expanded string value
175 bool QueryValue(const wxString& szValue, wxString& strValue, bool raw) const;
176
177 // set the numeric value
178 bool SetValue(const wxString& szValue, long lValue);
179 // return the numeric value
180 bool QueryValue(const wxString& szValue, long *plValue) const;
181 // set the binary value
182 bool SetValue(const wxString& szValue, const wxMemoryBuffer& buf);
183 // return the binary value
184 bool QueryValue(const wxString& szValue, wxMemoryBuffer& buf) const;
185
186 // query existence of a key/value
187 // return true if value exists
188 bool HasValue(const wxString& szKey) const;
189 // return true if given subkey exists
190 bool HasSubKey(const wxString& szKey) const;
191 // return true if any subkeys exist
192 bool HasSubkeys() const;
193 // return true if any values exist
194 bool HasValues() const;
195 // return true if the key is empty (nothing under this key)
196 bool IsEmpty() const { return !HasSubkeys() && !HasValues(); }
197
198 // enumerate values and subkeys
199 bool GetFirstValue(wxString& strValueName, long& lIndex);
200 bool GetNextValue (wxString& strValueName, long& lIndex) const;
201
202 bool GetFirstKey (wxString& strKeyName , long& lIndex);
203 bool GetNextKey (wxString& strKeyName , long& lIndex) const;
204
205 // export the contents of this key and all its subkeys to the given file
206 // (which won't be overwritten, it's an error if it already exists)
207 //
208 // note that we export the key in REGEDIT4 format, not RegSaveKey() binary
209 // format nor newer REGEDIT5 one
210 bool Export(const wxString& filename) const;
211
212 // same as above but write to the given (opened) stream
213 bool Export(wxOutputStream& ostr) const;
214
215
216 // for wxRegConfig usage only: preallocate some memory for the name
217 void ReserveMemoryForName(size_t bytes) { m_strKey.reserve(bytes); }
218
219 private:
220 // common part of all ctors
221 void Init()
222 {
223 m_hKey = (WXHKEY) NULL;
224 m_dwLastError = 0;
225 }
226
227 // recursive helper for Export()
228 bool DoExport(wxOutputStream& ostr) const;
229
230 // export a single value
231 bool DoExportValue(wxOutputStream& ostr, const wxString& name) const;
232
233 // return the text representation (in REGEDIT4 format) of the value with the
234 // given name
235 wxString FormatValue(const wxString& name) const;
236
237
238 WXHKEY m_hKey, // our handle
239 m_hRootKey; // handle of the top key (i.e. StdKey)
240 wxString m_strKey; // key name (relative to m_hRootKey)
241
242 AccessMode m_mode; // valid only if key is opened
243 long m_dwLastError; // last error (0 if none)
244
245
246 DECLARE_NO_COPY_CLASS(wxRegKey)
247 };
248
249 #endif // wxUSE_REGKEY
250
251 #endif // _WX_MSW_REGISTRY_H_
252