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"
37 #include "wx/module.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/missing.h" // IDC_HAND
43 // define functions missing in MicroWin
45 static inline void DestroyCursor(HCURSOR
) { }
46 static inline void SetCursor(HCURSOR
) { }
47 #endif // __WXMICROWIN__
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class WXDLLEXPORT wxCursorRefData
: public wxGDIImageRefData
56 // the second parameter is used to tell us to delete the cursor when we're
57 // done with it (normally we shouldn't call DestroyCursor() this is why it
58 // doesn't happen by default)
59 wxCursorRefData(HCURSOR hcursor
= 0, bool takeOwnership
= false);
61 virtual ~wxCursorRefData() { Free(); }
66 // return the size of the standard cursor: notice that the system only
67 // supports the cursors of this size
68 static wxCoord
GetStandardWidth();
69 static wxCoord
GetStandardHeight();
74 // standard cursor size, computed on first use
75 static wxSize ms_sizeStd
;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // Current cursor, in order to hang on to cursor handle when setting the cursor
90 static wxCursor
*gs_globalCursor
= NULL
;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 class wxCursorModule
: public wxModule
101 gs_globalCursor
= new wxCursor
;
106 virtual void OnExit()
108 delete gs_globalCursor
;
109 gs_globalCursor
= (wxCursor
*)NULL
;
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 wxSize
wxCursorRefData::ms_sizeStd
;
123 wxCoord
wxCursorRefData::GetStandardWidth()
126 ms_sizeStd
.x
= wxSystemSettings::GetMetric(wxSYS_CURSOR_X
);
131 wxCoord
wxCursorRefData::GetStandardHeight()
134 ms_sizeStd
.y
= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
);
139 wxCursorRefData::wxCursorRefData(HCURSOR hcursor
, bool destroy
)
141 m_hCursor
= (WXHCURSOR
)hcursor
;
145 m_width
= GetStandardWidth();
146 m_height
= GetStandardHeight();
149 m_destroyCursor
= destroy
;
152 void wxCursorRefData::Free()
157 if ( m_destroyCursor
)
158 ::DestroyCursor((HCURSOR
)m_hCursor
);
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
174 wxCursor::wxCursor(const wxImage
& image
)
176 // image has to be of the standard cursor size, otherwise we won't be able
178 const int w
= wxCursorRefData::GetStandardWidth();
179 const int h
= wxCursorRefData::GetStandardHeight();
181 int hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
182 int hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
183 int image_w
= image
.GetWidth();
184 int image_h
= image
.GetHeight();
186 wxASSERT_MSG( hotSpotX
>= 0 && hotSpotX
< image_w
&&
187 hotSpotY
>= 0 && hotSpotY
< image_h
,
188 _T("invalid cursor hot spot coordinates") );
190 wxImage
imageSized(image
); // final image of correct size
192 // if image is too small then place it in the center, resize it if too big
193 if ((w
> image_w
) && (h
> image_h
))
195 wxPoint
offset((w
- image_w
)/2, (h
- image_h
)/2);
196 hotSpotX
= hotSpotX
+ offset
.x
;
197 hotSpotY
= hotSpotY
+ offset
.y
;
199 imageSized
= image
.Size(wxSize(w
, h
), offset
);
201 else if ((w
!= image_w
) || (h
!= image_h
))
203 hotSpotX
= int(hotSpotX
* double(w
) / double(image_w
));
204 hotSpotY
= int(hotSpotY
* double(h
) / double(image_h
));
206 imageSized
= image
.Scale(w
, h
);
209 HCURSOR hcursor
= wxBitmapToHCURSOR( wxBitmap(imageSized
),
210 hotSpotX
, hotSpotY
);
214 wxLogWarning(_("Failed to create cursor."));
218 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
220 #endif // wxUSE_IMAGE
222 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
224 int WXUNUSED(height
),
225 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
226 const char WXUNUSED(maskBits
)[])
230 // MicroWin doesn't have support needed for the other ctors
231 #ifdef __WXMICROWIN__
233 wxCursor::wxCursor(const wxString
& WXUNUSED(filename
),
235 int WXUNUSED(hotSpotX
),
236 int WXUNUSED(hotSpotY
))
240 wxCursor::wxCursor(int WXUNUSED(cursor_type
))
244 #else // !__WXMICROWIN__
246 wxCursor::wxCursor(const wxString
& filename
,
254 case wxBITMAP_TYPE_CUR_RESOURCE
:
255 hcursor
= ::LoadCursor(wxGetInstance(), filename
.fn_str());
259 case wxBITMAP_TYPE_CUR
:
260 hcursor
= ::LoadCursorFromFile(filename
.fn_str());
264 case wxBITMAP_TYPE_ICO
:
265 hcursor
= wxBitmapToHCURSOR
267 wxIcon(filename
, wxBITMAP_TYPE_ICO
),
273 case wxBITMAP_TYPE_BMP
:
274 hcursor
= wxBitmapToHCURSOR
276 wxBitmap(filename
, wxBITMAP_TYPE_BMP
),
283 wxFAIL_MSG( _T("unknown cursor resource type") );
290 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
294 // Cursors by stock number
295 wxCursor::wxCursor(int idCursor
)
297 // all wxWidgets standard cursors
298 static const struct StdCursor
300 // is this a standard Windows cursor?
303 // the cursor name or id
307 { true, NULL
}, // wxCURSOR_NONE
308 { true, IDC_ARROW
}, // wxCURSOR_ARROW
309 { false, _T("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
310 { false, _T("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
311 { true, IDC_ARROW
}, // WXCURSOR_CHAR
313 // Displays as an I-beam on XP, so use a cursor file
314 // { true, IDC_CROSS }, // WXCURSOR_CROSS
315 { false, _T("WXCURSOR_CROSS") }, // WXCURSOR_CROSS
317 // See special handling below for wxCURSOR_HAND
318 // { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND
319 { true, IDC_HAND
}, // wxCURSOR_HAND
321 { true, IDC_IBEAM
}, // WXCURSOR_IBEAM
322 { true, IDC_ARROW
}, // WXCURSOR_LEFT_BUTTON
323 { false, _T("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
324 { true, IDC_ARROW
}, // WXCURSOR_MIDDLE_BUTTON
325 { true, IDC_NO
}, // WXCURSOR_NO_ENTRY
326 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH
327 { false, _T("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL
328 { false, _T("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT
329 { false, _T("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT
330 { true, IDC_HELP
}, // WXCURSOR_QUESTION_ARROW
331 { true, IDC_ARROW
}, // WXCURSOR_RIGHT_BUTTON
332 { true, IDC_SIZENESW
}, // WXCURSOR_SIZENESW
333 { true, IDC_SIZENS
}, // WXCURSOR_SIZENS
334 { true, IDC_SIZENWSE
}, // WXCURSOR_SIZENWSE
335 { true, IDC_SIZEWE
}, // WXCURSOR_SIZEWE
336 { true, IDC_SIZEALL
}, // WXCURSOR_SIZING
337 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN
338 { true, IDC_WAIT
}, // WXCURSOR_WAIT
339 { true, IDC_WAIT
}, // WXCURSOR_WATCH
340 { false, _T("WXCURSOR_BLANK") }, // wxCURSOR_BLANK
341 { true, IDC_APPSTARTING
}, // wxCURSOR_ARROWWAIT
343 // no entry for wxCURSOR_MAX
346 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors
) == wxCURSOR_MAX
,
347 CursorsIdArrayMismatch
);
349 wxCHECK_RET( idCursor
> 0 && (size_t)idCursor
< WXSIZEOF(stdCursors
),
350 _T("invalid cursor id in wxCursor() ctor") );
352 const StdCursor
& stdCursor
= stdCursors
[idCursor
];
353 bool deleteLater
= !stdCursor
.isStd
;
355 HCURSOR hcursor
= ::LoadCursor(stdCursor
.isStd
? NULL
: wxGetInstance(),
358 // IDC_HAND may not be available on some versions of Windows.
359 if ( !hcursor
&& idCursor
== wxCURSOR_HAND
)
361 hcursor
= ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND"));
367 wxLogLastError(_T("LoadCursor"));
371 m_refData
= new wxCursorRefData(hcursor
, deleteLater
);
375 #endif // __WXMICROWIN__/!__WXMICROWIN__
377 wxCursor::~wxCursor()
381 // ----------------------------------------------------------------------------
382 // other wxCursor functions
383 // ----------------------------------------------------------------------------
385 wxGDIImageRefData
*wxCursor::CreateData() const
387 return new wxCursorRefData
;
390 // ----------------------------------------------------------------------------
391 // Global cursor setting
392 // ----------------------------------------------------------------------------
394 const wxCursor
*wxGetGlobalCursor()
396 return gs_globalCursor
;
399 void wxSetCursor(const wxCursor
& cursor
)
403 ::SetCursor(GetHcursorOf(cursor
));
405 if ( gs_globalCursor
)
406 *gs_globalCursor
= cursor
;