]> git.saurik.com Git - wxWidgets.git/blame - src/msw/settings.cpp
Corrected wrong use of delete in region code.
[wxWidgets.git] / src / msw / settings.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
8bf30fe9 2// Name: 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$
8// Copyright: (c) Julian Smart and Markus Holzem
019a60d6 9// Licence: wxWindows license
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
27#ifndef WX_PRECOMP
e02e8816 28 #include "wx/utils.h"
8bf30fe9 29 #include "wx/gdicmn.h"
2bda0e17
KB
30#endif
31
32#include "wx/settings.h"
7516ed26 33
2bda0e17 34#include "wx/msw/private.h"
7516ed26 35
d1eebf40
SC
36#ifndef SPI_GETFLATMENU
37#define SPI_GETFLATMENU 0x1022
38#endif
39
2e6d38ad 40#include "wx/module.h"
04ef50df 41#include "wx/fontutil.h"
2bda0e17 42
8bf30fe9
VZ
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
7516ed26 47// the module which is used to clean up wxSystemSettingsNative data (this is a
8bf30fe9
VZ
48// singleton class so it can't be done in the dtor)
49class wxSystemSettingsModule : public wxModule
50{
51public:
52 virtual bool OnInit();
53 virtual void OnExit();
54
55private:
56 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
57};
58
59// ----------------------------------------------------------------------------
60// global data
61// ----------------------------------------------------------------------------
62
7516ed26
VZ
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
8bf30fe9
VZ
65static wxFont *gs_fontDefault = NULL;
66
67// ============================================================================
68// implementation
69// ============================================================================
70
7516ed26
VZ
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
8bf30fe9
VZ
78// ----------------------------------------------------------------------------
79// wxSystemSettingsModule
80// ----------------------------------------------------------------------------
81
82IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
83
84bool wxSystemSettingsModule::OnInit()
85{
86 return TRUE;
87}
88
89void wxSystemSettingsModule::OnExit()
90{
91 delete gs_fontDefault;
0cbff120 92 gs_fontDefault = NULL;
8bf30fe9
VZ
93}
94
95// ----------------------------------------------------------------------------
7516ed26 96// wxSystemSettingsNative
8bf30fe9
VZ
97// ----------------------------------------------------------------------------
98
7516ed26
VZ
99// ----------------------------------------------------------------------------
100// colours
101// ----------------------------------------------------------------------------
2bda0e17 102
7516ed26 103wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
2bda0e17 104{
7d6d3bf3
VZ
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;
d1eebf40
SC
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 }
7d6d3bf3
VZ
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 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 colSys = ::GetSysColor(index);
204 }
205
206 return wxRGBToColour(colSys);
2bda0e17
KB
207}
208
7516ed26
VZ
209// ----------------------------------------------------------------------------
210// fonts
211// ----------------------------------------------------------------------------
212
04ef50df 213wxFont wxCreateFontFromStockObject(int index)
2bda0e17 214{
8bf30fe9
VZ
215 wxFont font;
216
019a60d6 217 HFONT hFont = (HFONT) ::GetStockObject(index);
8bf30fe9 218 if ( hFont )
019a60d6
VS
219 {
220 LOGFONT lf;
221 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
222 {
04ef50df
JS
223 wxNativeFontInfo info;
224 info.lf = lf;
225 // Under MicroWindows we pass the HFONT as well
226 // because it's hard to convert HFONT -> LOGFONT -> HFONT
227 // It's OK to delete stock objects, the delete will be ignored.
228#ifdef __WXMICROWIN__
229 font.Create(info, (WXHFONT) hFont);
230#else
231 font.Create(info);
232#endif
019a60d6
VS
233 }
234 else
235 {
8bf30fe9 236 wxFAIL_MSG( _T("failed to get LOGFONT") );
019a60d6
VS
237 }
238 }
8bf30fe9
VZ
239 else // GetStockObject() failed
240 {
241 wxFAIL_MSG( _T("stock font not found") );
242 }
7516ed26 243
04ef50df
JS
244 return font;
245}
246
7516ed26 247wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
04ef50df
JS
248{
249 // wxWindow ctor calls GetSystemFont(wxSYS_DEFAULT_GUI_FONT) so we're
250 // called fairly often - this is why we cache this particular font
251 bool isDefaultRequested = index == wxSYS_DEFAULT_GUI_FONT;
252 if ( isDefaultRequested && gs_fontDefault )
253 {
254 return *gs_fontDefault;
255 }
256
257 wxFont font = wxCreateFontFromStockObject(index);
8bf30fe9
VZ
258
259 if ( isDefaultRequested )
019a60d6 260 {
8bf30fe9
VZ
261 // if we got here it means we hadn't cached it yet - do now
262 gs_fontDefault = new wxFont(font);
019a60d6 263 }
8bf30fe9
VZ
264
265 return font;
2bda0e17
KB
266}
267
7516ed26
VZ
268// ----------------------------------------------------------------------------
269// system metrics/features
270// ----------------------------------------------------------------------------
271
272// TODO: some of the "metrics" clearly should be features now that we have
273// HasFeature()!
274
275// the conversion table from wxSystemMetric enum to GetSystemMetrics() param
276//
277// if the constant is not defined, put -1 in the table to indicate that it is
278// unknown
279static const int gs_metricsMap[] =
2bda0e17 280{
fc29e86e 281 -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0.
2bda0e17 282#ifdef __WIN32__
7516ed26
VZ
283 SM_CMOUSEBUTTONS,
284#else
285 -1,
2bda0e17
KB
286#endif
287
7516ed26
VZ
288 SM_CXBORDER,
289 SM_CYBORDER,
290 SM_CXCURSOR,
291 SM_CYCURSOR,
292 SM_CXDOUBLECLK,
293 SM_CYDOUBLECLK,
2432b92d 294#if defined(__WIN32__) && defined(SM_CXDRAG)
7516ed26
VZ
295 SM_CXDRAG,
296 SM_CYDRAG,
297 SM_CXEDGE,
298 SM_CYEDGE,
299#else
300 -1, -1, -1, -1
2bda0e17 301#endif
7516ed26
VZ
302 SM_CXHSCROLL,
303 SM_CYHSCROLL,
304 SM_CXHTHUMB,
305 SM_CXICON,
306 SM_CYICON,
307 SM_CXICONSPACING,
308 SM_CYICONSPACING,
309 SM_CXMIN,
310 SM_CYMIN,
311 SM_CXSCREEN,
312 SM_CYSCREEN,
2432b92d
JS
313
314#if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
7516ed26
VZ
315 SM_CXSIZEFRAME,
316 SM_CYSIZEFRAME,
317 SM_CXSMICON,
318 SM_CYSMICON,
319#else
320 -1, -1, -1, -1
2bda0e17 321#endif
7516ed26
VZ
322 SM_CYHSCROLL,
323 SM_CXVSCROLL,
324 SM_CXVSCROLL,
325 SM_CYVSCROLL,
326 SM_CYVTHUMB,
327 SM_CYCAPTION,
328 SM_CYMENU,
2432b92d 329#if defined(__WIN32__) && defined(SM_NETWORK)
7516ed26
VZ
330 SM_NETWORK,
331#else
332 -1,
2bda0e17 333#endif
7516ed26 334 SM_PENWINDOWS,
2432b92d 335#if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
7516ed26
VZ
336 SM_SHOWSOUNDS,
337#else
338 -1,
2bda0e17 339#endif
7516ed26
VZ
340 SM_SWAPBUTTON,
341};
342
343// Get a system metric, e.g. scrollbar size
344int wxSystemSettingsNative::GetMetric(wxSystemMetric index)
345{
346#ifdef __WXMICROWIN__
347 // TODO: probably use wxUniv themes functionality
348 return 0;
349#else // !__WXMICROWIN__
68d95db4
VZ
350 wxCHECK_MSG( index > 0 && (size_t)index < WXSIZEOF(gs_metricsMap), 0,
351 _T("invalid metric") );
7516ed26
VZ
352
353 int indexMSW = gs_metricsMap[index];
354 if ( indexMSW == -1 )
355 {
356 // not supported under current system
357 return 0;
019a60d6 358 }
7516ed26
VZ
359
360 int rc = ::GetSystemMetrics(indexMSW);
361 if ( index == wxSYS_NETWORK_PRESENT )
362 {
363 // only the last bit is significant according to the MSDN
364 rc &= 1;
365 }
366
367 return rc;
368#endif // __WXMICROWIN__/!__WXMICROWIN__
2bda0e17
KB
369}
370
7516ed26 371bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
253293c1 372{
7516ed26 373 switch ( index )
253293c1 374 {
7516ed26 375 case wxSYS_CAN_ICONIZE_FRAME:
253293c1 376 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
bb6de5ff 377 return TRUE;
7516ed26 378
253293c1 379 default:
7516ed26
VZ
380 wxFAIL_MSG( _T("unknown system feature") );
381
253293c1
VS
382 return FALSE;
383 }
384}