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"
44 #include "wx/msw/private.h"
45 #include "wx/msw/missing.h" // IDC_HAND
47 // define functions missing in MicroWin
49 static inline void DestroyCursor(HCURSOR
) { }
50 static inline void SetCursor(HCURSOR
) { }
51 #endif // __WXMICROWIN__
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxCursorRefData
: public wxGDIImageRefData
60 // the second parameter is used to tell us to delete the cursor when we're
61 // done with it (normally we shouldn't call DestroyCursor() this is why it
62 // doesn't happen by default)
63 wxCursorRefData(HCURSOR hcursor
= 0, bool takeOwnership
= false);
65 virtual ~wxCursorRefData() { Free(); }
70 // return the size of the standard cursor: notice that the system only
71 // supports the cursors of this size
72 static wxCoord
GetStandardWidth();
73 static wxCoord
GetStandardHeight();
78 // standard cursor size, computed on first use
79 static wxSize ms_sizeStd
;
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // Current cursor, in order to hang on to cursor handle when setting the cursor
94 static wxCursor
*gs_globalCursor
= NULL
;
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 class wxCursorModule
: public wxModule
103 virtual bool OnInit()
105 gs_globalCursor
= new wxCursor
;
110 virtual void OnExit()
112 delete gs_globalCursor
;
113 gs_globalCursor
= (wxCursor
*)NULL
;
117 // ============================================================================
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxSize
wxCursorRefData::ms_sizeStd
;
127 wxCoord
wxCursorRefData::GetStandardWidth()
130 ms_sizeStd
.x
= wxSystemSettings::GetMetric(wxSYS_CURSOR_X
);
135 wxCoord
wxCursorRefData::GetStandardHeight()
138 ms_sizeStd
.y
= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
);
143 wxCursorRefData::wxCursorRefData(HCURSOR hcursor
, bool destroy
)
145 m_hCursor
= (WXHCURSOR
)hcursor
;
149 m_width
= GetStandardWidth();
150 m_height
= GetStandardHeight();
153 m_destroyCursor
= destroy
;
156 void wxCursorRefData::Free()
161 if ( m_destroyCursor
)
162 ::DestroyCursor((HCURSOR
)m_hCursor
);
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
178 wxCursor::wxCursor(const wxImage
& image
)
180 // image has to be of the standard cursor size, otherwise we won't be able
182 const int w
= wxCursorRefData::GetStandardWidth();
183 const int h
= wxCursorRefData::GetStandardHeight();
185 int hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
186 int hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
187 int image_w
= image
.GetWidth();
188 int image_h
= image
.GetHeight();
190 wxASSERT_MSG( hotSpotX
>= 0 && hotSpotX
< image_w
&&
191 hotSpotY
>= 0 && hotSpotY
< image_h
,
192 _T("invalid cursor hot spot coordinates") );
194 wxImage
imageSized(image
); // final image of correct size
196 // if image is too small then place it in the center, resize it if too big
197 if ((w
> image_w
) && (h
> image_h
))
199 wxPoint
offset((w
- image_w
)/2, (h
- image_h
)/2);
200 hotSpotX
= hotSpotX
+ offset
.x
;
201 hotSpotY
= hotSpotY
+ offset
.y
;
203 imageSized
= image
.Size(wxSize(w
, h
), offset
);
205 else if ((w
!= image_w
) || (h
!= image_h
))
207 hotSpotX
= int(hotSpotX
* double(w
) / double(image_w
));
208 hotSpotY
= int(hotSpotY
* double(h
) / double(image_h
));
210 imageSized
= image
.Scale(w
, h
);
213 HCURSOR hcursor
= wxBitmapToHCURSOR( wxBitmap(imageSized
),
214 hotSpotX
, hotSpotY
);
218 wxLogWarning(_("Failed to create cursor."));
222 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
226 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
228 int WXUNUSED(height
),
229 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
230 const char WXUNUSED(maskBits
)[])
234 // MicroWin doesn't have support needed for the other ctors
235 #ifdef __WXMICROWIN__
237 wxCursor::wxCursor(const wxString
& WXUNUSED(filename
),
239 int WXUNUSED(hotSpotX
),
240 int WXUNUSED(hotSpotY
))
244 wxCursor::wxCursor(int WXUNUSED(cursor_type
))
248 #else // !__WXMICROWIN__
250 wxCursor::wxCursor(const wxString
& filename
,
258 case wxBITMAP_TYPE_CUR_RESOURCE
:
259 hcursor
= ::LoadCursor(wxGetInstance(), filename
);
263 case wxBITMAP_TYPE_CUR
:
264 hcursor
= ::LoadCursorFromFile(filename
);
268 case wxBITMAP_TYPE_ICO
:
269 hcursor
= wxBitmapToHCURSOR
271 wxIcon(filename
, wxBITMAP_TYPE_ICO
),
277 case wxBITMAP_TYPE_BMP
:
278 hcursor
= wxBitmapToHCURSOR
280 wxBitmap(filename
, wxBITMAP_TYPE_BMP
),
287 wxFAIL_MSG( _T("unknown cursor resource type") );
294 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
298 // Cursors by stock number
299 wxCursor::wxCursor(int idCursor
)
301 // all wxWidgets standard cursors
302 static const struct StdCursor
304 // is this a standard Windows cursor?
307 // the cursor name or id
311 { true, NULL
}, // wxCURSOR_NONE
312 { true, IDC_ARROW
}, // wxCURSOR_ARROW
313 { false, _T("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
314 { false, _T("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
315 { true, IDC_ARROW
}, // WXCURSOR_CHAR
317 // Displays as an I-beam on XP, so use a cursor file
318 // { true, IDC_CROSS }, // WXCURSOR_CROSS
319 { false, _T("WXCURSOR_CROSS") }, // WXCURSOR_CROSS
321 // See special handling below for wxCURSOR_HAND
322 // { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND
323 { true, IDC_HAND
}, // wxCURSOR_HAND
325 { true, IDC_IBEAM
}, // WXCURSOR_IBEAM
326 { true, IDC_ARROW
}, // WXCURSOR_LEFT_BUTTON
327 { false, _T("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
328 { true, IDC_ARROW
}, // WXCURSOR_MIDDLE_BUTTON
329 { true, IDC_NO
}, // WXCURSOR_NO_ENTRY
330 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH
331 { false, _T("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL
332 { false, _T("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT
333 { false, _T("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT
334 { true, IDC_HELP
}, // WXCURSOR_QUESTION_ARROW
335 { true, IDC_ARROW
}, // WXCURSOR_RIGHT_BUTTON
336 { true, IDC_SIZENESW
}, // WXCURSOR_SIZENESW
337 { true, IDC_SIZENS
}, // WXCURSOR_SIZENS
338 { true, IDC_SIZENWSE
}, // WXCURSOR_SIZENWSE
339 { true, IDC_SIZEWE
}, // WXCURSOR_SIZEWE
340 { true, IDC_SIZEALL
}, // WXCURSOR_SIZING
341 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN
342 { true, IDC_WAIT
}, // WXCURSOR_WAIT
343 { true, IDC_WAIT
}, // WXCURSOR_WATCH
344 { false, _T("WXCURSOR_BLANK") }, // wxCURSOR_BLANK
345 { true, IDC_APPSTARTING
}, // wxCURSOR_ARROWWAIT
347 // no entry for wxCURSOR_MAX
350 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors
) == wxCURSOR_MAX
,
351 CursorsIdArrayMismatch
);
353 wxCHECK_RET( idCursor
> 0 && (size_t)idCursor
< WXSIZEOF(stdCursors
),
354 _T("invalid cursor id in wxCursor() ctor") );
356 const StdCursor
& stdCursor
= stdCursors
[idCursor
];
357 bool deleteLater
= !stdCursor
.isStd
;
359 HCURSOR hcursor
= ::LoadCursor(stdCursor
.isStd
? NULL
: wxGetInstance(),
362 // IDC_HAND may not be available on some versions of Windows.
363 if ( !hcursor
&& idCursor
== wxCURSOR_HAND
)
365 hcursor
= ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND"));
371 wxLogLastError(_T("LoadCursor"));
375 m_refData
= new wxCursorRefData(hcursor
, deleteLater
);
379 #endif // __WXMICROWIN__/!__WXMICROWIN__
381 wxCursor::~wxCursor()
385 // ----------------------------------------------------------------------------
386 // other wxCursor functions
387 // ----------------------------------------------------------------------------
389 bool wxCursor::operator==(const wxCursor
& cursor
) const
392 return !cursor
.m_refData
;
394 return cursor
.m_refData
&&
395 ((wxCursorRefData
*)m_refData
)->m_hCursor
==
396 ((wxCursorRefData
*)cursor
.m_refData
)->m_hCursor
;
399 wxGDIImageRefData
*wxCursor::CreateData() const
401 return new wxCursorRefData
;
404 // ----------------------------------------------------------------------------
405 // Global cursor setting
406 // ----------------------------------------------------------------------------
408 const wxCursor
*wxGetGlobalCursor()
410 return gs_globalCursor
;
413 void wxSetCursor(const wxCursor
& cursor
)
417 ::SetCursor(GetHcursorOf(cursor
));
419 if ( gs_globalCursor
)
420 *gs_globalCursor
= cursor
;