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