]> git.saurik.com Git - wxWidgets.git/blame - src/msw/regconf.cpp
New frame class
[wxWidgets.git] / src / msw / regconf.cpp
CommitLineData
19454fa0
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/regconf.cpp
6d833566 3// Purpose:
19454fa0 4// Author: Vadim Zeitlin
6d833566 5// Modified by:
19454fa0
VZ
6// Created: 27.04.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
cfe780fb
JS
12#ifdef __GNUG__
13#pragma implementation "regconf.h"
14#endif
15
a3b46648
UU
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
19454fa0 18
a3b46648
UU
19#ifdef __BORLANDC__
20#pragma hdrstop
82cf4761
VZ
21#endif
22
82cf4761 23#ifndef WX_PRECOMP
61ba49f2 24 #include "wx/string.h"
82cf4761
VZ
25#endif //WX_PRECOMP
26
61ba49f2
VZ
27#include "wx/event.h"
28#include "wx/app.h"
29#include "wx/log.h"
f6bcfd97
BP
30
31#if wxUSE_CONFIG
32
61ba49f2 33#include "wx/config.h"
3d05544e
JS
34
35#ifndef __WIN16__
36
61ba49f2
VZ
37#include "wx/msw/registry.h"
38#include "wx/msw/regconf.h"
19454fa0
VZ
39
40// ----------------------------------------------------------------------------
41// constants
42// ----------------------------------------------------------------------------
43
44// we put our data in HKLM\SOFTWARE_KEY\appname
c19a8a9a 45#define SOFTWARE_KEY wxString("Software\\")
19454fa0
VZ
46
47// ----------------------------------------------------------------------------
48// global functions
49// ----------------------------------------------------------------------------
50
51// get the value if the key is opened and it exists
52bool TryGetValue(const wxRegKey& key, const wxString& str, wxString& strVal)
53{
54 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, strVal);
55}
56
57bool TryGetValue(const wxRegKey& key, const wxString& str, long *plVal)
58{
59 return key.IsOpened() && key.HasValue(str) && key.QueryValue(str, plVal);
60}
61
62// ============================================================================
63// implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// ctor/dtor
68// ----------------------------------------------------------------------------
18244936 69
040f0110
VZ
70// create the config object which stores its data under HKCU\vendor\app and, if
71// style & wxCONFIG_USE_GLOBAL_FILE, under HKLM\vendor\app
72wxRegConfig::wxRegConfig(const wxString& appName, const wxString& vendorName,
73 const wxString& strLocal, const wxString& strGlobal,
74 long style)
75 : wxConfigBase(appName, vendorName, strLocal, strGlobal, style)
19454fa0 76{
040f0110 77 wxString strRoot;
19454fa0 78
040f0110 79 bool bDoUseGlobal = (style & wxCONFIG_USE_GLOBAL_FILE) != 0;
41286812 80
040f0110
VZ
81 // the convention is to put the programs keys under <vendor>\<appname>
82 // (but it can be overriden by specifying the pathes explicitly in strLocal
83 // and/or strGlobal)
84 if ( strLocal.IsEmpty() || (strGlobal.IsEmpty() && bDoUseGlobal) )
85 {
86 if ( vendorName.IsEmpty() )
87 {
88 if ( wxTheApp )
89 strRoot = wxTheApp->GetVendorName();
90 }
91 else
92 {
93 strRoot = vendorName;
94 }
18244936 95
040f0110
VZ
96 // no '\\' needed if no vendor name
97 if ( !strRoot.IsEmpty() )
18244936 98 {
040f0110 99 strRoot += '\\';
18244936 100 }
18244936 101
040f0110
VZ
102 if ( appName.IsEmpty() )
103 {
223d09f6 104 wxCHECK_RET( wxTheApp, wxT("No application name in wxRegConfig ctor!") );
040f0110
VZ
105 strRoot << wxTheApp->GetAppName();
106 }
107 else
18244936 108 {
040f0110 109 strRoot << appName;
18244936 110 }
040f0110
VZ
111 }
112 //else: we don't need to do all the complicated stuff above
18244936 113
040f0110
VZ
114 wxString str = strLocal.IsEmpty() ? strRoot : strLocal;
115 m_keyLocalRoot.SetName(wxRegKey::HKCU, SOFTWARE_KEY + str);
116 m_keyLocal.SetName(m_keyLocalRoot, "");
18244936 117
040f0110
VZ
118 if ( bDoUseGlobal )
119 {
120 str = strGlobal.IsEmpty() ? strRoot : strGlobal;
121 m_keyGlobalRoot.SetName(wxRegKey::HKLM, SOFTWARE_KEY + str);
122 m_keyGlobal.SetName(m_keyGlobalRoot, "");
123 }
18244936
JS
124
125 // Create() will Open() if key already exists
126 m_keyLocalRoot.Create();
127
128 // as it's the same key, Open() shouldn't fail (i.e. no need for Create())
129 m_keyLocal.Open();
130
040f0110
VZ
131 // OTOH, this key may perfectly not exist, so suppress error messages the call
132 // to Open() might generate
133 if ( bDoUseGlobal )
134 {
135 wxLogNull nolog;
136 m_keyGlobalRoot.Open();
137 }
18244936 138}
19454fa0
VZ
139
140wxRegConfig::~wxRegConfig()
141{
142 // nothing to do - key will be closed in their dtors
143}
144
145// ----------------------------------------------------------------------------
146// path management
147// ----------------------------------------------------------------------------
148void wxRegConfig::SetPath(const wxString& strPath)
149{
82cf4761 150 wxArrayString aParts;
19454fa0 151
41286812
VZ
152 // because GetPath() returns "" when we're at root, we must understand
153 // empty string as "/"
154 if ( strPath.IsEmpty() || (strPath[0] == wxCONFIG_PATH_SEPARATOR) ) {
19454fa0 155 // absolute path
82cf4761 156 wxSplitPath(aParts, strPath);
19454fa0
VZ
157 }
158 else {
159 // relative path, combine with current one
160 wxString strFullPath = GetPath();
4d0c0756 161 strFullPath << wxCONFIG_PATH_SEPARATOR << strPath;
82cf4761 162 wxSplitPath(aParts, strFullPath);
19454fa0
VZ
163 }
164
165 // recombine path parts in one variable
b568d04f 166 wxString strOldPath = m_strPath, strRegPath;
19454fa0 167 m_strPath.Empty();
c86f1403 168 for ( size_t n = 0; n < aParts.Count(); n++ ) {
19454fa0 169 strRegPath << '\\' << aParts[n];
4d0c0756 170 m_strPath << wxCONFIG_PATH_SEPARATOR << aParts[n];
19454fa0
VZ
171 }
172
b568d04f
VZ
173 if ( m_strPath == strOldPath )
174 return;
175
176 // as we create the registry key when SetPath(key) is done, we can be left
177 // with plenty of empty keys if this was only done to try to read some value
178 // which, in fact, doesn't exist - to prevent this from happening we
179 // automatically delete the old key if it was empty
180 if ( m_keyLocal.IsEmpty() )
181 {
182 m_keyLocal.DeleteSelf();
183 }
184
19454fa0
VZ
185 // change current key(s)
186 m_keyLocal.SetName(m_keyLocalRoot, strRegPath);
187 m_keyGlobal.SetName(m_keyGlobalRoot, strRegPath);
188 m_keyLocal.Create();
189
190 wxLogNull nolog;
191 m_keyGlobal.Open();
192}
193
194// ----------------------------------------------------------------------------
195// enumeration (works only with current group)
196// ----------------------------------------------------------------------------
197
198/*
6d833566 199 We want to enumerate all local keys/values after the global ones, but, of
19454fa0
VZ
200 course, we don't want to repeat a key which appears locally as well as
201 globally twice.
202
203 We use the 15th bit of lIndex for distinction between global and local.
204 */
205
206#define LOCAL_MASK 0x8000
207#define IS_LOCAL_INDEX(l) (((l) & LOCAL_MASK) != 0)
208
02569ba8 209bool wxRegConfig::GetFirstGroup(wxString& str, long& lIndex) const
19454fa0
VZ
210{
211 lIndex = 0;
212 return GetNextGroup(str, lIndex);
213}
214
02569ba8 215bool wxRegConfig::GetNextGroup(wxString& str, long& lIndex) const
19454fa0
VZ
216{
217 // are we already enumerating local entries?
218 if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) {
219 // try to find a global entry which doesn't appear locally
220 do {
221 if ( !m_keyGlobal.GetNextKey(str, lIndex) ) {
222 // no more global entries
223 lIndex |= LOCAL_MASK;
224 break;
225 }
226 } while( m_keyLocal.HasSubKey(str) );
227 }
228
229 // much easier with local entries: get the next one we find
230 // (don't forget to clear our flag bit and set it again later)
231 lIndex &= ~LOCAL_MASK;
232 bool bOk = m_keyLocal.GetNextKey(str, lIndex);
233 lIndex |= LOCAL_MASK;
234
235 return bOk;
236}
237
02569ba8 238bool wxRegConfig::GetFirstEntry(wxString& str, long& lIndex) const
19454fa0
VZ
239{
240 lIndex = 0;
82cf4761 241 return GetNextEntry(str, lIndex);
19454fa0
VZ
242}
243
02569ba8 244bool wxRegConfig::GetNextEntry(wxString& str, long& lIndex) const
19454fa0
VZ
245{
246 // are we already enumerating local entries?
247 if ( m_keyGlobal.IsOpened() && !IS_LOCAL_INDEX(lIndex) ) {
248 // try to find a global entry which doesn't appear locally
249 do {
82cf4761 250 if ( !m_keyGlobal.GetNextValue(str, lIndex) ) {
19454fa0
VZ
251 // no more global entries
252 lIndex |= LOCAL_MASK;
253 break;
254 }
82cf4761 255 } while( m_keyLocal.HasValue(str) );
19454fa0
VZ
256 }
257
258 // much easier with local entries: get the next one we find
259 // (don't forget to clear our flag bit and set it again later)
260 lIndex &= ~LOCAL_MASK;
82cf4761 261 bool bOk = m_keyLocal.GetNextValue(str, lIndex);
19454fa0
VZ
262 lIndex |= LOCAL_MASK;
263
264 return bOk;
265}
266
c86f1403 267size_t wxRegConfig::GetNumberOfEntries(bool bRecursive) const
82cf4761 268{
c86f1403 269 size_t nEntries = 0;
82cf4761
VZ
270
271 // dummy vars
272 wxString str;
273 long l;
6a23cbce 274 bool bCont = ((wxRegConfig*)this)->GetFirstEntry(str, l);
82cf4761
VZ
275 while ( bCont ) {
276 nEntries++;
277
6a23cbce 278 bCont = ((wxRegConfig*)this)->GetNextEntry(str, l);
82cf4761
VZ
279 }
280
281 return nEntries;
282}
283
c86f1403 284size_t wxRegConfig::GetNumberOfGroups(bool bRecursive) const
82cf4761 285{
c86f1403 286 size_t nGroups = 0;
82cf4761
VZ
287
288 // dummy vars
289 wxString str;
290 long l;
6a23cbce 291 bool bCont = ((wxRegConfig*)this)->GetFirstGroup(str, l);
82cf4761
VZ
292 while ( bCont ) {
293 nGroups++;
294
6a23cbce 295 bCont = ((wxRegConfig*)this)->GetNextGroup(str, l);
82cf4761
VZ
296 }
297
298 return nGroups;
299}
300
6d833566
VZ
301// ----------------------------------------------------------------------------
302// tests for existence
303// ----------------------------------------------------------------------------
304
61ba49f2 305bool wxRegConfig::HasGroup(const wxString& key) const
6d833566 306{
61ba49f2
VZ
307 wxConfigPathChanger path(this, key);
308
309 wxString strName(path.Name());
310
311 return m_keyLocal.HasSubKey(strName) || m_keyGlobal.HasSubKey(strName);
6d833566
VZ
312}
313
61ba49f2 314bool wxRegConfig::HasEntry(const wxString& key) const
6d833566 315{
61ba49f2
VZ
316 wxConfigPathChanger path(this, key);
317
318 wxString strName(path.Name());
319
320 return m_keyLocal.HasValue(strName) || m_keyGlobal.HasValue(strName);
321}
322
323wxConfigBase::EntryType wxRegConfig::GetEntryType(const wxString& key) const
324{
325 wxConfigPathChanger path(this, key);
326
327 wxString strName(path.Name());
328
329 bool isNumeric;
330 if ( m_keyLocal.HasValue(strName) )
331 isNumeric = m_keyLocal.IsNumericValue(strName);
332 else if ( m_keyGlobal.HasValue(strName) )
333 isNumeric = m_keyGlobal.IsNumericValue(strName);
334 else
335 return wxConfigBase::Type_Unknown;
336
337 return isNumeric ? wxConfigBase::Type_Integer : wxConfigBase::Type_String;
6d833566
VZ
338}
339
19454fa0
VZ
340// ----------------------------------------------------------------------------
341// reading/writing
342// ----------------------------------------------------------------------------
343
18244936 344bool wxRegConfig::Read(const wxString& key, wxString *pStr) const
19454fa0 345{
18244936 346 wxConfigPathChanger path(this, key);
19454fa0 347
cf447356 348 bool bQueryGlobal = TRUE;
19454fa0
VZ
349
350 // if immutable key exists in global key we must check that it's not
351 // overriden by the local key with the same name
352 if ( IsImmutable(path.Name()) ) {
02569ba8 353 if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) {
19454fa0 354 if ( m_keyLocal.HasValue(path.Name()) ) {
223d09f6 355 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
19454fa0
VZ
356 path.Name().c_str());
357 }
18244936 358 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
cf447356 359 return TRUE;
19454fa0
VZ
360 }
361 else {
362 // don't waste time - it's not there anyhow
cf447356 363 bQueryGlobal = FALSE;
19454fa0
VZ
364 }
365 }
366
367 // first try local key
02569ba8
VZ
368 if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
369 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
41286812 370 // nothing to do
18244936 371
18244936
JS
372 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
373 return TRUE;
374 }
375
376 return FALSE;
377}
378
379bool wxRegConfig::Read(const wxString& key, wxString *pStr,
380 const wxString& szDefault) const
381{
382 wxConfigPathChanger path(this, key);
383
384 bool bQueryGlobal = TRUE;
385
386 // if immutable key exists in global key we must check that it's not
387 // overriden by the local key with the same name
388 if ( IsImmutable(path.Name()) ) {
389 if ( TryGetValue(m_keyGlobal, path.Name(), *pStr) ) {
390 if ( m_keyLocal.HasValue(path.Name()) ) {
223d09f6 391 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
18244936
JS
392 path.Name().c_str());
393 }
394
395 return TRUE;
396 }
397 else {
398 // don't waste time - it's not there anyhow
399 bQueryGlobal = FALSE;
400 }
401 }
402
403 // first try local key
404 if ( TryGetValue(m_keyLocal, path.Name(), *pStr) ||
405 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), *pStr)) ) {
406 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
407 return TRUE;
41286812
VZ
408 }
409 else {
410 if ( IsRecordingDefaults() ) {
18244936 411 ((wxRegConfig*)this)->Write(key, szDefault);
41286812
VZ
412 }
413
414 // default value
415 *pStr = szDefault;
19454fa0
VZ
416 }
417
41286812 418 *pStr = wxConfigBase::ExpandEnvVars(*pStr);
baeed289 419
cf447356 420 return FALSE;
19454fa0
VZ
421}
422
18244936 423bool wxRegConfig::Read(const wxString& key, long *plResult) const
19454fa0 424{
18244936 425 wxConfigPathChanger path(this, key);
19454fa0 426
cf447356 427 bool bQueryGlobal = TRUE;
19454fa0
VZ
428
429 // if immutable key exists in global key we must check that it's not
430 // overriden by the local key with the same name
431 if ( IsImmutable(path.Name()) ) {
02569ba8 432 if ( TryGetValue(m_keyGlobal, path.Name(), plResult) ) {
19454fa0 433 if ( m_keyLocal.HasValue(path.Name()) ) {
223d09f6 434 wxLogWarning(wxT("User value for immutable key '%s' ignored."),
19454fa0
VZ
435 path.Name().c_str());
436 }
437
cf447356 438 return TRUE;
19454fa0
VZ
439 }
440 else {
441 // don't waste time - it's not there anyhow
cf447356 442 bQueryGlobal = FALSE;
19454fa0
VZ
443 }
444 }
445
446 // first try local key
02569ba8
VZ
447 if ( TryGetValue(m_keyLocal, path.Name(), plResult) ||
448 (bQueryGlobal && TryGetValue(m_keyGlobal, path.Name(), plResult)) ) {
cf447356 449 return TRUE;
19454fa0 450 }
cf447356 451 return FALSE;
19454fa0
VZ
452}
453
18244936 454bool wxRegConfig::Write(const wxString& key, const wxString& szValue)
19454fa0 455{
18244936 456 wxConfigPathChanger path(this, key);
19454fa0
VZ
457
458 if ( IsImmutable(path.Name()) ) {
223d09f6 459 wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
cf447356 460 return FALSE;
19454fa0
VZ
461 }
462
463 return m_keyLocal.SetValue(path.Name(), szValue);
464}
465
18244936 466bool wxRegConfig::Write(const wxString& key, long lValue)
19454fa0 467{
18244936 468 wxConfigPathChanger path(this, key);
19454fa0
VZ
469
470 if ( IsImmutable(path.Name()) ) {
223d09f6 471 wxLogError(wxT("Can't change immutable entry '%s'."), path.Name().c_str());
cf447356 472 return FALSE;
19454fa0
VZ
473 }
474
475 return m_keyLocal.SetValue(path.Name(), lValue);
476}
477
478// ----------------------------------------------------------------------------
89077ebc
VZ
479// renaming
480// ----------------------------------------------------------------------------
481
482bool wxRegConfig::RenameEntry(const wxString& oldName, const wxString& newName)
483{
484 // check that the old entry exists...
485 if ( !HasEntry(oldName) )
486 return FALSE;
487
488 // and that the new one doesn't
489 if ( HasEntry(newName) )
490 return FALSE;
491
492 // delete the old entry and create the new one - but do in the reverse
493 // order to not lose the data if Create() fails
494
495 bool ok;
496 if ( m_keyLocal.IsNumericValue(oldName) )
497 {
498 long val;
499 ok = m_keyLocal.QueryValue(oldName, &val) &&
500 m_keyLocal.SetValue(newName, val);
501 }
502 else
503 {
504 wxString val;
505 ok = m_keyLocal.QueryValue(oldName, val) &&
506 m_keyLocal.SetValue(newName, val);
507 }
508
509 if ( !ok )
510 return FALSE;
511
512 if ( !m_keyLocal.DeleteValue(oldName) )
513 {
514 m_keyLocal.DeleteValue(newName);
515
516 return FALSE;
517 }
518
519 return TRUE;
520}
521
522bool wxRegConfig::RenameGroup(const wxString& oldName, const wxString& newName)
523{
524 // check that the old group exists...
525 if ( !HasGroup(oldName) )
526 return FALSE;
527
528 // and that the new one doesn't
529 if ( HasGroup(newName) )
530 return FALSE;
531
532 // TODO there is no way to rename a registry key - we must do a deep copy
533 // ourselves
223d09f6 534 wxFAIL_MSG(wxT("Registry key renaming not implemented"));
89077ebc
VZ
535
536 return FALSE;
537}
538
539// ----------------------------------------------------------------------------
19454fa0
VZ
540// deleting
541// ----------------------------------------------------------------------------
18244936 542bool wxRegConfig::DeleteEntry(const wxString& value, bool bGroupIfEmptyAlso)
19454fa0 543{
18244936 544 wxConfigPathChanger path(this, value);
19454fa0
VZ
545
546 if ( !m_keyLocal.DeleteValue(path.Name()) )
cf447356 547 return FALSE;
19454fa0 548
92049cd4 549 if ( m_keyLocal.IsEmpty() ) {
08159082 550 wxString strKey = GetPath().AfterLast(wxCONFIG_PATH_SEPARATOR);
19454fa0
VZ
551 SetPath(".."); // changes m_keyLocal
552 return m_keyLocal.DeleteKey(strKey);
553 }
554
cf447356 555 return TRUE;
19454fa0
VZ
556}
557
18244936 558bool wxRegConfig::DeleteGroup(const wxString& key)
19454fa0 559{
18244936 560 wxConfigPathChanger path(this, key);
19454fa0
VZ
561
562 return m_keyLocal.DeleteKey(path.Name());
563}
564
565bool wxRegConfig::DeleteAll()
566{
19454fa0
VZ
567 m_keyLocal.Close();
568 m_keyGlobal.Close();
90186e52 569
19454fa0 570 bool bOk = m_keyLocalRoot.DeleteSelf();
90186e52
VZ
571
572 // make sure that we opened m_keyGlobalRoot and so it has a reasonable name:
573 // otherwise we will delete HKEY_CLASSES_ROOT recursively
574 if ( bOk && m_keyGlobalRoot.IsOpened() )
19454fa0
VZ
575 bOk = m_keyGlobalRoot.DeleteSelf();
576
577 return bOk;
578}
3d05544e
JS
579
580#endif
581 // __WIN16__
582
f6bcfd97
BP
583#endif
584 // wxUSE_CONFIG