]> git.saurik.com Git - wxWidgets.git/blame - src/msw/settings.cpp
Compilation fix for wxMotif PCH build.
[wxWidgets.git] / src / msw / settings.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
02761f6c 2// Name: src/msw/settings.cpp
7516ed26 3// Purpose: wxSystemSettingsNative implementation for MSW
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
8bf30fe9
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
7516ed26 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
9eddec69
WS
27#include "wx/settings.h"
28
2bda0e17 29#ifndef WX_PRECOMP
e02e8816 30 #include "wx/utils.h"
8bf30fe9 31 #include "wx/gdicmn.h"
02761f6c 32 #include "wx/module.h"
2bda0e17
KB
33#endif
34
2bda0e17 35#include "wx/msw/private.h"
7212d155 36#include "wx/msw/missing.h" // for SM_CXCURSOR, SM_CYCURSOR, SM_TABLETPC
16b0c553 37#include "wx/msw/private/metrics.h"
7516ed26 38
d1eebf40
SC
39#ifndef SPI_GETFLATMENU
40#define SPI_GETFLATMENU 0x1022
41#endif
42
04ef50df 43#include "wx/fontutil.h"
e3527f7b 44#include "wx/fontenum.h"
2bda0e17 45
8bf30fe9
VZ
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
7516ed26 50// the module which is used to clean up wxSystemSettingsNative data (this is a
8bf30fe9
VZ
51// singleton class so it can't be done in the dtor)
52class wxSystemSettingsModule : public wxModule
53{
54public:
55 virtual bool OnInit();
56 virtual void OnExit();
57
58private:
59 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
60};
61
62// ----------------------------------------------------------------------------
63// global data
64// ----------------------------------------------------------------------------
65
7516ed26
VZ
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
8bf30fe9
VZ
68static wxFont *gs_fontDefault = NULL;
69
70// ============================================================================
71// implementation
72// ============================================================================
73
7516ed26
VZ
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
8bf30fe9
VZ
81// ----------------------------------------------------------------------------
82// wxSystemSettingsModule
83// ----------------------------------------------------------------------------
84
85IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
86
87bool wxSystemSettingsModule::OnInit()
88{
57f4f925 89 return true;
8bf30fe9
VZ
90}
91
92void wxSystemSettingsModule::OnExit()
93{
5276b0a5 94 wxDELETE(gs_fontDefault);
8bf30fe9
VZ
95}
96
97// ----------------------------------------------------------------------------
7516ed26 98// wxSystemSettingsNative
8bf30fe9
VZ
99// ----------------------------------------------------------------------------
100
7516ed26
VZ
101// ----------------------------------------------------------------------------
102// colours
103// ----------------------------------------------------------------------------
2bda0e17 104
7516ed26 105wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
2bda0e17 106{
7d6d3bf3
VZ
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;
57f4f925 111 bool hasCol = false;
7d6d3bf3
VZ
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
9f2968ad
VZ
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 )
7d6d3bf3
VZ
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
7d6d3bf3
VZ
147 int verMaj, verMin;
148 wxGetOsVersion(&verMaj, &verMin);
149 if ( verMaj < 4 )
150 {
151 // NT 3.5
57f4f925 152 useDefault = true;
7d6d3bf3
VZ
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
57f4f925
WS
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 }
92218ce6 178 }
7d6d3bf3
VZ
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 {
d285d708 192 unsigned int n = index - wxSYS_COLOUR_BTNHIGHLIGHT;
7d6d3bf3
VZ
193
194 wxASSERT_MSG( n < WXSIZEOF(s_defaultSysColors),
9a83f860 195 wxT("forgot tp update the default colours array") );
7d6d3bf3
VZ
196
197 colSys = s_defaultSysColors[n];
57f4f925 198 hasCol = true;
7d6d3bf3
VZ
199 }
200 }
201 }
202
203 if ( !hasCol )
204 {
39d2f9a7
JS
205#ifdef __WXWINCE__
206 colSys = ::GetSysColor(index|SYS_COLOR_INDEX_FLAG);
207#else
7d6d3bf3 208 colSys = ::GetSysColor(index);
39d2f9a7 209#endif
7d6d3bf3 210 }
03647350 211
e3527f7b
FM
212 wxColour ret = wxRGBToColour(colSys);
213 wxASSERT(ret.IsOk());
214 return ret;
2bda0e17
KB
215}
216
7516ed26
VZ
217// ----------------------------------------------------------------------------
218// fonts
219// ----------------------------------------------------------------------------
220
04ef50df 221wxFont wxCreateFontFromStockObject(int index)
2bda0e17 222{
8bf30fe9
VZ
223 wxFont font;
224
019a60d6 225 HFONT hFont = (HFONT) ::GetStockObject(index);
8bf30fe9 226 if ( hFont )
019a60d6
VS
227 {
228 LOGFONT lf;
229 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
230 {
04ef50df
JS
231 wxNativeFontInfo info;
232 info.lf = lf;
233 // Under MicroWindows we pass the HFONT as well
234 // because it's hard to convert HFONT -> LOGFONT -> HFONT
235 // It's OK to delete stock objects, the delete will be ignored.
236#ifdef __WXMICROWIN__
237 font.Create(info, (WXHFONT) hFont);
238#else
239 font.Create(info);
240#endif
019a60d6
VS
241 }
242 else
243 {
9a83f860 244 wxFAIL_MSG( wxT("failed to get LOGFONT") );
019a60d6
VS
245 }
246 }
8bf30fe9
VZ
247 else // GetStockObject() failed
248 {
9a83f860 249 wxFAIL_MSG( wxT("stock font not found") );
8bf30fe9 250 }
7516ed26 251
04ef50df
JS
252 return font;
253}
254
7516ed26 255wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
04ef50df 256{
27adb8ba
VZ
257#ifdef __WXWINCE__
258 // under CE only a single SYSTEM_FONT exists
259 index;
260
261 if ( !gs_fontDefault )
262 {
263 gs_fontDefault = new wxFont(wxCreateFontFromStockObject(SYSTEM_FONT));
264 }
265
03647350 266 wxASSERT(gs_fontDefault->IsOk() &&
e3527f7b 267 wxFontEnumerator::IsValidFacename(gs_fontDefault->GetFaceName()));
27adb8ba
VZ
268 return *gs_fontDefault;
269#else // !__WXWINCE__
46697f31 270 // wxWindow ctor calls GetFont(wxSYS_DEFAULT_GUI_FONT) so we're
553c5bcc 271 // called fairly often -- this is why we cache this particular font
16b0c553 272 if ( index == wxSYS_DEFAULT_GUI_FONT )
04ef50df 273 {
16b0c553
VZ
274 if ( !gs_fontDefault )
275 {
276 // http://blogs.msdn.com/oldnewthing/archive/2005/07/07/436435.aspx
277 // explains why neither SYSTEM_FONT nor DEFAULT_GUI_FONT should be
278 // used here
279 //
280 // the message box font seems to be the one which should be used
281 // for most (simple) controls, e.g. buttons and such but other
282 // controls may prefer to use lfStatusFont or lfCaptionFont if it
283 // is more appropriate for them
284 wxNativeFontInfo info;
285 info.lf = wxMSWImpl::GetNonClientMetrics().lfMessageFont;
286 gs_fontDefault = new wxFont(info);
287 }
288
289 return *gs_fontDefault;
04ef50df
JS
290 }
291
292 wxFont font = wxCreateFontFromStockObject(index);
8bf30fe9 293
b7081ff8
VZ
294 wxASSERT(font.IsOk());
295
296#if wxUSE_FONTENUM
297 wxASSERT(wxFontEnumerator::IsValidFacename(font.GetFaceName()));
298#endif // wxUSE_FONTENUM
299
8bf30fe9 300 return font;
27adb8ba 301#endif // __WXWINCE__/!__WXWINCE__
2bda0e17
KB
302}
303
7516ed26
VZ
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
315static const int gs_metricsMap[] =
2bda0e17 316{
fc29e86e 317 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
4676948b 318#if defined(__WIN32__) && !defined(__WXWINCE__)
7516ed26
VZ
319 SM_CMOUSEBUTTONS,
320#else
321 -1,
2bda0e17
KB
322#endif
323
7516ed26
VZ
324 SM_CXBORDER,
325 SM_CYBORDER,
c1dfa9eb 326#ifdef SM_CXCURSOR
9eddec69 327 SM_CXCURSOR,
7516ed26 328 SM_CYCURSOR,
c1dfa9eb 329#else
9eddec69 330 -1, -1,
c1dfa9eb 331#endif
7516ed26
VZ
332 SM_CXDOUBLECLK,
333 SM_CYDOUBLECLK,
2432b92d 334#if defined(__WIN32__) && defined(SM_CXDRAG)
7516ed26
VZ
335 SM_CXDRAG,
336 SM_CYDRAG,
337 SM_CXEDGE,
338 SM_CYEDGE,
339#else
4676948b 340 -1, -1, -1, -1,
2bda0e17 341#endif
7516ed26
VZ
342 SM_CXHSCROLL,
343 SM_CYHSCROLL,
4676948b 344#ifdef SM_CXHTHUMB
7516ed26 345 SM_CXHTHUMB,
4676948b
JS
346#else
347 -1,
348#endif
7516ed26
VZ
349 SM_CXICON,
350 SM_CYICON,
351 SM_CXICONSPACING,
352 SM_CYICONSPACING,
4676948b 353#ifdef SM_CXHTHUMB
7516ed26
VZ
354 SM_CXMIN,
355 SM_CYMIN,
4676948b
JS
356#else
357 -1, -1,
358#endif
7516ed26
VZ
359 SM_CXSCREEN,
360 SM_CYSCREEN,
2432b92d
JS
361
362#if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
7516ed26
VZ
363 SM_CXSIZEFRAME,
364 SM_CYSIZEFRAME,
365 SM_CXSMICON,
366 SM_CYSMICON,
367#else
4676948b 368 -1, -1, -1, -1,
2bda0e17 369#endif
7516ed26 370 SM_CYHSCROLL,
5573f5b8 371 SM_CXHSCROLL,
7516ed26
VZ
372 SM_CXVSCROLL,
373 SM_CYVSCROLL,
4676948b 374#ifdef SM_CYVTHUMB
7516ed26 375 SM_CYVTHUMB,
4676948b
JS
376#else
377 -1,
378#endif
7516ed26
VZ
379 SM_CYCAPTION,
380 SM_CYMENU,
2432b92d 381#if defined(__WIN32__) && defined(SM_NETWORK)
7516ed26
VZ
382 SM_NETWORK,
383#else
384 -1,
2bda0e17 385#endif
4676948b 386#ifdef SM_PENWINDOWS
7516ed26 387 SM_PENWINDOWS,
4676948b
JS
388#else
389 -1,
390#endif
2432b92d 391#if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
7516ed26
VZ
392 SM_SHOWSOUNDS,
393#else
394 -1,
2bda0e17 395#endif
d9fda37b
VZ
396 // SM_SWAPBUTTON is not available under CE and it doesn't make sense to ask
397 // for it there
398#ifdef SM_SWAPBUTTON
7516ed26 399 SM_SWAPBUTTON,
d9fda37b
VZ
400#else
401 -1,
402#endif
5595181f 403 -1 // wxSYS_DCLICK_MSEC - not available as system metric
7516ed26
VZ
404};
405
406// Get a system metric, e.g. scrollbar size
9b0b5ba7 407int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
7516ed26
VZ
408{
409#ifdef __WXMICROWIN__
410 // TODO: probably use wxUniv themes functionality
411 return 0;
412#else // !__WXMICROWIN__
68d95db4 413 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
9a83f860 414 wxT("invalid metric") );
7516ed26 415
5595181f
VZ
416 if ( index == wxSYS_DCLICK_MSEC )
417 {
418 // This one is not a Win32 system metric
419 return ::GetDoubleClickTime();
420 }
421
7516ed26
VZ
422 int indexMSW = gs_metricsMap[index];
423 if ( indexMSW == -1 )
424 {
425 // not supported under current system
1d451c5b 426 return -1;
019a60d6 427 }
7516ed26
VZ
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__
2bda0e17
KB
438}
439
7516ed26 440bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
253293c1 441{
7516ed26 442 switch ( index )
253293c1 443 {
7516ed26 444 case wxSYS_CAN_ICONIZE_FRAME:
253293c1 445 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
57f4f925 446 return true;
7516ed26 447
ddec9f1e
JS
448 case wxSYS_TABLET_PRESENT:
449 return ::GetSystemMetrics(SM_TABLETPC) != 0;
450
253293c1 451 default:
9a83f860 452 wxFAIL_MSG( wxT("unknown system feature") );
7516ed26 453
57f4f925 454 return false;
253293c1
VS
455 }
456}
553c5bcc
VZ
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
464extern 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 {
406d283a 475 case wxOS_WINDOWS_9X:
553c5bcc 476 // 4.10 is Win98
b594d7f7 477 useIconFont = verMaj == 4 && verMin >= 10;
553c5bcc
VZ
478 break;
479
406d283a 480 case wxOS_WINDOWS_NT:
553c5bcc
VZ
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 {
9a83f860 504 wxLogLastError(wxT("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
553c5bcc
VZ
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