]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
Added #include "wx/bitmap.h"
[wxWidgets.git] / src / msw / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "cursor.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/list.h"
33 #include "wx/utils.h"
34 #include "wx/app.h"
35 #include "wx/bitmap.h"
36 #include "wx/icon.h"
37 #include "wx/cursor.h"
38 #endif
39
40 #include "wx/msw/private.h"
41 #include "wx/msw/dib.h"
42
43 #if wxUSE_RESOURCE_LOADING_IN_MSW
44 #include "wx/msw/curico.h"
45 #include "wx/msw/curicop.h"
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // wxWin macros
50 // ----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxCursorBase)
53
54 // ----------------------------------------------------------------------------
55 // globals
56 // ----------------------------------------------------------------------------
57
58 // Current cursor, in order to hang on to cursor handle when setting the cursor
59 // globally
60 static wxCursor *gs_globalCursor = NULL;
61
62 // ----------------------------------------------------------------------------
63 // private classes
64 // ----------------------------------------------------------------------------
65
66 class wxCursorModule : public wxModule
67 {
68 public:
69 virtual bool OnInit()
70 {
71 gs_globalCursor = new wxCursor;
72
73 return TRUE;
74 }
75
76 virtual void OnExit()
77 {
78 delete gs_globalCursor;
79 gs_globalCursor = (wxCursor *)NULL;
80 }
81 };
82
83 // ============================================================================
84 // implementation
85 // ============================================================================
86
87 // ----------------------------------------------------------------------------
88 // wxCursorRefData
89 // ----------------------------------------------------------------------------
90
91 wxCursorRefData::wxCursorRefData()
92 {
93 m_width = 32;
94 m_height = 32;
95
96 m_destroyCursor = FALSE;
97 }
98
99 void wxCursorRefData::Free()
100 {
101 if ( m_hCursor )
102 {
103 if ( m_destroyCursor )
104 ::DestroyCursor((HCURSOR)m_hCursor);
105
106 m_hCursor = 0;
107 }
108 }
109
110 // ----------------------------------------------------------------------------
111 // Cursors
112 // ----------------------------------------------------------------------------
113
114 wxCursor::wxCursor()
115 {
116 }
117
118 wxCursor::wxCursor(const char WXUNUSED(bits)[],
119 int WXUNUSED(width),
120 int WXUNUSED(height),
121 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
122 const char WXUNUSED(maskBits)[])
123 {
124 }
125
126 wxCursor::wxCursor(const wxString& cursor_file,
127 long flags,
128 int hotSpotX, int hotSpotY)
129 {
130 wxCursorRefData *refData = new wxCursorRefData;
131 m_refData = refData;
132
133 refData->m_destroyCursor = FALSE;
134
135 if (flags == wxBITMAP_TYPE_CUR_RESOURCE)
136 {
137 #ifdef __WIN95__
138 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, 0);
139 #else
140 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), cursor_file);
141 #endif
142 }
143 else if (flags == wxBITMAP_TYPE_CUR)
144 {
145 #ifdef __WIN95__
146 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
147 #else
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;
151 #endif
152 #endif
153 }
154 else if (flags == wxBITMAP_TYPE_ICO)
155 {
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;
159 #endif
160 }
161 else if (flags == wxBITMAP_TYPE_BMP)
162 {
163 #if wxUSE_RESOURCE_LOADING_IN_MSW
164 HBITMAP hBitmap = 0;
165 HPALETTE hPalette = 0;
166 bool success = wxReadDIB(WXSTRINGCAST cursor_file, &hBitmap, &hPalette) != 0;
167 if (!success)
168 return;
169 if (hPalette)
170 DeleteObject(hPalette);
171 POINT pnt;
172 pnt.x = hotSpotX;
173 pnt.y = hotSpotY;
174 refData->m_hCursor = (WXHCURSOR) MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pnt);
175 refData->m_destroyCursor = TRUE;
176 DeleteObject(hBitmap);
177 #endif
178 }
179
180 #if WXWIN_COMPATIBILITY_2
181 refData->SetOk();
182 #endif // WXWIN_COMPATIBILITY_2
183 }
184
185 // Cursors by stock number
186 wxCursor::wxCursor(int cursor_type)
187 {
188 wxCursorRefData *refData = new wxCursorRefData;
189 m_refData = refData;
190
191 switch (cursor_type)
192 {
193 case wxCURSOR_WAIT:
194 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_WAIT);
195 break;
196 case wxCURSOR_IBEAM:
197 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_IBEAM);
198 break;
199 case wxCURSOR_CROSS:
200 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_CROSS);
201 break;
202 case wxCURSOR_SIZENWSE:
203 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENWSE);
204 break;
205 case wxCURSOR_SIZENESW:
206 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENESW);
207 break;
208 case wxCURSOR_SIZEWE:
209 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZEWE);
210 break;
211 case wxCURSOR_SIZENS:
212 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENS);
213 break;
214 case wxCURSOR_CHAR:
215 {
216 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
217 break;
218 }
219 case wxCURSOR_HAND:
220 {
221 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
222 break;
223 }
224 case wxCURSOR_BULLSEYE:
225 {
226 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
227 break;
228 }
229 case wxCURSOR_PENCIL:
230 {
231 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
232 break;
233 }
234 case wxCURSOR_MAGNIFIER:
235 {
236 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
237 break;
238 }
239 case wxCURSOR_NO_ENTRY:
240 {
241 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
242 break;
243 }
244 case wxCURSOR_LEFT_BUTTON:
245 {
246 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
247 break;
248 }
249 case wxCURSOR_RIGHT_BUTTON:
250 {
251 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
252 break;
253 }
254 case wxCURSOR_MIDDLE_BUTTON:
255 {
256 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
257 break;
258 }
259 case wxCURSOR_SIZING:
260 {
261 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
262 break;
263 }
264 case wxCURSOR_WATCH:
265 {
266 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
267 break;
268 }
269 case wxCURSOR_SPRAYCAN:
270 {
271 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
272 break;
273 }
274 case wxCURSOR_PAINT_BRUSH:
275 {
276 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
277 break;
278 }
279 case wxCURSOR_POINT_LEFT:
280 {
281 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
282 break;
283 }
284 case wxCURSOR_POINT_RIGHT:
285 {
286 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
287 break;
288 }
289 case wxCURSOR_QUESTION_ARROW:
290 {
291 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
292 break;
293 }
294 case wxCURSOR_BLANK:
295 {
296 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
297 break;
298 }
299 default:
300 case wxCURSOR_ARROW:
301 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
302 break;
303 }
304 }
305
306 wxCursor::~wxCursor()
307 {
308 }
309
310 // ----------------------------------------------------------------------------
311 // Global cursor setting
312 // ----------------------------------------------------------------------------
313
314 const wxCursor *wxGetGlobalCursor()
315 {
316 return gs_globalCursor;
317 }
318
319 void wxSetCursor(const wxCursor& cursor)
320 {
321 if ( cursor.Ok() )
322 {
323 ::SetCursor(GetHcursorOf(cursor));
324
325 if ( gs_globalCursor )
326 *gs_globalCursor = cursor;
327 }
328 }
329
330