]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/cursor.cpp
don't crash in wxGetWindowXXX() if HWND is 0
[wxWidgets.git] / src / msw / cursor.cpp
... / ...
CommitLineData
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/module.h"
41#include "wx/msw/private.h"
42#ifndef __WXMICROWIN__
43#include "wx/msw/dib.h"
44#endif
45
46#if wxUSE_RESOURCE_LOADING_IN_MSW
47 #include "wx/msw/curico.h"
48 #include "wx/msw/curicop.h"
49#endif
50
51// ----------------------------------------------------------------------------
52// wxWin macros
53// ----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxCursorBase)
56
57// ----------------------------------------------------------------------------
58// globals
59// ----------------------------------------------------------------------------
60
61// Current cursor, in order to hang on to cursor handle when setting the cursor
62// globally
63static wxCursor *gs_globalCursor = NULL;
64
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
68
69class wxCursorModule : public wxModule
70{
71public:
72 virtual bool OnInit()
73 {
74 gs_globalCursor = new wxCursor;
75
76 return TRUE;
77 }
78
79 virtual void OnExit()
80 {
81 delete gs_globalCursor;
82 gs_globalCursor = (wxCursor *)NULL;
83 }
84};
85
86// ============================================================================
87// implementation
88// ============================================================================
89
90// ----------------------------------------------------------------------------
91// wxCursorRefData
92// ----------------------------------------------------------------------------
93
94wxCursorRefData::wxCursorRefData()
95{
96 m_width = 32;
97 m_height = 32;
98
99 m_destroyCursor = FALSE;
100}
101
102void wxCursorRefData::Free()
103{
104 if ( m_hCursor )
105 {
106#ifndef __WXMICROWIN__
107 if ( m_destroyCursor )
108 ::DestroyCursor((HCURSOR)m_hCursor);
109#endif
110
111 m_hCursor = 0;
112 }
113}
114
115// ----------------------------------------------------------------------------
116// Cursors
117// ----------------------------------------------------------------------------
118
119wxCursor::wxCursor()
120{
121}
122
123wxCursor::wxCursor(const char WXUNUSED(bits)[],
124 int WXUNUSED(width),
125 int WXUNUSED(height),
126 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
127 const char WXUNUSED(maskBits)[])
128{
129}
130
131wxCursor::wxCursor(const wxString& cursor_file,
132 long flags,
133 int hotSpotX, int hotSpotY)
134{
135#ifdef __WXMICROWIN__
136 m_refData = NULL;
137#else
138 wxCursorRefData *refData = new wxCursorRefData;
139 m_refData = refData;
140
141 if (flags == wxBITMAP_TYPE_CUR_RESOURCE)
142 {
143#ifdef __WIN95__
144 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, 0);
145#else
146 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), cursor_file);
147#endif
148 refData->m_destroyCursor = FALSE;
149 }
150 else if (flags == wxBITMAP_TYPE_CUR)
151 {
152#ifdef __WIN95__
153 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
154#else
155#if wxUSE_RESOURCE_LOADING_IN_MSW
156 refData->m_hCursor = (WXHCURSOR) ReadCursorFile(WXSTRINGCAST cursor_file, wxGetInstance(), &refData->m_width, &refData->m_height);
157#endif
158#endif
159 }
160 else if (flags == wxBITMAP_TYPE_ICO)
161 {
162#if wxUSE_RESOURCE_LOADING_IN_MSW
163 refData->m_hCursor = (WXHCURSOR) IconToCursor(WXSTRINGCAST cursor_file, wxGetInstance(), hotSpotX, hotSpotY, &refData->m_width, &refData->m_height);
164#endif
165 }
166 else if (flags == wxBITMAP_TYPE_BMP)
167 {
168#if wxUSE_RESOURCE_LOADING_IN_MSW
169 HBITMAP hBitmap = 0;
170 HPALETTE hPalette = 0;
171 bool success = wxReadDIB(WXSTRINGCAST cursor_file, &hBitmap, &hPalette) != 0;
172 if (!success)
173 return;
174 if (hPalette)
175 DeleteObject(hPalette);
176 POINT pnt;
177 pnt.x = hotSpotX;
178 pnt.y = hotSpotY;
179 refData->m_hCursor = (WXHCURSOR) MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pnt);
180 DeleteObject(hBitmap);
181#endif
182 }
183
184#if WXWIN_COMPATIBILITY_2
185 refData->SetOk();
186#endif // WXWIN_COMPATIBILITY_2
187
188#endif
189}
190
191// Cursors by stock number
192wxCursor::wxCursor(int cursor_type)
193{
194#ifdef __WXMICROWIN__
195 m_refData = NULL;
196#else
197 wxCursorRefData *refData = new wxCursorRefData;
198 m_refData = refData;
199
200 switch (cursor_type)
201 {
202 case wxCURSOR_ARROWWAIT:
203#ifndef __WIN16__
204 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_APPSTARTING);
205 break;
206#endif
207 case wxCURSOR_WAIT:
208 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_WAIT);
209 break;
210 case wxCURSOR_IBEAM:
211 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_IBEAM);
212 break;
213 case wxCURSOR_CROSS:
214 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_CROSS);
215 break;
216 case wxCURSOR_SIZENWSE:
217 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENWSE);
218 break;
219 case wxCURSOR_SIZENESW:
220 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENESW);
221 break;
222 case wxCURSOR_SIZEWE:
223 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZEWE);
224 break;
225 case wxCURSOR_SIZENS:
226 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENS);
227 break;
228 case wxCURSOR_CHAR:
229 {
230 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
231 break;
232 }
233 case wxCURSOR_HAND:
234 {
235 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
236 break;
237 }
238 case wxCURSOR_BULLSEYE:
239 {
240 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
241 break;
242 }
243 case wxCURSOR_PENCIL:
244 {
245 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
246 break;
247 }
248 case wxCURSOR_MAGNIFIER:
249 {
250 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
251 break;
252 }
253 case wxCURSOR_NO_ENTRY:
254 {
255 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
256 break;
257 }
258 case wxCURSOR_LEFT_BUTTON:
259 {
260 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
261 break;
262 }
263 case wxCURSOR_RIGHT_BUTTON:
264 {
265 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
266 break;
267 }
268 case wxCURSOR_MIDDLE_BUTTON:
269 {
270 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
271 break;
272 }
273 case wxCURSOR_SIZING:
274 {
275 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
276 break;
277 }
278 case wxCURSOR_WATCH:
279 {
280 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
281 break;
282 }
283 case wxCURSOR_SPRAYCAN:
284 {
285 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
286 break;
287 }
288 case wxCURSOR_PAINT_BRUSH:
289 {
290 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
291 break;
292 }
293 case wxCURSOR_POINT_LEFT:
294 {
295 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
296 break;
297 }
298 case wxCURSOR_POINT_RIGHT:
299 {
300 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
301 break;
302 }
303 case wxCURSOR_QUESTION_ARROW:
304 {
305// refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), wxT("wxCURSOR_QARROW"), IMAGE_CURSOR, 16, 16, LR_MONOCHROME);
306 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
307 break;
308 }
309 case wxCURSOR_BLANK:
310 {
311 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
312 break;
313 }
314 default:
315 case wxCURSOR_ARROW:
316 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
317 break;
318 }
319#endif
320}
321
322wxCursor::~wxCursor()
323{
324}
325
326// ----------------------------------------------------------------------------
327// Global cursor setting
328// ----------------------------------------------------------------------------
329
330const wxCursor *wxGetGlobalCursor()
331{
332 return gs_globalCursor;
333}
334
335void wxSetCursor(const wxCursor& cursor)
336{
337 if ( cursor.Ok() )
338 {
339#ifndef __WXMICROWIN__
340 ::SetCursor(GetHcursorOf(cursor));
341#endif
342
343 if ( gs_globalCursor )
344 *gs_globalCursor = cursor;
345 }
346}
347
348