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