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/module.h"
41 #include "wx/msw/private.h"
42 #include "wx/msw/dib.h"
44 #if wxUSE_RESOURCE_LOADING_IN_MSW
45 #include "wx/msw/curico.h"
46 #include "wx/msw/curicop.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxCursorBase
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // Current cursor, in order to hang on to cursor handle when setting the cursor
61 static wxCursor
*gs_globalCursor
= NULL
;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 class wxCursorModule
: public wxModule
72 gs_globalCursor
= new wxCursor
;
79 delete gs_globalCursor
;
80 gs_globalCursor
= (wxCursor
*)NULL
;
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 wxCursorRefData::wxCursorRefData()
97 m_destroyCursor
= FALSE
;
100 void wxCursorRefData::Free()
104 if ( m_destroyCursor
)
105 ::DestroyCursor((HCURSOR
)m_hCursor
);
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
119 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
121 int WXUNUSED(height
),
122 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
123 const char WXUNUSED(maskBits
)[])
127 wxCursor::wxCursor(const wxString
& cursor_file
,
129 int hotSpotX
, int hotSpotY
)
131 wxCursorRefData
*refData
= new wxCursorRefData
;
134 refData
->m_destroyCursor
= FALSE
;
136 if (flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
139 refData
->m_hCursor
= (WXHCURSOR
) LoadImage(wxGetInstance(), cursor_file
, IMAGE_CURSOR
, 0, 0, 0);
141 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), cursor_file
);
144 else if (flags
== wxBITMAP_TYPE_CUR
)
147 refData
->m_hCursor
= (WXHCURSOR
) LoadImage(wxGetInstance(), cursor_file
, IMAGE_CURSOR
, 0, 0, LR_LOADFROMFILE
);
149 #if wxUSE_RESOURCE_LOADING_IN_MSW
150 refData
->m_hCursor
= (WXHCURSOR
) ReadCursorFile(WXSTRINGCAST cursor_file
, wxGetInstance(), &refData
->m_width
, &refData
->m_height
);
151 refData
->m_destroyCursor
= TRUE
;
155 else if (flags
== wxBITMAP_TYPE_ICO
)
157 #if wxUSE_RESOURCE_LOADING_IN_MSW
158 refData
->m_hCursor
= (WXHCURSOR
) IconToCursor(WXSTRINGCAST cursor_file
, wxGetInstance(), hotSpotX
, hotSpotY
, &refData
->m_width
, &refData
->m_height
);
159 refData
->m_destroyCursor
= TRUE
;
162 else if (flags
== wxBITMAP_TYPE_BMP
)
164 #if wxUSE_RESOURCE_LOADING_IN_MSW
166 HPALETTE hPalette
= 0;
167 bool success
= wxReadDIB(WXSTRINGCAST cursor_file
, &hBitmap
, &hPalette
) != 0;
171 DeleteObject(hPalette
);
175 refData
->m_hCursor
= (WXHCURSOR
) MakeCursorFromBitmap(wxGetInstance(), hBitmap
, &pnt
);
176 refData
->m_destroyCursor
= TRUE
;
177 DeleteObject(hBitmap
);
181 #if WXWIN_COMPATIBILITY_2
183 #endif // WXWIN_COMPATIBILITY_2
186 // Cursors by stock number
187 wxCursor::wxCursor(int cursor_type
)
189 wxCursorRefData
*refData
= new wxCursorRefData
;
194 case wxCURSOR_ARROWWAIT
:
195 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_APPSTARTING
);
198 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_WAIT
);
201 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_IBEAM
);
204 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_CROSS
);
206 case wxCURSOR_SIZENWSE
:
207 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENWSE
);
209 case wxCURSOR_SIZENESW
:
210 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENESW
);
212 case wxCURSOR_SIZEWE
:
213 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZEWE
);
215 case wxCURSOR_SIZENS
:
216 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENS
);
220 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
225 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
228 case wxCURSOR_BULLSEYE
:
230 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
233 case wxCURSOR_PENCIL
:
235 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
238 case wxCURSOR_MAGNIFIER
:
240 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
243 case wxCURSOR_NO_ENTRY
:
245 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
248 case wxCURSOR_LEFT_BUTTON
:
250 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
253 case wxCURSOR_RIGHT_BUTTON
:
255 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
258 case wxCURSOR_MIDDLE_BUTTON
:
260 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
263 case wxCURSOR_SIZING
:
265 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
270 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
273 case wxCURSOR_SPRAYCAN
:
275 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
278 case wxCURSOR_PAINT_BRUSH
:
280 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
283 case wxCURSOR_POINT_LEFT
:
285 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
288 case wxCURSOR_POINT_RIGHT
:
290 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
293 case wxCURSOR_QUESTION_ARROW
:
295 // refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), wxT("wxCURSOR_QARROW"), IMAGE_CURSOR, 16, 16, LR_MONOCHROME);
296 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
301 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
306 refData
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
311 wxCursor::~wxCursor()
315 // ----------------------------------------------------------------------------
316 // Global cursor setting
317 // ----------------------------------------------------------------------------
319 const wxCursor
*wxGetGlobalCursor()
321 return gs_globalCursor
;
324 void wxSetCursor(const wxCursor
& cursor
)
328 ::SetCursor(GetHcursorOf(cursor
));
330 if ( gs_globalCursor
)
331 *gs_globalCursor
= cursor
;