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