1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCursor class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "cursor.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/cursor.h"
33 #include "wx/msw/private.h"
34 #include "wx/msw/dib.h"
38 #if wxUSE_RESOURCE_LOADING_IN_MSW
39 #include "wx/msw/curico.h"
40 #include "wx/msw/curicop.h"
43 #if !USE_SHARED_LIBRARIES
44 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxBitmap
)
47 wxCursorRefData::wxCursorRefData(void)
49 m_width
= 32; m_height
= 32;
51 m_destroyCursor
= FALSE
;
54 wxCursorRefData::~wxCursorRefData(void)
56 if ( m_hCursor
&& m_destroyCursor
)
58 ::DestroyCursor((HCURSOR
) m_hCursor
);
60 ::DestroyCursor((HICON
) m_hCursor
);
65 wxCursor::wxCursor(void)
69 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
70 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
74 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
76 m_refData
= new wxCursorRefData
;
78 M_CURSORDATA
->m_destroyCursor
= FALSE
;
79 M_CURSORDATA
->m_hCursor
= 0;
80 M_CURSORDATA
->m_ok
= FALSE
;
81 if (flags
& wxBITMAP_TYPE_CUR_RESOURCE
)
84 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadImage(wxGetInstance(), cursor_file
, IMAGE_CURSOR
, 0, 0, 0);
86 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), cursor_file
);
88 if (M_CURSORDATA
->m_hCursor
)
89 M_CURSORDATA
->m_ok
= TRUE
;
91 M_CURSORDATA
->m_ok
= FALSE
;
93 else if (flags
& wxBITMAP_TYPE_CUR
)
96 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadImage(wxGetInstance(), cursor_file
, IMAGE_CURSOR
, 0, 0, LR_LOADFROMFILE
);
98 #if wxUSE_RESOURCE_LOADING_IN_MSW
99 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) ReadCursorFile(WXSTRINGCAST cursor_file
, wxGetInstance(), &M_CURSORDATA
->m_width
, &M_CURSORDATA
->m_height
);
100 M_CURSORDATA
->m_destroyCursor
= TRUE
;
104 else if (flags
& wxBITMAP_TYPE_ICO
)
106 #if wxUSE_RESOURCE_LOADING_IN_MSW
107 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) IconToCursor(WXSTRINGCAST cursor_file
, wxGetInstance(), hotSpotX
, hotSpotY
, &M_CURSORDATA
->m_width
, &M_CURSORDATA
->m_height
);
108 M_CURSORDATA
->m_destroyCursor
= TRUE
;
111 else if (flags
& wxBITMAP_TYPE_BMP
)
113 #if wxUSE_RESOURCE_LOADING_IN_MSW
115 HPALETTE hPalette
= 0;
116 bool success
= wxReadDIB(WXSTRINGCAST cursor_file
, &hBitmap
, &hPalette
) != 0;
120 DeleteObject(hPalette
);
124 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) MakeCursorFromBitmap(wxGetInstance(), hBitmap
, &pnt
);
125 M_CURSORDATA
->m_destroyCursor
= TRUE
;
126 DeleteObject(hBitmap
);
127 if (M_CURSORDATA
->m_hCursor
)
128 M_CURSORDATA
->m_ok
= TRUE
;
133 // Cursors by stock number
134 wxCursor::wxCursor(int cursor_type
)
136 m_refData
= new wxCursorRefData
;
141 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_WAIT
);
144 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_IBEAM
);
147 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_CROSS
);
149 case wxCURSOR_SIZENWSE
:
150 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENWSE
);
152 case wxCURSOR_SIZENESW
:
153 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENESW
);
155 case wxCURSOR_SIZEWE
:
156 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZEWE
);
158 case wxCURSOR_SIZENS
:
159 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENS
);
163 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
168 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_HAND"));
171 case wxCURSOR_BULLSEYE
:
173 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_BULLSEYE"));
176 case wxCURSOR_PENCIL
:
178 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_PENCIL"));
181 case wxCURSOR_MAGNIFIER
:
183 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_MAGNIFIER"));
186 case wxCURSOR_NO_ENTRY
:
188 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_NO_ENTRY"));
191 case wxCURSOR_LEFT_BUTTON
:
193 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
196 case wxCURSOR_RIGHT_BUTTON
:
198 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
201 case wxCURSOR_MIDDLE_BUTTON
:
203 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
206 case wxCURSOR_SIZING
:
208 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_SIZING"));
213 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_WATCH"));
216 case wxCURSOR_SPRAYCAN
:
218 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_ROLLER"));
221 case wxCURSOR_PAINT_BRUSH
:
223 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_PBRUSH"));
226 case wxCURSOR_POINT_LEFT
:
228 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_PLEFT("));
231 case wxCURSOR_POINT_RIGHT
:
233 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_PRIGHT("));
236 case wxCURSOR_QUESTION_ARROW
:
238 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_QARROW"));
243 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), T("wxCURSOR_BLANK"));
248 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
253 wxCursor::~wxCursor(void)
255 // FreeResource(TRUE);
258 bool wxCursor::FreeResource(bool WXUNUSED(force
))
260 if (M_CURSORDATA
&& M_CURSORDATA
->m_hCursor
&& M_CURSORDATA
->m_destroyCursor
)
262 DestroyCursor((HCURSOR
) M_CURSORDATA
->m_hCursor
);
263 M_CURSORDATA
->m_hCursor
= 0;
268 void wxCursor::SetHCURSOR(WXHCURSOR cursor
)
271 m_refData
= new wxCursorRefData
;
273 M_CURSORDATA
->m_hCursor
= cursor
;
276 // Global cursor setting
277 void wxSetCursor(const wxCursor
& cursor
)
279 extern wxCursor
*g_globalCursor
;
281 if ( cursor
.Ok() && cursor
.GetHCURSOR() )
283 ::SetCursor((HCURSOR
) cursor
.GetHCURSOR());
285 if ( g_globalCursor
)
286 (*g_globalCursor
) = cursor
;