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