compilation warning fix for the daily builds
[wxWidgets.git] / src / msw / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 and Markus Holzem
9 // Licence: wxWindows license
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 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/gdicmn.h"
30 #endif
31
32 #include "wx/settings.h"
33
34 #include "wx/msw/private.h"
35
36 #include "wx/module.h"
37 #include "wx/fontutil.h"
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 // the module which is used to clean up wxSystemSettingsNative data (this is a
44 // singleton class so it can't be done in the dtor)
45 class wxSystemSettingsModule : public wxModule
46 {
47 public:
48 virtual bool OnInit();
49 virtual void OnExit();
50
51 private:
52 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
53 };
54
55 // ----------------------------------------------------------------------------
56 // global data
57 // ----------------------------------------------------------------------------
58
59 // the font returned by GetFont(wxSYS_DEFAULT_GUI_FONT): it is created when
60 // GetFont() is called for the first time and deleted by wxSystemSettingsModule
61 static wxFont *gs_fontDefault = NULL;
62
63 // ============================================================================
64 // implementation
65 // ============================================================================
66
67 // TODO: see ::SystemParametersInfo for all sorts of Windows settings.
68 // Different args are required depending on the id. How does this differ
69 // from GetSystemMetric, and should it? Perhaps call it GetSystemParameter
70 // and pass an optional void* arg to get further info.
71 // Should also have SetSystemParameter.
72 // Also implement WM_WININICHANGE (NT) / WM_SETTINGCHANGE (Win95)
73
74 // ----------------------------------------------------------------------------
75 // wxSystemSettingsModule
76 // ----------------------------------------------------------------------------
77
78 IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
79
80 bool wxSystemSettingsModule::OnInit()
81 {
82 return TRUE;
83 }
84
85 void wxSystemSettingsModule::OnExit()
86 {
87 delete gs_fontDefault;
88 gs_fontDefault = NULL;
89 }
90
91 // ----------------------------------------------------------------------------
92 // wxSystemSettingsNative
93 // ----------------------------------------------------------------------------
94
95 // ----------------------------------------------------------------------------
96 // colours
97 // ----------------------------------------------------------------------------
98
99 wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
100 {
101 // we use 0 as the default value just to avoid compiler warnings, as there
102 // is no invalid colour value we use hasCol as the real indicator of
103 // whether colSys was initialized or not
104 COLORREF colSys = 0;
105 bool hasCol = FALSE;
106
107 // the default colours for the entries after BTNHIGHLIGHT
108 static const COLORREF s_defaultSysColors[] =
109 {
110 0x000000, // 3DDKSHADOW
111 0xdfdfdf, // 3DLIGHT
112 0x000000, // INFOTEXT
113 0xe1ffff, // INFOBK
114
115 0, // filler - no std colour with this index
116
117 // TODO: please fill in the standard values of those, I don't have them
118 0, // HOTLIGHT
119 0, // GRADIENTACTIVECAPTION
120 0, // GRADIENTINACTIVECAPTION
121 0, // MENU
122 0, // MENUBAR (unused)
123 };
124
125 if ( index == wxSYS_COLOUR_LISTBOX )
126 {
127 // there is no standard colour with this index, map to another one
128 index = wxSYS_COLOUR_WINDOW;
129 }
130 else if ( index > wxSYS_COLOUR_BTNHIGHLIGHT )
131 {
132 // the indices before BTNHIGHLIGHT are understood by GetSysColor() in
133 // all Windows version, for the other ones we have to check
134 bool useDefault;
135
136 // none of the is supported under Win16 anyhow
137 #ifdef __WIN32__
138 int verMaj, verMin;
139 wxGetOsVersion(&verMaj, &verMin);
140 if ( verMaj < 4 )
141 {
142 // NT 3.5
143 useDefault = TRUE;
144 }
145 else if ( verMaj == 4 )
146 {
147 // Win95/NT 4.0
148 useDefault = index > wxSYS_COLOUR_INFOBK;
149 }
150 else if ( verMaj == 5 && verMin == 0 )
151 {
152 // Win98/Win2K
153 useDefault = index > wxSYS_COLOUR_GRADIENTINACTIVECAPTION;
154 }
155 else // >= 5.1
156 {
157 // 5.1 is Windows XP
158 useDefault = FALSE;
159 }
160 #else
161 useDefault = TRUE;
162 #endif // __WIN32__
163
164 if ( useDefault )
165 {
166 // special handling for MENUBAR colour: we use this in wxToolBar
167 // and wxStatusBar to have correct bg colour under Windows XP
168 // (which uses COLOR_MENUBAR for them) but they should still look
169 // correctly under previous Windows versions as well
170 if ( index == wxSYS_COLOUR_MENUBAR )
171 {
172 index = wxSYS_COLOUR_3DFACE;
173 }
174 else // replace with default colour
175 {
176 int n = index - wxSYS_COLOUR_BTNHIGHLIGHT;
177
178 wxASSERT_MSG( n < WXSIZEOF(s_defaultSysColors),
179 _T("forgot tp update the default colours array") );
180
181 colSys = s_defaultSysColors[n];
182 hasCol = TRUE;
183 }
184 }
185 }
186
187 if ( !hasCol )
188 {
189 colSys = ::GetSysColor(index);
190 }
191
192 return wxRGBToColour(colSys);
193 }
194
195 // ----------------------------------------------------------------------------
196 // fonts
197 // ----------------------------------------------------------------------------
198
199 wxFont wxCreateFontFromStockObject(int index)
200 {
201 wxFont font;
202
203 HFONT hFont = (HFONT) ::GetStockObject(index);
204 if ( hFont )
205 {
206 LOGFONT lf;
207 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
208 {
209 wxNativeFontInfo info;
210 info.lf = lf;
211 // Under MicroWindows we pass the HFONT as well
212 // because it's hard to convert HFONT -> LOGFONT -> HFONT
213 // It's OK to delete stock objects, the delete will be ignored.
214 #ifdef __WXMICROWIN__
215 font.Create(info, (WXHFONT) hFont);
216 #else
217 font.Create(info);
218 #endif
219 }
220 else
221 {
222 wxFAIL_MSG( _T("failed to get LOGFONT") );
223 }
224 }
225 else // GetStockObject() failed
226 {
227 wxFAIL_MSG( _T("stock font not found") );
228 }
229
230 return font;
231 }
232
233 wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
234 {
235 // wxWindow ctor calls GetSystemFont(wxSYS_DEFAULT_GUI_FONT) so we're
236 // called fairly often - this is why we cache this particular font
237 bool isDefaultRequested = index == wxSYS_DEFAULT_GUI_FONT;
238 if ( isDefaultRequested && gs_fontDefault )
239 {
240 return *gs_fontDefault;
241 }
242
243 wxFont font = wxCreateFontFromStockObject(index);
244
245 if ( isDefaultRequested )
246 {
247 // if we got here it means we hadn't cached it yet - do now
248 gs_fontDefault = new wxFont(font);
249 }
250
251 return font;
252 }
253
254 // ----------------------------------------------------------------------------
255 // system metrics/features
256 // ----------------------------------------------------------------------------
257
258 // TODO: some of the "metrics" clearly should be features now that we have
259 // HasFeature()!
260
261 // the conversion table from wxSystemMetric enum to GetSystemMetrics() param
262 //
263 // if the constant is not defined, put -1 in the table to indicate that it is
264 // unknown
265 static const int gs_metricsMap[] =
266 {
267 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
268 #ifdef __WIN32__
269 SM_CMOUSEBUTTONS,
270 #else
271 -1,
272 #endif
273
274 SM_CXBORDER,
275 SM_CYBORDER,
276 SM_CXCURSOR,
277 SM_CYCURSOR,
278 SM_CXDOUBLECLK,
279 SM_CYDOUBLECLK,
280 #if defined(__WIN32__) && defined(SM_CXDRAG)
281 SM_CXDRAG,
282 SM_CYDRAG,
283 SM_CXEDGE,
284 SM_CYEDGE,
285 #else
286 -1, -1, -1, -1
287 #endif
288 SM_CXHSCROLL,
289 SM_CYHSCROLL,
290 SM_CXHTHUMB,
291 SM_CXICON,
292 SM_CYICON,
293 SM_CXICONSPACING,
294 SM_CYICONSPACING,
295 SM_CXMIN,
296 SM_CYMIN,
297 SM_CXSCREEN,
298 SM_CYSCREEN,
299
300 #if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
301 SM_CXSIZEFRAME,
302 SM_CYSIZEFRAME,
303 SM_CXSMICON,
304 SM_CYSMICON,
305 #else
306 -1, -1, -1, -1
307 #endif
308 SM_CYHSCROLL,
309 SM_CXVSCROLL,
310 SM_CXVSCROLL,
311 SM_CYVSCROLL,
312 SM_CYVTHUMB,
313 SM_CYCAPTION,
314 SM_CYMENU,
315 #if defined(__WIN32__) && defined(SM_NETWORK)
316 SM_NETWORK,
317 #else
318 -1,
319 #endif
320 SM_PENWINDOWS,
321 #if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
322 SM_SHOWSOUNDS,
323 #else
324 -1,
325 #endif
326 SM_SWAPBUTTON,
327 };
328
329 // Get a system metric, e.g. scrollbar size
330 int wxSystemSettingsNative::GetMetric(wxSystemMetric index)
331 {
332 #ifdef __WXMICROWIN__
333 // TODO: probably use wxUniv themes functionality
334 return 0;
335 #else // !__WXMICROWIN__
336 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
337 _T("invalid metric") );
338
339 int indexMSW = gs_metricsMap[index];
340 if ( indexMSW == -1 )
341 {
342 // not supported under current system
343 return 0;
344 }
345
346 int rc = ::GetSystemMetrics(indexMSW);
347 if ( index == wxSYS_NETWORK_PRESENT )
348 {
349 // only the last bit is significant according to the MSDN
350 rc &= 1;
351 }
352
353 return rc;
354 #endif // __WXMICROWIN__/!__WXMICROWIN__
355 }
356
357 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
358 {
359 switch ( index )
360 {
361 case wxSYS_CAN_ICONIZE_FRAME:
362 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
363 return TRUE;
364
365 default:
366 wxFAIL_MSG( _T("unknown system feature") );
367
368 return FALSE;
369 }
370 }