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