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