]> git.saurik.com Git - wxWidgets.git/blame - src/msw/settings.cpp
Use wxFileInputStream if wxFFileInputStream is not available.
[wxWidgets.git] / src / msw / settings.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
02761f6c 2// Name: src/msw/settings.cpp
7516ed26 3// Purpose: wxSystemSettingsNative implementation for MSW
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
8bf30fe9
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
7516ed26 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
9eddec69
WS
27#include "wx/settings.h"
28
2bda0e17 29#ifndef WX_PRECOMP
e02e8816 30 #include "wx/utils.h"
8bf30fe9 31 #include "wx/gdicmn.h"
02761f6c 32 #include "wx/module.h"
2bda0e17
KB
33#endif
34
2bda0e17 35#include "wx/msw/private.h"
7212d155 36#include "wx/msw/missing.h" // for SM_CXCURSOR, SM_CYCURSOR, SM_TABLETPC
7516ed26 37
d1eebf40
SC
38#ifndef SPI_GETFLATMENU
39#define SPI_GETFLATMENU 0x1022
40#endif
41
04ef50df 42#include "wx/fontutil.h"
e3527f7b 43#include "wx/fontenum.h"
2bda0e17 44
8bf30fe9
VZ
45// ----------------------------------------------------------------------------
46// private classes
47// ----------------------------------------------------------------------------
48
7516ed26 49// the module which is used to clean up wxSystemSettingsNative data (this is a
8bf30fe9
VZ
50// singleton class so it can't be done in the dtor)
51class wxSystemSettingsModule : public wxModule
52{
53public:
54 virtual bool OnInit();
55 virtual void OnExit();
56
57private:
58 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
59};
60
61// ----------------------------------------------------------------------------
62// global data
63// ----------------------------------------------------------------------------
64
7516ed26
VZ
65// the font returned by GetFont(wxSYS_DEFAULT_GUI_FONT): it is created when
66// GetFont() is called for the first time and deleted by wxSystemSettingsModule
8bf30fe9
VZ
67static wxFont *gs_fontDefault = NULL;
68
69// ============================================================================
70// implementation
71// ============================================================================
72
7516ed26
VZ
73// TODO: see ::SystemParametersInfo for all sorts of Windows settings.
74// Different args are required depending on the id. How does this differ
75// from GetSystemMetric, and should it? Perhaps call it GetSystemParameter
76// and pass an optional void* arg to get further info.
77// Should also have SetSystemParameter.
78// Also implement WM_WININICHANGE (NT) / WM_SETTINGCHANGE (Win95)
79
8bf30fe9
VZ
80// ----------------------------------------------------------------------------
81// wxSystemSettingsModule
82// ----------------------------------------------------------------------------
83
84IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
85
86bool wxSystemSettingsModule::OnInit()
87{
57f4f925 88 return true;
8bf30fe9
VZ
89}
90
91void wxSystemSettingsModule::OnExit()
92{
93 delete gs_fontDefault;
0cbff120 94 gs_fontDefault = NULL;
8bf30fe9
VZ
95}
96
97// ----------------------------------------------------------------------------
7516ed26 98// wxSystemSettingsNative
8bf30fe9
VZ
99// ----------------------------------------------------------------------------
100
7516ed26
VZ
101// ----------------------------------------------------------------------------
102// colours
103// ----------------------------------------------------------------------------
2bda0e17 104
7516ed26 105wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
2bda0e17 106{
7d6d3bf3
VZ
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;
57f4f925 111 bool hasCol = false;
7d6d3bf3
VZ
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
9f2968ad
VZ
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 )
7d6d3bf3
VZ
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
7d6d3bf3
VZ
147 int verMaj, verMin;
148 wxGetOsVersion(&verMaj, &verMin);
149 if ( verMaj < 4 )
150 {
151 // NT 3.5
57f4f925 152 useDefault = true;
7d6d3bf3
VZ
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
57f4f925
WS
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 }
92218ce6 178 }
7d6d3bf3
VZ
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 {
d285d708 192 unsigned int n = index - wxSYS_COLOUR_BTNHIGHLIGHT;
7d6d3bf3
VZ
193
194 wxASSERT_MSG( n < WXSIZEOF(s_defaultSysColors),
9a83f860 195 wxT("forgot tp update the default colours array") );
7d6d3bf3
VZ
196
197 colSys = s_defaultSysColors[n];
57f4f925 198 hasCol = true;
7d6d3bf3
VZ
199 }
200 }
201 }
202
203 if ( !hasCol )
204 {
39d2f9a7
JS
205#ifdef __WXWINCE__
206 colSys = ::GetSysColor(index|SYS_COLOR_INDEX_FLAG);
207#else
7d6d3bf3 208 colSys = ::GetSysColor(index);
39d2f9a7 209#endif
7d6d3bf3 210 }
e3527f7b
FM
211
212 wxColour ret = wxRGBToColour(colSys);
213 wxASSERT(ret.IsOk());
214 return ret;
2bda0e17
KB
215}
216
7516ed26
VZ
217// ----------------------------------------------------------------------------
218// fonts
219// ----------------------------------------------------------------------------
220
04ef50df 221wxFont wxCreateFontFromStockObject(int index)
2bda0e17 222{
8bf30fe9
VZ
223 wxFont font;
224
019a60d6 225 HFONT hFont = (HFONT) ::GetStockObject(index);
8bf30fe9 226 if ( hFont )
019a60d6
VS
227 {
228 LOGFONT lf;
229 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
230 {
04ef50df
JS
231 wxNativeFontInfo info;
232 info.lf = lf;
f8c10795 233#ifndef __WXWINCE__
b4772c24
JS
234 // We want Windows 2000 or later to have new fonts even MS Shell Dlg
235 // is returned as default GUI font for compatibility
236 int verMaj;
406d283a 237 if(index == DEFAULT_GUI_FONT && wxGetOsVersion(&verMaj) == wxOS_WINDOWS_NT && verMaj >= 5)
b4772c24 238 wxStrcpy(info.lf.lfFaceName, wxT("MS Shell Dlg 2"));
f8c10795 239#endif
04ef50df
JS
240 // Under MicroWindows we pass the HFONT as well
241 // because it's hard to convert HFONT -> LOGFONT -> HFONT
242 // It's OK to delete stock objects, the delete will be ignored.
243#ifdef __WXMICROWIN__
244 font.Create(info, (WXHFONT) hFont);
245#else
246 font.Create(info);
247#endif
019a60d6
VS
248 }
249 else
250 {
9a83f860 251 wxFAIL_MSG( wxT("failed to get LOGFONT") );
019a60d6
VS
252 }
253 }
8bf30fe9
VZ
254 else // GetStockObject() failed
255 {
9a83f860 256 wxFAIL_MSG( wxT("stock font not found") );
8bf30fe9 257 }
7516ed26 258
04ef50df
JS
259 return font;
260}
261
7516ed26 262wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
04ef50df 263{
27adb8ba
VZ
264#ifdef __WXWINCE__
265 // under CE only a single SYSTEM_FONT exists
266 index;
267
268 if ( !gs_fontDefault )
269 {
270 gs_fontDefault = new wxFont(wxCreateFontFromStockObject(SYSTEM_FONT));
271 }
272
e3527f7b
FM
273 wxASSERT(gs_fontDefault->IsOk() &&
274 wxFontEnumerator::IsValidFacename(gs_fontDefault->GetFaceName()));
27adb8ba
VZ
275 return *gs_fontDefault;
276#else // !__WXWINCE__
46697f31 277 // wxWindow ctor calls GetFont(wxSYS_DEFAULT_GUI_FONT) so we're
553c5bcc
VZ
278 // called fairly often -- this is why we cache this particular font
279 const bool isDefaultRequested = index == wxSYS_DEFAULT_GUI_FONT;
280 if ( isDefaultRequested )
04ef50df 281 {
553c5bcc
VZ
282 if ( gs_fontDefault )
283 return *gs_fontDefault;
04ef50df
JS
284 }
285
286 wxFont font = wxCreateFontFromStockObject(index);
8bf30fe9
VZ
287
288 if ( isDefaultRequested )
019a60d6 289 {
8bf30fe9
VZ
290 // if we got here it means we hadn't cached it yet - do now
291 gs_fontDefault = new wxFont(font);
019a60d6 292 }
8bf30fe9 293
e3527f7b 294 wxASSERT(font.IsOk() && wxFontEnumerator::IsValidFacename(font.GetFaceName()));
8bf30fe9 295 return font;
27adb8ba 296#endif // __WXWINCE__/!__WXWINCE__
2bda0e17
KB
297}
298
7516ed26
VZ
299// ----------------------------------------------------------------------------
300// system metrics/features
301// ----------------------------------------------------------------------------
302
303// TODO: some of the "metrics" clearly should be features now that we have
304// HasFeature()!
305
306// the conversion table from wxSystemMetric enum to GetSystemMetrics() param
307//
308// if the constant is not defined, put -1 in the table to indicate that it is
309// unknown
310static const int gs_metricsMap[] =
2bda0e17 311{
fc29e86e 312 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
4676948b 313#if defined(__WIN32__) && !defined(__WXWINCE__)
7516ed26
VZ
314 SM_CMOUSEBUTTONS,
315#else
316 -1,
2bda0e17
KB
317#endif
318
7516ed26
VZ
319 SM_CXBORDER,
320 SM_CYBORDER,
c1dfa9eb 321#ifdef SM_CXCURSOR
9eddec69 322 SM_CXCURSOR,
7516ed26 323 SM_CYCURSOR,
c1dfa9eb 324#else
9eddec69 325 -1, -1,
c1dfa9eb 326#endif
7516ed26
VZ
327 SM_CXDOUBLECLK,
328 SM_CYDOUBLECLK,
2432b92d 329#if defined(__WIN32__) && defined(SM_CXDRAG)
7516ed26
VZ
330 SM_CXDRAG,
331 SM_CYDRAG,
332 SM_CXEDGE,
333 SM_CYEDGE,
334#else
4676948b 335 -1, -1, -1, -1,
2bda0e17 336#endif
7516ed26
VZ
337 SM_CXHSCROLL,
338 SM_CYHSCROLL,
4676948b 339#ifdef SM_CXHTHUMB
7516ed26 340 SM_CXHTHUMB,
4676948b
JS
341#else
342 -1,
343#endif
7516ed26
VZ
344 SM_CXICON,
345 SM_CYICON,
346 SM_CXICONSPACING,
347 SM_CYICONSPACING,
4676948b 348#ifdef SM_CXHTHUMB
7516ed26
VZ
349 SM_CXMIN,
350 SM_CYMIN,
4676948b
JS
351#else
352 -1, -1,
353#endif
7516ed26
VZ
354 SM_CXSCREEN,
355 SM_CYSCREEN,
2432b92d
JS
356
357#if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
7516ed26
VZ
358 SM_CXSIZEFRAME,
359 SM_CYSIZEFRAME,
360 SM_CXSMICON,
361 SM_CYSMICON,
362#else
4676948b 363 -1, -1, -1, -1,
2bda0e17 364#endif
7516ed26 365 SM_CYHSCROLL,
5573f5b8 366 SM_CXHSCROLL,
7516ed26
VZ
367 SM_CXVSCROLL,
368 SM_CYVSCROLL,
4676948b 369#ifdef SM_CYVTHUMB
7516ed26 370 SM_CYVTHUMB,
4676948b
JS
371#else
372 -1,
373#endif
7516ed26
VZ
374 SM_CYCAPTION,
375 SM_CYMENU,
2432b92d 376#if defined(__WIN32__) && defined(SM_NETWORK)
7516ed26
VZ
377 SM_NETWORK,
378#else
379 -1,
2bda0e17 380#endif
4676948b 381#ifdef SM_PENWINDOWS
7516ed26 382 SM_PENWINDOWS,
4676948b
JS
383#else
384 -1,
385#endif
2432b92d 386#if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
7516ed26
VZ
387 SM_SHOWSOUNDS,
388#else
389 -1,
2bda0e17 390#endif
d9fda37b
VZ
391 // SM_SWAPBUTTON is not available under CE and it doesn't make sense to ask
392 // for it there
393#ifdef SM_SWAPBUTTON
7516ed26 394 SM_SWAPBUTTON,
d9fda37b
VZ
395#else
396 -1,
397#endif
5595181f 398 -1 // wxSYS_DCLICK_MSEC - not available as system metric
7516ed26
VZ
399};
400
401// Get a system metric, e.g. scrollbar size
9b0b5ba7 402int wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
7516ed26
VZ
403{
404#ifdef __WXMICROWIN__
405 // TODO: probably use wxUniv themes functionality
406 return 0;
407#else // !__WXMICROWIN__
68d95db4 408 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
9a83f860 409 wxT("invalid metric") );
7516ed26 410
5595181f
VZ
411 if ( index == wxSYS_DCLICK_MSEC )
412 {
413 // This one is not a Win32 system metric
414 return ::GetDoubleClickTime();
415 }
416
7516ed26
VZ
417 int indexMSW = gs_metricsMap[index];
418 if ( indexMSW == -1 )
419 {
420 // not supported under current system
1d451c5b 421 return -1;
019a60d6 422 }
7516ed26
VZ
423
424 int rc = ::GetSystemMetrics(indexMSW);
425 if ( index == wxSYS_NETWORK_PRESENT )
426 {
427 // only the last bit is significant according to the MSDN
428 rc &= 1;
429 }
430
431 return rc;
432#endif // __WXMICROWIN__/!__WXMICROWIN__
2bda0e17
KB
433}
434
7516ed26 435bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
253293c1 436{
7516ed26 437 switch ( index )
253293c1 438 {
7516ed26 439 case wxSYS_CAN_ICONIZE_FRAME:
253293c1 440 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
57f4f925 441 return true;
7516ed26 442
ddec9f1e
JS
443 case wxSYS_TABLET_PRESENT:
444 return ::GetSystemMetrics(SM_TABLETPC) != 0;
445
253293c1 446 default:
9a83f860 447 wxFAIL_MSG( wxT("unknown system feature") );
7516ed26 448
57f4f925 449 return false;
253293c1
VS
450 }
451}
553c5bcc
VZ
452
453// ----------------------------------------------------------------------------
454// function from wx/msw/wrapcctl.h: there is really no other place for it...
455// ----------------------------------------------------------------------------
456
457#if wxUSE_LISTCTRL || wxUSE_TREECTRL
458
459extern wxFont wxGetCCDefaultFont()
460{
461#ifndef __WXWINCE__
462 // under the systems enumerated below (anything released after Win98), the
463 // default font used for the common controls seems to be the desktop font
464 // which is also used for the icon titles and not the stock default GUI
465 // font
466 bool useIconFont;
467 int verMaj, verMin;
468 switch ( wxGetOsVersion(&verMaj, &verMin) )
469 {
406d283a 470 case wxOS_WINDOWS_9X:
553c5bcc 471 // 4.10 is Win98
b594d7f7 472 useIconFont = verMaj == 4 && verMin >= 10;
553c5bcc
VZ
473 break;
474
406d283a 475 case wxOS_WINDOWS_NT:
553c5bcc
VZ
476 // 5.0 is Win2k
477 useIconFont = verMaj >= 5;
478 break;
479
480 default:
481 useIconFont = false;
482 }
483
484 if ( useIconFont )
485 {
486 LOGFONT lf;
487 if ( ::SystemParametersInfo
488 (
489 SPI_GETICONTITLELOGFONT,
490 sizeof(lf),
491 &lf,
492 0
493 ) )
494 {
495 return wxFont(wxCreateFontFromLogFont(&lf));
496 }
497 else
498 {
9a83f860 499 wxLogLastError(wxT("SystemParametersInfo(SPI_GETICONTITLELOGFONT"));
553c5bcc
VZ
500 }
501 }
502#endif // __WXWINCE__
503
504 // fall back to the default font for the normal controls
505 return wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
506}
507
508#endif // wxUSE_LISTCTRL || wxUSE_TREECTRL