1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/settings.cpp
3 // Purpose: wxSystemSettingsNative implementation for MSW
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/settings.h"
31 #include "wx/gdicmn.h"
32 #include "wx/module.h"
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"
39 #ifndef SPI_GETFLATMENU
40 #define SPI_GETFLATMENU 0x1022
43 #include "wx/fontutil.h"
44 #include "wx/fontenum.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
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)
52 class wxSystemSettingsModule
: public wxModule
55 virtual bool OnInit();
56 virtual void OnExit();
59 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule
)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
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
68 static wxFont
*gs_fontDefault
= NULL
;
70 // ============================================================================
72 // ============================================================================
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)
81 // ----------------------------------------------------------------------------
82 // wxSystemSettingsModule
83 // ----------------------------------------------------------------------------
85 IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule
, wxModule
)
87 bool wxSystemSettingsModule::OnInit()
92 void wxSystemSettingsModule::OnExit()
94 delete gs_fontDefault
;
95 gs_fontDefault
= NULL
;
98 // ----------------------------------------------------------------------------
99 // wxSystemSettingsNative
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 wxColour
wxSystemSettingsNative::GetColour(wxSystemColour index
)
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
114 // the default colours for the entries after BTNHIGHLIGHT
115 static const COLORREF s_defaultSysColors
[] =
117 0x000000, // 3DDKSHADOW
119 0x000000, // INFOTEXT
122 0, // filler - no std colour with this index
124 // TODO: please fill in the standard values of those, I don't have them
126 0, // GRADIENTACTIVECAPTION
127 0, // GRADIENTINACTIVECAPTION
129 0, // MENUBAR (unused)
132 if ( index
== wxSYS_COLOUR_LISTBOXTEXT
)
134 // there is no standard colour with this index, map to another one
135 index
= wxSYS_COLOUR_WINDOWTEXT
;
137 else if ( index
== wxSYS_COLOUR_LISTBOX
)
139 // there is no standard colour with this index, map to another one
140 index
= wxSYS_COLOUR_WINDOW
;
142 else if ( index
> wxSYS_COLOUR_BTNHIGHLIGHT
)
144 // the indices before BTNHIGHLIGHT are understood by GetSysColor() in
145 // all Windows version, for the other ones we have to check
149 wxGetOsVersion(&verMaj
, &verMin
);
155 else if ( verMaj
== 4 )
158 useDefault
= index
> wxSYS_COLOUR_INFOBK
;
160 else if ( verMaj
== 5 && verMin
== 0 )
163 useDefault
= index
> wxSYS_COLOUR_GRADIENTINACTIVECAPTION
;
169 // Determine if we are using flat menus, only then allow wxSYS_COLOUR_MENUBAR
170 if ( index
== wxSYS_COLOUR_MENUBAR
)
173 if ( SystemParametersInfo( SPI_GETFLATMENU
, 0 ,&isFlat
, 0 ) )
176 index
= wxSYS_COLOUR_MENU
;
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
)
189 index
= wxSYS_COLOUR_3DFACE
;
191 else // replace with default colour
193 unsigned int n
= index
- wxSYS_COLOUR_BTNHIGHLIGHT
;
195 wxASSERT_MSG( n
< WXSIZEOF(s_defaultSysColors
),
196 wxT("forgot tp update the default colours array") );
198 colSys
= s_defaultSysColors
[n
];
207 colSys
= ::GetSysColor(index
|SYS_COLOR_INDEX_FLAG
);
209 colSys
= ::GetSysColor(index
);
213 wxColour ret
= wxRGBToColour(colSys
);
214 wxASSERT(ret
.IsOk());
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 wxFont
wxCreateFontFromStockObject(int index
)
226 HFONT hFont
= (HFONT
) ::GetStockObject(index
);
230 if ( ::GetObject(hFont
, sizeof(LOGFONT
), &lf
) != 0 )
232 wxNativeFontInfo info
;
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
);
245 wxFAIL_MSG( wxT("failed to get LOGFONT") );
248 else // GetStockObject() failed
250 wxFAIL_MSG( wxT("stock font not found") );
256 wxFont
wxSystemSettingsNative::GetFont(wxSystemFont index
)
259 // under CE only a single SYSTEM_FONT exists
262 if ( !gs_fontDefault
)
264 gs_fontDefault
= new wxFont(wxCreateFontFromStockObject(SYSTEM_FONT
));
267 wxASSERT(gs_fontDefault
->IsOk() &&
268 wxFontEnumerator::IsValidFacename(gs_fontDefault
->GetFaceName()));
269 return *gs_fontDefault
;
270 #else // !__WXWINCE__
271 // wxWindow ctor calls GetFont(wxSYS_DEFAULT_GUI_FONT) so we're
272 // called fairly often -- this is why we cache this particular font
273 if ( index
== wxSYS_DEFAULT_GUI_FONT
)
275 if ( !gs_fontDefault
)
277 // http://blogs.msdn.com/oldnewthing/archive/2005/07/07/436435.aspx
278 // explains why neither SYSTEM_FONT nor DEFAULT_GUI_FONT should be
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
);
290 return *gs_fontDefault
;
293 wxFont font
= wxCreateFontFromStockObject(index
);
295 wxASSERT(font
.IsOk());
298 wxASSERT(wxFontEnumerator::IsValidFacename(font
.GetFaceName()));
299 #endif // wxUSE_FONTENUM
302 #endif // __WXWINCE__/!__WXWINCE__
305 // ----------------------------------------------------------------------------
306 // system metrics/features
307 // ----------------------------------------------------------------------------
309 // TODO: some of the "metrics" clearly should be features now that we have
312 // the conversion table from wxSystemMetric enum to GetSystemMetrics() param
314 // if the constant is not defined, put -1 in the table to indicate that it is
316 static const int gs_metricsMap
[] =
318 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
319 #if defined(__WIN32__) && !defined(__WXWINCE__)
335 #if defined(__WIN32__) && defined(SM_CXDRAG)
363 #if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
382 #if defined(__WIN32__) && defined(SM_NETWORK)
392 #if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
397 // SM_SWAPBUTTON is not available under CE and it doesn't make sense to ask
404 -1 // wxSYS_DCLICK_MSEC - not available as system metric
407 // Get a system metric, e.g. scrollbar size
408 int wxSystemSettingsNative::GetMetric(wxSystemMetric index
, wxWindow
* WXUNUSED(win
))
410 #ifdef __WXMICROWIN__
411 // TODO: probably use wxUniv themes functionality
413 #else // !__WXMICROWIN__
414 wxCHECK_MSG( index
> 0 && (size_t)index
< WXSIZEOF(gs_metricsMap
), 0,
415 wxT("invalid metric") );
417 if ( index
== wxSYS_DCLICK_MSEC
)
419 // This one is not a Win32 system metric
420 return ::GetDoubleClickTime();
423 int indexMSW
= gs_metricsMap
[index
];
424 if ( indexMSW
== -1 )
426 // not supported under current system
430 int rc
= ::GetSystemMetrics(indexMSW
);
431 if ( index
== wxSYS_NETWORK_PRESENT
)
433 // only the last bit is significant according to the MSDN
438 #endif // __WXMICROWIN__/!__WXMICROWIN__
441 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
445 case wxSYS_CAN_ICONIZE_FRAME
:
446 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
:
449 case wxSYS_TABLET_PRESENT
:
450 return ::GetSystemMetrics(SM_TABLETPC
) != 0;
453 wxFAIL_MSG( wxT("unknown system feature") );
459 // ----------------------------------------------------------------------------
460 // function from wx/msw/wrapcctl.h: there is really no other place for it...
461 // ----------------------------------------------------------------------------
463 #if wxUSE_LISTCTRL || wxUSE_TREECTRL
465 extern wxFont
wxGetCCDefaultFont()
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
474 switch ( wxGetOsVersion(&verMaj
, &verMin
) )
476 case wxOS_WINDOWS_9X
:
478 useIconFont
= verMaj
== 4 && verMin
>= 10;
481 case wxOS_WINDOWS_NT
:
483 useIconFont
= verMaj
>= 5;
493 if ( ::SystemParametersInfo
495 SPI_GETICONTITLELOGFONT
,
501 return wxFont(wxCreateFontFromLogFont(&lf
));
505 wxLogLastError(wxT("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
508 #endif // __WXWINCE__
510 // fall back to the default font for the normal controls
511 return wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
514 #endif // wxUSE_LISTCTRL || wxUSE_TREECTRL