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