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