| 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 | -1, // wxSystemMetric enums start at 1, so give a dummy value for pos 0. |
| 178 | #ifdef __WIN32__ |
| 179 | SM_CMOUSEBUTTONS, |
| 180 | #else |
| 181 | -1, |
| 182 | #endif |
| 183 | |
| 184 | SM_CXBORDER, |
| 185 | SM_CYBORDER, |
| 186 | SM_CXCURSOR, |
| 187 | SM_CYCURSOR, |
| 188 | SM_CXDOUBLECLK, |
| 189 | SM_CYDOUBLECLK, |
| 190 | #if defined(__WIN32__) && defined(SM_CXDRAG) |
| 191 | SM_CXDRAG, |
| 192 | SM_CYDRAG, |
| 193 | SM_CXEDGE, |
| 194 | SM_CYEDGE, |
| 195 | #else |
| 196 | -1, -1, -1, -1 |
| 197 | #endif |
| 198 | SM_CXHSCROLL, |
| 199 | SM_CYHSCROLL, |
| 200 | SM_CXHTHUMB, |
| 201 | SM_CXICON, |
| 202 | SM_CYICON, |
| 203 | SM_CXICONSPACING, |
| 204 | SM_CYICONSPACING, |
| 205 | SM_CXMIN, |
| 206 | SM_CYMIN, |
| 207 | SM_CXSCREEN, |
| 208 | SM_CYSCREEN, |
| 209 | |
| 210 | #if defined(__WIN32__) && defined(SM_CXSIZEFRAME) |
| 211 | SM_CXSIZEFRAME, |
| 212 | SM_CYSIZEFRAME, |
| 213 | SM_CXSMICON, |
| 214 | SM_CYSMICON, |
| 215 | #else |
| 216 | -1, -1, -1, -1 |
| 217 | #endif |
| 218 | SM_CYHSCROLL, |
| 219 | SM_CXVSCROLL, |
| 220 | SM_CXVSCROLL, |
| 221 | SM_CYVSCROLL, |
| 222 | SM_CYVTHUMB, |
| 223 | SM_CYCAPTION, |
| 224 | SM_CYMENU, |
| 225 | #if defined(__WIN32__) && defined(SM_NETWORK) |
| 226 | SM_NETWORK, |
| 227 | #else |
| 228 | -1, |
| 229 | #endif |
| 230 | SM_PENWINDOWS, |
| 231 | #if defined(__WIN32__) && defined(SM_SHOWSOUNDS) |
| 232 | SM_SHOWSOUNDS, |
| 233 | #else |
| 234 | -1, |
| 235 | #endif |
| 236 | SM_SWAPBUTTON, |
| 237 | }; |
| 238 | |
| 239 | // Get a system metric, e.g. scrollbar size |
| 240 | int wxSystemSettingsNative::GetMetric(wxSystemMetric index) |
| 241 | { |
| 242 | #ifdef __WXMICROWIN__ |
| 243 | // TODO: probably use wxUniv themes functionality |
| 244 | return 0; |
| 245 | #else // !__WXMICROWIN__ |
| 246 | wxCHECK_MSG( index < WXSIZEOF(gs_metricsMap), 0, _T("invalid metric") ); |
| 247 | |
| 248 | int indexMSW = gs_metricsMap[index]; |
| 249 | if ( indexMSW == -1 ) |
| 250 | { |
| 251 | // not supported under current system |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int rc = ::GetSystemMetrics(indexMSW); |
| 256 | if ( index == wxSYS_NETWORK_PRESENT ) |
| 257 | { |
| 258 | // only the last bit is significant according to the MSDN |
| 259 | rc &= 1; |
| 260 | } |
| 261 | |
| 262 | return rc; |
| 263 | #endif // __WXMICROWIN__/!__WXMICROWIN__ |
| 264 | } |
| 265 | |
| 266 | bool wxSystemSettingsNative::HasFeature(wxSystemFeature index) |
| 267 | { |
| 268 | switch ( index ) |
| 269 | { |
| 270 | case wxSYS_CAN_ICONIZE_FRAME: |
| 271 | case wxSYS_CAN_DRAW_FRAME_DECORATIONS: |
| 272 | return TRUE; |
| 273 | |
| 274 | default: |
| 275 | wxFAIL_MSG( _T("unknown system feature") ); |
| 276 | |
| 277 | return FALSE; |
| 278 | } |
| 279 | } |