]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/settings.cpp
Initialize all fields of struct tm used by wxDateTime::Format().
[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#include "wx/msw/private/metrics.h"
38
39#ifndef SPI_GETFLATMENU
40#define SPI_GETFLATMENU 0x1022
41#endif
42
43#include "wx/fontutil.h"
44#include "wx/fontenum.h"
45
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
50// the module which is used to clean up wxSystemSettingsNative data (this is a
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
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
68static wxFont *gs_fontDefault = NULL;
69
70// ============================================================================
71// implementation
72// ============================================================================
73
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
81// ----------------------------------------------------------------------------
82// wxSystemSettingsModule
83// ----------------------------------------------------------------------------
84
85IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
86
87bool wxSystemSettingsModule::OnInit()
88{
89 return true;
90}
91
92void wxSystemSettingsModule::OnExit()
93{
94 wxDELETE(gs_fontDefault);
95}
96
97// ----------------------------------------------------------------------------
98// wxSystemSettingsNative
99// ----------------------------------------------------------------------------
100
101// ----------------------------------------------------------------------------
102// colours
103// ----------------------------------------------------------------------------
104
105wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
106{
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;
111 bool hasCol = false;
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
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 )
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
147 int verMaj, verMin;
148 wxGetOsVersion(&verMaj, &verMin);
149 if ( verMaj < 4 )
150 {
151 // NT 3.5
152 useDefault = true;
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
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 }
178 }
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 {
192 unsigned int n = index - wxSYS_COLOUR_BTNHIGHLIGHT;
193
194 wxASSERT_MSG( n < WXSIZEOF(s_defaultSysColors),
195 wxT("forgot tp update the default colours array") );
196
197 colSys = s_defaultSysColors[n];
198 hasCol = true;
199 }
200 }
201 }
202
203 if ( !hasCol )
204 {
205#ifdef __WXWINCE__
206 colSys = ::GetSysColor(index|SYS_COLOR_INDEX_FLAG);
207#else
208 colSys = ::GetSysColor(index);
209#endif
210 }
211
212 wxColour ret = wxRGBToColour(colSys);
213 wxASSERT(ret.IsOk());
214 return ret;
215}
216
217// ----------------------------------------------------------------------------
218// fonts
219// ----------------------------------------------------------------------------
220
221wxFont wxCreateFontFromStockObject(int index)
222{
223 wxFont font;
224
225 HFONT hFont = (HFONT) ::GetStockObject(index);
226 if ( hFont )
227 {
228 LOGFONT lf;
229 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
230 {
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
241 }
242 else
243 {
244 wxFAIL_MSG( wxT("failed to get LOGFONT") );
245 }
246 }
247 else // GetStockObject() failed
248 {
249 wxFAIL_MSG( wxT("stock font not found") );
250 }
251
252 return font;
253}
254
255wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
256{
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
266 wxASSERT(gs_fontDefault->IsOk() &&
267 wxFontEnumerator::IsValidFacename(gs_fontDefault->GetFaceName()));
268 return *gs_fontDefault;
269#else // !__WXWINCE__
270 // wxWindow ctor calls GetFont(wxSYS_DEFAULT_GUI_FONT) so we're
271 // called fairly often -- this is why we cache this particular font
272 if ( index == wxSYS_DEFAULT_GUI_FONT )
273 {
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;
290 }
291
292 wxFont font = wxCreateFontFromStockObject(index);
293
294 wxASSERT(font.IsOk());
295
296#if wxUSE_FONTENUM
297 wxASSERT(wxFontEnumerator::IsValidFacename(font.GetFaceName()));
298#endif // wxUSE_FONTENUM
299
300 return font;
301#endif // __WXWINCE__/!__WXWINCE__
302}
303
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[] =
316{
317 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
318#if defined(__WIN32__) && !defined(__WXWINCE__)
319 SM_CMOUSEBUTTONS,
320#else
321 -1,
322#endif
323
324 SM_CXBORDER,
325 SM_CYBORDER,
326#ifdef SM_CXCURSOR
327 SM_CXCURSOR,
328 SM_CYCURSOR,
329#else
330 -1, -1,
331#endif
332 SM_CXDOUBLECLK,
333 SM_CYDOUBLECLK,
334#if defined(__WIN32__) && defined(SM_CXDRAG)
335 SM_CXDRAG,
336 SM_CYDRAG,
337 SM_CXEDGE,
338 SM_CYEDGE,
339#else
340 -1, -1, -1, -1,
341#endif
342 SM_CXHSCROLL,
343 SM_CYHSCROLL,
344#ifdef SM_CXHTHUMB
345 SM_CXHTHUMB,
346#else
347 -1,
348#endif
349 SM_CXICON,
350 SM_CYICON,
351 SM_CXICONSPACING,
352 SM_CYICONSPACING,
353#ifdef SM_CXHTHUMB
354 SM_CXMIN,
355 SM_CYMIN,
356#else
357 -1, -1,
358#endif
359 SM_CXSCREEN,
360 SM_CYSCREEN,
361
362#if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
363 SM_CXSIZEFRAME,
364 SM_CYSIZEFRAME,
365 SM_CXSMICON,
366 SM_CYSMICON,
367#else
368 -1, -1, -1, -1,
369#endif
370 SM_CYHSCROLL,
371 SM_CXHSCROLL,
372 SM_CXVSCROLL,
373 SM_CYVSCROLL,
374#ifdef SM_CYVTHUMB
375 SM_CYVTHUMB,
376#else
377 -1,
378#endif
379 SM_CYCAPTION,
380 SM_CYMENU,
381#if defined(__WIN32__) && defined(SM_NETWORK)
382 SM_NETWORK,
383#else
384 -1,
385#endif
386#ifdef SM_PENWINDOWS
387 SM_PENWINDOWS,
388#else
389 -1,
390#endif
391#if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
392 SM_SHOWSOUNDS,
393#else
394 -1,
395#endif
396 // SM_SWAPBUTTON is not available under CE and it doesn't make sense to ask
397 // for it there
398#ifdef SM_SWAPBUTTON
399 SM_SWAPBUTTON,
400#else
401 -1,
402#endif
403 -1 // wxSYS_DCLICK_MSEC - not available as system metric
404};
405
406// Get a system metric, e.g. scrollbar size
407int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
408{
409#ifdef __WXMICROWIN__
410 // TODO: probably use wxUniv themes functionality
411 return 0;
412#else // !__WXMICROWIN__
413 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
414 wxT("invalid metric") );
415
416 if ( index == wxSYS_DCLICK_MSEC )
417 {
418 // This one is not a Win32 system metric
419 return ::GetDoubleClickTime();
420 }
421
422 int indexMSW = gs_metricsMap[index];
423 if ( indexMSW == -1 )
424 {
425 // not supported under current system
426 return -1;
427 }
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__
438}
439
440bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
441{
442 switch ( index )
443 {
444 case wxSYS_CAN_ICONIZE_FRAME:
445 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
446 return true;
447
448 case wxSYS_TABLET_PRESENT:
449 return ::GetSystemMetrics(SM_TABLETPC) != 0;
450
451 default:
452 wxFAIL_MSG( wxT("unknown system feature") );
453
454 return false;
455 }
456}
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 {
475 case wxOS_WINDOWS_9X:
476 // 4.10 is Win98
477 useIconFont = verMaj == 4 && verMin >= 10;
478 break;
479
480 case wxOS_WINDOWS_NT:
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 {
504 wxLogLastError(wxT("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
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