]> git.saurik.com Git - wxWidgets.git/blame - src/common/config.cpp
add support for alpha in color dialog on OSX, see #14127
[wxWidgets.git] / src / common / config.cpp
CommitLineData
c801d85f 1///////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/common/config.cpp
5fe256de 3// Purpose: implementation of wxConfigBase class
c801d85f 4// Author: Vadim Zeitlin
ba7f9a90 5// Modified by:
c801d85f
KB
6// Created: 07.04.98
7// RCS-ID: $Id$
9d55bfef 8// Copyright: (c) 1997 Karsten Ballueder Ballueder@usa.net
c801d85f 9// Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 10// Licence: wxWindows licence
c801d85f
KB
11///////////////////////////////////////////////////////////////////////////////
12
855e92a8
VZ
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
c801d85f 16
9869734d 17#include "wx/wxprec.h"
c801d85f 18
88a7a4e1
WS
19#ifdef __BORLANDC__
20 #pragma hdrstop
c801d85f
KB
21#endif //__BORLANDC__
22
ecef04c2
VZ
23#ifndef wxUSE_CONFIG_NATIVE
24 #define wxUSE_CONFIG_NATIVE 1
25#endif
26
21709999 27#include "wx/config.h"
88a7a4e1
WS
28
29#ifndef WX_PRECOMP
30 #include "wx/intl.h"
e4db172a 31 #include "wx/log.h"
670f9935 32 #include "wx/app.h"
de6185e2 33 #include "wx/utils.h"
aaa6d89a 34 #include "wx/arrstr.h"
18680f86 35 #include "wx/math.h"
88a7a4e1
WS
36#endif //WX_PRECOMP
37
48c8a76b 38#if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
ce4169a4 39
84281b92 40#include "wx/apptrait.h"
9869734d 41#include "wx/file.h"
9869734d 42
9869734d 43#include <stdlib.h>
f6bcfd97 44#include <ctype.h>
2ba41305 45#include <limits.h> // for INT_MAX
384859f8 46#include <float.h> // for FLT_MAX
855e92a8
VZ
47
48// ----------------------------------------------------------------------------
49// global and class static variables
50// ----------------------------------------------------------------------------
51
275bf4c1 52wxConfigBase *wxConfigBase::ms_pConfig = NULL;
c9d59ee7 53bool wxConfigBase::ms_bAutoCreate = true;
c801d85f
KB
54
55// ============================================================================
56// implementation
57// ============================================================================
58
84281b92
VZ
59// ----------------------------------------------------------------------------
60// wxAppTraitsBase
61// ----------------------------------------------------------------------------
62
63wxConfigBase *wxAppTraitsBase::CreateConfig()
64{
65 return new
d98a58c5 66 #if defined(__WINDOWS__) && wxUSE_CONFIG_NATIVE
84281b92 67 wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
84281b92
VZ
68 #else // either we're under Unix or wish to use files even under Windows
69 wxFileConfig(wxTheApp->GetAppName());
70 #endif
71}
72
c801d85f 73// ----------------------------------------------------------------------------
5fe256de 74// wxConfigBase
c801d85f 75// ----------------------------------------------------------------------------
c4ec0ce8 76IMPLEMENT_ABSTRACT_CLASS(wxConfigBase, wxObject)
c801d85f 77
f6bcfd97
BP
78// Not all args will always be used by derived classes, but including them all
79// in each class ensures compatibility.
80wxConfigBase::wxConfigBase(const wxString& appName,
81 const wxString& vendorName,
82 const wxString& WXUNUSED(localFilename),
83 const wxString& WXUNUSED(globalFilename),
84 long style)
85 : m_appName(appName), m_vendorName(vendorName), m_style(style)
18244936 86{
c9d59ee7
WS
87 m_bExpandEnvVars = true;
88 m_bRecordDefaults = false;
18244936
JS
89}
90
5e233068
WS
91wxConfigBase::~wxConfigBase()
92{
93 // required here for Darwin
94}
95
5fe256de 96wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
855e92a8 97{
5fe256de 98 wxConfigBase *pOld = ms_pConfig;
855e92a8
VZ
99 ms_pConfig = pConfig;
100 return pOld;
101}
102
5fe256de 103wxConfigBase *wxConfigBase::Create()
855e92a8 104{
275bf4c1 105 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
84281b92 106 wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
9a83f860 107 wxCHECK_MSG( traits, NULL, wxT("create wxApp before calling this") );
84281b92
VZ
108
109 ms_pConfig = traits->CreateConfig();
275bf4c1
VZ
110 }
111
112 return ms_pConfig;
855e92a8
VZ
113}
114
2ba41305
VZ
115// ----------------------------------------------------------------------------
116// wxConfigBase reading entries
117// ----------------------------------------------------------------------------
18244936 118
2ba41305
VZ
119// implement both Read() overloads for the given type in terms of DoRead()
120#define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
121 bool wxConfigBase::Read(const wxString& key, type *val) const \
122 { \
9a83f860 123 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
2ba41305 124 \
2b5f62a0 125 if ( !DoRead##name(key, val) ) \
c9d59ee7 126 return false; \
2b5f62a0
VZ
127 \
128 *val = extra(*val); \
129 \
c9d59ee7 130 return true; \
2ba41305
VZ
131 } \
132 \
133 bool wxConfigBase::Read(const wxString& key, \
134 type *val, \
135 deftype defVal) const \
136 { \
9a83f860 137 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); \
2ba41305 138 \
2b5f62a0
VZ
139 bool read = DoRead##name(key, val); \
140 if ( !read ) \
2ba41305 141 { \
2b5f62a0
VZ
142 if ( IsRecordingDefaults() ) \
143 { \
144 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
145 } \
146 \
147 *val = defVal; \
2ba41305
VZ
148 } \
149 \
2b5f62a0 150 *val = extra(*val); \
2ba41305 151 \
2b5f62a0 152 return read; \
18244936 153 }
18244936 154
18244936 155
2ba41305
VZ
156IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
157IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
2ba41305
VZ
158IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
159IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
f6bcfd97 160
2ba41305 161#undef IMPLEMENT_READ_FOR_TYPE
18244936 162
3e1512cd
VZ
163// int is stored as long
164bool wxConfigBase::Read(const wxString& key, int *pi) const
18244936 165{
3e1512cd
VZ
166 long l = *pi;
167 bool r = Read(key, &l);
9a83f860 168 wxASSERT_MSG( l < INT_MAX, wxT("int overflow in wxConfig::Read") );
2ba41305 169 *pi = (int)l;
3e1512cd
VZ
170 return r;
171}
dfad0599 172
3e1512cd
VZ
173bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const
174{
175 long l = *pi;
176 bool r = Read(key, &l, defVal);
9a83f860 177 wxASSERT_MSG( l < INT_MAX, wxT("int overflow in wxConfig::Read") );
3e1512cd
VZ
178 *pi = (int)l;
179 return r;
dfad0599
JS
180}
181
384859f8
VZ
182// Read floats as doubles then just type cast it down.
183bool wxConfigBase::Read(const wxString& key, float* val) const
184{
185 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
186
187 double temp;
188 if ( !Read(key, &temp) )
189 return false;
190
191 wxCHECK_MSG( fabs(temp) <= FLT_MAX, false,
192 wxT("float overflow in wxConfig::Read") );
193 wxCHECK_MSG( (temp == 0.0) || (fabs(temp) >= FLT_MIN), false,
194 wxT("float underflow in wxConfig::Read") );
195
196 *val = static_cast<float>(temp);
197
198 return true;
199}
200
201bool wxConfigBase::Read(const wxString& key, float* val, float defVal) const
202{
203 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
204
205 if ( Read(key, val) )
206 return true;
207
208 *val = defVal;
209 return false;
210}
211
3e1512cd
VZ
212// the DoReadXXX() for the other types have implementation in the base class
213// but can be overridden in the derived ones
2ba41305 214bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
dfad0599 215{
9a83f860 216 wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") );
2ba41305 217
dfad0599 218 long l;
2ba41305 219 if ( !DoReadLong(key, &l) )
c9d59ee7 220 return false;
dfad0599 221
0231d18f
VZ
222 if ( l != 0 && l != 1 )
223 {
224 // Don't assert here as this could happen in the result of user editing
225 // the file directly and this not indicate a bug in the program but
226 // still complain that something is wrong.
227 wxLogWarning(_("Invalid value %ld for a boolean key \"%s\" in "
228 "config file."),
229 l, key);
230 }
855e92a8 231
2ba41305
VZ
232 *val = l != 0;
233
c9d59ee7 234 return true;
7c74e7fe 235}
1341230e 236
2ba41305 237bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
68275b85 238{
2ba41305
VZ
239 wxString str;
240 if ( Read(key, &str) )
241 {
7e22c2bd
VZ
242 if ( str.ToCDouble(val) )
243 return true;
244
245 // Previous versions of wxFileConfig wrote the numbers out using the
246 // current locale and not the C one as now, so attempt to parse the
247 // string as a number in the current locale too, for compatibility.
248 if ( str.ToDouble(val) )
249 return true;
2ba41305
VZ
250 }
251
c9d59ee7 252 return false;
68275b85
VZ
253}
254
2ba41305 255// string reading helper
6164d85e 256wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
8bbe427f
VZ
257{
258 wxString tmp; // Required for BC++
259 if (IsExpandingEnvVars())
260 tmp = wxExpandEnvVars(str);
261 else
262 tmp = str;
263 return tmp;
264}
18244936 265
2ba41305
VZ
266// ----------------------------------------------------------------------------
267// wxConfigBase writing
268// ----------------------------------------------------------------------------
269
270bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
271{
7e22c2bd
VZ
272 // Notice that we always write out the numbers in C locale and not the
273 // current one. This makes the config files portable between machines using
274 // different locales.
275 return DoWriteString(key, wxString::FromCDouble(val));
2ba41305
VZ
276}
277
2ba41305
VZ
278bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
279{
280 return DoWriteLong(key, value ? 1l : 0l);
281}
282
855e92a8 283// ----------------------------------------------------------------------------
18244936 284// wxConfigPathChanger
855e92a8
VZ
285// ----------------------------------------------------------------------------
286
18244936 287wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
6f73419b 288 const wxString& strEntry)
855e92a8 289{
6f73419b 290 m_bChanged = false;
f48a1159 291 m_pContainer = const_cast<wxConfigBase *>(pContainer);
08159082 292
1af292a6
VZ
293 // the path is everything which precedes the last slash and the name is
294 // everything after it -- and this works correctly if there is no slash too
295 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR, &m_strName);
855e92a8 296
08159082 297 // except in the special case of "/keyname" when there is nothing before "/"
b713f891
WS
298 if ( strPath.empty() &&
299 ((!strEntry.empty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
08159082 300 {
da468d38 301 strPath = wxCONFIG_PATH_SEPARATOR;
08159082 302 }
855e92a8 303
b713f891 304 if ( !strPath.empty() )
18860312
VZ
305 {
306 if ( m_pContainer->GetPath() != strPath )
307 {
6f73419b 308 // we do change the path so restore it later
18860312 309 m_bChanged = true;
b9a59c91
JS
310
311 /* JACS: work around a memory bug that causes an assert
312 when using wxRegConfig, related to reference-counting.
86501081 313 Can be reproduced by removing .wc_str() below and
b9a59c91
JS
314 adding the following code to the config sample OnInit under
315 Windows:
316
317 pConfig->SetPath(wxT("MySettings"));
318 pConfig->SetPath(wxT(".."));
319 int value;
9a83f860 320 pConfig->Read(wxT("MainWindowX"), & value);
b9a59c91 321 */
86501081 322 m_strOldPath = m_pContainer->GetPath().wc_str();
18860312
VZ
323 if ( *m_strOldPath.c_str() != wxCONFIG_PATH_SEPARATOR )
324 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
325 m_pContainer->SetPath(strPath);
326 }
855e92a8
VZ
327 }
328}
329
41f30152
VZ
330void wxConfigPathChanger::UpdateIfDeleted()
331{
332 // we don't have to do anything at all if we didn't change the path
333 if ( !m_bChanged )
334 return;
335
336 // find the deepest still existing parent path of the original path
337 while ( !m_pContainer->HasGroup(m_strOldPath) )
338 {
339 m_strOldPath = m_strOldPath.BeforeLast(wxCONFIG_PATH_SEPARATOR);
340 if ( m_strOldPath.empty() )
341 m_strOldPath = wxCONFIG_PATH_SEPARATOR;
342 }
343}
344
18244936 345wxConfigPathChanger::~wxConfigPathChanger()
855e92a8
VZ
346{
347 // only restore path if it was changed
348 if ( m_bChanged ) {
349 m_pContainer->SetPath(m_strOldPath);
350 }
351}
352
f559e621
VZ
353// this is a wxConfig method but it's mainly used with wxConfigPathChanger
354/* static */
355wxString wxConfigBase::RemoveTrailingSeparator(const wxString& key)
356{
357 wxString path(key);
358
359 // don't remove the only separator from a root group path!
360 while ( path.length() > 1 )
361 {
362 if ( *path.rbegin() != wxCONFIG_PATH_SEPARATOR )
363 break;
364
365 path.erase(path.end() - 1);
366 }
367
368 return path;
369}
370
21709999
JS
371#endif // wxUSE_CONFIG
372
c801d85f
KB
373// ----------------------------------------------------------------------------
374// static & global functions
375// ----------------------------------------------------------------------------
376
377// understands both Unix and Windows (but only under Windows) environment
378// variables expansion: i.e. $var, $(var) and ${var} are always understood
379// and in addition under Windows %var% is also.
1fd87ff7
SN
380
381// don't change the values the enum elements: they must be equal
382// to the matching [closing] delimiter.
383enum Bracket
384{
385 Bracket_None,
386 Bracket_Normal = ')',
387 Bracket_Curly = '}',
d98a58c5 388#ifdef __WINDOWS__
1fd87ff7
SN
389 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
390#endif
391 Bracket_Max
392};
393
f40ccee9 394wxString wxExpandEnvVars(const wxString& str)
c801d85f
KB
395{
396 wxString strResult;
aaa6d89a 397 strResult.Alloc(str.length());
c801d85f 398
c86f1403 399 size_t m;
aaa6d89a 400 for ( size_t n = 0; n < str.length(); n++ ) {
c9f78968 401 switch ( str[n].GetValue() ) {
d98a58c5 402#ifdef __WINDOWS__
f6bcfd97 403 case wxT('%'):
d98a58c5 404#endif // __WINDOWS__
f6bcfd97 405 case wxT('$'):
c801d85f
KB
406 {
407 Bracket bracket;
d98a58c5 408 #ifdef __WINDOWS__
f6bcfd97 409 if ( str[n] == wxT('%') )
c801d85f
KB
410 bracket = Bracket_Windows;
411 else
d98a58c5 412 #endif // __WINDOWS__
aaa6d89a 413 if ( n == str.length() - 1 ) {
c801d85f
KB
414 bracket = Bracket_None;
415 }
416 else {
c9f78968 417 switch ( str[n + 1].GetValue() ) {
f6bcfd97 418 case wxT('('):
ba7f9a90 419 bracket = Bracket_Normal;
c801d85f
KB
420 n++; // skip the bracket
421 break;
422
f6bcfd97 423 case wxT('{'):
c801d85f
KB
424 bracket = Bracket_Curly;
425 n++; // skip the bracket
426 break;
427
428 default:
429 bracket = Bracket_None;
430 }
431 }
432
433 m = n + 1;
434
aaa6d89a 435 while ( m < str.length() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
c801d85f 436 m++;
03647350 437
c801d85f
KB
438 wxString strVarName(str.c_str() + n + 1, m - n - 1);
439
1c193821 440#ifdef __WXWINCE__
86501081 441 const bool expanded = false;
1c193821 442#else
bf7f7793
RR
443 // NB: use wxGetEnv instead of wxGetenv as otherwise variables
444 // set through wxSetEnv may not be read correctly!
86501081 445 bool expanded = false;
bf7f7793
RR
446 wxString tmp;
447 if (wxGetEnv(strVarName, &tmp))
86501081
VS
448 {
449 strResult += tmp;
450 expanded = true;
c801d85f 451 }
86501081
VS
452 else
453#endif
454 {
c801d85f 455 // variable doesn't exist => don't change anything
d98a58c5 456 #ifdef __WINDOWS__
c801d85f
KB
457 if ( bracket != Bracket_Windows )
458 #endif
459 if ( bracket != Bracket_None )
460 strResult << str[n - 1];
461 strResult << str[n] << strVarName;
462 }
463
464 // check the closing bracket
465 if ( bracket != Bracket_None ) {
aaa6d89a 466 if ( m == str.length() || str[m] != (wxChar)bracket ) {
2b5f62a0
VZ
467 // under MSW it's common to have '%' characters in the registry
468 // and it's annoying to have warnings about them each time, so
469 // ignroe them silently if they are not used for env vars
470 //
471 // under Unix, OTOH, this warning could be useful for the user to
472 // understand why isn't the variable expanded as intended
d98a58c5 473 #ifndef __WINDOWS__
c6d60564
RN
474 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %u in '%s'."),
475 (char)bracket, (unsigned int) (m + 1), str.c_str());
d98a58c5 476 #endif // __WINDOWS__
c801d85f
KB
477 }
478 else {
906373bf 479 // skip closing bracket unless the variables wasn't expanded
86501081 480 if ( !expanded )
f6eaf52a 481 strResult << (wxChar)bracket;
c801d85f
KB
482 m++;
483 }
484 }
485
486 n = m - 1; // skip variable name
487 }
488 break;
489
c9f78968 490 case wxT('\\'):
906373bf 491 // backslash can be used to suppress special meaning of % and $
aaa6d89a 492 if ( n != str.length() - 1 &&
b11dd163 493 (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
906373bf
VZ
494 strResult += str[++n];
495
496 break;
497 }
498 //else: fall through
499
c801d85f
KB
500 default:
501 strResult += str[n];
502 }
503 }
504
505 return strResult;
506}
855e92a8
VZ
507
508// this function is used to properly interpret '..' in path
86501081 509void wxSplitPath(wxArrayString& aParts, const wxString& path)
855e92a8 510{
df5168c4 511 aParts.clear();
855e92a8
VZ
512
513 wxString strCurrent;
86501081 514 wxString::const_iterator pc = path.begin();
855e92a8 515 for ( ;; ) {
86501081 516 if ( pc == path.end() || *pc == wxCONFIG_PATH_SEPARATOR ) {
223d09f6 517 if ( strCurrent == wxT(".") ) {
855e92a8
VZ
518 // ignore
519 }
223d09f6 520 else if ( strCurrent == wxT("..") ) {
855e92a8 521 // go up one level
df5168c4 522 if ( aParts.size() == 0 )
43b2d5e7 523 {
86501081 524 wxLogWarning(_("'%s' has extra '..', ignored."), path);
43b2d5e7 525 }
855e92a8 526 else
43b2d5e7 527 {
df5168c4 528 aParts.erase(aParts.end() - 1);
43b2d5e7 529 }
937d4f4d
VZ
530
531 strCurrent.Empty();
855e92a8 532 }
b713f891 533 else if ( !strCurrent.empty() ) {
df5168c4 534 aParts.push_back(strCurrent);
855e92a8
VZ
535 strCurrent.Empty();
536 }
537 //else:
538 // could log an error here, but we prefer to ignore extra '/'
539
86501081 540 if ( pc == path.end() )
f6bcfd97 541 break;
855e92a8
VZ
542 }
543 else
544 strCurrent += *pc;
545
86501081 546 ++pc;
855e92a8
VZ
547 }
548}