]> git.saurik.com Git - wxWidgets.git/blob - src/common/config.cpp
Instead of using GetAdjustedBestSize use the minsize if there is one,
[wxWidgets.git] / src / common / config.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: config.cpp
3 // Purpose: implementation of wxConfigBase class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 07.04.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Karsten Ballüder Ballueder@usa.net
9 // Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "confbase.h"
18 #endif
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif //__BORLANDC__
25
26 #ifndef wxUSE_CONFIG_NATIVE
27 #define wxUSE_CONFIG_NATIVE 1
28 #endif
29
30 #include "wx/config.h"
31 #include "wx/intl.h"
32 #include "wx/log.h"
33
34 #if wxUSE_CONFIG && ((wxUSE_FILE && wxUSE_TEXTFILE) || wxUSE_CONFIG_NATIVE)
35
36 #include "wx/app.h"
37 #include "wx/file.h"
38 #include "wx/textfile.h"
39 #include "wx/utils.h"
40 #include "wx/utils.h"
41
42 #include <stdlib.h>
43 #include <math.h>
44 #include <ctype.h>
45 #include <limits.h> // for INT_MAX
46
47 // ----------------------------------------------------------------------------
48 // global and class static variables
49 // ----------------------------------------------------------------------------
50
51 wxConfigBase *wxConfigBase::ms_pConfig = NULL;
52 bool wxConfigBase::ms_bAutoCreate = TRUE;
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxConfigBase
60 // ----------------------------------------------------------------------------
61
62 // Not all args will always be used by derived classes, but including them all
63 // in each class ensures compatibility.
64 wxConfigBase::wxConfigBase(const wxString& appName,
65 const wxString& vendorName,
66 const wxString& WXUNUSED(localFilename),
67 const wxString& WXUNUSED(globalFilename),
68 long style)
69 : m_appName(appName), m_vendorName(vendorName), m_style(style)
70 {
71 m_bExpandEnvVars = TRUE;
72 m_bRecordDefaults = FALSE;
73 }
74
75 wxConfigBase::~wxConfigBase()
76 {
77 }
78
79 wxConfigBase *wxConfigBase::Set(wxConfigBase *pConfig)
80 {
81 wxConfigBase *pOld = ms_pConfig;
82 ms_pConfig = pConfig;
83 return pOld;
84 }
85
86 wxConfigBase *wxConfigBase::Create()
87 {
88 if ( ms_bAutoCreate && ms_pConfig == NULL ) {
89 ms_pConfig =
90 #if defined(__WXMSW__) && wxUSE_CONFIG_NATIVE
91 new wxRegConfig(wxTheApp->GetAppName(), wxTheApp->GetVendorName());
92 #else // either we're under Unix or wish to use files even under Windows
93 new wxFileConfig(wxTheApp->GetAppName());
94 #endif
95 }
96
97 return ms_pConfig;
98 }
99
100 // ----------------------------------------------------------------------------
101 // wxConfigBase reading entries
102 // ----------------------------------------------------------------------------
103
104 // implement both Read() overloads for the given type in terms of DoRead()
105 #define IMPLEMENT_READ_FOR_TYPE(name, type, deftype, extra) \
106 bool wxConfigBase::Read(const wxString& key, type *val) const \
107 { \
108 wxCHECK_MSG( val, FALSE, _T("wxConfig::Read(): NULL parameter") ); \
109 \
110 if ( !DoRead##name(key, val) ) \
111 return FALSE; \
112 \
113 *val = extra(*val); \
114 \
115 return TRUE; \
116 } \
117 \
118 bool wxConfigBase::Read(const wxString& key, \
119 type *val, \
120 deftype defVal) const \
121 { \
122 wxCHECK_MSG( val, FALSE, _T("wxConfig::Read(): NULL parameter") ); \
123 \
124 bool read = DoRead##name(key, val); \
125 if ( !read ) \
126 { \
127 if ( IsRecordingDefaults() ) \
128 { \
129 ((wxConfigBase *)this)->DoWrite##name(key, defVal); \
130 } \
131 \
132 *val = defVal; \
133 } \
134 \
135 *val = extra(*val); \
136 \
137 return read; \
138 }
139
140
141 IMPLEMENT_READ_FOR_TYPE(String, wxString, const wxString&, ExpandEnvVars)
142 IMPLEMENT_READ_FOR_TYPE(Long, long, long, long)
143 IMPLEMENT_READ_FOR_TYPE(Int, int, int, int)
144 IMPLEMENT_READ_FOR_TYPE(Double, double, double, double)
145 IMPLEMENT_READ_FOR_TYPE(Bool, bool, bool, bool)
146
147 #undef IMPLEMENT_READ_FOR_TYPE
148
149 // the DoReadXXX() for the other types have implementation in the base class
150 // but can be overridden in the derived ones
151 bool wxConfigBase::DoReadInt(const wxString& key, int *pi) const
152 {
153 wxCHECK_MSG( pi, FALSE, _T("wxConfig::Read(): NULL parameter") );
154
155 long l;
156 if ( !DoReadLong(key, &l) )
157 return FALSE;
158
159 wxASSERT_MSG( l < INT_MAX, _T("overflow in wxConfig::DoReadInt") );
160
161 *pi = (int)l;
162
163 return TRUE;
164 }
165
166 bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const
167 {
168 wxCHECK_MSG( val, FALSE, _T("wxConfig::Read(): NULL parameter") );
169
170 long l;
171 if ( !DoReadLong(key, &l) )
172 return FALSE;
173
174 wxASSERT_MSG( l == 0 || l == 1, _T("bad bool value in wxConfig::DoReadInt") );
175
176 *val = l != 0;
177
178 return TRUE;
179 }
180
181 bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const
182 {
183 wxString str;
184 if ( Read(key, &str) )
185 {
186 return str.ToDouble(val);
187 }
188
189 return FALSE;
190 }
191
192 // string reading helper
193 wxString wxConfigBase::ExpandEnvVars(const wxString& str) const
194 {
195 wxString tmp; // Required for BC++
196 if (IsExpandingEnvVars())
197 tmp = wxExpandEnvVars(str);
198 else
199 tmp = str;
200 return tmp;
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxConfigBase writing
205 // ----------------------------------------------------------------------------
206
207 bool wxConfigBase::DoWriteDouble(const wxString& key, double val)
208 {
209 return DoWriteString(key, wxString::Format(_T("%g"), val));
210 }
211
212 bool wxConfigBase::DoWriteInt(const wxString& key, int value)
213 {
214 return DoWriteLong(key, (long)value);
215 }
216
217 bool wxConfigBase::DoWriteBool(const wxString& key, bool value)
218 {
219 return DoWriteLong(key, value ? 1l : 0l);
220 }
221
222 // ----------------------------------------------------------------------------
223 // wxConfigPathChanger
224 // ----------------------------------------------------------------------------
225
226 wxConfigPathChanger::wxConfigPathChanger(const wxConfigBase *pContainer,
227 const wxString& strEntry)
228 {
229 m_pContainer = (wxConfigBase *)pContainer;
230
231 // the path is everything which precedes the last slash
232 wxString strPath = strEntry.BeforeLast(wxCONFIG_PATH_SEPARATOR);
233
234 // except in the special case of "/keyname" when there is nothing before "/"
235 if ( strPath.IsEmpty() &&
236 ((!strEntry.IsEmpty()) && strEntry[0] == wxCONFIG_PATH_SEPARATOR) )
237 {
238 strPath = wxCONFIG_PATH_SEPARATOR;
239 }
240
241 if ( !strPath.IsEmpty() ) {
242 // do change the path
243 m_bChanged = TRUE;
244 m_strName = strEntry.AfterLast(wxCONFIG_PATH_SEPARATOR);
245 m_strOldPath = m_pContainer->GetPath();
246 if ( m_strOldPath.Len() == 0 ||
247 m_strOldPath.Last() != wxCONFIG_PATH_SEPARATOR )
248 m_strOldPath += wxCONFIG_PATH_SEPARATOR;
249 m_pContainer->SetPath(strPath);
250 }
251 else {
252 // it's a name only, without path - nothing to do
253 m_bChanged = FALSE;
254 m_strName = strEntry;
255 }
256 }
257
258 wxConfigPathChanger::~wxConfigPathChanger()
259 {
260 // only restore path if it was changed
261 if ( m_bChanged ) {
262 m_pContainer->SetPath(m_strOldPath);
263 }
264 }
265
266 #endif // wxUSE_CONFIG
267
268 // ----------------------------------------------------------------------------
269 // static & global functions
270 // ----------------------------------------------------------------------------
271
272 // understands both Unix and Windows (but only under Windows) environment
273 // variables expansion: i.e. $var, $(var) and ${var} are always understood
274 // and in addition under Windows %var% is also.
275 wxString wxExpandEnvVars(const wxString& str)
276 {
277 wxString strResult;
278 strResult.Alloc(str.Len());
279
280 // don't change the values the enum elements: they must be equal
281 // to the matching [closing] delimiter.
282 enum Bracket
283 {
284 Bracket_None,
285 Bracket_Normal = ')',
286 Bracket_Curly = '}',
287 #ifdef __WXMSW__
288 Bracket_Windows = '%', // yeah, Windows people are a bit strange ;-)
289 #endif
290 Bracket_Max
291 };
292
293 size_t m;
294 for ( size_t n = 0; n < str.Len(); n++ ) {
295 switch ( str[n] ) {
296 #ifdef __WXMSW__
297 case wxT('%'):
298 #endif //WINDOWS
299 case wxT('$'):
300 {
301 Bracket bracket;
302 #ifdef __WXMSW__
303 if ( str[n] == wxT('%') )
304 bracket = Bracket_Windows;
305 else
306 #endif //WINDOWS
307 if ( n == str.Len() - 1 ) {
308 bracket = Bracket_None;
309 }
310 else {
311 switch ( str[n + 1] ) {
312 case wxT('('):
313 bracket = Bracket_Normal;
314 n++; // skip the bracket
315 break;
316
317 case wxT('{'):
318 bracket = Bracket_Curly;
319 n++; // skip the bracket
320 break;
321
322 default:
323 bracket = Bracket_None;
324 }
325 }
326
327 m = n + 1;
328
329 while ( m < str.Len() && (wxIsalnum(str[m]) || str[m] == wxT('_')) )
330 m++;
331
332 wxString strVarName(str.c_str() + n + 1, m - n - 1);
333
334 #ifdef __WXWINCE__
335 const wxChar *pszValue = NULL;
336 #else
337 const wxChar *pszValue = wxGetenv(strVarName);
338 #endif
339 if ( pszValue != NULL ) {
340 strResult += pszValue;
341 }
342 else {
343 // variable doesn't exist => don't change anything
344 #ifdef __WXMSW__
345 if ( bracket != Bracket_Windows )
346 #endif
347 if ( bracket != Bracket_None )
348 strResult << str[n - 1];
349 strResult << str[n] << strVarName;
350 }
351
352 // check the closing bracket
353 if ( bracket != Bracket_None ) {
354 if ( m == str.Len() || str[m] != (char)bracket ) {
355 // under MSW it's common to have '%' characters in the registry
356 // and it's annoying to have warnings about them each time, so
357 // ignroe them silently if they are not used for env vars
358 //
359 // under Unix, OTOH, this warning could be useful for the user to
360 // understand why isn't the variable expanded as intended
361 #ifndef __WXMSW__
362 wxLogWarning(_("Environment variables expansion failed: missing '%c' at position %d in '%s'."),
363 (char)bracket, m + 1, str.c_str());
364 #endif // __WXMSW__
365 }
366 else {
367 // skip closing bracket unless the variables wasn't expanded
368 if ( pszValue == NULL )
369 strResult << (char)bracket;
370 m++;
371 }
372 }
373
374 n = m - 1; // skip variable name
375 }
376 break;
377
378 case '\\':
379 // backslash can be used to suppress special meaning of % and $
380 if ( n != str.Len() && (str[n + 1] == wxT('%') || str[n + 1] == wxT('$')) ) {
381 strResult += str[++n];
382
383 break;
384 }
385 //else: fall through
386
387 default:
388 strResult += str[n];
389 }
390 }
391
392 return strResult;
393 }
394
395 // this function is used to properly interpret '..' in path
396 void wxSplitPath(wxArrayString& aParts, const wxChar *sz)
397 {
398 aParts.clear();
399
400 wxString strCurrent;
401 const wxChar *pc = sz;
402 for ( ;; ) {
403 if ( *pc == wxT('\0') || *pc == wxCONFIG_PATH_SEPARATOR ) {
404 if ( strCurrent == wxT(".") ) {
405 // ignore
406 }
407 else if ( strCurrent == wxT("..") ) {
408 // go up one level
409 if ( aParts.size() == 0 )
410 wxLogWarning(_("'%s' has extra '..', ignored."), sz);
411 else
412 aParts.erase(aParts.end() - 1);
413
414 strCurrent.Empty();
415 }
416 else if ( !strCurrent.IsEmpty() ) {
417 aParts.push_back(strCurrent);
418 strCurrent.Empty();
419 }
420 //else:
421 // could log an error here, but we prefer to ignore extra '/'
422
423 if ( *pc == wxT('\0') )
424 break;
425 }
426 else
427 strCurrent += *pc;
428
429 pc++;
430 }
431 }
432
433