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