]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
fs_inet depends on protocol classes
[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 #include "wx/intl.h"
39 #endif
40
41 #include "wx/module.h"
42 #include "wx/image.h"
43 #include "wx/msw/private.h"
44
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
55 class WXDLLEXPORT wxCursorRefData : public wxGDIImageRefData
56 {
57 public:
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
73 private:
74 bool m_destroyCursor;
75
76 // standard cursor size, computed on first use
77 static wxSize ms_sizeStd;
78 };
79
80 // ----------------------------------------------------------------------------
81 // wxWin macros
82 // ----------------------------------------------------------------------------
83
84 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
85
86 // ----------------------------------------------------------------------------
87 // globals
88 // ----------------------------------------------------------------------------
89
90 // Current cursor, in order to hang on to cursor handle when setting the cursor
91 // globally
92 static wxCursor *gs_globalCursor = NULL;
93
94 // ----------------------------------------------------------------------------
95 // private classes
96 // ----------------------------------------------------------------------------
97
98 class wxCursorModule : public wxModule
99 {
100 public:
101 virtual bool OnInit()
102 {
103 gs_globalCursor = new wxCursor;
104
105 return TRUE;
106 }
107
108 virtual void OnExit()
109 {
110 delete gs_globalCursor;
111 gs_globalCursor = (wxCursor *)NULL;
112 }
113 };
114
115 // ============================================================================
116 // implementation
117 // ============================================================================
118
119 // ----------------------------------------------------------------------------
120 // wxCursorRefData
121 // ----------------------------------------------------------------------------
122
123 wxSize wxCursorRefData::ms_sizeStd;
124
125 wxCoord 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
133 wxCoord wxCursorRefData::GetStandardHeight()
134 {
135 if ( !ms_sizeStd.y )
136 ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y);
137
138 return ms_sizeStd.y;
139 }
140
141 wxCursorRefData::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 }
150
151 m_destroyCursor = destroy;
152 }
153
154 void wxCursorRefData::Free()
155 {
156 if ( m_hCursor )
157 {
158 if ( m_destroyCursor )
159 ::DestroyCursor((HCURSOR)m_hCursor);
160
161 m_hCursor = 0;
162 }
163 }
164
165 // ----------------------------------------------------------------------------
166 // Cursors
167 // ----------------------------------------------------------------------------
168
169 wxCursor::wxCursor()
170 {
171 }
172
173 wxCursor::wxCursor(const wxImage& image)
174 {
175 // image has to be of the standard cursor size, otherwise we won't be able
176 // to create it
177 const int w = wxCursorRefData::GetStandardWidth();
178 const int h = wxCursorRefData::GetStandardHeight();
179
180 const int hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
181 const int hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
182
183 wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < w &&
184 hotSpotY >= 0 && hotSpotY < h,
185 _T("invalid cursor hot spot coordinates") );
186
187 HCURSOR hcursor = wxBitmapToHCURSOR
188 (
189 wxBitmap(image.Scale(w, h)),
190 hotSpotX,
191 hotSpotY
192 );
193 if ( !hcursor )
194 {
195 wxLogWarning(_("Failed to create cursor."));
196 return;
197 }
198
199 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
200 }
201
202 wxCursor::wxCursor(const char WXUNUSED(bits)[],
203 int WXUNUSED(width),
204 int WXUNUSED(height),
205 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
206 const char WXUNUSED(maskBits)[])
207 {
208 }
209
210 // MicroWin doesn't have support needed for the other ctors
211 #ifdef __WXMICROWIN__
212
213 wxCursor::wxCursor(const wxString& WXUNUSED(filename),
214 long WXUNUSED(kind),
215 int WXUNUSED(hotSpotX),
216 int WXUNUSED(hotSpotY))
217 {
218 }
219
220 wxCursor::wxCursor(int WXUNUSED(cursor_type))
221 {
222 }
223
224 #else // !__WXMICROWIN__
225
226 wxCursor::wxCursor(const wxString& filename,
227 long kind,
228 int hotSpotX,
229 int hotSpotY)
230 {
231 HCURSOR hcursor;
232 switch ( kind )
233 {
234 case wxBITMAP_TYPE_CUR_RESOURCE:
235 hcursor = ::LoadCursor(wxGetInstance(), filename);
236 break;
237
238 case wxBITMAP_TYPE_CUR:
239 hcursor = ::LoadCursorFromFile(filename);
240 break;
241
242 case wxBITMAP_TYPE_ICO:
243 hcursor = wxBitmapToHCURSOR
244 (
245 wxIcon(filename, wxBITMAP_TYPE_ICO),
246 hotSpotX,
247 hotSpotY
248 );
249 break;
250
251 case wxBITMAP_TYPE_BMP:
252 hcursor = wxBitmapToHCURSOR
253 (
254 wxBitmap(filename, wxBITMAP_TYPE_BMP),
255 hotSpotX,
256 hotSpotY
257 );
258 break;
259
260 default:
261 wxFAIL_MSG( _T("unknown cursor resource type") );
262
263 hcursor = NULL;
264 }
265
266 if ( hcursor )
267 {
268 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
269
270 #if WXWIN_COMPATIBILITY_2
271 ((wxCursorRefData *)m_refData)->SetOk();
272 #endif // WXWIN_COMPATIBILITY_2
273 }
274 }
275
276 // Cursors by stock number
277 wxCursor::wxCursor(int idCursor)
278 {
279 // all wxWindows standard cursors
280 static const struct StdCursor
281 {
282 // is this a standard Windows cursor?
283 bool isStd;
284
285 // the cursor name or id
286 LPCTSTR name;
287 } stdCursors[] =
288 {
289 { true, NULL }, // wxCURSOR_NONE
290 { true, IDC_ARROW }, // wxCURSOR_ARROW
291 { false, _T("wxCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
292 { false, _T("wxCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
293 { true, IDC_ARROW }, // wxCURSOR_CHAR
294 { true, IDC_CROSS }, // wxCURSOR_CROSS
295 { false, _T("wxCURSOR_HAND") }, // wxCURSOR_HAND
296 { true, IDC_IBEAM }, // wxCURSOR_IBEAM
297 { true, IDC_ARROW }, // wxCURSOR_LEFT_BUTTON
298 { false, _T("wxCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
299 { true, IDC_ARROW }, // wxCURSOR_MIDDLE_BUTTON
300 { true, IDC_NO }, // wxCURSOR_NO_ENTRY
301 { false, _T("wxCURSOR_PAINT_BRUSH") }, // wxCURSOR_PAINT_BRUSH
302 { false, _T("wxCURSOR_PENCIL") }, // wxCURSOR_PENCIL
303 { false, _T("wxCURSOR_POINT_LEFT") }, // wxCURSOR_POINT_LEFT
304 { false, _T("wxCURSOR_POINT_RIGHT") }, // wxCURSOR_POINT_RIGHT
305 { true, IDC_HELP }, // wxCURSOR_QUESTION_ARROW
306 { true, IDC_ARROW }, // wxCURSOR_RIGHT_BUTTON
307 { true, IDC_SIZENESW }, // wxCURSOR_SIZENESW
308 { true, IDC_SIZENS }, // wxCURSOR_SIZENS
309 { true, IDC_SIZENWSE }, // wxCURSOR_SIZENWSE
310 { true, IDC_SIZEWE }, // wxCURSOR_SIZEWE
311 { true, IDC_SIZEALL }, // wxCURSOR_SIZING
312 { false, _T("wxCURSOR_SPRAYCAN") }, // wxCURSOR_SPRAYCAN
313 { true, IDC_WAIT }, // wxCURSOR_WAIT
314 { true, IDC_WAIT }, // wxCURSOR_WATCH
315 { false, _T("wxCURSOR_BLANK") }, // wxCURSOR_BLANK
316 { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT
317
318 // no entry for wxCURSOR_MAX
319 };
320
321 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX,
322 CursorsIdArrayMismatch );
323
324 wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors),
325 _T("invalid cursor id in wxCursor() ctor") );
326
327 const StdCursor& stdCursor = stdCursors[idCursor];
328
329 HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(),
330 stdCursor.name);
331
332 if ( !hcursor )
333 {
334 wxLogLastError(_T("LoadCursor"));
335 }
336 else
337 {
338 m_refData = new wxCursorRefData(hcursor);
339 }
340 }
341
342 #endif // __WXMICROWIN__/!__WXMICROWIN__
343
344 wxCursor::~wxCursor()
345 {
346 }
347
348 // ----------------------------------------------------------------------------
349 // other wxCursor functions
350 // ----------------------------------------------------------------------------
351
352 bool wxCursor::operator==(const wxCursor& cursor) const
353 {
354 if ( !m_refData )
355 return !cursor.m_refData;
356
357 return cursor.m_refData &&
358 ((wxCursorRefData *)m_refData)->m_hCursor ==
359 ((wxCursorRefData *)cursor.m_refData)->m_hCursor;
360 }
361
362 wxGDIImageRefData *wxCursor::CreateData() const
363 {
364 return new wxCursorRefData;
365 }
366
367 // ----------------------------------------------------------------------------
368 // Global cursor setting
369 // ----------------------------------------------------------------------------
370
371 const wxCursor *wxGetGlobalCursor()
372 {
373 return gs_globalCursor;
374 }
375
376 void wxSetCursor(const wxCursor& cursor)
377 {
378 if ( cursor.Ok() )
379 {
380 ::SetCursor(GetHcursorOf(cursor));
381
382 if ( gs_globalCursor )
383 *gs_globalCursor = cursor;
384 }
385 }
386
387