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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/cursor.h"
32 #include "wx/bitmap.h"
34 #include "wx/settings.h"
39 #include "wx/module.h"
41 #include "wx/msw/private.h"
42 #include "wx/msw/missing.h" // IDC_HAND
44 // define functions missing in MicroWin
46 static inline void DestroyCursor(HCURSOR
) { }
47 static inline void SetCursor(HCURSOR
) { }
48 #endif // __WXMICROWIN__
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class WXDLLEXPORT wxCursorRefData
: public wxGDIImageRefData
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);
62 virtual ~wxCursorRefData() { Free(); }
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();
75 // standard cursor size, computed on first use
76 static wxSize ms_sizeStd
;
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // Current cursor, in order to hang on to cursor handle when setting the cursor
91 static wxCursor
*gs_globalCursor
= NULL
;
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 class wxCursorModule
: public wxModule
100 virtual bool OnInit()
102 gs_globalCursor
= new wxCursor
;
107 virtual void OnExit()
109 delete gs_globalCursor
;
110 gs_globalCursor
= (wxCursor
*)NULL
;
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 wxSize
wxCursorRefData::ms_sizeStd
;
124 wxCoord
wxCursorRefData::GetStandardWidth()
127 ms_sizeStd
.x
= wxSystemSettings::GetMetric(wxSYS_CURSOR_X
);
132 wxCoord
wxCursorRefData::GetStandardHeight()
135 ms_sizeStd
.y
= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
);
140 wxCursorRefData::wxCursorRefData(HCURSOR hcursor
, bool destroy
)
142 m_hCursor
= (WXHCURSOR
)hcursor
;
146 m_width
= GetStandardWidth();
147 m_height
= GetStandardHeight();
150 m_destroyCursor
= destroy
;
153 void wxCursorRefData::Free()
158 if ( m_destroyCursor
)
159 ::DestroyCursor((HCURSOR
)m_hCursor
);
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
175 wxCursor::wxCursor(const wxImage
& image
)
177 // image has to be of the standard cursor size, otherwise we won't be able
179 const int w
= wxCursorRefData::GetStandardWidth();
180 const int h
= wxCursorRefData::GetStandardHeight();
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();
187 wxASSERT_MSG( hotSpotX
>= 0 && hotSpotX
< image_w
&&
188 hotSpotY
>= 0 && hotSpotY
< image_h
,
189 _T("invalid cursor hot spot coordinates") );
191 wxImage
imageSized(image
); // final image of correct size
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
))
196 wxPoint
offset((w
- image_w
)/2, (h
- image_h
)/2);
197 hotSpotX
= hotSpotX
+ offset
.x
;
198 hotSpotY
= hotSpotY
+ offset
.y
;
200 imageSized
= image
.Size(wxSize(w
, h
), offset
);
202 else if ((w
!= image_w
) || (h
!= image_h
))
204 hotSpotX
= int(hotSpotX
* double(w
) / double(image_w
));
205 hotSpotY
= int(hotSpotY
* double(h
) / double(image_h
));
207 imageSized
= image
.Scale(w
, h
);
210 HCURSOR hcursor
= wxBitmapToHCURSOR( wxBitmap(imageSized
),
211 hotSpotX
, hotSpotY
);
216 wxLogWarning(_("Failed to create cursor."));
223 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
227 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
229 int WXUNUSED(height
),
230 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
231 const char WXUNUSED(maskBits
)[])
235 // MicroWin doesn't have support needed for the other ctors
236 #ifdef __WXMICROWIN__
238 wxCursor::wxCursor(const wxString
& WXUNUSED(filename
),
240 int WXUNUSED(hotSpotX
),
241 int WXUNUSED(hotSpotY
))
245 wxCursor::wxCursor(int WXUNUSED(cursor_type
))
249 #else // !__WXMICROWIN__
251 wxCursor::wxCursor(const wxString
& filename
,
259 case wxBITMAP_TYPE_CUR_RESOURCE
:
260 hcursor
= ::LoadCursor(wxGetInstance(), filename
);
264 case wxBITMAP_TYPE_CUR
:
265 hcursor
= ::LoadCursorFromFile(filename
);
269 case wxBITMAP_TYPE_ICO
:
270 hcursor
= wxBitmapToHCURSOR
272 wxIcon(filename
, wxBITMAP_TYPE_ICO
),
278 case wxBITMAP_TYPE_BMP
:
279 hcursor
= wxBitmapToHCURSOR
281 wxBitmap(filename
, wxBITMAP_TYPE_BMP
),
288 wxFAIL_MSG( _T("unknown cursor resource type") );
295 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
299 // Cursors by stock number
300 wxCursor::wxCursor(int idCursor
)
302 // all wxWidgets standard cursors
303 static const struct StdCursor
305 // is this a standard Windows cursor?
308 // the cursor name or id
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
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
322 // See special handling below for wxCURSOR_HAND
323 // { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND
324 { true, IDC_HAND
}, // wxCURSOR_HAND
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
348 // no entry for wxCURSOR_MAX
351 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors
) == wxCURSOR_MAX
,
352 CursorsIdArrayMismatch
);
354 wxCHECK_RET( idCursor
> 0 && (size_t)idCursor
< WXSIZEOF(stdCursors
),
355 _T("invalid cursor id in wxCursor() ctor") );
357 const StdCursor
& stdCursor
= stdCursors
[idCursor
];
358 bool deleteLater
= !stdCursor
.isStd
;
360 HCURSOR hcursor
= ::LoadCursor(stdCursor
.isStd
? NULL
: wxGetInstance(),
363 // IDC_HAND may not be available on some versions of Windows.
364 if ( !hcursor
&& idCursor
== wxCURSOR_HAND
)
366 hcursor
= ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND"));
372 wxLogLastError(_T("LoadCursor"));
376 m_refData
= new wxCursorRefData(hcursor
, deleteLater
);
380 #endif // __WXMICROWIN__/!__WXMICROWIN__
382 wxCursor::~wxCursor()
386 // ----------------------------------------------------------------------------
387 // other wxCursor functions
388 // ----------------------------------------------------------------------------
390 bool wxCursor::operator==(const wxCursor
& cursor
) const
393 return !cursor
.m_refData
;
395 return cursor
.m_refData
&&
396 ((wxCursorRefData
*)m_refData
)->m_hCursor
==
397 ((wxCursorRefData
*)cursor
.m_refData
)->m_hCursor
;
400 wxGDIImageRefData
*wxCursor::CreateData() const
402 return new wxCursorRefData
;
405 // ----------------------------------------------------------------------------
406 // Global cursor setting
407 // ----------------------------------------------------------------------------
409 const wxCursor
*wxGetGlobalCursor()
411 return gs_globalCursor
;
414 void wxSetCursor(const wxCursor
& cursor
)
418 ::SetCursor(GetHcursorOf(cursor
));
420 if ( gs_globalCursor
)
421 *gs_globalCursor
= cursor
;