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