Added missing wxUSE_FONTENUM check.
[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 wxT("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( wxT("failed to get LOGFONT") );
252 }
253 }
254 else // GetStockObject() failed
255 {
256 wxFAIL_MSG( wxT("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());
295
296 #if wxUSE_FONTENUM
297 wxASSERT(wxFontEnumerator::IsValidFacename(font.GetFaceName()));
298 #endif // wxUSE_FONTENUM
299
300 return font;
301 #endif // __WXWINCE__/!__WXWINCE__
302 }
303
304 // ----------------------------------------------------------------------------
305 // system metrics/features
306 // ----------------------------------------------------------------------------
307
308 // TODO: some of the "metrics" clearly should be features now that we have
309 // HasFeature()!
310
311 // the conversion table from wxSystemMetric enum to GetSystemMetrics() param
312 //
313 // if the constant is not defined, put -1 in the table to indicate that it is
314 // unknown
315 static const int gs_metricsMap[] =
316 {
317 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
318 #if defined(__WIN32__) && !defined(__WXWINCE__)
319 SM_CMOUSEBUTTONS,
320 #else
321 -1,
322 #endif
323
324 SM_CXBORDER,
325 SM_CYBORDER,
326 #ifdef SM_CXCURSOR
327 SM_CXCURSOR,
328 SM_CYCURSOR,
329 #else
330 -1, -1,
331 #endif
332 SM_CXDOUBLECLK,
333 SM_CYDOUBLECLK,
334 #if defined(__WIN32__) && defined(SM_CXDRAG)
335 SM_CXDRAG,
336 SM_CYDRAG,
337 SM_CXEDGE,
338 SM_CYEDGE,
339 #else
340 -1, -1, -1, -1,
341 #endif
342 SM_CXHSCROLL,
343 SM_CYHSCROLL,
344 #ifdef SM_CXHTHUMB
345 SM_CXHTHUMB,
346 #else
347 -1,
348 #endif
349 SM_CXICON,
350 SM_CYICON,
351 SM_CXICONSPACING,
352 SM_CYICONSPACING,
353 #ifdef SM_CXHTHUMB
354 SM_CXMIN,
355 SM_CYMIN,
356 #else
357 -1, -1,
358 #endif
359 SM_CXSCREEN,
360 SM_CYSCREEN,
361
362 #if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
363 SM_CXSIZEFRAME,
364 SM_CYSIZEFRAME,
365 SM_CXSMICON,
366 SM_CYSMICON,
367 #else
368 -1, -1, -1, -1,
369 #endif
370 SM_CYHSCROLL,
371 SM_CXHSCROLL,
372 SM_CXVSCROLL,
373 SM_CYVSCROLL,
374 #ifdef SM_CYVTHUMB
375 SM_CYVTHUMB,
376 #else
377 -1,
378 #endif
379 SM_CYCAPTION,
380 SM_CYMENU,
381 #if defined(__WIN32__) && defined(SM_NETWORK)
382 SM_NETWORK,
383 #else
384 -1,
385 #endif
386 #ifdef SM_PENWINDOWS
387 SM_PENWINDOWS,
388 #else
389 -1,
390 #endif
391 #if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
392 SM_SHOWSOUNDS,
393 #else
394 -1,
395 #endif
396 // SM_SWAPBUTTON is not available under CE and it doesn't make sense to ask
397 // for it there
398 #ifdef SM_SWAPBUTTON
399 SM_SWAPBUTTON,
400 #else
401 -1,
402 #endif
403 -1 // wxSYS_DCLICK_MSEC - not available as system metric
404 };
405
406 // Get a system metric, e.g. scrollbar size
407 int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
408 {
409 #ifdef __WXMICROWIN__
410 // TODO: probably use wxUniv themes functionality
411 return 0;
412 #else // !__WXMICROWIN__
413 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
414 wxT("invalid metric") );
415
416 if ( index == wxSYS_DCLICK_MSEC )
417 {
418 // This one is not a Win32 system metric
419 return ::GetDoubleClickTime();
420 }
421
422 int indexMSW = gs_metricsMap[index];
423 if ( indexMSW == -1 )
424 {
425 // not supported under current system
426 return -1;
427 }
428
429 int rc = ::GetSystemMetrics(indexMSW);
430 if ( index == wxSYS_NETWORK_PRESENT )
431 {
432 // only the last bit is significant according to the MSDN
433 rc &= 1;
434 }
435
436 return rc;
437 #endif // __WXMICROWIN__/!__WXMICROWIN__
438 }
439
440 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
441 {
442 switch ( index )
443 {
444 case wxSYS_CAN_ICONIZE_FRAME:
445 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
446 return true;
447
448 case wxSYS_TABLET_PRESENT:
449 return ::GetSystemMetrics(SM_TABLETPC) != 0;
450
451 default:
452 wxFAIL_MSG( wxT("unknown system feature") );
453
454 return false;
455 }
456 }
457
458 // ----------------------------------------------------------------------------
459 // function from wx/msw/wrapcctl.h: there is really no other place for it...
460 // ----------------------------------------------------------------------------
461
462 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
463
464 extern wxFont wxGetCCDefaultFont()
465 {
466 #ifndef __WXWINCE__
467 // under the systems enumerated below (anything released after Win98), the
468 // default font used for the common controls seems to be the desktop font
469 // which is also used for the icon titles and not the stock default GUI
470 // font
471 bool useIconFont;
472 int verMaj, verMin;
473 switch ( wxGetOsVersion(&verMaj, &verMin) )
474 {
475 case wxOS_WINDOWS_9X:
476 // 4.10 is Win98
477 useIconFont = verMaj == 4 && verMin >= 10;
478 break;
479
480 case wxOS_WINDOWS_NT:
481 // 5.0 is Win2k
482 useIconFont = verMaj >= 5;
483 break;
484
485 default:
486 useIconFont = false;
487 }
488
489 if ( useIconFont )
490 {
491 LOGFONT lf;
492 if ( ::SystemParametersInfo
493 (
494 SPI_GETICONTITLELOGFONT,
495 sizeof(lf),
496 &lf,
497 0
498 ) )
499 {
500 return wxFont(wxCreateFontFromLogFont(&lf));
501 }
502 else
503 {
504 wxLogLastError(wxT("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
505 }
506 }
507 #endif // __WXWINCE__
508
509 // fall back to the default font for the normal controls
510 return wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
511 }
512
513 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL