]> git.saurik.com Git - wxWidgets.git/blob - src/msw/settings.cpp
wxSystemSettings class API face lift: better names for its methods
[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 and Markus Holzem
9 // Licence: wxWindows license
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/gdicmn.h"
29 #endif
30
31 #include "wx/settings.h"
32
33 #include "wx/msw/private.h"
34
35 #include "wx/module.h"
36 #include "wx/fontutil.h"
37
38 // ----------------------------------------------------------------------------
39 // private classes
40 // ----------------------------------------------------------------------------
41
42 // the module which is used to clean up wxSystemSettingsNative data (this is a
43 // singleton class so it can't be done in the dtor)
44 class wxSystemSettingsModule : public wxModule
45 {
46 public:
47 virtual bool OnInit();
48 virtual void OnExit();
49
50 private:
51 DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
52 };
53
54 // ----------------------------------------------------------------------------
55 // global data
56 // ----------------------------------------------------------------------------
57
58 // the font returned by GetFont(wxSYS_DEFAULT_GUI_FONT): it is created when
59 // GetFont() is called for the first time and deleted by wxSystemSettingsModule
60 static wxFont *gs_fontDefault = NULL;
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66 // TODO: see ::SystemParametersInfo for all sorts of Windows settings.
67 // Different args are required depending on the id. How does this differ
68 // from GetSystemMetric, and should it? Perhaps call it GetSystemParameter
69 // and pass an optional void* arg to get further info.
70 // Should also have SetSystemParameter.
71 // Also implement WM_WININICHANGE (NT) / WM_SETTINGCHANGE (Win95)
72
73 // ----------------------------------------------------------------------------
74 // wxSystemSettingsModule
75 // ----------------------------------------------------------------------------
76
77 IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
78
79 bool wxSystemSettingsModule::OnInit()
80 {
81 return TRUE;
82 }
83
84 void wxSystemSettingsModule::OnExit()
85 {
86 delete gs_fontDefault;
87 gs_fontDefault = NULL;
88 }
89
90 // ----------------------------------------------------------------------------
91 // wxSystemSettingsNative
92 // ----------------------------------------------------------------------------
93
94 // ----------------------------------------------------------------------------
95 // colours
96 // ----------------------------------------------------------------------------
97
98 wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
99 {
100 wxColour col;
101 wxRGBToColour(col, ::GetSysColor(index));
102 return col;
103 }
104
105 // ----------------------------------------------------------------------------
106 // fonts
107 // ----------------------------------------------------------------------------
108
109 wxFont wxCreateFontFromStockObject(int index)
110 {
111 wxFont font;
112
113 HFONT hFont = (HFONT) ::GetStockObject(index);
114 if ( hFont )
115 {
116 LOGFONT lf;
117 if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
118 {
119 wxNativeFontInfo info;
120 info.lf = lf;
121 // Under MicroWindows we pass the HFONT as well
122 // because it's hard to convert HFONT -> LOGFONT -> HFONT
123 // It's OK to delete stock objects, the delete will be ignored.
124 #ifdef __WXMICROWIN__
125 font.Create(info, (WXHFONT) hFont);
126 #else
127 font.Create(info);
128 #endif
129 }
130 else
131 {
132 wxFAIL_MSG( _T("failed to get LOGFONT") );
133 }
134 }
135 else // GetStockObject() failed
136 {
137 wxFAIL_MSG( _T("stock font not found") );
138 }
139
140 return font;
141 }
142
143 wxFont wxSystemSettingsNative::GetFont(wxSystemFont index)
144 {
145 // wxWindow ctor calls GetSystemFont(wxSYS_DEFAULT_GUI_FONT) so we're
146 // called fairly often - this is why we cache this particular font
147 bool isDefaultRequested = index == wxSYS_DEFAULT_GUI_FONT;
148 if ( isDefaultRequested && gs_fontDefault )
149 {
150 return *gs_fontDefault;
151 }
152
153 wxFont font = wxCreateFontFromStockObject(index);
154
155 if ( isDefaultRequested )
156 {
157 // if we got here it means we hadn't cached it yet - do now
158 gs_fontDefault = new wxFont(font);
159 }
160
161 return font;
162 }
163
164 // ----------------------------------------------------------------------------
165 // system metrics/features
166 // ----------------------------------------------------------------------------
167
168 // TODO: some of the "metrics" clearly should be features now that we have
169 // HasFeature()!
170
171 // the conversion table from wxSystemMetric enum to GetSystemMetrics() param
172 //
173 // if the constant is not defined, put -1 in the table to indicate that it is
174 // unknown
175 static const int gs_metricsMap[] =
176 {
177 #ifdef __WIN32__
178 SM_CMOUSEBUTTONS,
179 #else
180 -1,
181 #endif
182
183 SM_CXBORDER,
184 SM_CYBORDER,
185 SM_CXCURSOR,
186 SM_CYCURSOR,
187 SM_CXDOUBLECLK,
188 SM_CYDOUBLECLK,
189 #if defined(__WIN32__) && defined(SM_CXDRAG)
190 SM_CXDRAG,
191 SM_CYDRAG,
192 SM_CXEDGE,
193 SM_CYEDGE,
194 #else
195 -1, -1, -1, -1
196 #endif
197 SM_CXHSCROLL,
198 SM_CYHSCROLL,
199 SM_CXHTHUMB,
200 SM_CXICON,
201 SM_CYICON,
202 SM_CXICONSPACING,
203 SM_CYICONSPACING,
204 SM_CXMIN,
205 SM_CYMIN,
206 SM_CXSCREEN,
207 SM_CYSCREEN,
208
209 #if defined(__WIN32__) && defined(SM_CXSIZEFRAME)
210 SM_CXSIZEFRAME,
211 SM_CYSIZEFRAME,
212 SM_CXSMICON,
213 SM_CYSMICON,
214 #else
215 -1, -1, -1, -1
216 #endif
217 SM_CYHSCROLL,
218 SM_CXVSCROLL,
219 SM_CXVSCROLL,
220 SM_CYVSCROLL,
221 SM_CYVTHUMB,
222 SM_CYCAPTION,
223 SM_CYMENU,
224 #if defined(__WIN32__) && defined(SM_NETWORK)
225 SM_NETWORK,
226 #else
227 -1,
228 #endif
229 SM_PENWINDOWS,
230 #if defined(__WIN32__) && defined(SM_SHOWSOUNDS)
231 SM_SHOWSOUNDS,
232 #else
233 -1,
234 #endif
235 SM_SWAPBUTTON,
236 };
237
238 // Get a system metric, e.g. scrollbar size
239 int wxSystemSettingsNative::GetMetric(wxSystemMetric index)
240 {
241 #ifdef __WXMICROWIN__
242 // TODO: probably use wxUniv themes functionality
243 return 0;
244 #else // !__WXMICROWIN__
245 wxCHECK_MSG( index < WXSIZEOF(gs_metricsMap), 0, _T("invalid metric") );
246
247 int indexMSW = gs_metricsMap[index];
248 if ( indexMSW == -1 )
249 {
250 // not supported under current system
251 return 0;
252 }
253
254 int rc = ::GetSystemMetrics(indexMSW);
255 if ( index == wxSYS_NETWORK_PRESENT )
256 {
257 // only the last bit is significant according to the MSDN
258 rc &= 1;
259 }
260
261 return rc;
262 #endif // __WXMICROWIN__/!__WXMICROWIN__
263 }
264
265 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
266 {
267 switch ( index )
268 {
269 case wxSYS_CAN_ICONIZE_FRAME:
270 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
271 return TRUE;
272
273 default:
274 wxFAIL_MSG( _T("unknown system feature") );
275
276 return FALSE;
277 }
278 }