]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
allow changing the page from keyboard in property sheet like controls even when the...
[wxWidgets.git] / src / msw / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997-2003 Julian Smart and Vadim Zeitlin
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/cursor.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/utils.h"
31 #include "wx/app.h"
32 #include "wx/bitmap.h"
33 #include "wx/icon.h"
34 #include "wx/settings.h"
35 #include "wx/intl.h"
36 #include "wx/image.h"
37 #endif
38
39 #include "wx/module.h"
40
41 #include "wx/msw/private.h"
42 #include "wx/msw/missing.h" // IDC_HAND
43
44 // define functions missing in MicroWin
45 #ifdef __WXMICROWIN__
46 static inline void DestroyCursor(HCURSOR) { }
47 static inline void SetCursor(HCURSOR) { }
48 #endif // __WXMICROWIN__
49
50 // ----------------------------------------------------------------------------
51 // private classes
52 // ----------------------------------------------------------------------------
53
54 class WXDLLEXPORT wxCursorRefData : public wxGDIImageRefData
55 {
56 public:
57 // the second parameter is used to tell us to delete the cursor when we're
58 // done with it (normally we shouldn't call DestroyCursor() this is why it
59 // doesn't happen by default)
60 wxCursorRefData(HCURSOR hcursor = 0, bool takeOwnership = false);
61
62 virtual ~wxCursorRefData() { Free(); }
63
64 virtual void Free();
65
66
67 // return the size of the standard cursor: notice that the system only
68 // supports the cursors of this size
69 static wxCoord GetStandardWidth();
70 static wxCoord GetStandardHeight();
71
72 private:
73 bool m_destroyCursor;
74
75 // standard cursor size, computed on first use
76 static wxSize ms_sizeStd;
77 };
78
79 // ----------------------------------------------------------------------------
80 // wxWin macros
81 // ----------------------------------------------------------------------------
82
83 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
84
85 // ----------------------------------------------------------------------------
86 // globals
87 // ----------------------------------------------------------------------------
88
89 // Current cursor, in order to hang on to cursor handle when setting the cursor
90 // globally
91 static wxCursor *gs_globalCursor = NULL;
92
93 // ----------------------------------------------------------------------------
94 // private classes
95 // ----------------------------------------------------------------------------
96
97 class wxCursorModule : public wxModule
98 {
99 public:
100 virtual bool OnInit()
101 {
102 gs_globalCursor = new wxCursor;
103
104 return true;
105 }
106
107 virtual void OnExit()
108 {
109 delete gs_globalCursor;
110 gs_globalCursor = (wxCursor *)NULL;
111 }
112 };
113
114 // ============================================================================
115 // implementation
116 // ============================================================================
117
118 // ----------------------------------------------------------------------------
119 // wxCursorRefData
120 // ----------------------------------------------------------------------------
121
122 wxSize wxCursorRefData::ms_sizeStd;
123
124 wxCoord wxCursorRefData::GetStandardWidth()
125 {
126 if ( !ms_sizeStd.x )
127 ms_sizeStd.x = wxSystemSettings::GetMetric(wxSYS_CURSOR_X);
128
129 return ms_sizeStd.x;
130 }
131
132 wxCoord wxCursorRefData::GetStandardHeight()
133 {
134 if ( !ms_sizeStd.y )
135 ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y);
136
137 return ms_sizeStd.y;
138 }
139
140 wxCursorRefData::wxCursorRefData(HCURSOR hcursor, bool destroy)
141 {
142 m_hCursor = (WXHCURSOR)hcursor;
143
144 if ( m_hCursor )
145 {
146 m_width = GetStandardWidth();
147 m_height = GetStandardHeight();
148 }
149
150 m_destroyCursor = destroy;
151 }
152
153 void wxCursorRefData::Free()
154 {
155 if ( m_hCursor )
156 {
157 #ifndef __WXWINCE__
158 if ( m_destroyCursor )
159 ::DestroyCursor((HCURSOR)m_hCursor);
160 #endif
161
162 m_hCursor = 0;
163 }
164 }
165
166 // ----------------------------------------------------------------------------
167 // Cursors
168 // ----------------------------------------------------------------------------
169
170 wxCursor::wxCursor()
171 {
172 }
173
174 #if wxUSE_IMAGE
175 wxCursor::wxCursor(const wxImage& image)
176 {
177 // image has to be of the standard cursor size, otherwise we won't be able
178 // to create it
179 const int w = wxCursorRefData::GetStandardWidth();
180 const int h = wxCursorRefData::GetStandardHeight();
181
182 int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
183 int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
184 int image_w = image.GetWidth();
185 int image_h = image.GetHeight();
186
187 wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w &&
188 hotSpotY >= 0 && hotSpotY < image_h,
189 _T("invalid cursor hot spot coordinates") );
190
191 wxImage imageSized(image); // final image of correct size
192
193 // if image is too small then place it in the center, resize it if too big
194 if ((w > image_w) && (h > image_h))
195 {
196 wxPoint offset((w - image_w)/2, (h - image_h)/2);
197 hotSpotX = hotSpotX + offset.x;
198 hotSpotY = hotSpotY + offset.y;
199
200 imageSized = image.Size(wxSize(w, h), offset);
201 }
202 else if ((w != image_w) || (h != image_h))
203 {
204 hotSpotX = int(hotSpotX * double(w) / double(image_w));
205 hotSpotY = int(hotSpotY * double(h) / double(image_h));
206
207 imageSized = image.Scale(w, h);
208 }
209
210 HCURSOR hcursor = wxBitmapToHCURSOR( wxBitmap(imageSized),
211 hotSpotX, hotSpotY );
212
213 #if wxUSE_WXDIB
214 if ( !hcursor )
215 {
216 wxLogWarning(_("Failed to create cursor."));
217 return;
218 }
219 #else
220 HCURSOR hcursor = 0;
221 #endif
222
223 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
224 }
225 #endif
226
227 wxCursor::wxCursor(const char WXUNUSED(bits)[],
228 int WXUNUSED(width),
229 int WXUNUSED(height),
230 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
231 const char WXUNUSED(maskBits)[])
232 {
233 }
234
235 // MicroWin doesn't have support needed for the other ctors
236 #ifdef __WXMICROWIN__
237
238 wxCursor::wxCursor(const wxString& WXUNUSED(filename),
239 long WXUNUSED(kind),
240 int WXUNUSED(hotSpotX),
241 int WXUNUSED(hotSpotY))
242 {
243 }
244
245 wxCursor::wxCursor(int WXUNUSED(cursor_type))
246 {
247 }
248
249 #else // !__WXMICROWIN__
250
251 wxCursor::wxCursor(const wxString& filename,
252 long kind,
253 int hotSpotX,
254 int hotSpotY)
255 {
256 HCURSOR hcursor;
257 switch ( kind )
258 {
259 case wxBITMAP_TYPE_CUR_RESOURCE:
260 hcursor = ::LoadCursor(wxGetInstance(), filename);
261 break;
262
263 #ifndef __WXWINCE__
264 case wxBITMAP_TYPE_CUR:
265 hcursor = ::LoadCursorFromFile(filename);
266 break;
267 #endif
268
269 case wxBITMAP_TYPE_ICO:
270 hcursor = wxBitmapToHCURSOR
271 (
272 wxIcon(filename, wxBITMAP_TYPE_ICO),
273 hotSpotX,
274 hotSpotY
275 );
276 break;
277
278 case wxBITMAP_TYPE_BMP:
279 hcursor = wxBitmapToHCURSOR
280 (
281 wxBitmap(filename, wxBITMAP_TYPE_BMP),
282 hotSpotX,
283 hotSpotY
284 );
285 break;
286
287 default:
288 wxFAIL_MSG( _T("unknown cursor resource type") );
289
290 hcursor = NULL;
291 }
292
293 if ( hcursor )
294 {
295 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
296 }
297 }
298
299 // Cursors by stock number
300 wxCursor::wxCursor(int idCursor)
301 {
302 // all wxWidgets standard cursors
303 static const struct StdCursor
304 {
305 // is this a standard Windows cursor?
306 bool isStd;
307
308 // the cursor name or id
309 LPCTSTR name;
310 } stdCursors[] =
311 {
312 { true, NULL }, // wxCURSOR_NONE
313 { true, IDC_ARROW }, // wxCURSOR_ARROW
314 { false, _T("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
315 { false, _T("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
316 { true, IDC_ARROW }, // WXCURSOR_CHAR
317
318 // Displays as an I-beam on XP, so use a cursor file
319 // { true, IDC_CROSS }, // WXCURSOR_CROSS
320 { false, _T("WXCURSOR_CROSS") }, // WXCURSOR_CROSS
321
322 // See special handling below for wxCURSOR_HAND
323 // { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND
324 { true, IDC_HAND }, // wxCURSOR_HAND
325
326 { true, IDC_IBEAM }, // WXCURSOR_IBEAM
327 { true, IDC_ARROW }, // WXCURSOR_LEFT_BUTTON
328 { false, _T("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
329 { true, IDC_ARROW }, // WXCURSOR_MIDDLE_BUTTON
330 { true, IDC_NO }, // WXCURSOR_NO_ENTRY
331 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH
332 { false, _T("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL
333 { false, _T("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT
334 { false, _T("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT
335 { true, IDC_HELP }, // WXCURSOR_QUESTION_ARROW
336 { true, IDC_ARROW }, // WXCURSOR_RIGHT_BUTTON
337 { true, IDC_SIZENESW }, // WXCURSOR_SIZENESW
338 { true, IDC_SIZENS }, // WXCURSOR_SIZENS
339 { true, IDC_SIZENWSE }, // WXCURSOR_SIZENWSE
340 { true, IDC_SIZEWE }, // WXCURSOR_SIZEWE
341 { true, IDC_SIZEALL }, // WXCURSOR_SIZING
342 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN
343 { true, IDC_WAIT }, // WXCURSOR_WAIT
344 { true, IDC_WAIT }, // WXCURSOR_WATCH
345 { false, _T("WXCURSOR_BLANK") }, // wxCURSOR_BLANK
346 { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT
347
348 // no entry for wxCURSOR_MAX
349 };
350
351 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX,
352 CursorsIdArrayMismatch );
353
354 wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors),
355 _T("invalid cursor id in wxCursor() ctor") );
356
357 const StdCursor& stdCursor = stdCursors[idCursor];
358 bool deleteLater = !stdCursor.isStd;
359
360 HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(),
361 stdCursor.name);
362
363 // IDC_HAND may not be available on some versions of Windows.
364 if ( !hcursor && idCursor == wxCURSOR_HAND)
365 {
366 hcursor = ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND"));
367 deleteLater = true;
368 }
369
370 if ( !hcursor )
371 {
372 wxLogLastError(_T("LoadCursor"));
373 }
374 else
375 {
376 m_refData = new wxCursorRefData(hcursor, deleteLater);
377 }
378 }
379
380 #endif // __WXMICROWIN__/!__WXMICROWIN__
381
382 wxCursor::~wxCursor()
383 {
384 }
385
386 // ----------------------------------------------------------------------------
387 // other wxCursor functions
388 // ----------------------------------------------------------------------------
389
390 bool wxCursor::operator==(const wxCursor& cursor) const
391 {
392 if ( !m_refData )
393 return !cursor.m_refData;
394
395 return cursor.m_refData &&
396 ((wxCursorRefData *)m_refData)->m_hCursor ==
397 ((wxCursorRefData *)cursor.m_refData)->m_hCursor;
398 }
399
400 wxGDIImageRefData *wxCursor::CreateData() const
401 {
402 return new wxCursorRefData;
403 }
404
405 // ----------------------------------------------------------------------------
406 // Global cursor setting
407 // ----------------------------------------------------------------------------
408
409 const wxCursor *wxGetGlobalCursor()
410 {
411 return gs_globalCursor;
412 }
413
414 void wxSetCursor(const wxCursor& cursor)
415 {
416 if ( cursor.Ok() )
417 {
418 ::SetCursor(GetHcursorOf(cursor));
419
420 if ( gs_globalCursor )
421 *gs_globalCursor = cursor;
422 }
423 }