]>
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 | |
eff4ffbf | 7 | // Copyright: (c) 1997-2003 Julian Smart and Vadim Zeitlin |
65571936 | 8 | // Licence: wxWindows licence |
2bda0e17 KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
0d0512bd VZ |
11 | // ============================================================================ |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
2bda0e17 KB |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
0d0512bd | 23 | #pragma hdrstop |
2bda0e17 KB |
24 | #endif |
25 | ||
c8326d64 WS |
26 | #include "wx/cursor.h" |
27 | ||
2bda0e17 | 28 | #ifndef WX_PRECOMP |
0d0512bd VZ |
29 | #include "wx/utils.h" |
30 | #include "wx/app.h" | |
fef15b42 | 31 | #include "wx/bitmap.h" |
0d0512bd | 32 | #include "wx/icon.h" |
ed39ff57 | 33 | #include "wx/settings.h" |
51d5ec54 | 34 | #include "wx/intl.h" |
155ecd4c | 35 | #include "wx/image.h" |
02761f6c | 36 | #include "wx/module.h" |
2bda0e17 KB |
37 | #endif |
38 | ||
39 | #include "wx/msw/private.h" | |
a6c2e2c7 | 40 | #include "wx/msw/missing.h" // IDC_HAND |
eff4ffbf | 41 | |
eff4ffbf VZ |
42 | // define functions missing in MicroWin |
43 | #ifdef __WXMICROWIN__ | |
44 | static inline void DestroyCursor(HCURSOR) { } | |
45 | static inline void SetCursor(HCURSOR) { } | |
46 | #endif // __WXMICROWIN__ | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // private classes | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | class WXDLLEXPORT wxCursorRefData : public wxGDIImageRefData | |
53 | { | |
54 | public: | |
55 | // the second parameter is used to tell us to delete the cursor when we're | |
56 | // done with it (normally we shouldn't call DestroyCursor() this is why it | |
57 | // doesn't happen by default) | |
58 | wxCursorRefData(HCURSOR hcursor = 0, bool takeOwnership = false); | |
59 | ||
60 | virtual ~wxCursorRefData() { Free(); } | |
61 | ||
62 | virtual void Free(); | |
63 | ||
64 | ||
65 | // return the size of the standard cursor: notice that the system only | |
66 | // supports the cursors of this size | |
67 | static wxCoord GetStandardWidth(); | |
68 | static wxCoord GetStandardHeight(); | |
69 | ||
70 | private: | |
71 | bool m_destroyCursor; | |
72 | ||
73 | // standard cursor size, computed on first use | |
74 | static wxSize ms_sizeStd; | |
75 | }; | |
76 | ||
0d0512bd VZ |
77 | // ---------------------------------------------------------------------------- |
78 | // wxWin macros | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
621b3e21 | 81 | IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject) |
bfbd6dc1 VZ |
82 | |
83 | // ---------------------------------------------------------------------------- | |
84 | // globals | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | // Current cursor, in order to hang on to cursor handle when setting the cursor | |
88 | // globally | |
89 | static wxCursor *gs_globalCursor = NULL; | |
90 | ||
91 | // ---------------------------------------------------------------------------- | |
92 | // private classes | |
93 | // ---------------------------------------------------------------------------- | |
94 | ||
95 | class wxCursorModule : public wxModule | |
96 | { | |
97 | public: | |
98 | virtual bool OnInit() | |
99 | { | |
100 | gs_globalCursor = new wxCursor; | |
101 | ||
02b7b6b0 | 102 | return true; |
bfbd6dc1 VZ |
103 | } |
104 | ||
105 | virtual void OnExit() | |
106 | { | |
5276b0a5 | 107 | wxDELETE(gs_globalCursor); |
bfbd6dc1 VZ |
108 | } |
109 | }; | |
110 | ||
111 | // ============================================================================ | |
112 | // implementation | |
113 | // ============================================================================ | |
2bda0e17 | 114 | |
0d0512bd VZ |
115 | // ---------------------------------------------------------------------------- |
116 | // wxCursorRefData | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
eff4ffbf VZ |
119 | wxSize wxCursorRefData::ms_sizeStd; |
120 | ||
121 | wxCoord wxCursorRefData::GetStandardWidth() | |
122 | { | |
123 | if ( !ms_sizeStd.x ) | |
124 | ms_sizeStd.x = wxSystemSettings::GetMetric(wxSYS_CURSOR_X); | |
125 | ||
126 | return ms_sizeStd.x; | |
127 | } | |
128 | ||
129 | wxCoord wxCursorRefData::GetStandardHeight() | |
2bda0e17 | 130 | { |
eff4ffbf VZ |
131 | if ( !ms_sizeStd.y ) |
132 | ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y); | |
133 | ||
134 | return ms_sizeStd.y; | |
135 | } | |
136 | ||
137 | wxCursorRefData::wxCursorRefData(HCURSOR hcursor, bool destroy) | |
138 | { | |
139 | m_hCursor = (WXHCURSOR)hcursor; | |
140 | ||
141 | if ( m_hCursor ) | |
142 | { | |
143 | m_width = GetStandardWidth(); | |
144 | m_height = GetStandardHeight(); | |
145 | } | |
0d0512bd | 146 | |
eff4ffbf | 147 | m_destroyCursor = destroy; |
2bda0e17 KB |
148 | } |
149 | ||
0d0512bd | 150 | void wxCursorRefData::Free() |
2bda0e17 | 151 | { |
032af30f VZ |
152 | if ( m_hCursor ) |
153 | { | |
7f0586ef | 154 | #ifndef __WXWINCE__ |
032af30f VZ |
155 | if ( m_destroyCursor ) |
156 | ::DestroyCursor((HCURSOR)m_hCursor); | |
7f0586ef | 157 | #endif |
032af30f VZ |
158 | |
159 | m_hCursor = 0; | |
160 | } | |
2bda0e17 KB |
161 | } |
162 | ||
0d0512bd | 163 | // ---------------------------------------------------------------------------- |
2bda0e17 | 164 | // Cursors |
0d0512bd VZ |
165 | // ---------------------------------------------------------------------------- |
166 | ||
167 | wxCursor::wxCursor() | |
2bda0e17 KB |
168 | { |
169 | } | |
170 | ||
461dae94 | 171 | #if wxUSE_IMAGE |
eff4ffbf | 172 | wxCursor::wxCursor(const wxImage& image) |
bff4ec63 | 173 | { |
eff4ffbf VZ |
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 | ||
b737ad10 RR |
179 | int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); |
180 | int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
181 | int image_w = image.GetWidth(); | |
182 | int image_h = image.GetHeight(); | |
eff4ffbf | 183 | |
b737ad10 RR |
184 | wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w && |
185 | hotSpotY >= 0 && hotSpotY < image_h, | |
9a83f860 | 186 | wxT("invalid cursor hot spot coordinates") ); |
eff4ffbf | 187 | |
b737ad10 RR |
188 | wxImage imageSized(image); // final image of correct size |
189 | ||
190 | // if image is too small then place it in the center, resize it if too big | |
191 | if ((w > image_w) && (h > image_h)) | |
192 | { | |
193 | wxPoint offset((w - image_w)/2, (h - image_h)/2); | |
194 | hotSpotX = hotSpotX + offset.x; | |
195 | hotSpotY = hotSpotY + offset.y; | |
196 | ||
197 | imageSized = image.Size(wxSize(w, h), offset); | |
198 | } | |
199 | else if ((w != image_w) || (h != image_h)) | |
200 | { | |
ccbb33c9 JS |
201 | hotSpotX = int(hotSpotX * double(w) / double(image_w)); |
202 | hotSpotY = int(hotSpotY * double(h) / double(image_h)); | |
b737ad10 RR |
203 | |
204 | imageSized = image.Scale(w, h); | |
205 | } | |
206 | ||
ccbb33c9 | 207 | HCURSOR hcursor = wxBitmapToHCURSOR( wxBitmap(imageSized), |
b737ad10 RR |
208 | hotSpotX, hotSpotY ); |
209 | ||
eff4ffbf VZ |
210 | if ( !hcursor ) |
211 | { | |
7fd328a3 VZ |
212 | wxLogWarning(_("Failed to create cursor.")); |
213 | return; | |
eff4ffbf | 214 | } |
7fd328a3 VZ |
215 | |
216 | m_refData = new wxCursorRefData(hcursor, true /* delete it later */); | |
bff4ec63 | 217 | } |
c7c441cc | 218 | #endif // wxUSE_IMAGE |
bff4ec63 | 219 | |
eff4ffbf | 220 | // MicroWin doesn't have support needed for the other ctors |
04ef50df | 221 | #ifdef __WXMICROWIN__ |
2bda0e17 | 222 | |
0ef5b1da | 223 | wxCursor::InitFromStock(wxStockCursor WXUNUSED(cursor_type)) |
eff4ffbf VZ |
224 | { |
225 | } | |
226 | ||
227 | #else // !__WXMICROWIN__ | |
228 | ||
229 | wxCursor::wxCursor(const wxString& filename, | |
dd021ce2 | 230 | wxBitmapType kind, |
eff4ffbf VZ |
231 | int hotSpotX, |
232 | int hotSpotY) | |
233 | { | |
234 | HCURSOR hcursor; | |
235 | switch ( kind ) | |
0d0512bd | 236 | { |
eff4ffbf | 237 | case wxBITMAP_TYPE_CUR_RESOURCE: |
715e4f7e | 238 | hcursor = ::LoadCursor(wxGetInstance(), filename.t_str()); |
eff4ffbf VZ |
239 | break; |
240 | ||
7f0586ef | 241 | #ifndef __WXWINCE__ |
72bd600b | 242 | case wxBITMAP_TYPE_ANI: |
eff4ffbf | 243 | case wxBITMAP_TYPE_CUR: |
715e4f7e | 244 | hcursor = ::LoadCursorFromFile(filename.t_str()); |
eff4ffbf | 245 | break; |
7f0586ef | 246 | #endif |
eff4ffbf | 247 | |
677a9e28 | 248 | case wxBITMAP_TYPE_ICO: |
7fd328a3 VZ |
249 | hcursor = wxBitmapToHCURSOR |
250 | ( | |
251 | wxIcon(filename, wxBITMAP_TYPE_ICO), | |
252 | hotSpotX, | |
253 | hotSpotY | |
254 | ); | |
eff4ffbf VZ |
255 | break; |
256 | ||
257 | case wxBITMAP_TYPE_BMP: | |
7fd328a3 VZ |
258 | hcursor = wxBitmapToHCURSOR |
259 | ( | |
260 | wxBitmap(filename, wxBITMAP_TYPE_BMP), | |
261 | hotSpotX, | |
262 | hotSpotY | |
263 | ); | |
eff4ffbf VZ |
264 | break; |
265 | ||
266 | default: | |
9a83f860 | 267 | wxLogError( wxT("unknown cursor resource type '%d'"), kind ); |
eff4ffbf VZ |
268 | |
269 | hcursor = NULL; | |
0d0512bd | 270 | } |
eff4ffbf VZ |
271 | |
272 | if ( hcursor ) | |
0d0512bd | 273 | { |
eff4ffbf | 274 | m_refData = new wxCursorRefData(hcursor, true /* delete it later */); |
eff4ffbf | 275 | } |
2bda0e17 KB |
276 | } |
277 | ||
6586750c VZ |
278 | namespace |
279 | { | |
280 | ||
281 | void ReverseBitmap(HBITMAP bitmap, int width, int height) | |
282 | { | |
283 | MemoryHDC hdc; | |
284 | SelectInHDC selBitmap(hdc, bitmap); | |
285 | ::StretchBlt(hdc, width - 1, 0, -width, height, | |
286 | hdc, 0, 0, width, height, SRCCOPY); | |
287 | } | |
288 | ||
289 | HCURSOR CreateReverseCursor(HCURSOR cursor) | |
290 | { | |
291 | ICONINFO info; | |
292 | if ( !::GetIconInfo(cursor, &info) ) | |
293 | return NULL; | |
294 | ||
295 | HCURSOR cursorRev = NULL; | |
296 | ||
297 | BITMAP bmp; | |
298 | if ( ::GetObject(info.hbmMask, sizeof(bmp), &bmp) ) | |
299 | { | |
300 | ReverseBitmap(info.hbmMask, bmp.bmWidth, bmp.bmHeight); | |
301 | if ( info.hbmColor ) | |
302 | ReverseBitmap(info.hbmColor, bmp.bmWidth, bmp.bmHeight); | |
303 | info.xHotspot = (DWORD)bmp.bmWidth - 1 - info.xHotspot; | |
304 | ||
305 | cursorRev = ::CreateIconIndirect(&info); | |
306 | } | |
307 | ||
308 | ::DeleteObject(info.hbmMask); | |
309 | if ( info.hbmColor ) | |
310 | ::DeleteObject(info.hbmColor); | |
311 | ||
312 | return cursorRev; | |
313 | } | |
314 | ||
315 | } // anonymous namespace | |
316 | ||
7da60d7c | 317 | // Cursors by stock number |
0ef5b1da | 318 | void wxCursor::InitFromStock(wxStockCursor idCursor) |
7da60d7c | 319 | { |
1c6f2414 WS |
320 | // all wxWidgets standard cursors |
321 | static const struct StdCursor | |
322 | { | |
323 | // is this a standard Windows cursor? | |
324 | bool isStd; | |
325 | ||
326 | // the cursor name or id | |
327 | LPCTSTR name; | |
328 | } stdCursors[] = | |
329 | { | |
330 | { true, NULL }, // wxCURSOR_NONE | |
331 | { true, IDC_ARROW }, // wxCURSOR_ARROW | |
9a83f860 VZ |
332 | { false, wxT("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW |
333 | { false, wxT("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE | |
1c6f2414 | 334 | { true, IDC_ARROW }, // WXCURSOR_CHAR |
d890beb9 | 335 | { true, IDC_CROSS }, // WXCURSOR_CROSS |
fe384096 | 336 | { true, IDC_HAND }, // wxCURSOR_HAND |
1c6f2414 WS |
337 | { true, IDC_IBEAM }, // WXCURSOR_IBEAM |
338 | { true, IDC_ARROW }, // WXCURSOR_LEFT_BUTTON | |
9a83f860 | 339 | { false, wxT("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER |
1c6f2414 WS |
340 | { true, IDC_ARROW }, // WXCURSOR_MIDDLE_BUTTON |
341 | { true, IDC_NO }, // WXCURSOR_NO_ENTRY | |
9a83f860 VZ |
342 | { false, wxT("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH |
343 | { false, wxT("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL | |
344 | { false, wxT("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT | |
345 | { false, wxT("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT | |
1c6f2414 WS |
346 | { true, IDC_HELP }, // WXCURSOR_QUESTION_ARROW |
347 | { true, IDC_ARROW }, // WXCURSOR_RIGHT_BUTTON | |
348 | { true, IDC_SIZENESW }, // WXCURSOR_SIZENESW | |
349 | { true, IDC_SIZENS }, // WXCURSOR_SIZENS | |
350 | { true, IDC_SIZENWSE }, // WXCURSOR_SIZENWSE | |
351 | { true, IDC_SIZEWE }, // WXCURSOR_SIZEWE | |
352 | { true, IDC_SIZEALL }, // WXCURSOR_SIZING | |
9a83f860 | 353 | { false, wxT("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN |
1c6f2414 | 354 | { true, IDC_WAIT }, // WXCURSOR_WAIT |
97000852 | 355 | { true, IDC_WAIT }, // WXCURSOR_WATCH |
9a83f860 | 356 | { false, wxT("WXCURSOR_BLANK") }, // wxCURSOR_BLANK |
1c6f2414 WS |
357 | { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT |
358 | ||
359 | // no entry for wxCURSOR_MAX | |
360 | }; | |
361 | ||
362 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX, | |
363 | CursorsIdArrayMismatch ); | |
364 | ||
eff4ffbf | 365 | wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors), |
9a83f860 | 366 | wxT("invalid cursor id in wxCursor() ctor") ); |
eff4ffbf VZ |
367 | |
368 | const StdCursor& stdCursor = stdCursors[idCursor]; | |
fe384096 | 369 | bool deleteLater = !stdCursor.isStd; |
eff4ffbf VZ |
370 | |
371 | HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(), | |
372 | stdCursor.name); | |
373 | ||
fe384096 RD |
374 | // IDC_HAND may not be available on some versions of Windows. |
375 | if ( !hcursor && idCursor == wxCURSOR_HAND) | |
376 | { | |
9a83f860 | 377 | hcursor = ::LoadCursor(wxGetInstance(), wxT("WXCURSOR_HAND")); |
fe384096 RD |
378 | deleteLater = true; |
379 | } | |
c8326d64 | 380 | |
6586750c VZ |
381 | if ( !hcursor && idCursor == wxCURSOR_RIGHT_ARROW) |
382 | { | |
383 | hcursor = ::LoadCursor(NULL, IDC_ARROW); | |
384 | if ( hcursor ) | |
385 | { | |
386 | hcursor = CreateReverseCursor(hcursor); | |
387 | deleteLater = true; | |
388 | } | |
389 | } | |
390 | ||
eff4ffbf | 391 | if ( !hcursor ) |
2bda0e17 | 392 | { |
6b2f5553 VZ |
393 | if ( !stdCursor.isStd ) |
394 | { | |
395 | // it may be not obvious to the programmer why did loading fail, | |
396 | // try to help by pointing to the by far the most probable reason | |
397 | wxFAIL_MSG(wxT("Loading a cursor defined by wxWidgets failed, ") | |
398 | wxT("did you include include/wx/msw/wx.rc file from ") | |
399 | wxT("your resource file?")); | |
400 | } | |
401 | ||
9a83f860 | 402 | wxLogLastError(wxT("LoadCursor")); |
2bda0e17 | 403 | } |
eff4ffbf | 404 | else |
15dadf31 | 405 | { |
fe384096 | 406 | m_refData = new wxCursorRefData(hcursor, deleteLater); |
15dadf31 | 407 | } |
2bda0e17 KB |
408 | } |
409 | ||
eff4ffbf VZ |
410 | #endif // __WXMICROWIN__/!__WXMICROWIN__ |
411 | ||
0d0512bd | 412 | wxCursor::~wxCursor() |
2bda0e17 | 413 | { |
2bda0e17 KB |
414 | } |
415 | ||
eff4ffbf VZ |
416 | // ---------------------------------------------------------------------------- |
417 | // other wxCursor functions | |
418 | // ---------------------------------------------------------------------------- | |
419 | ||
eff4ffbf VZ |
420 | wxGDIImageRefData *wxCursor::CreateData() const |
421 | { | |
422 | return new wxCursorRefData; | |
423 | } | |
424 | ||
0d0512bd | 425 | // ---------------------------------------------------------------------------- |
2bda0e17 | 426 | // Global cursor setting |
0d0512bd VZ |
427 | // ---------------------------------------------------------------------------- |
428 | ||
bfbd6dc1 | 429 | const wxCursor *wxGetGlobalCursor() |
2bda0e17 | 430 | { |
bfbd6dc1 VZ |
431 | return gs_globalCursor; |
432 | } | |
2bda0e17 | 433 | |
bfbd6dc1 VZ |
434 | void wxSetCursor(const wxCursor& cursor) |
435 | { | |
a1b806b9 | 436 | if ( cursor.IsOk() ) |
6bf57206 | 437 | { |
bfbd6dc1 | 438 | ::SetCursor(GetHcursorOf(cursor)); |
6bf57206 | 439 | |
bfbd6dc1 VZ |
440 | if ( gs_globalCursor ) |
441 | *gs_globalCursor = cursor; | |
6bf57206 | 442 | } |
2bda0e17 | 443 | } |