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"
32 #include "wx/msw/private.h"
33 #include "wx/msw/dib.h"
37 #if wxUSE_RESOURCE_LOADING_IN_MSW
38 #include "wx/msw/curico.h"
39 #include "wx/msw/curicop.h"
42 #if !USE_SHARED_LIBRARIES
43 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxBitmap
)
46 wxCursorRefData::wxCursorRefData(void)
48 m_width
= 32; m_height
= 32;
50 m_destroyCursor
= FALSE
;
53 wxCursorRefData::~wxCursorRefData(void)
55 if ( m_hCursor
&& m_destroyCursor
)
56 ::DestroyCursor((HICON
) m_hCursor
);
60 wxCursor::wxCursor(void)
64 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
65 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
69 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
71 m_refData
= new wxIconRefData
;
73 M_CURSORDATA
->m_destroyCursor
= FALSE
;
74 M_CURSORDATA
->m_hCursor
= 0;
75 M_CURSORDATA
->m_ok
= FALSE
;
76 if (flags
& wxBITMAP_TYPE_CUR_RESOURCE
)
78 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), cursor_file
);
79 if (M_CURSORDATA
->m_hCursor
)
80 M_CURSORDATA
->m_ok
= TRUE
;
82 M_CURSORDATA
->m_ok
= FALSE
;
84 else if (flags
& wxBITMAP_TYPE_CUR
)
86 #if wxUSE_RESOURCE_LOADING_IN_MSW
87 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) ReadCursorFile((char *)(const char *)cursor_file
, wxGetInstance(), &M_CURSORDATA
->m_width
, &M_CURSORDATA
->m_height
);
88 M_CURSORDATA
->m_destroyCursor
= TRUE
;
91 else if (flags
& wxBITMAP_TYPE_ICO
)
93 #if wxUSE_RESOURCE_LOADING_IN_MSW
94 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) IconToCursor((char *)(const char *)cursor_file
, wxGetInstance(), hotSpotX
, hotSpotY
, &M_CURSORDATA
->m_width
, &M_CURSORDATA
->m_height
);
95 M_CURSORDATA
->m_destroyCursor
= TRUE
;
98 else if (flags
& wxBITMAP_TYPE_BMP
)
100 #if wxUSE_RESOURCE_LOADING_IN_MSW
102 HPALETTE hPalette
= 0;
103 bool success
= ReadDIB((char *)(const char *)cursor_file
, &hBitmap
, &hPalette
) != 0;
107 DeleteObject(hPalette
);
111 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) MakeCursorFromBitmap(wxGetInstance(), hBitmap
, &pnt
);
112 M_CURSORDATA
->m_destroyCursor
= TRUE
;
113 DeleteObject(hBitmap
);
114 if (M_CURSORDATA
->m_hCursor
)
115 M_CURSORDATA
->m_ok
= TRUE
;
120 // Cursors by stock number
121 wxCursor::wxCursor(int cursor_type
)
123 m_refData
= new wxIconRefData
;
128 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_WAIT
);
131 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_IBEAM
);
134 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_CROSS
);
136 case wxCURSOR_SIZENWSE
:
137 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENWSE
);
139 case wxCURSOR_SIZENESW
:
140 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENESW
);
142 case wxCURSOR_SIZEWE
:
143 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZEWE
);
145 case wxCURSOR_SIZENS
:
146 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_SIZENS
);
150 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
155 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_HAND");
158 case wxCURSOR_BULLSEYE
:
160 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_BULLSEYE");
163 case wxCURSOR_PENCIL
:
165 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PENCIL");
168 case wxCURSOR_MAGNIFIER
:
170 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_MAGNIFIER");
173 case wxCURSOR_NO_ENTRY
:
175 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_NO_ENTRY");
178 case wxCURSOR_LEFT_BUTTON
:
180 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
183 case wxCURSOR_RIGHT_BUTTON
:
185 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
188 case wxCURSOR_MIDDLE_BUTTON
:
190 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
193 case wxCURSOR_SIZING
:
195 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_SIZING");
200 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_WATCH");
203 case wxCURSOR_SPRAYCAN
:
205 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_ROLLER");
208 case wxCURSOR_PAINT_BRUSH
:
210 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PBRUSH");
213 case wxCURSOR_POINT_LEFT
:
215 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PLEFT");
218 case wxCURSOR_POINT_RIGHT
:
220 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_PRIGHT");
223 case wxCURSOR_QUESTION_ARROW
:
225 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_QARROW");
230 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor(wxGetInstance(), "wxCURSOR_BLANK");
235 M_CURSORDATA
->m_hCursor
= (WXHCURSOR
) LoadCursor((HINSTANCE
) NULL
, IDC_ARROW
);
240 wxCursor::~wxCursor(void)
242 // FreeResource(TRUE);
245 bool wxCursor::FreeResource(bool WXUNUSED(force
))
247 if (M_CURSORDATA
&& M_CURSORDATA
->m_hCursor
&& M_CURSORDATA
->m_destroyCursor
)
249 DestroyCursor((HCURSOR
) M_CURSORDATA
->m_hCursor
);
250 M_CURSORDATA
->m_hCursor
= 0;
255 void wxCursor::SetHCURSOR(WXHCURSOR cursor
)
258 m_refData
= new wxCursorRefData
;
260 M_CURSORDATA
->m_hCursor
= cursor
;
263 // Global cursor setting
264 void wxSetCursor(const wxCursor
& cursor
)
266 extern wxCursor
*g_globalCursor
;
267 if ( g_globalCursor
)
268 (*g_globalCursor
) = cursor
;
270 if (cursor
.Ok() && cursor
.GetHCURSOR())
271 ::SetCursor((HCURSOR
) cursor
.GetHCURSOR());