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 wxDELETE(gs_globalCursor
);
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 wxSize
wxCursorRefData::ms_sizeStd
;
122 wxCoord
wxCursorRefData::GetStandardWidth()
125 ms_sizeStd
.x
= wxSystemSettings::GetMetric(wxSYS_CURSOR_X
);
130 wxCoord
wxCursorRefData::GetStandardHeight()
133 ms_sizeStd
.y
= wxSystemSettings::GetMetric(wxSYS_CURSOR_Y
);
138 wxCursorRefData::wxCursorRefData(HCURSOR hcursor
, bool destroy
)
140 m_hCursor
= (WXHCURSOR
)hcursor
;
144 m_width
= GetStandardWidth();
145 m_height
= GetStandardHeight();
148 m_destroyCursor
= destroy
;
151 void wxCursorRefData::Free()
156 if ( m_destroyCursor
)
157 ::DestroyCursor((HCURSOR
)m_hCursor
);
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
173 wxCursor::wxCursor(const wxImage
& image
)
175 // image has to be of the standard cursor size, otherwise we won't be able
177 const int w
= wxCursorRefData::GetStandardWidth();
178 const int h
= wxCursorRefData::GetStandardHeight();
180 int hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
181 int hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
182 int image_w
= image
.GetWidth();
183 int image_h
= image
.GetHeight();
185 wxASSERT_MSG( hotSpotX
>= 0 && hotSpotX
< image_w
&&
186 hotSpotY
>= 0 && hotSpotY
< image_h
,
187 wxT("invalid cursor hot spot coordinates") );
189 wxImage
imageSized(image
); // final image of correct size
191 // if image is too small then place it in the center, resize it if too big
192 if ((w
> image_w
) && (h
> image_h
))
194 wxPoint
offset((w
- image_w
)/2, (h
- image_h
)/2);
195 hotSpotX
= hotSpotX
+ offset
.x
;
196 hotSpotY
= hotSpotY
+ offset
.y
;
198 imageSized
= image
.Size(wxSize(w
, h
), offset
);
200 else if ((w
!= image_w
) || (h
!= image_h
))
202 hotSpotX
= int(hotSpotX
* double(w
) / double(image_w
));
203 hotSpotY
= int(hotSpotY
* double(h
) / double(image_h
));
205 imageSized
= image
.Scale(w
, h
);
208 HCURSOR hcursor
= wxBitmapToHCURSOR( wxBitmap(imageSized
),
209 hotSpotX
, hotSpotY
);
213 wxLogWarning(_("Failed to create cursor."));
217 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
219 #endif // wxUSE_IMAGE
221 // MicroWin doesn't have support needed for the other ctors
222 #ifdef __WXMICROWIN__
224 wxCursor::InitFromStock(wxStockCursor
WXUNUSED(cursor_type
))
228 #else // !__WXMICROWIN__
230 wxCursor::wxCursor(const wxString
& filename
,
238 case wxBITMAP_TYPE_CUR_RESOURCE
:
239 hcursor
= ::LoadCursor(wxGetInstance(), filename
.t_str());
243 case wxBITMAP_TYPE_ANI
:
244 case wxBITMAP_TYPE_CUR
:
245 hcursor
= ::LoadCursorFromFile(filename
.t_str());
249 case wxBITMAP_TYPE_ICO
:
250 hcursor
= wxBitmapToHCURSOR
252 wxIcon(filename
, wxBITMAP_TYPE_ICO
),
258 case wxBITMAP_TYPE_BMP
:
259 hcursor
= wxBitmapToHCURSOR
261 wxBitmap(filename
, wxBITMAP_TYPE_BMP
),
268 wxLogError( wxT("unknown cursor resource type '%d'"), kind
);
275 m_refData
= new wxCursorRefData(hcursor
, true /* delete it later */);
282 void ReverseBitmap(HBITMAP bitmap
, int width
, int height
)
285 SelectInHDC
selBitmap(hdc
, bitmap
);
286 ::StretchBlt(hdc
, width
- 1, 0, -width
, height
,
287 hdc
, 0, 0, width
, height
, SRCCOPY
);
290 HCURSOR
CreateReverseCursor(HCURSOR cursor
)
293 if ( !::GetIconInfo(cursor
, &info
) )
296 HCURSOR cursorRev
= NULL
;
299 if ( ::GetObject(info
.hbmMask
, sizeof(bmp
), &bmp
) )
301 ReverseBitmap(info
.hbmMask
, bmp
.bmWidth
, bmp
.bmHeight
);
303 ReverseBitmap(info
.hbmColor
, bmp
.bmWidth
, bmp
.bmHeight
);
304 info
.xHotspot
= (DWORD
)bmp
.bmWidth
- 1 - info
.xHotspot
;
306 cursorRev
= ::CreateIconIndirect(&info
);
309 ::DeleteObject(info
.hbmMask
);
311 ::DeleteObject(info
.hbmColor
);
316 } // anonymous namespace
318 // Cursors by stock number
319 void wxCursor::InitFromStock(wxStockCursor idCursor
)
321 // all wxWidgets standard cursors
322 static const struct StdCursor
324 // is this a standard Windows cursor?
327 // the cursor name or id
331 { true, NULL
}, // wxCURSOR_NONE
332 { true, IDC_ARROW
}, // wxCURSOR_ARROW
333 { false, wxT("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
334 { false, wxT("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
335 { true, IDC_ARROW
}, // WXCURSOR_CHAR
336 { true, IDC_CROSS
}, // WXCURSOR_CROSS
337 { true, IDC_HAND
}, // wxCURSOR_HAND
338 { true, IDC_IBEAM
}, // WXCURSOR_IBEAM
339 { true, IDC_ARROW
}, // WXCURSOR_LEFT_BUTTON
340 { false, wxT("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
341 { true, IDC_ARROW
}, // WXCURSOR_MIDDLE_BUTTON
342 { true, IDC_NO
}, // WXCURSOR_NO_ENTRY
343 { false, wxT("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH
344 { false, wxT("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL
345 { false, wxT("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT
346 { false, wxT("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT
347 { true, IDC_HELP
}, // WXCURSOR_QUESTION_ARROW
348 { true, IDC_ARROW
}, // WXCURSOR_RIGHT_BUTTON
349 { true, IDC_SIZENESW
}, // WXCURSOR_SIZENESW
350 { true, IDC_SIZENS
}, // WXCURSOR_SIZENS
351 { true, IDC_SIZENWSE
}, // WXCURSOR_SIZENWSE
352 { true, IDC_SIZEWE
}, // WXCURSOR_SIZEWE
353 { true, IDC_SIZEALL
}, // WXCURSOR_SIZING
354 { false, wxT("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN
355 { true, IDC_WAIT
}, // WXCURSOR_WAIT
356 { true, IDC_WAIT
}, // WXCURSOR_WATCH
357 { false, wxT("WXCURSOR_BLANK") }, // wxCURSOR_BLANK
358 { true, IDC_APPSTARTING
}, // wxCURSOR_ARROWWAIT
360 // no entry for wxCURSOR_MAX
363 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors
) == wxCURSOR_MAX
,
364 CursorsIdArrayMismatch
);
366 wxCHECK_RET( idCursor
> 0 && (size_t)idCursor
< WXSIZEOF(stdCursors
),
367 wxT("invalid cursor id in wxCursor() ctor") );
369 const StdCursor
& stdCursor
= stdCursors
[idCursor
];
370 bool deleteLater
= !stdCursor
.isStd
;
372 HCURSOR hcursor
= ::LoadCursor(stdCursor
.isStd
? NULL
: wxGetInstance(),
375 // IDC_HAND may not be available on some versions of Windows.
376 if ( !hcursor
&& idCursor
== wxCURSOR_HAND
)
378 hcursor
= ::LoadCursor(wxGetInstance(), wxT("WXCURSOR_HAND"));
382 if ( !hcursor
&& idCursor
== wxCURSOR_RIGHT_ARROW
)
384 hcursor
= ::LoadCursor(NULL
, IDC_ARROW
);
387 hcursor
= CreateReverseCursor(hcursor
);
394 if ( !stdCursor
.isStd
)
396 // it may be not obvious to the programmer why did loading fail,
397 // try to help by pointing to the by far the most probable reason
398 wxFAIL_MSG(wxT("Loading a cursor defined by wxWidgets failed, ")
399 wxT("did you include include/wx/msw/wx.rc file from ")
400 wxT("your resource file?"));
403 wxLogLastError(wxT("LoadCursor"));
407 m_refData
= new wxCursorRefData(hcursor
, deleteLater
);
411 #endif // __WXMICROWIN__/!__WXMICROWIN__
413 wxCursor::~wxCursor()
417 // ----------------------------------------------------------------------------
418 // other wxCursor functions
419 // ----------------------------------------------------------------------------
421 wxGDIImageRefData
*wxCursor::CreateData() const
423 return new wxCursorRefData
;
426 // ----------------------------------------------------------------------------
427 // Global cursor setting
428 // ----------------------------------------------------------------------------
430 const wxCursor
*wxGetGlobalCursor()
432 return gs_globalCursor
;
435 void wxSetCursor(const wxCursor
& cursor
)
439 ::SetCursor(GetHcursorOf(cursor
));
441 if ( gs_globalCursor
)
442 *gs_globalCursor
= cursor
;