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
)
57 ::DestroyCursor((HICON
) m_hCursor
);
61 wxCursor::wxCursor(void)
65 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
66 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
70 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
72 m_refData
= new wxIconRefData
;
74 M_CURSORDATA
->m_destroyCursor
= FALSE
;
75 M_CURSORDATA
->m_hCursor
= 0;
76 M_CURSORDATA
->m_ok
= FALSE
;
77 if (flags
& wxBITMAP_TYPE_CUR_RESOURCE
)
79 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), cursor_file
);
80 if (M_CURSORDATA
->m_hCursor
)
81 M_CURSORDATA
->m_ok
= TRUE
;
83 M_CURSORDATA
->m_ok
= FALSE
;
85 else if (flags
& wxBITMAP_TYPE_CUR
)
87 #if wxUSE_RESOURCE_LOADING_IN_MSW
88 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) ReadCursorFile((char *)(const char *)cursor_file
, wxGetInstance(), &M_CURSORDATA
->m_width
, &M_CURSORDATA
->m_height
);
89 M_CURSORDATA
->m_destroyCursor
= TRUE
;
92 else if (flags
& wxBITMAP_TYPE_ICO
)
94 #if wxUSE_RESOURCE_LOADING_IN_MSW
95 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) IconToCursor((char *)(const char *)cursor_file
, wxGetInstance(), hotSpotX
, hotSpotY
, &M_CURSORDATA
->m_width
, &M_CURSORDATA
->m_height
);
96 M_CURSORDATA
->m_destroyCursor
= TRUE
;
99 else if (flags
& wxBITMAP_TYPE_BMP
)
101 #if wxUSE_RESOURCE_LOADING_IN_MSW
103 HPALETTE hPalette
= 0;
104 bool success
= ReadDIB((char *)(const char *)cursor_file
, &hBitmap
, &hPalette
) != 0;
108 DeleteObject(hPalette
);
112 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) MakeCursorFromBitmap(wxGetInstance(), hBitmap
, &pnt
);
113 M_CURSORDATA
->m_destroyCursor
= TRUE
;
114 DeleteObject(hBitmap
);
115 if (M_CURSORDATA
->m_hCursor
)
116 M_CURSORDATA
->m_ok
= TRUE
;
121 // Cursors by stock number
122 wxCursor::wxCursor(int cursor_type
)
124 m_refData
= new wxIconRefData
;
129 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_WAIT
);
132 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_IBEAM
);
135 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_CROSS
);
137 case wxCURSOR_SIZENWSE
:
138 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENWSE
);
140 case wxCURSOR_SIZENESW
:
141 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENESW
);
143 case wxCURSOR_SIZEWE
:
144 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZEWE
);
146 case wxCURSOR_SIZENS
:
147 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENS
);
151 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
156 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_HAND");
159 case wxCURSOR_BULLSEYE
:
161 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_BULLSEYE");
164 case wxCURSOR_PENCIL
:
166 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PENCIL");
169 case wxCURSOR_MAGNIFIER
:
171 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_MAGNIFIER");
174 case wxCURSOR_NO_ENTRY
:
176 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_NO_ENTRY");
179 case wxCURSOR_LEFT_BUTTON
:
181 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
184 case wxCURSOR_RIGHT_BUTTON
:
186 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
189 case wxCURSOR_MIDDLE_BUTTON
:
191 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
194 case wxCURSOR_SIZING
:
196 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_SIZING");
201 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_WATCH");
204 case wxCURSOR_SPRAYCAN
:
206 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_ROLLER");
209 case wxCURSOR_PAINT_BRUSH
:
211 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PBRUSH");
214 case wxCURSOR_POINT_LEFT
:
216 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PLEFT");
219 case wxCURSOR_POINT_RIGHT
:
221 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PRIGHT");
224 case wxCURSOR_QUESTION_ARROW
:
226 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_QARROW");
231 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_BLANK");
236 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
241 wxCursor::~wxCursor(void)
243 // FreeResource(TRUE);
246 bool wxCursor::FreeResource(bool WXUNUSED(force
))
248 if (M_CURSORDATA
&& M_CURSORDATA
->m_hCursor
&& M_CURSORDATA
->m_destroyCursor
)
250 DestroyCursor((HCURSOR
) M_CURSORDATA
->m_hCursor
);
251 M_CURSORDATA
->m_hCursor
= 0;
256 void wxCursor::SetHCURSOR(WXHCURSOR cursor
)
259 m_refData
= new wxCursorRefData
;
261 M_CURSORDATA
->m_hCursor
= cursor
;
264 // Global cursor setting
265 void wxSetCursor(const wxCursor
& cursor
)
267 extern wxCursor
*g_globalCursor
;
269 if ( cursor
.Ok() && cursor
.GetHCURSOR() )
271 ::SetCursor((HCURSOR
) cursor
.GetHCURSOR());
273 if ( g_globalCursor
)
274 (*g_globalCursor
) = cursor
;