]> git.saurik.com Git - wxWidgets.git/blob - src/msw/settings.cpp
use our own mbstowcs() and wcstombs() implementations with Metrowerks as MSL CRT...
[wxWidgets.git] / src / msw / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/settings.cpp
3 // Purpose: wxSystemSettingsNative implementation for MSW
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/gdicmn.h"
30 #endif
31
32 #include "wx/settings.h"
33
34 #include "wx/msw/private.h"
35
36 #ifndef SPI_GETFLATMENU
37 #define SPI_GETFLATMENU 0x1022
38 #endif
39
40 #include "wx/module.h"
41 #include "wx/fontutil.h"
42
43 #ifdef __WXWINCE__ // for SM_CXCURSOR and SM_CYCURSOR
44 #include "wx/msw/wince/missing.h"
45 #endif // __WXWINCE__
46
47 // ----------------------------------------------------------------------------
48 // private classes
49 // ----------------------------------------------------------------------------
50
51 // the module which is used to clean up wxSystemSettingsNative data (this is a
52 // singleton class so it can't be done in the dtor)
53 class wxSystemSettingsModule : public wxModule
54 {
55 public:
56 virtual bool OnInit();
57 virtual void OnExit();
58
59 private:
60 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
61 };
62
63 // ----------------------------------------------------------------------------
64 // global data
65 // ----------------------------------------------------------------------------
66
67 // the font returned by GetFont(wxSYS_DEFAULT_GUI_FONT): it is created when
68 // GetFont() is called for the first time and deleted by wxSystemSettingsModule
69 static wxFont *gs_fontDefault = NULL;
70
71 // ============================================================================
72 // implementation
73 // ============================================================================
74
75 // TODO: see ::SystemParametersInfo for all sorts of Windows settings.
76 // Different args are required depending on the id. How does this differ
77 // from GetSystemMetric, and should it? Perhaps call it GetSystemParameter
78 // and pass an optional void* arg to get further info.
79 // Should also have SetSystemParameter.
80 // Also implement WM_WININICHANGE (NT) / WM_SETTINGCHANGE (Win95)
81
82 // ----------------------------------------------------------------------------
83 // wxSystemSettingsModule
84 // ----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
87
88 bool wxSystemSettingsModule::OnInit()
89 {
90 return true;
91 }
92
93 void wxSystemSettingsModule::OnExit()
94 {
95 delete gs_fontDefault;
96 gs_fontDefault = NULL;
97 }
98
99 // ----------------------------------------------------------------------------
100 // wxSystemSettingsNative
101 // ----------------------------------------------------------------------------
102
103 // ----------------------------------------------------------------------------
104 // colours
105 // ----------------------------------------------------------------------------
106
107 wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
108 {
109 // we use 0 as the default value just to avoid compiler warnings, as there
110 // is no invalid colour value we use hasCol as the real indicator of
111 // whether colSys was initialized or not
112 COLORREF colSys = 0;
113 bool hasCol = false;
114
115 // the default colours for the entries after BTNHIGHLIGHT
116 static const COLORREF s_defaultSysColors[] =
117 {
118 0x000000, // 3DDKSHADOW
119 0xdfdfdf, // 3DLIGHT
120 0x000000, // INFOTEXT
121 0xe1ffff, // INFOBK
122
123 0, // filler - no std colour with this index
124
125 // TODO: please fill in the standard values of those, I don't have them
126 0, // HOTLIGHT
127 0, // GRADIENTACTIVECAPTION
128 0, // GRADIENTINACTIVECAPTION
129 0, // MENU
130 0, // MENUBAR (unused)
131 };
132
133 if ( index == wxSYS_COLOUR_LISTBOX )
134 {
135 // there is no standard colour with this index, map to another one
136 index = wxSYS_COLOUR_WINDOW;
137 }
138 else if ( index > wxSYS_COLOUR_BTNHIGHLIGHT )
139 {
140 // the indices before BTNHIGHLIGHT are understood by GetSysColor() in
141 // all Windows version, for the other ones we have to check
142 bool useDefault;
143
144 int verMaj, verMin;
145 wxGetOsVersion(&verMaj, &verMin);
146 if ( verMaj < 4 )
147 {
148 // NT 3.5
149 useDefault = true;
150 }
151 else if ( verMaj == 4 )
152 {
153 // Win95/NT 4.0
154 useDefault = index > wxSYS_COLOUR_INFOBK;
155 }
156 else if ( verMaj == 5 && verMin == 0 )
157 {
158 // Win98/Win2K
159 useDefault = index > wxSYS_COLOUR_GRADIENTINACTIVECAPTION;
160 }
161 else // >= 5.1
162 {
163 // 5.1 is Windows XP
164 useDefault = false;
165 // Determine if we are using flat menus, only then allow wxSYS_COLOUR_MENUBAR
166 if ( index == wxSYS_COLOUR_MENUBAR )
167 {
168 BOOL isFlat ;
169 if ( SystemParametersInfo( SPI_GETFLATMENU , 0 ,&isFlat, 0 ) )
170 {
171 if ( !isFlat )
172 index = wxSYS_COLOUR_MENU ;
173 }
174 }
175 }
176
177 if ( useDefault )
178 {
179 // special handling for MENUBAR colour: we use this in wxToolBar
180 // and wxStatusBar to have correct bg colour under Windows XP
181 // (which uses COLOR_MENUBAR for them) but they should still look
182 // correctly under previous Windows versions as well
183 if ( index == wxSYS_COLOUR_MENUBAR )
184 {
185 index = wxSYS_COLOUR_3DFACE;
186 }
187 else // replace with default colour
188 {
189 unsigned int n = index - wxSYS_COLOUR_BTNHIGHLIGHT;
190
191 wxASSERT_MSG( n < WXSIZEOF(s_defaultSysColors),
192 _T("forgot tp update the default colours array") );
193
194 colSys = s_defaultSysColors[n];
195 hasCol = true;
196 }
197 }
198 }
199
200 if ( !hasCol )
201 {
202 #ifdef __WXWINCE__
203 colSys = ::GetSysColor(index|SYS_COLOR_INDEX_FLAG);
204 #else
205 colSys = ::GetSysColor(index);
206 #endif
207 }
208
209 return wxRGBToColour(colSys);
210 }
211
212 // ----------------------------------------------------------------------------
213 // fonts
214 // ----------------------------------------------------------------------------
215
216 wxFont wxCreateFontFromStockObject(int index)
217 {
218 wxFont font;
219
220 HFONT hFont = (HFONT) ::GetStockObject(index);
221 if ( hFont )
222 {
223 LOGFONT lf;
224 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
225 {
226 wxNativeFontInfo info;
227 info.lf = lf;
228 #ifndef __WXWINCE__
229 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
230 // is returned as default GUI font for compatibility
231 int verMaj;
232 if(index == DEFAULT_GUI_FONT && wxGetOsVersion(&verMaj) == wxWINDOWS_NT && verMaj >= 5)
233 wxStrcpy(info.lf.lfFaceName, wxT("MS Shell Dlg 2"));
234 #endif
235 // Under MicroWindows we pass the HFONT as well
236 // because it's hard to convert HFONT -> LOGFONT -> HFONT
237 // It's OK to delete stock objects, the delete will be ignored.
238 #ifdef __WXMICROWIN__
239 font.Create(info, (WXHFONT) hFont);
240 #else
241 font.Create(info);
242 #endif
243 }
244 else
245 {
246 wxFAIL_MSG( _T("failed to get LOGFONT") );
247 }
248 }
249 else // GetStockObject() failed
250 {
251 wxFAIL_MSG( _T("stock font not found") );
252 }
253
254 return font;
255 }
256
257 wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
258 {
259 #ifdef __WXWINCE__
260 // under CE only a single SYSTEM_FONT exists
261 index;
262
263 if ( !gs_fontDefault )
264 {
265 gs_fontDefault = new wxFont(wxCreateFontFromStockObject(SYSTEM_FONT));
266 }
267
268 return *gs_fontDefault;
269 #else // !__WXWINCE__
270 // wxWindow ctor calls GetFont(wxSYS_DEFAULT_GUI_FONT) so we're
271 // called fairly often -- this is why we cache this particular font
272 const bool isDefaultRequested = index == wxSYS_DEFAULT_GUI_FONT;
273 if ( isDefaultRequested )
274 {
275 if ( gs_fontDefault )
276 return *gs_fontDefault;
277 }
278
279 wxFont font = wxCreateFontFromStockObject(index);
280
281 if ( isDefaultRequested )
282 {
283 // if we got here it means we hadn't cached it yet - do now
284 gs_fontDefault = new wxFont(font);
285 }
286
287 return font;
288 #endif // __WXWINCE__/!__WXWINCE__
289 }
290
291 // ----------------------------------------------------------------------------
292 // system metrics/features
293 // ----------------------------------------------------------------------------
294
295 // TODO: some of the "metrics" clearly should be features now that we have
296 // HasFeature()!
297
298 // the conversion table from wxSystemMetric enum to GetSystemMetrics() param
299 //
300 // if the constant is not defined, put -1 in the table to indicate that it is
301 // unknown
302 static const int gs_metricsMap[] =
303 {
304 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
305 #if defined(__WIN32__) && !defined(__WXWINCE__)
306 SM_CMOUSEBUTTONS,
307 #else
308 -1,
309 #endif
310
311 SM_CXBORDER,
312 SM_CYBORDER,
313 SM_CXCURSOR,
314 SM_CYCURSOR,
315 SM_CXDOUBLECLK,
316 SM_CYDOUBLECLK,
317 #if defined(__WIN32__) && defined(SM_CXDRAG)
318 SM_CXDRAG,
319 SM_CYDRAG,
320 SM_CXEDGE,
321 SM_CYEDGE,
322 #else
323 -1, -1, -1, -1,
324 #endif
325 SM_CXHSCROLL,
326 SM_CYHSCROLL,
327 #ifdef SM_CXHTHUMB
328 SM_CXHTHUMB,
329 #else
330 -1,
331 #endif
332 SM_CXICON,
333 SM_CYICON,
334 SM_CXICONSPACING,
335 SM_CYICONSPACING,
336 #ifdef SM_CXHTHUMB
337 SM_CXMIN,
338 SM_CYMIN,
339 #else
340 -1, -1,
341 #endif
342 SM_CXSCREEN,
343 SM_CYSCREEN,
344
345 #if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
346 SM_CXSIZEFRAME,
347 SM_CYSIZEFRAME,
348 SM_CXSMICON,
349 SM_CYSMICON,
350 #else
351 -1, -1, -1, -1,
352 #endif
353 SM_CYHSCROLL,
354 SM_CXVSCROLL,
355 SM_CXVSCROLL,
356 SM_CYVSCROLL,
357 #ifdef SM_CYVTHUMB
358 SM_CYVTHUMB,
359 #else
360 -1,
361 #endif
362 SM_CYCAPTION,
363 SM_CYMENU,
364 #if defined(__WIN32__) && defined(SM_NETWORK)
365 SM_NETWORK,
366 #else
367 -1,
368 #endif
369 #ifdef SM_PENWINDOWS
370 SM_PENWINDOWS,
371 #else
372 -1,
373 #endif
374 #if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
375 SM_SHOWSOUNDS,
376 #else
377 -1,
378 #endif
379 #ifdef SM_SWAPBUTTON
380 SM_SWAPBUTTON,
381 #else
382 -1
383 #endif
384 };
385
386 // Get a system metric, e.g. scrollbar size
387 int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
388 {
389 #ifdef __WXMICROWIN__
390 // TODO: probably use wxUniv themes functionality
391 return 0;
392 #else // !__WXMICROWIN__
393 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
394 _T("invalid metric") );
395
396 int indexMSW = gs_metricsMap[index];
397 if ( indexMSW == -1 )
398 {
399 // not supported under current system
400 return -1;
401 }
402
403 int rc = ::GetSystemMetrics(indexMSW);
404 if ( index == wxSYS_NETWORK_PRESENT )
405 {
406 // only the last bit is significant according to the MSDN
407 rc &= 1;
408 }
409
410 return rc;
411 #endif // __WXMICROWIN__/!__WXMICROWIN__
412 }
413
414 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
415 {
416 switch ( index )
417 {
418 case wxSYS_CAN_ICONIZE_FRAME:
419 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
420 return true;
421
422 default:
423 wxFAIL_MSG( _T("unknown system feature") );
424
425 return false;
426 }
427 }
428
429 // ----------------------------------------------------------------------------
430 // function from wx/msw/wrapcctl.h: there is really no other place for it...
431 // ----------------------------------------------------------------------------
432
433 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
434
435 extern wxFont wxGetCCDefaultFont()
436 {
437 #ifndef __WXWINCE__
438 // under the systems enumerated below (anything released after Win98), the
439 // default font used for the common controls seems to be the desktop font
440 // which is also used for the icon titles and not the stock default GUI
441 // font
442 bool useIconFont;
443 int verMaj, verMin;
444 switch ( wxGetOsVersion(&verMaj, &verMin) )
445 {
446 case wxWIN95:
447 // 4.10 is Win98
448 useIconFont = verMaj == 4 && verMin >= 10;
449 break;
450
451 case wxWINDOWS_NT:
452 // 5.0 is Win2k
453 useIconFont = verMaj >= 5;
454 break;
455
456 default:
457 useIconFont = false;
458 }
459
460 if ( useIconFont )
461 {
462 LOGFONT lf;
463 if ( ::SystemParametersInfo
464 (
465 SPI_GETICONTITLELOGFONT,
466 sizeof(lf),
467 &lf,
468 0
469 ) )
470 {
471 return wxFont(wxCreateFontFromLogFont(&lf));
472 }
473 else
474 {
475 wxLogLastError(_T("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
476 }
477 }
478 #endif // __WXWINCE__
479
480 // fall back to the default font for the normal controls
481 return wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
482 }
483
484 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL