]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
use wxDIB class instead of duplicating DDB -> DIB conversion code once again (aaaargh...
[wxWidgets.git] / src / msw / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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) 1997-2003 Julian Smart and Vadim Zeitlin
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/utils.h"
33 #include "wx/app.h"
34 #include "wx/bitmap.h"
35 #include "wx/icon.h"
36 #include "wx/cursor.h"
37 #include "wx/settings.h"
38 #endif
39
40 #include "wx/module.h"
41 #include "wx/image.h"
42 #include "wx/msw/private.h"
43
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
54 class WXDLLEXPORT wxCursorRefData : public wxGDIImageRefData
55 {
56 public:
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
72 private:
73 bool m_destroyCursor;
74
75 // standard cursor size, computed on first use
76 static wxSize ms_sizeStd;
77 };
78
79 // ----------------------------------------------------------------------------
80 // wxWin macros
81 // ----------------------------------------------------------------------------
82
83 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
84
85 // ----------------------------------------------------------------------------
86 // globals
87 // ----------------------------------------------------------------------------
88
89 // Current cursor, in order to hang on to cursor handle when setting the cursor
90 // globally
91 static wxCursor *gs_globalCursor = NULL;
92
93 // ----------------------------------------------------------------------------
94 // private classes
95 // ----------------------------------------------------------------------------
96
97 class wxCursorModule : public wxModule
98 {
99 public:
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 // ============================================================================
117
118 // ----------------------------------------------------------------------------
119 // wxCursorRefData
120 // ----------------------------------------------------------------------------
121
122 wxSize wxCursorRefData::ms_sizeStd;
123
124 wxCoord 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
132 wxCoord wxCursorRefData::GetStandardHeight()
133 {
134 if ( !ms_sizeStd.y )
135 ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y);
136
137 return ms_sizeStd.y;
138 }
139
140 wxCursorRefData::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 }
149
150 m_destroyCursor = destroy;
151 }
152
153 void wxCursorRefData::Free()
154 {
155 if ( m_hCursor )
156 {
157 if ( m_destroyCursor )
158 ::DestroyCursor((HCURSOR)m_hCursor);
159
160 m_hCursor = 0;
161 }
162 }
163
164 // ----------------------------------------------------------------------------
165 // Cursors
166 // ----------------------------------------------------------------------------
167
168 wxCursor::wxCursor()
169 {
170 }
171
172 wxCursor::wxCursor(const wxImage& image)
173 {
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
179 const int hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
180 const int hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
181
182 wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < w &&
183 hotSpotY >= 0 && hotSpotY < h,
184 _T("invalid cursor hot spot coordinates") );
185
186 HCURSOR hcursor = wxBitmapToHCURSOR
187 (
188 wxBitmap(image.Scale(w, h)),
189 hotSpotX,
190 hotSpotY
191 );
192 if ( !hcursor )
193 {
194 wxLogWarning(_("Failed to create cursor."));
195 return;
196 }
197
198 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
199 }
200
201 wxCursor::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)[])
206 {
207 }
208
209 // MicroWin doesn't have support needed for the other ctors
210 #ifdef __WXMICROWIN__
211
212 wxCursor::wxCursor(const wxString& WXUNUSED(filename),
213 long WXUNUSED(kind),
214 int WXUNUSED(hotSpotX),
215 int WXUNUSED(hotSpotY))
216 {
217 }
218
219 wxCursor::wxCursor(int WXUNUSED(cursor_type))
220 {
221 }
222
223 #else // !__WXMICROWIN__
224
225 wxCursor::wxCursor(const wxString& filename,
226 long kind,
227 int hotSpotX,
228 int hotSpotY)
229 {
230 HCURSOR hcursor;
231 switch ( kind )
232 {
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
241 case wxBITMAP_TYPE_ICO:
242 hcursor = wxBitmapToHCURSOR
243 (
244 wxIcon(filename, wxBITMAP_TYPE_ICO),
245 hotSpotX,
246 hotSpotY
247 );
248 break;
249
250 case wxBITMAP_TYPE_BMP:
251 hcursor = wxBitmapToHCURSOR
252 (
253 wxBitmap(filename, wxBITMAP_TYPE_BMP),
254 hotSpotX,
255 hotSpotY
256 );
257 break;
258
259 default:
260 wxFAIL_MSG( _T("unknown cursor resource type") );
261
262 hcursor = NULL;
263 }
264
265 if ( hcursor )
266 {
267 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
268
269 #if WXWIN_COMPATIBILITY_2
270 ((wxCursorRefData *)m_refData)->SetOk();
271 #endif // WXWIN_COMPATIBILITY_2
272 }
273 }
274
275 // Cursors by stock number
276 wxCursor::wxCursor(int idCursor)
277 {
278 // all wxWindows standard cursors
279 static const struct StdCursor
280 {
281 // is this a standard Windows cursor?
282 bool isStd;
283
284 // the cursor name or id
285 LPCTSTR name;
286 } stdCursors[] =
287 {
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 )
332 {
333 wxLogLastError(_T("LoadCursor"));
334 }
335 else
336 {
337 m_refData = new wxCursorRefData(hcursor);
338 }
339 }
340
341 #endif // __WXMICROWIN__/!__WXMICROWIN__
342
343 wxCursor::~wxCursor()
344 {
345 }
346
347 // ----------------------------------------------------------------------------
348 // other wxCursor functions
349 // ----------------------------------------------------------------------------
350
351 bool 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
361 wxGDIImageRefData *wxCursor::CreateData() const
362 {
363 return new wxCursorRefData;
364 }
365
366 // ----------------------------------------------------------------------------
367 // Global cursor setting
368 // ----------------------------------------------------------------------------
369
370 const wxCursor *wxGetGlobalCursor()
371 {
372 return gs_globalCursor;
373 }
374
375 void wxSetCursor(const wxCursor& cursor)
376 {
377 if ( cursor.Ok() )
378 {
379 ::SetCursor(GetHcursorOf(cursor));
380
381 if ( gs_globalCursor )
382 *gs_globalCursor = cursor;
383 }
384 }
385
386