]> git.saurik.com Git - wxWidgets.git/blame - src/msw/settings.cpp
Forward port 2.8 fix.
[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{
94 delete gs_fontDefault;
0cbff120 95 gs_fontDefault = NULL;
8bf30fe9
VZ
96}
97
98// ----------------------------------------------------------------------------
7516ed26 99// wxSystemSettingsNative
8bf30fe9
VZ
100// ----------------------------------------------------------------------------
101
7516ed26
VZ
102// ----------------------------------------------------------------------------
103// colours
104// ----------------------------------------------------------------------------
2bda0e17 105
7516ed26 106wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
2bda0e17 107{
7d6d3bf3
VZ
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;
57f4f925 112 bool hasCol = false;
7d6d3bf3
VZ
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
9f2968ad
VZ
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 )
7d6d3bf3
VZ
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
7d6d3bf3
VZ
148 int verMaj, verMin;
149 wxGetOsVersion(&verMaj, &verMin);
150 if ( verMaj < 4 )
151 {
152 // NT 3.5
57f4f925 153 useDefault = true;
7d6d3bf3
VZ
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
57f4f925
WS
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 }
92218ce6 179 }
7d6d3bf3
VZ
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 {
d285d708 193 unsigned int n = index - wxSYS_COLOUR_BTNHIGHLIGHT;
7d6d3bf3
VZ
194
195 wxASSERT_MSG( n < WXSIZEOF(s_defaultSysColors),
9a83f860 196 wxT("forgot tp update the default colours array") );
7d6d3bf3
VZ
197
198 colSys = s_defaultSysColors[n];
57f4f925 199 hasCol = true;
7d6d3bf3
VZ
200 }
201 }
202 }
203
204 if ( !hasCol )
205 {
39d2f9a7
JS
206#ifdef __WXWINCE__
207 colSys = ::GetSysColor(index|SYS_COLOR_INDEX_FLAG);
208#else
7d6d3bf3 209 colSys = ::GetSysColor(index);
39d2f9a7 210#endif
7d6d3bf3 211 }
03647350 212
e3527f7b
FM
213 wxColour ret = wxRGBToColour(colSys);
214 wxASSERT(ret.IsOk());
215 return ret;
2bda0e17
KB
216}
217
7516ed26
VZ
218// ----------------------------------------------------------------------------
219// fonts
220// ----------------------------------------------------------------------------
221
04ef50df 222wxFont wxCreateFontFromStockObject(int index)
2bda0e17 223{
8bf30fe9
VZ
224 wxFont font;
225
019a60d6 226 HFONT hFont = (HFONT) ::GetStockObject(index);
8bf30fe9 227 if ( hFont )
019a60d6
VS
228 {
229 LOGFONT lf;
230 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
231 {
04ef50df
JS
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
019a60d6
VS
242 }
243 else
244 {
9a83f860 245 wxFAIL_MSG( wxT("failed to get LOGFONT") );
019a60d6
VS
246 }
247 }
8bf30fe9
VZ
248 else // GetStockObject() failed
249 {
9a83f860 250 wxFAIL_MSG( wxT("stock font not found") );
8bf30fe9 251 }
7516ed26 252
04ef50df
JS
253 return font;
254}
255
7516ed26 256wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
04ef50df 257{
27adb8ba
VZ
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
03647350 267 wxASSERT(gs_fontDefault->IsOk() &&
e3527f7b 268 wxFontEnumerator::IsValidFacename(gs_fontDefault->GetFaceName()));
27adb8ba
VZ
269 return *gs_fontDefault;
270#else // !__WXWINCE__
46697f31 271 // wxWindow ctor calls GetFont(wxSYS_DEFAULT_GUI_FONT) so we're
553c5bcc 272 // called fairly often -- this is why we cache this particular font
16b0c553 273 if ( index == wxSYS_DEFAULT_GUI_FONT )
04ef50df 274 {
16b0c553
VZ
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;
04ef50df
JS
291 }
292
293 wxFont font = wxCreateFontFromStockObject(index);
8bf30fe9 294
b7081ff8
VZ
295 wxASSERT(font.IsOk());
296
297#if wxUSE_FONTENUM
298 wxASSERT(wxFontEnumerator::IsValidFacename(font.GetFaceName()));
299#endif // wxUSE_FONTENUM
300
8bf30fe9 301 return font;
27adb8ba 302#endif // __WXWINCE__/!__WXWINCE__
2bda0e17
KB
303}
304
7516ed26
VZ
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
316static const int gs_metricsMap[] =
2bda0e17 317{
fc29e86e 318 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
4676948b 319#if defined(__WIN32__) && !defined(__WXWINCE__)
7516ed26
VZ
320 SM_CMOUSEBUTTONS,
321#else
322 -1,
2bda0e17
KB
323#endif
324
7516ed26
VZ
325 SM_CXBORDER,
326 SM_CYBORDER,
c1dfa9eb 327#ifdef SM_CXCURSOR
9eddec69 328 SM_CXCURSOR,
7516ed26 329 SM_CYCURSOR,
c1dfa9eb 330#else
9eddec69 331 -1, -1,
c1dfa9eb 332#endif
7516ed26
VZ
333 SM_CXDOUBLECLK,
334 SM_CYDOUBLECLK,
2432b92d 335#if defined(__WIN32__) && defined(SM_CXDRAG)
7516ed26
VZ
336 SM_CXDRAG,
337 SM_CYDRAG,
338 SM_CXEDGE,
339 SM_CYEDGE,
340#else
4676948b 341 -1, -1, -1, -1,
2bda0e17 342#endif
7516ed26
VZ
343 SM_CXHSCROLL,
344 SM_CYHSCROLL,
4676948b 345#ifdef SM_CXHTHUMB
7516ed26 346 SM_CXHTHUMB,
4676948b
JS
347#else
348 -1,
349#endif
7516ed26
VZ
350 SM_CXICON,
351 SM_CYICON,
352 SM_CXICONSPACING,
353 SM_CYICONSPACING,
4676948b 354#ifdef SM_CXHTHUMB
7516ed26
VZ
355 SM_CXMIN,
356 SM_CYMIN,
4676948b
JS
357#else
358 -1, -1,
359#endif
7516ed26
VZ
360 SM_CXSCREEN,
361 SM_CYSCREEN,
2432b92d
JS
362
363#if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
7516ed26
VZ
364 SM_CXSIZEFRAME,
365 SM_CYSIZEFRAME,
366 SM_CXSMICON,
367 SM_CYSMICON,
368#else
4676948b 369 -1, -1, -1, -1,
2bda0e17 370#endif
7516ed26 371 SM_CYHSCROLL,
5573f5b8 372 SM_CXHSCROLL,
7516ed26
VZ
373 SM_CXVSCROLL,
374 SM_CYVSCROLL,
4676948b 375#ifdef SM_CYVTHUMB
7516ed26 376 SM_CYVTHUMB,
4676948b
JS
377#else
378 -1,
379#endif
7516ed26
VZ
380 SM_CYCAPTION,
381 SM_CYMENU,
2432b92d 382#if defined(__WIN32__) && defined(SM_NETWORK)
7516ed26
VZ
383 SM_NETWORK,
384#else
385 -1,
2bda0e17 386#endif
4676948b 387#ifdef SM_PENWINDOWS
7516ed26 388 SM_PENWINDOWS,
4676948b
JS
389#else
390 -1,
391#endif
2432b92d 392#if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
7516ed26
VZ
393 SM_SHOWSOUNDS,
394#else
395 -1,
2bda0e17 396#endif
d9fda37b
VZ
397 // SM_SWAPBUTTON is not available under CE and it doesn't make sense to ask
398 // for it there
399#ifdef SM_SWAPBUTTON
7516ed26 400 SM_SWAPBUTTON,
d9fda37b
VZ
401#else
402 -1,
403#endif
5595181f 404 -1 // wxSYS_DCLICK_MSEC - not available as system metric
7516ed26
VZ
405};
406
407// Get a system metric, e.g. scrollbar size
9b0b5ba7 408int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
7516ed26
VZ
409{
410#ifdef __WXMICROWIN__
411 // TODO: probably use wxUniv themes functionality
412 return 0;
413#else // !__WXMICROWIN__
68d95db4 414 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
9a83f860 415 wxT("invalid metric") );
7516ed26 416
5595181f
VZ
417 if ( index == wxSYS_DCLICK_MSEC )
418 {
419 // This one is not a Win32 system metric
420 return ::GetDoubleClickTime();
421 }
422
7516ed26
VZ
423 int indexMSW = gs_metricsMap[index];
424 if ( indexMSW == -1 )
425 {
426 // not supported under current system
1d451c5b 427 return -1;
019a60d6 428 }
7516ed26
VZ
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__
2bda0e17
KB
439}
440
7516ed26 441bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
253293c1 442{
7516ed26 443 switch ( index )
253293c1 444 {
7516ed26 445 case wxSYS_CAN_ICONIZE_FRAME:
253293c1 446 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
57f4f925 447 return true;
7516ed26 448
ddec9f1e
JS
449 case wxSYS_TABLET_PRESENT:
450 return ::GetSystemMetrics(SM_TABLETPC) != 0;
451
253293c1 452 default:
9a83f860 453 wxFAIL_MSG( wxT("unknown system feature") );
7516ed26 454
57f4f925 455 return false;
253293c1
VS
456 }
457}
553c5bcc
VZ
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
465extern 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 {
406d283a 476 case wxOS_WINDOWS_9X:
553c5bcc 477 // 4.10 is Win98
b594d7f7 478 useIconFont = verMaj == 4 && verMin >= 10;
553c5bcc
VZ
479 break;
480
406d283a 481 case wxOS_WINDOWS_NT:
553c5bcc
VZ
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 {
9a83f860 505 wxLogLastError(wxT("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
553c5bcc
VZ
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