]>
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 | 8 | // Copyright: (c) 1997-2003 Julian Smart and Vadim Zeitlin |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
0d0512bd VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
0d0512bd | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
c8326d64 WS |
27 | #include "wx/cursor.h" |
28 | ||
2bda0e17 | 29 | #ifndef WX_PRECOMP |
0d0512bd VZ |
30 | #include "wx/utils.h" |
31 | #include "wx/app.h" | |
fef15b42 | 32 | #include "wx/bitmap.h" |
0d0512bd | 33 | #include "wx/icon.h" |
ed39ff57 | 34 | #include "wx/settings.h" |
51d5ec54 | 35 | #include "wx/intl.h" |
155ecd4c | 36 | #include "wx/image.h" |
02761f6c | 37 | #include "wx/module.h" |
2bda0e17 KB |
38 | #endif |
39 | ||
40 | #include "wx/msw/private.h" | |
a6c2e2c7 | 41 | #include "wx/msw/missing.h" // IDC_HAND |
eff4ffbf | 42 | |
eff4ffbf VZ |
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 | ||
0d0512bd VZ |
78 | // ---------------------------------------------------------------------------- |
79 | // wxWin macros | |
80 | // ---------------------------------------------------------------------------- | |
81 | ||
621b3e21 | 82 | IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject) |
bfbd6dc1 VZ |
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 | ||
02b7b6b0 | 103 | return true; |
bfbd6dc1 VZ |
104 | } |
105 | ||
106 | virtual void OnExit() | |
107 | { | |
108 | delete gs_globalCursor; | |
109 | gs_globalCursor = (wxCursor *)NULL; | |
110 | } | |
111 | }; | |
112 | ||
113 | // ============================================================================ | |
114 | // implementation | |
115 | // ============================================================================ | |
2bda0e17 | 116 | |
0d0512bd VZ |
117 | // ---------------------------------------------------------------------------- |
118 | // wxCursorRefData | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
eff4ffbf VZ |
121 | wxSize wxCursorRefData::ms_sizeStd; |
122 | ||
123 | wxCoord wxCursorRefData::GetStandardWidth() | |
124 | { | |
125 | if ( !ms_sizeStd.x ) | |
126 | ms_sizeStd.x = wxSystemSettings::GetMetric(wxSYS_CURSOR_X); | |
127 | ||
128 | return ms_sizeStd.x; | |
129 | } | |
130 | ||
131 | wxCoord wxCursorRefData::GetStandardHeight() | |
2bda0e17 | 132 | { |
eff4ffbf VZ |
133 | if ( !ms_sizeStd.y ) |
134 | ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y); | |
135 | ||
136 | return ms_sizeStd.y; | |
137 | } | |
138 | ||
139 | wxCursorRefData::wxCursorRefData(HCURSOR hcursor, bool destroy) | |
140 | { | |
141 | m_hCursor = (WXHCURSOR)hcursor; | |
142 | ||
143 | if ( m_hCursor ) | |
144 | { | |
145 | m_width = GetStandardWidth(); | |
146 | m_height = GetStandardHeight(); | |
147 | } | |
0d0512bd | 148 | |
eff4ffbf | 149 | m_destroyCursor = destroy; |
2bda0e17 KB |
150 | } |
151 | ||
0d0512bd | 152 | void wxCursorRefData::Free() |
2bda0e17 | 153 | { |
032af30f VZ |
154 | if ( m_hCursor ) |
155 | { | |
7f0586ef | 156 | #ifndef __WXWINCE__ |
032af30f VZ |
157 | if ( m_destroyCursor ) |
158 | ::DestroyCursor((HCURSOR)m_hCursor); | |
7f0586ef | 159 | #endif |
032af30f VZ |
160 | |
161 | m_hCursor = 0; | |
162 | } | |
2bda0e17 KB |
163 | } |
164 | ||
0d0512bd | 165 | // ---------------------------------------------------------------------------- |
2bda0e17 | 166 | // Cursors |
0d0512bd VZ |
167 | // ---------------------------------------------------------------------------- |
168 | ||
169 | wxCursor::wxCursor() | |
2bda0e17 KB |
170 | { |
171 | } | |
172 | ||
461dae94 | 173 | #if wxUSE_IMAGE |
eff4ffbf | 174 | wxCursor::wxCursor(const wxImage& image) |
bff4ec63 | 175 | { |
eff4ffbf VZ |
176 | // image has to be of the standard cursor size, otherwise we won't be able |
177 | // to create it | |
178 | const int w = wxCursorRefData::GetStandardWidth(); | |
179 | const int h = wxCursorRefData::GetStandardHeight(); | |
180 | ||
b737ad10 RR |
181 | int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); |
182 | int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
183 | int image_w = image.GetWidth(); | |
184 | int image_h = image.GetHeight(); | |
eff4ffbf | 185 | |
b737ad10 RR |
186 | wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w && |
187 | hotSpotY >= 0 && hotSpotY < image_h, | |
7fd328a3 | 188 | _T("invalid cursor hot spot coordinates") ); |
eff4ffbf | 189 | |
b737ad10 RR |
190 | wxImage imageSized(image); // final image of correct size |
191 | ||
192 | // if image is too small then place it in the center, resize it if too big | |
193 | if ((w > image_w) && (h > image_h)) | |
194 | { | |
195 | wxPoint offset((w - image_w)/2, (h - image_h)/2); | |
196 | hotSpotX = hotSpotX + offset.x; | |
197 | hotSpotY = hotSpotY + offset.y; | |
198 | ||
199 | imageSized = image.Size(wxSize(w, h), offset); | |
200 | } | |
201 | else if ((w != image_w) || (h != image_h)) | |
202 | { | |
ccbb33c9 JS |
203 | hotSpotX = int(hotSpotX * double(w) / double(image_w)); |
204 | hotSpotY = int(hotSpotY * double(h) / double(image_h)); | |
b737ad10 RR |
205 | |
206 | imageSized = image.Scale(w, h); | |
207 | } | |
208 | ||
ccbb33c9 | 209 | HCURSOR hcursor = wxBitmapToHCURSOR( wxBitmap(imageSized), |
b737ad10 RR |
210 | hotSpotX, hotSpotY ); |
211 | ||
64c288fa | 212 | #if wxUSE_WXDIB |
eff4ffbf VZ |
213 | if ( !hcursor ) |
214 | { | |
7fd328a3 VZ |
215 | wxLogWarning(_("Failed to create cursor.")); |
216 | return; | |
eff4ffbf | 217 | } |
b928bd05 | 218 | #endif // wxUSE_WXDIB |
7fd328a3 VZ |
219 | |
220 | m_refData = new wxCursorRefData(hcursor, true /* delete it later */); | |
bff4ec63 | 221 | } |
461dae94 | 222 | #endif |
bff4ec63 | 223 | |
0d0512bd VZ |
224 | wxCursor::wxCursor(const char WXUNUSED(bits)[], |
225 | int WXUNUSED(width), | |
226 | int WXUNUSED(height), | |
227 | int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), | |
228 | const char WXUNUSED(maskBits)[]) | |
2bda0e17 KB |
229 | { |
230 | } | |
231 | ||
eff4ffbf | 232 | // MicroWin doesn't have support needed for the other ctors |
04ef50df | 233 | #ifdef __WXMICROWIN__ |
2bda0e17 | 234 | |
eff4ffbf VZ |
235 | wxCursor::wxCursor(const wxString& WXUNUSED(filename), |
236 | long WXUNUSED(kind), | |
237 | int WXUNUSED(hotSpotX), | |
238 | int WXUNUSED(hotSpotY)) | |
239 | { | |
240 | } | |
241 | ||
242 | wxCursor::wxCursor(int WXUNUSED(cursor_type)) | |
243 | { | |
244 | } | |
245 | ||
246 | #else // !__WXMICROWIN__ | |
247 | ||
248 | wxCursor::wxCursor(const wxString& filename, | |
249 | long kind, | |
250 | int hotSpotX, | |
251 | int hotSpotY) | |
252 | { | |
253 | HCURSOR hcursor; | |
254 | switch ( kind ) | |
0d0512bd | 255 | { |
eff4ffbf VZ |
256 | case wxBITMAP_TYPE_CUR_RESOURCE: |
257 | hcursor = ::LoadCursor(wxGetInstance(), filename); | |
258 | break; | |
259 | ||
7f0586ef | 260 | #ifndef __WXWINCE__ |
eff4ffbf VZ |
261 | case wxBITMAP_TYPE_CUR: |
262 | hcursor = ::LoadCursorFromFile(filename); | |
263 | break; | |
7f0586ef | 264 | #endif |
eff4ffbf | 265 | |
677a9e28 | 266 | case wxBITMAP_TYPE_ICO: |
7fd328a3 VZ |
267 | hcursor = wxBitmapToHCURSOR |
268 | ( | |
269 | wxIcon(filename, wxBITMAP_TYPE_ICO), | |
270 | hotSpotX, | |
271 | hotSpotY | |
272 | ); | |
eff4ffbf VZ |
273 | break; |
274 | ||
275 | case wxBITMAP_TYPE_BMP: | |
7fd328a3 VZ |
276 | hcursor = wxBitmapToHCURSOR |
277 | ( | |
278 | wxBitmap(filename, wxBITMAP_TYPE_BMP), | |
279 | hotSpotX, | |
280 | hotSpotY | |
281 | ); | |
eff4ffbf VZ |
282 | break; |
283 | ||
284 | default: | |
285 | wxFAIL_MSG( _T("unknown cursor resource type") ); | |
286 | ||
287 | hcursor = NULL; | |
0d0512bd | 288 | } |
eff4ffbf VZ |
289 | |
290 | if ( hcursor ) | |
0d0512bd | 291 | { |
eff4ffbf | 292 | m_refData = new wxCursorRefData(hcursor, true /* delete it later */); |
eff4ffbf | 293 | } |
2bda0e17 KB |
294 | } |
295 | ||
7da60d7c WS |
296 | // Cursors by stock number |
297 | wxCursor::wxCursor(int idCursor) | |
298 | { | |
1c6f2414 WS |
299 | // all wxWidgets standard cursors |
300 | static const struct StdCursor | |
301 | { | |
302 | // is this a standard Windows cursor? | |
303 | bool isStd; | |
304 | ||
305 | // the cursor name or id | |
306 | LPCTSTR name; | |
307 | } stdCursors[] = | |
308 | { | |
309 | { true, NULL }, // wxCURSOR_NONE | |
310 | { true, IDC_ARROW }, // wxCURSOR_ARROW | |
311 | { false, _T("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW | |
312 | { false, _T("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE | |
313 | { true, IDC_ARROW }, // WXCURSOR_CHAR | |
c8326d64 | 314 | |
27e7e859 JS |
315 | // Displays as an I-beam on XP, so use a cursor file |
316 | // { true, IDC_CROSS }, // WXCURSOR_CROSS | |
317 | { false, _T("WXCURSOR_CROSS") }, // WXCURSOR_CROSS | |
fe384096 RD |
318 | |
319 | // See special handling below for wxCURSOR_HAND | |
320 | // { false, _T("WXCURSOR_HAND") }, // wxCURSOR_HAND | |
321 | { true, IDC_HAND }, // wxCURSOR_HAND | |
c8326d64 | 322 | |
1c6f2414 WS |
323 | { true, IDC_IBEAM }, // WXCURSOR_IBEAM |
324 | { true, IDC_ARROW }, // WXCURSOR_LEFT_BUTTON | |
325 | { false, _T("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER | |
326 | { true, IDC_ARROW }, // WXCURSOR_MIDDLE_BUTTON | |
327 | { true, IDC_NO }, // WXCURSOR_NO_ENTRY | |
328 | { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH | |
329 | { false, _T("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL | |
330 | { false, _T("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT | |
331 | { false, _T("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT | |
332 | { true, IDC_HELP }, // WXCURSOR_QUESTION_ARROW | |
333 | { true, IDC_ARROW }, // WXCURSOR_RIGHT_BUTTON | |
334 | { true, IDC_SIZENESW }, // WXCURSOR_SIZENESW | |
335 | { true, IDC_SIZENS }, // WXCURSOR_SIZENS | |
336 | { true, IDC_SIZENWSE }, // WXCURSOR_SIZENWSE | |
337 | { true, IDC_SIZEWE }, // WXCURSOR_SIZEWE | |
338 | { true, IDC_SIZEALL }, // WXCURSOR_SIZING | |
339 | { false, _T("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN | |
340 | { true, IDC_WAIT }, // WXCURSOR_WAIT | |
97000852 | 341 | { true, IDC_WAIT }, // WXCURSOR_WATCH |
1c6f2414 WS |
342 | { false, _T("WXCURSOR_BLANK") }, // wxCURSOR_BLANK |
343 | { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT | |
344 | ||
345 | // no entry for wxCURSOR_MAX | |
346 | }; | |
347 | ||
348 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX, | |
349 | CursorsIdArrayMismatch ); | |
350 | ||
eff4ffbf VZ |
351 | wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors), |
352 | _T("invalid cursor id in wxCursor() ctor") ); | |
353 | ||
354 | const StdCursor& stdCursor = stdCursors[idCursor]; | |
fe384096 | 355 | bool deleteLater = !stdCursor.isStd; |
eff4ffbf VZ |
356 | |
357 | HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(), | |
358 | stdCursor.name); | |
359 | ||
fe384096 RD |
360 | // IDC_HAND may not be available on some versions of Windows. |
361 | if ( !hcursor && idCursor == wxCURSOR_HAND) | |
362 | { | |
363 | hcursor = ::LoadCursor(wxGetInstance(), _T("WXCURSOR_HAND")); | |
364 | deleteLater = true; | |
365 | } | |
c8326d64 | 366 | |
eff4ffbf | 367 | if ( !hcursor ) |
2bda0e17 | 368 | { |
eff4ffbf | 369 | wxLogLastError(_T("LoadCursor")); |
2bda0e17 | 370 | } |
eff4ffbf | 371 | else |
15dadf31 | 372 | { |
fe384096 | 373 | m_refData = new wxCursorRefData(hcursor, deleteLater); |
15dadf31 | 374 | } |
2bda0e17 KB |
375 | } |
376 | ||
eff4ffbf VZ |
377 | #endif // __WXMICROWIN__/!__WXMICROWIN__ |
378 | ||
0d0512bd | 379 | wxCursor::~wxCursor() |
2bda0e17 | 380 | { |
2bda0e17 KB |
381 | } |
382 | ||
eff4ffbf VZ |
383 | // ---------------------------------------------------------------------------- |
384 | // other wxCursor functions | |
385 | // ---------------------------------------------------------------------------- | |
386 | ||
eff4ffbf VZ |
387 | wxGDIImageRefData *wxCursor::CreateData() const |
388 | { | |
389 | return new wxCursorRefData; | |
390 | } | |
391 | ||
0d0512bd | 392 | // ---------------------------------------------------------------------------- |
2bda0e17 | 393 | // Global cursor setting |
0d0512bd VZ |
394 | // ---------------------------------------------------------------------------- |
395 | ||
bfbd6dc1 | 396 | const wxCursor *wxGetGlobalCursor() |
2bda0e17 | 397 | { |
bfbd6dc1 VZ |
398 | return gs_globalCursor; |
399 | } | |
2bda0e17 | 400 | |
bfbd6dc1 VZ |
401 | void wxSetCursor(const wxCursor& cursor) |
402 | { | |
403 | if ( cursor.Ok() ) | |
6bf57206 | 404 | { |
bfbd6dc1 | 405 | ::SetCursor(GetHcursorOf(cursor)); |
6bf57206 | 406 | |
bfbd6dc1 VZ |
407 | if ( gs_globalCursor ) |
408 | *gs_globalCursor = cursor; | |
6bf57206 | 409 | } |
2bda0e17 | 410 | } |