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