]> git.saurik.com Git - wxWidgets.git/blame - src/msw/cursor.cpp
don't crash when destroying a not initialized socket (patch 1489095)
[wxWidgets.git] / src / msw / cursor.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
eff4ffbf 2// Name: src/msw/cursor.cpp
2bda0e17
KB
3// Purpose: wxCursor class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
eff4ffbf 8// Copyright: (c) 1997-2003 Julian Smart and Vadim Zeitlin
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
0d0512bd
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
0d0512bd 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
c8326d64
WS
27#include "wx/cursor.h"
28
2bda0e17 29#ifndef WX_PRECOMP
0d0512bd
VZ
30 #include "wx/utils.h"
31 #include "wx/app.h"
fef15b42 32 #include "wx/bitmap.h"
0d0512bd 33 #include "wx/icon.h"
ed39ff57 34 #include "wx/settings.h"
51d5ec54 35 #include "wx/intl.h"
2bda0e17
KB
36#endif
37
2b0b4c55 38#include "wx/module.h"
6cab7411 39#include "wx/image.h"
c77c2920 40
2bda0e17 41#include "wx/msw/private.h"
c77c2920 42#include "wx/msw/missing.h" // IDC_HAND
eff4ffbf 43
eff4ffbf
VZ
44// define functions missing in MicroWin
45#ifdef __WXMICROWIN__
46 static inline void DestroyCursor(HCURSOR) { }
47 static inline void SetCursor(HCURSOR) { }
48#endif // __WXMICROWIN__
49
50// ----------------------------------------------------------------------------
51// private classes
52// ----------------------------------------------------------------------------
53
54class WXDLLEXPORT wxCursorRefData : public wxGDIImageRefData
55{
56public:
57 // the second parameter is used to tell us to delete the cursor when we're
58 // done with it (normally we shouldn't call DestroyCursor() this is why it
59 // doesn't happen by default)
60 wxCursorRefData(HCURSOR hcursor = 0, bool takeOwnership = false);
61
62 virtual ~wxCursorRefData() { Free(); }
63
64 virtual void Free();
65
66
67 // return the size of the standard cursor: notice that the system only
68 // supports the cursors of this size
69 static wxCoord GetStandardWidth();
70 static wxCoord GetStandardHeight();
71
72private:
73 bool m_destroyCursor;
74
75 // standard cursor size, computed on first use
76 static wxSize ms_sizeStd;
77};
78
0d0512bd
VZ
79// ----------------------------------------------------------------------------
80// wxWin macros
81// ----------------------------------------------------------------------------
82
621b3e21 83IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
bfbd6dc1
VZ
84
85// ----------------------------------------------------------------------------
86// globals
87// ----------------------------------------------------------------------------
88
89// Current cursor, in order to hang on to cursor handle when setting the cursor
90// globally
91static wxCursor *gs_globalCursor = NULL;
92
93// ----------------------------------------------------------------------------
94// private classes
95// ----------------------------------------------------------------------------
96
97class wxCursorModule : public wxModule
98{
99public:
100 virtual bool OnInit()
101 {
102 gs_globalCursor = new wxCursor;
103
02b7b6b0 104 return true;
bfbd6dc1
VZ
105 }
106
107 virtual void OnExit()
108 {
109 delete gs_globalCursor;
110 gs_globalCursor = (wxCursor *)NULL;
111 }
112};
113
114// ============================================================================
115// implementation
116// ============================================================================
2bda0e17 117
0d0512bd
VZ
118// ----------------------------------------------------------------------------
119// wxCursorRefData
120// ----------------------------------------------------------------------------
121
eff4ffbf
VZ
122wxSize wxCursorRefData::ms_sizeStd;
123
124wxCoord wxCursorRefData::GetStandardWidth()
125{
126 if ( !ms_sizeStd.x )
127 ms_sizeStd.x = wxSystemSettings::GetMetric(wxSYS_CURSOR_X);
128
129 return ms_sizeStd.x;
130}
131
132wxCoord wxCursorRefData::GetStandardHeight()
2bda0e17 133{
eff4ffbf
VZ
134 if ( !ms_sizeStd.y )
135 ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y);
136
137 return ms_sizeStd.y;
138}
139
140wxCursorRefData::wxCursorRefData(HCURSOR hcursor, bool destroy)
141{
142 m_hCursor = (WXHCURSOR)hcursor;
143
144 if ( m_hCursor )
145 {
146 m_width = GetStandardWidth();
147 m_height = GetStandardHeight();
148 }
0d0512bd 149
eff4ffbf 150 m_destroyCursor = destroy;
2bda0e17
KB
151}
152
0d0512bd 153void wxCursorRefData::Free()
2bda0e17 154{
032af30f
VZ
155 if ( m_hCursor )
156 {
7f0586ef 157#ifndef __WXWINCE__
032af30f
VZ
158 if ( m_destroyCursor )
159 ::DestroyCursor((HCURSOR)m_hCursor);
7f0586ef 160#endif
032af30f
VZ
161
162 m_hCursor = 0;
163 }
2bda0e17
KB
164}
165
0d0512bd 166// ----------------------------------------------------------------------------
2bda0e17 167// Cursors
0d0512bd
VZ
168// ----------------------------------------------------------------------------
169
170wxCursor::wxCursor()
2bda0e17
KB
171{
172}
173
461dae94 174#if wxUSE_IMAGE
eff4ffbf 175wxCursor::wxCursor(const wxImage& image)
bff4ec63 176{
eff4ffbf
VZ
177 // image has to be of the standard cursor size, otherwise we won't be able
178 // to create it
179 const int w = wxCursorRefData::GetStandardWidth();
180 const int h = wxCursorRefData::GetStandardHeight();
181
b737ad10
RR
182 int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
183 int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
184 int image_w = image.GetWidth();
185 int image_h = image.GetHeight();
eff4ffbf 186
b737ad10
RR
187 wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w &&
188 hotSpotY >= 0 && hotSpotY < image_h,
7fd328a3 189 _T("invalid cursor hot spot coordinates") );
eff4ffbf 190
b737ad10
RR
191 wxImage imageSized(image); // final image of correct size
192
193 // if image is too small then place it in the center, resize it if too big
194 if ((w > image_w) && (h > image_h))
195 {
196 wxPoint offset((w - image_w)/2, (h - image_h)/2);
197 hotSpotX = hotSpotX + offset.x;
198 hotSpotY = hotSpotY + offset.y;
199
200 imageSized = image.Size(wxSize(w, h), offset);
201 }
202 else if ((w != image_w) || (h != image_h))
203 {
ccbb33c9
JS
204 hotSpotX = int(hotSpotX * double(w) / double(image_w));
205 hotSpotY = int(hotSpotY * double(h) / double(image_h));
b737ad10
RR
206
207 imageSized = image.Scale(w, h);
208 }
209
ccbb33c9 210 HCURSOR hcursor = wxBitmapToHCURSOR( wxBitmap(imageSized),
b737ad10
RR
211 hotSpotX, hotSpotY );
212
64c288fa 213#if wxUSE_WXDIB
eff4ffbf
VZ
214 if ( !hcursor )
215 {
7fd328a3
VZ
216 wxLogWarning(_("Failed to create cursor."));
217 return;
eff4ffbf 218 }
64c288fa
JS
219#else
220 HCURSOR hcursor = 0;
c8326d64 221#endif
7fd328a3
VZ
222
223 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
bff4ec63 224}
461dae94 225#endif
bff4ec63 226
0d0512bd
VZ
227wxCursor::wxCursor(const char WXUNUSED(bits)[],
228 int WXUNUSED(width),
229 int WXUNUSED(height),
230 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
231 const char WXUNUSED(maskBits)[])
2bda0e17
KB
232{
233}
234
eff4ffbf 235// MicroWin doesn't have support needed for the other ctors
04ef50df 236#ifdef __WXMICROWIN__
2bda0e17 237
eff4ffbf
VZ
238wxCursor::wxCursor(const wxString& WXUNUSED(filename),
239 long WXUNUSED(kind),
240 int WXUNUSED(hotSpotX),
241 int WXUNUSED(hotSpotY))
242{
243}
244
245wxCursor::wxCursor(int WXUNUSED(cursor_type))
246{
247}
248
249#else // !__WXMICROWIN__
250
251wxCursor::wxCursor(const wxString& filename,
252 long kind,
253 int hotSpotX,
254 int hotSpotY)
255{
256 HCURSOR hcursor;
257 switch ( kind )
0d0512bd 258 {
eff4ffbf
VZ
259 case wxBITMAP_TYPE_CUR_RESOURCE:
260 hcursor = ::LoadCursor(wxGetInstance(), filename);
261 break;
262
7f0586ef 263#ifndef __WXWINCE__
eff4ffbf
VZ
264 case wxBITMAP_TYPE_CUR:
265 hcursor = ::LoadCursorFromFile(filename);
266 break;
7f0586ef 267#endif
eff4ffbf 268
677a9e28 269 case wxBITMAP_TYPE_ICO:
7fd328a3
VZ
270 hcursor = wxBitmapToHCURSOR
271 (
272 wxIcon(filename, wxBITMAP_TYPE_ICO),
273 hotSpotX,
274 hotSpotY
275 );
eff4ffbf
VZ
276 break;
277
278 case wxBITMAP_TYPE_BMP:
7fd328a3
VZ
279 hcursor = wxBitmapToHCURSOR
280 (
281 wxBitmap(filename, wxBITMAP_TYPE_BMP),
282 hotSpotX,
283 hotSpotY
284 );
eff4ffbf
VZ
285 break;
286
287 default:
288 wxFAIL_MSG( _T("unknown cursor resource type") );
289
290 hcursor = NULL;
0d0512bd 291 }
eff4ffbf
VZ
292
293 if ( hcursor )
0d0512bd 294 {
eff4ffbf 295 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
eff4ffbf 296 }
2bda0e17
KB
297}
298
7da60d7c
WS
299// Cursors by stock number
300wxCursor::wxCursor(int idCursor)
301{
1c6f2414
WS
302 // all wxWidgets standard cursors
303 static const struct StdCursor
304 {
305 // is this a standard Windows cursor?
306 bool isStd;
307
308 // the cursor name or id
309 LPCTSTR name;
310 } stdCursors[] =
311 {
312 { true, NULL }, // wxCURSOR_NONE
313 { true, IDC_ARROW }, // wxCURSOR_ARROW
314 { false, _T("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
315 { false, _T("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
316 { true, IDC_ARROW }, // WXCURSOR_CHAR
c8326d64 317
27e7e859
JS
318 // Displays as an I-beam on XP, so use a cursor file
319// { true, IDC_CROSS }, // WXCURSOR_CROSS
320 { false, _T("WXCURSOR_CROSS") }, // WXCURSOR_CROSS
fe384096
RD
321
322 // See special handling below for wxCURSOR_HAND
323// { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND
324 { true, IDC_HAND }, // wxCURSOR_HAND
c8326d64 325
1c6f2414
WS
326 { true, IDC_IBEAM }, // WXCURSOR_IBEAM
327 { true, IDC_ARROW }, // WXCURSOR_LEFT_BUTTON
328 { false, _T("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
329 { true, IDC_ARROW }, // WXCURSOR_MIDDLE_BUTTON
330 { true, IDC_NO }, // WXCURSOR_NO_ENTRY
331 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH
332 { false, _T("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL
333 { false, _T("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT
334 { false, _T("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT
335 { true, IDC_HELP }, // WXCURSOR_QUESTION_ARROW
336 { true, IDC_ARROW }, // WXCURSOR_RIGHT_BUTTON
337 { true, IDC_SIZENESW }, // WXCURSOR_SIZENESW
338 { true, IDC_SIZENS }, // WXCURSOR_SIZENS
339 { true, IDC_SIZENWSE }, // WXCURSOR_SIZENWSE
340 { true, IDC_SIZEWE }, // WXCURSOR_SIZEWE
341 { true, IDC_SIZEALL }, // WXCURSOR_SIZING
342 { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN
343 { true, IDC_WAIT }, // WXCURSOR_WAIT
97000852 344 { true, IDC_WAIT }, // WXCURSOR_WATCH
1c6f2414
WS
345 { false, _T("WXCURSOR_BLANK") }, // wxCURSOR_BLANK
346 { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT
347
348 // no entry for wxCURSOR_MAX
349 };
350
351 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX,
352 CursorsIdArrayMismatch );
353
eff4ffbf
VZ
354 wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors),
355 _T("invalid cursor id in wxCursor() ctor") );
356
357 const StdCursor& stdCursor = stdCursors[idCursor];
fe384096 358 bool deleteLater = !stdCursor.isStd;
eff4ffbf
VZ
359
360 HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(),
361 stdCursor.name);
362
fe384096
RD
363 // IDC_HAND may not be available on some versions of Windows.
364 if ( !hcursor && idCursor == wxCURSOR_HAND)
365 {
366 hcursor = ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND"));
367 deleteLater = true;
368 }
c8326d64 369
eff4ffbf 370 if ( !hcursor )
2bda0e17 371 {
eff4ffbf 372 wxLogLastError(_T("LoadCursor"));
2bda0e17 373 }
eff4ffbf 374 else
15dadf31 375 {
fe384096 376 m_refData = new wxCursorRefData(hcursor, deleteLater);
15dadf31 377 }
2bda0e17
KB
378}
379
eff4ffbf
VZ
380#endif // __WXMICROWIN__/!__WXMICROWIN__
381
0d0512bd 382wxCursor::~wxCursor()
2bda0e17 383{
2bda0e17
KB
384}
385
eff4ffbf
VZ
386// ----------------------------------------------------------------------------
387// other wxCursor functions
388// ----------------------------------------------------------------------------
389
390bool wxCursor::operator==(const wxCursor& cursor) const
391{
392 if ( !m_refData )
393 return !cursor.m_refData;
394
395 return cursor.m_refData &&
396 ((wxCursorRefData *)m_refData)->m_hCursor ==
397 ((wxCursorRefData *)cursor.m_refData)->m_hCursor;
398}
399
400wxGDIImageRefData *wxCursor::CreateData() const
401{
402 return new wxCursorRefData;
403}
404
0d0512bd 405// ----------------------------------------------------------------------------
2bda0e17 406// Global cursor setting
0d0512bd
VZ
407// ----------------------------------------------------------------------------
408
bfbd6dc1 409const wxCursor *wxGetGlobalCursor()
2bda0e17 410{
bfbd6dc1
VZ
411 return gs_globalCursor;
412}
2bda0e17 413
bfbd6dc1
VZ
414void wxSetCursor(const wxCursor& cursor)
415{
416 if ( cursor.Ok() )
6bf57206 417 {
bfbd6dc1 418 ::SetCursor(GetHcursorOf(cursor));
6bf57206 419
bfbd6dc1
VZ
420 if ( gs_globalCursor )
421 *gs_globalCursor = cursor;
6bf57206 422 }
2bda0e17
KB
423}
424
425