1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCursor class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "cursor.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/bitmap.h"
37 #include "wx/cursor.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/dib.h"
43 #if wxUSE_RESOURCE_LOADING_IN_MSW
44 #include "wx/msw/curico.h"
45 #include "wx/msw/curicop.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxCursorBase
)
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // Current cursor, in order to hang on to cursor handle when setting the cursor
60 static wxCursor
*gs_globalCursor
= NULL
;
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class wxCursorModule
: public wxModule
71 gs_globalCursor
= new wxCursor
;
78 delete gs_globalCursor
;
79 gs_globalCursor
= (wxCursor
*)NULL
;
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 wxCursorRefData::wxCursorRefData()
96 m_destroyCursor
= FALSE
;
99 void wxCursorRefData::Free()
103 if ( m_destroyCursor
)
104 ::DestroyCursor((HCURSOR
)m_hCursor
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
118 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
120 int WXUNUSED(height
),
121 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
122 const char WXUNUSED(maskBits
)[])
126 wxCursor::wxCursor(const wxString
& cursor_file
,
128 int hotSpotX
, int hotSpotY
)
130 wxCursorRefData
*refData
= new wxCursorRefData
;
133 refData
->m_destroyCursor
= FALSE
;
135 if (flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
138 refData
->m_hCursor
= (WXHCURSOR
) LoadImage(wxGetInstance(), cursor_file
, IMAGE_CURSOR
, 0, 0, 0);
140 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), cursor_file
);
143 else if (flags
== wxBITMAP_TYPE_CUR
)
146 refData
->m_hCursor
= (WXHCURSOR
) LoadImage(wxGetInstance(), cursor_file
, IMAGE_CURSOR
, 0, 0, LR_LOADFROMFILE
);
148 #if wxUSE_RESOURCE_LOADING_IN_MSW
149 refData
->m_hCursor
= (WXHCURSOR
) ReadCursorFile(WXSTRINGCAST cursor_file
, wxGetInstance(), &refData
->m_width
, &refData
->m_height
);
150 refData
->m_destroyCursor
= TRUE
;
154 else if (flags
== wxBITMAP_TYPE_ICO
)
156 #if wxUSE_RESOURCE_LOADING_IN_MSW
157 refData
->m_hCursor
= (WXHCURSOR
) IconToCursor(WXSTRINGCAST cursor_file
, wxGetInstance(), hotSpotX
, hotSpotY
, &refData
->m_width
, &refData
->m_height
);
158 refData
->m_destroyCursor
= TRUE
;
161 else if (flags
== wxBITMAP_TYPE_BMP
)
163 #if wxUSE_RESOURCE_LOADING_IN_MSW
165 HPALETTE hPalette
= 0;
166 bool success
= wxReadDIB(WXSTRINGCAST cursor_file
, &hBitmap
, &hPalette
) != 0;
170 DeleteObject(hPalette
);
174 refData
->m_hCursor
= (WXHCURSOR
) MakeCursorFromBitmap(wxGetInstance(), hBitmap
, &pnt
);
175 refData
->m_destroyCursor
= TRUE
;
176 DeleteObject(hBitmap
);
180 #if WXWIN_COMPATIBILITY_2
182 #endif // WXWIN_COMPATIBILITY_2
185 // Cursors by stock number
186 wxCursor::wxCursor(int cursor_type
)
188 wxCursorRefData
*refData
= new wxCursorRefData
;
194 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_WAIT
);
197 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_IBEAM
);
200 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_CROSS
);
202 case wxCURSOR_SIZENWSE
:
203 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENWSE
);
205 case wxCURSOR_SIZENESW
:
206 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENESW
);
208 case wxCURSOR_SIZEWE
:
209 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZEWE
);
211 case wxCURSOR_SIZENS
:
212 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENS
);
216 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
221 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
224 case wxCURSOR_BULLSEYE
:
226 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
229 case wxCURSOR_PENCIL
:
231 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
234 case wxCURSOR_MAGNIFIER
:
236 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
239 case wxCURSOR_NO_ENTRY
:
241 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
244 case wxCURSOR_LEFT_BUTTON
:
246 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
249 case wxCURSOR_RIGHT_BUTTON
:
251 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
254 case wxCURSOR_MIDDLE_BUTTON
:
256 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
259 case wxCURSOR_SIZING
:
261 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
266 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
269 case wxCURSOR_SPRAYCAN
:
271 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
274 case wxCURSOR_PAINT_BRUSH
:
276 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
279 case wxCURSOR_POINT_LEFT
:
281 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
284 case wxCURSOR_POINT_RIGHT
:
286 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
289 case wxCURSOR_QUESTION_ARROW
:
291 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
296 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
301 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
306 wxCursor::~wxCursor()
310 // ----------------------------------------------------------------------------
311 // Global cursor setting
312 // ----------------------------------------------------------------------------
314 const wxCursor
*wxGetGlobalCursor()
316 return gs_globalCursor
;
319 void wxSetCursor(const wxCursor
& cursor
)
323 ::SetCursor(GetHcursorOf(cursor
));
325 if ( gs_globalCursor
)
326 *gs_globalCursor
= cursor
;