]> git.saurik.com Git - wxWidgets.git/blame - src/msw/cursor.cpp
removed unneeded #include dib.h
[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
VZ
8// Copyright: (c) 1997-2003 Julian Smart and Vadim Zeitlin
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
0d0512bd
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
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"
2bda0e17
KB
38#endif
39
2b0b4c55 40#include "wx/module.h"
6cab7411 41#include "wx/image.h"
2bda0e17 42#include "wx/msw/private.h"
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
104 return TRUE;
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 {
157 if ( m_destroyCursor )
158 ::DestroyCursor((HCURSOR)m_hCursor);
159
160 m_hCursor = 0;
161 }
2bda0e17
KB
162}
163
0d0512bd 164// ----------------------------------------------------------------------------
2bda0e17 165// Cursors
0d0512bd
VZ
166// ----------------------------------------------------------------------------
167
168wxCursor::wxCursor()
2bda0e17
KB
169{
170}
171
eff4ffbf 172wxCursor::wxCursor(const wxImage& image)
bff4ec63 173{
eff4ffbf
VZ
174 // image has to be of the standard cursor size, otherwise we won't be able
175 // to create it
176 const int w = wxCursorRefData::GetStandardWidth();
177 const int h = wxCursorRefData::GetStandardHeight();
178
7fd328a3
VZ
179 const int hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
180 const int hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
eff4ffbf 181
7fd328a3
VZ
182 wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < w &&
183 hotSpotY >= 0 && hotSpotY < h,
184 _T("invalid cursor hot spot coordinates") );
eff4ffbf 185
7fd328a3
VZ
186 HCURSOR hcursor = wxBitmapToHCURSOR
187 (
188 wxBitmap(image.Scale(w, h)),
189 hotSpotX,
190 hotSpotY
191 );
eff4ffbf
VZ
192 if ( !hcursor )
193 {
7fd328a3
VZ
194 wxLogWarning(_("Failed to create cursor."));
195 return;
eff4ffbf 196 }
7fd328a3
VZ
197
198 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
bff4ec63
VZ
199}
200
0d0512bd
VZ
201wxCursor::wxCursor(const char WXUNUSED(bits)[],
202 int WXUNUSED(width),
203 int WXUNUSED(height),
204 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
205 const char WXUNUSED(maskBits)[])
2bda0e17
KB
206{
207}
208
eff4ffbf 209// MicroWin doesn't have support needed for the other ctors
04ef50df 210#ifdef __WXMICROWIN__
2bda0e17 211
eff4ffbf
VZ
212wxCursor::wxCursor(const wxString& WXUNUSED(filename),
213 long WXUNUSED(kind),
214 int WXUNUSED(hotSpotX),
215 int WXUNUSED(hotSpotY))
216{
217}
218
219wxCursor::wxCursor(int WXUNUSED(cursor_type))
220{
221}
222
223#else // !__WXMICROWIN__
224
225wxCursor::wxCursor(const wxString& filename,
226 long kind,
227 int hotSpotX,
228 int hotSpotY)
229{
230 HCURSOR hcursor;
231 switch ( kind )
0d0512bd 232 {
eff4ffbf
VZ
233 case wxBITMAP_TYPE_CUR_RESOURCE:
234 hcursor = ::LoadCursor(wxGetInstance(), filename);
235 break;
236
237 case wxBITMAP_TYPE_CUR:
238 hcursor = ::LoadCursorFromFile(filename);
239 break;
240
677a9e28 241 case wxBITMAP_TYPE_ICO:
7fd328a3
VZ
242 hcursor = wxBitmapToHCURSOR
243 (
244 wxIcon(filename, wxBITMAP_TYPE_ICO),
245 hotSpotX,
246 hotSpotY
247 );
eff4ffbf
VZ
248 break;
249
250 case wxBITMAP_TYPE_BMP:
7fd328a3
VZ
251 hcursor = wxBitmapToHCURSOR
252 (
253 wxBitmap(filename, wxBITMAP_TYPE_BMP),
254 hotSpotX,
255 hotSpotY
256 );
eff4ffbf
VZ
257 break;
258
259 default:
260 wxFAIL_MSG( _T("unknown cursor resource type") );
261
262 hcursor = NULL;
0d0512bd 263 }
eff4ffbf
VZ
264
265 if ( hcursor )
0d0512bd 266 {
eff4ffbf 267 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
0d0512bd
VZ
268
269#if WXWIN_COMPATIBILITY_2
83911a5a 270 ((wxCursorRefData *)m_refData)->SetOk();
0d0512bd 271#endif // WXWIN_COMPATIBILITY_2
eff4ffbf 272 }
2bda0e17
KB
273}
274
275// Cursors by stock number
eff4ffbf 276wxCursor::wxCursor(int idCursor)
2bda0e17 277{
eff4ffbf
VZ
278 // all wxWindows standard cursors
279 static const struct StdCursor
2bda0e17 280 {
eff4ffbf
VZ
281 // is this a standard Windows cursor?
282 bool isStd;
283
284 // the cursor name or id
285 LPCTSTR name;
286 } stdCursors[] =
2bda0e17 287 {
eff4ffbf
VZ
288 { true, NULL }, // wxCURSOR_NONE
289 { true, IDC_ARROW }, // wxCURSOR_ARROW
290 { false, _T("wxCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
291 { false, _T("wxCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
292 { true, IDC_ARROW }, // wxCURSOR_CHAR
293 { true, IDC_CROSS }, // wxCURSOR_CROSS
294 { false, _T("wxCURSOR_HAND") }, // wxCURSOR_HAND
295 { true, IDC_IBEAM }, // wxCURSOR_IBEAM
296 { true, IDC_ARROW }, // wxCURSOR_LEFT_BUTTON
297 { false, _T("wxCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
298 { true, IDC_ARROW }, // wxCURSOR_MIDDLE_BUTTON
299 { true, IDC_NO }, // wxCURSOR_NO_ENTRY
300 { false, _T("wxCURSOR_PAINT_BRUSH") }, // wxCURSOR_PAINT_BRUSH
301 { false, _T("wxCURSOR_PENCIL") }, // wxCURSOR_PENCIL
302 { false, _T("wxCURSOR_POINT_LEFT") }, // wxCURSOR_POINT_LEFT
303 { false, _T("wxCURSOR_POINT_RIGHT") }, // wxCURSOR_POINT_RIGHT
304 { true, IDC_HELP }, // wxCURSOR_QUESTION_ARROW
305 { true, IDC_ARROW }, // wxCURSOR_RIGHT_BUTTON
306 { true, IDC_SIZENESW }, // wxCURSOR_SIZENESW
307 { true, IDC_SIZENS }, // wxCURSOR_SIZENS
308 { true, IDC_SIZENWSE }, // wxCURSOR_SIZENWSE
309 { true, IDC_SIZEWE }, // wxCURSOR_SIZEWE
310 { true, IDC_SIZEALL }, // wxCURSOR_SIZING
311 { false, _T("wxCURSOR_SPRAYCAN") }, // wxCURSOR_SPRAYCAN
312 { true, IDC_WAIT }, // wxCURSOR_WAIT
313 { true, IDC_WAIT }, // wxCURSOR_WATCH
314 { false, _T("wxCURSOR_BLANK") }, // wxCURSOR_BLANK
315 { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT
316
317 // no entry for wxCURSOR_MAX
318 };
319
320 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX,
321 CursorsIdArrayMismatch );
322
323 wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors),
324 _T("invalid cursor id in wxCursor() ctor") );
325
326 const StdCursor& stdCursor = stdCursors[idCursor];
327
328 HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(),
329 stdCursor.name);
330
331 if ( !hcursor )
2bda0e17 332 {
eff4ffbf 333 wxLogLastError(_T("LoadCursor"));
2bda0e17 334 }
eff4ffbf 335 else
15dadf31 336 {
eff4ffbf 337 m_refData = new wxCursorRefData(hcursor);
15dadf31 338 }
2bda0e17
KB
339}
340
eff4ffbf
VZ
341#endif // __WXMICROWIN__/!__WXMICROWIN__
342
0d0512bd 343wxCursor::~wxCursor()
2bda0e17 344{
2bda0e17
KB
345}
346
eff4ffbf
VZ
347// ----------------------------------------------------------------------------
348// other wxCursor functions
349// ----------------------------------------------------------------------------
350
351bool wxCursor::operator==(const wxCursor& cursor) const
352{
353 if ( !m_refData )
354 return !cursor.m_refData;
355
356 return cursor.m_refData &&
357 ((wxCursorRefData *)m_refData)->m_hCursor ==
358 ((wxCursorRefData *)cursor.m_refData)->m_hCursor;
359}
360
361wxGDIImageRefData *wxCursor::CreateData() const
362{
363 return new wxCursorRefData;
364}
365
0d0512bd 366// ----------------------------------------------------------------------------
2bda0e17 367// Global cursor setting
0d0512bd
VZ
368// ----------------------------------------------------------------------------
369
bfbd6dc1 370const wxCursor *wxGetGlobalCursor()
2bda0e17 371{
bfbd6dc1
VZ
372 return gs_globalCursor;
373}
2bda0e17 374
bfbd6dc1
VZ
375void wxSetCursor(const wxCursor& cursor)
376{
377 if ( cursor.Ok() )
6bf57206 378 {
bfbd6dc1 379 ::SetCursor(GetHcursorOf(cursor));
6bf57206 380
bfbd6dc1
VZ
381 if ( gs_globalCursor )
382 *gs_globalCursor = cursor;
6bf57206 383 }
2bda0e17
KB
384}
385
386