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