1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Julian Smart
8 // Copyright: (c) 1997-2003 Julian Smart and Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "cursor.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/bitmap.h"
36 #include "wx/cursor.h"
37 #include "wx/settings.h"
41 #include "wx/module.h"
43 #include "wx/msw/private.h"
45 // define functions missing in MicroWin
47 static inline void DestroyCursor(HCURSOR
) { }
48 static inline void SetCursor(HCURSOR
) { }
49 #endif // __WXMICROWIN__
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxCursorRefData
: public wxGDIImageRefData
58 // the second parameter is used to tell us to delete the cursor when we're
59 // done with it (normally we shouldn't call DestroyCursor() this is why it
60 // doesn't happen by default)
61 wxCursorRefData(HCURSOR hcursor
= 0, bool takeOwnership
= false);
63 virtual ~wxCursorRefData() { Free(); }
68 // return the size of the standard cursor: notice that the system only
69 // supports the cursors of this size
70 static wxCoord
GetStandardWidth();
71 static wxCoord
GetStandardHeight();
76 // standard cursor size, computed on first use
77 static wxSize ms_sizeStd
;
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // Current cursor, in order to hang on to cursor handle when setting the cursor
92 static wxCursor
*gs_globalCursor
= NULL
;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 class wxCursorModule
: public wxModule
101 virtual bool OnInit()
103 gs_globalCursor
= new wxCursor
;
108 virtual void OnExit()
110 delete gs_globalCursor
;
111 gs_globalCursor
= (wxCursor
*)NULL
;
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxSize
wxCursorRefData::ms_sizeStd
;
125 wxCoord
wxCursorRefData::GetStandardWidth()
128 ms_sizeStd
.x
= wxSystemSettings::GetMetric(wxSYS_CURSOR_X
);
133 wxCoord
wxCursorRefData::GetStandardHeight()
136 ms_sizeStd
.y
= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
);
141 wxCursorRefData::wxCursorRefData(HCURSOR hcursor
, bool destroy
)
143 m_hCursor
= (WXHCURSOR
)hcursor
;
147 m_width
= GetStandardWidth();
148 m_height
= GetStandardHeight();
151 m_destroyCursor
= destroy
;
154 void wxCursorRefData::Free()
159 if ( m_destroyCursor
)
160 ::DestroyCursor((HCURSOR
)m_hCursor
);
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
176 wxCursor::wxCursor(const wxImage
& image
)
178 // image has to be of the standard cursor size, otherwise we won't be able
180 const int w
= wxCursorRefData::GetStandardWidth();
181 const int h
= wxCursorRefData::GetStandardHeight();
183 int hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
184 int hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
185 int image_w
= image
.GetWidth();
186 int image_h
= image
.GetHeight();
188 wxASSERT_MSG( hotSpotX
>= 0 && hotSpotX
< image_w
&&
189 hotSpotY
>= 0 && hotSpotY
< image_h
,
190 _T("invalid cursor hot spot coordinates") );
192 wxImage
imageSized(image
); // final image of correct size
194 // if image is too small then place it in the center, resize it if too big
195 if ((w
> image_w
) && (h
> image_h
))
197 wxPoint
offset((w
- image_w
)/2, (h
- image_h
)/2);
198 hotSpotX
= hotSpotX
+ offset
.x
;
199 hotSpotY
= hotSpotY
+ offset
.y
;
201 imageSized
= image
.Size(wxSize(w
, h
), offset
);
203 else if ((w
!= image_w
) || (h
!= image_h
))
205 hotSpotX
= int(hotSpotX
* double(w
) / double(image_w
));
206 hotSpotY
= int(hotSpotY
* double(h
) / double(image_h
));
208 imageSized
= image
.Scale(w
, h
);
211 HCURSOR hcursor
= wxBitmapToHCURSOR( wxBitmap(imageSized
),
212 hotSpotX
, hotSpotY
);
216 wxLogWarning(_("Failed to create cursor."));
220 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
224 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
226 int WXUNUSED(height
),
227 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
228 const char WXUNUSED(maskBits
)[])
232 // MicroWin doesn't have support needed for the other ctors
233 #ifdef __WXMICROWIN__
235 wxCursor::wxCursor(const wxString
& WXUNUSED(filename
),
237 int WXUNUSED(hotSpotX
),
238 int WXUNUSED(hotSpotY
))
242 wxCursor::wxCursor(int WXUNUSED(cursor_type
))
246 #else // !__WXMICROWIN__
248 wxCursor::wxCursor(const wxString
& filename
,
256 case wxBITMAP_TYPE_CUR_RESOURCE
:
257 hcursor
= ::LoadCursor(wxGetInstance(), filename
);
261 case wxBITMAP_TYPE_CUR
:
262 hcursor
= ::LoadCursorFromFile(filename
);
266 case wxBITMAP_TYPE_ICO
:
267 hcursor
= wxBitmapToHCURSOR
269 wxIcon(filename
, wxBITMAP_TYPE_ICO
),
275 case wxBITMAP_TYPE_BMP
:
276 hcursor
= wxBitmapToHCURSOR
278 wxBitmap(filename
, wxBITMAP_TYPE_BMP
),
285 wxFAIL_MSG( _T("unknown cursor resource type") );
292 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
296 // Cursors by stock number
297 wxCursor::wxCursor(int idCursor
)
299 // all wxWidgets standard cursors
300 static const struct StdCursor
302 // is this a standard Windows cursor?
305 // the cursor name or id
309 { true, NULL
}, // wxCURSOR_NONE
310 { true, IDC_ARROW
}, // wxCURSOR_ARROW
311 { false, _T("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
312 { false, _T("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
313 { true, IDC_ARROW
}, // WXCURSOR_CHAR
315 // Displays as an I-beam on XP, so use a cursor file
316 // { true, IDC_CROSS }, // WXCURSOR_CROSS
317 { false, _T("WXCURSOR_CROSS") }, // WXCURSOR_CROSS
319 // See special handling below for wxCURSOR_HAND
320 // { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND
321 { true, IDC_HAND
}, // wxCURSOR_HAND
323 { true, IDC_IBEAM
}, // WXCURSOR_IBEAM
324 { true, IDC_ARROW
}, // WXCURSOR_LEFT_BUTTON
325 { false, _T("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
326 { true, IDC_ARROW
}, // WXCURSOR_MIDDLE_BUTTON
327 { true, IDC_NO
}, // WXCURSOR_NO_ENTRY
328 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH
329 { false, _T("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL
330 { false, _T("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT
331 { false, _T("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT
332 { true, IDC_HELP
}, // WXCURSOR_QUESTION_ARROW
333 { true, IDC_ARROW
}, // WXCURSOR_RIGHT_BUTTON
334 { true, IDC_SIZENESW
}, // WXCURSOR_SIZENESW
335 { true, IDC_SIZENS
}, // WXCURSOR_SIZENS
336 { true, IDC_SIZENWSE
}, // WXCURSOR_SIZENWSE
337 { true, IDC_SIZEWE
}, // WXCURSOR_SIZEWE
338 { true, IDC_SIZEALL
}, // WXCURSOR_SIZING
339 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN
340 { true, IDC_WAIT
}, // WXCURSOR_WAIT
341 { true, IDC_WAIT
}, // WXCURSOR_WATCH
342 { false, _T("WXCURSOR_BLANK") }, // wxCURSOR_BLANK
343 { true, IDC_APPSTARTING
}, // wxCURSOR_ARROWWAIT
345 // no entry for wxCURSOR_MAX
348 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors
) == wxCURSOR_MAX
,
349 CursorsIdArrayMismatch
);
351 wxCHECK_RET( idCursor
> 0 && (size_t)idCursor
< WXSIZEOF(stdCursors
),
352 _T("invalid cursor id in wxCursor() ctor") );
354 const StdCursor
& stdCursor
= stdCursors
[idCursor
];
355 bool deleteLater
= !stdCursor
.isStd
;
357 HCURSOR hcursor
= ::LoadCursor(stdCursor
.isStd
? NULL
: wxGetInstance(),
360 // IDC_HAND may not be available on some versions of Windows.
361 if ( !hcursor
&& idCursor
== wxCURSOR_HAND
)
363 hcursor
= ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND"));
369 wxLogLastError(_T("LoadCursor"));
373 m_refData
= new wxCursorRefData(hcursor
, deleteLater
);
377 #endif // __WXMICROWIN__/!__WXMICROWIN__
379 wxCursor::~wxCursor()
383 // ----------------------------------------------------------------------------
384 // other wxCursor functions
385 // ----------------------------------------------------------------------------
387 bool wxCursor::operator==(const wxCursor
& cursor
) const
390 return !cursor
.m_refData
;
392 return cursor
.m_refData
&&
393 ((wxCursorRefData
*)m_refData
)->m_hCursor
==
394 ((wxCursorRefData
*)cursor
.m_refData
)->m_hCursor
;
397 wxGDIImageRefData
*wxCursor::CreateData() const
399 return new wxCursorRefData
;
402 // ----------------------------------------------------------------------------
403 // Global cursor setting
404 // ----------------------------------------------------------------------------
406 const wxCursor
*wxGetGlobalCursor()
408 return gs_globalCursor
;
411 void wxSetCursor(const wxCursor
& cursor
)
415 ::SetCursor(GetHcursorOf(cursor
));
417 if ( gs_globalCursor
)
418 *gs_globalCursor
= cursor
;