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