]>
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 | { | |
5276b0a5 | 108 | wxDELETE(gs_globalCursor); |
bfbd6dc1 VZ |
109 | } |
110 | }; | |
111 | ||
112 | // ============================================================================ | |
113 | // implementation | |
114 | // ============================================================================ | |
2bda0e17 | 115 | |
0d0512bd VZ |
116 | // ---------------------------------------------------------------------------- |
117 | // wxCursorRefData | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
eff4ffbf VZ |
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() | |
2bda0e17 | 131 | { |
eff4ffbf VZ |
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 | } | |
0d0512bd | 147 | |
eff4ffbf | 148 | m_destroyCursor = destroy; |
2bda0e17 KB |
149 | } |
150 | ||
0d0512bd | 151 | void wxCursorRefData::Free() |
2bda0e17 | 152 | { |
032af30f VZ |
153 | if ( m_hCursor ) |
154 | { | |
7f0586ef | 155 | #ifndef __WXWINCE__ |
032af30f VZ |
156 | if ( m_destroyCursor ) |
157 | ::DestroyCursor((HCURSOR)m_hCursor); | |
7f0586ef | 158 | #endif |
032af30f VZ |
159 | |
160 | m_hCursor = 0; | |
161 | } | |
2bda0e17 KB |
162 | } |
163 | ||
0d0512bd | 164 | // ---------------------------------------------------------------------------- |
2bda0e17 | 165 | // Cursors |
0d0512bd VZ |
166 | // ---------------------------------------------------------------------------- |
167 | ||
168 | wxCursor::wxCursor() | |
2bda0e17 KB |
169 | { |
170 | } | |
171 | ||
461dae94 | 172 | #if wxUSE_IMAGE |
eff4ffbf | 173 | wxCursor::wxCursor(const wxImage& image) |
bff4ec63 | 174 | { |
eff4ffbf VZ |
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 | ||
b737ad10 RR |
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(); | |
eff4ffbf | 184 | |
b737ad10 RR |
185 | wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w && |
186 | hotSpotY >= 0 && hotSpotY < image_h, | |
9a83f860 | 187 | wxT("invalid cursor hot spot coordinates") ); |
eff4ffbf | 188 | |
b737ad10 RR |
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 | { | |
ccbb33c9 JS |
202 | hotSpotX = int(hotSpotX * double(w) / double(image_w)); |
203 | hotSpotY = int(hotSpotY * double(h) / double(image_h)); | |
b737ad10 RR |
204 | |
205 | imageSized = image.Scale(w, h); | |
206 | } | |
207 | ||
ccbb33c9 | 208 | HCURSOR hcursor = wxBitmapToHCURSOR( wxBitmap(imageSized), |
b737ad10 RR |
209 | hotSpotX, hotSpotY ); |
210 | ||
eff4ffbf VZ |
211 | if ( !hcursor ) |
212 | { | |
7fd328a3 VZ |
213 | wxLogWarning(_("Failed to create cursor.")); |
214 | return; | |
eff4ffbf | 215 | } |
7fd328a3 VZ |
216 | |
217 | m_refData = new wxCursorRefData(hcursor, true /* delete it later */); | |
bff4ec63 | 218 | } |
c7c441cc | 219 | #endif // wxUSE_IMAGE |
bff4ec63 | 220 | |
eff4ffbf | 221 | // MicroWin doesn't have support needed for the other ctors |
04ef50df | 222 | #ifdef __WXMICROWIN__ |
2bda0e17 | 223 | |
0ef5b1da | 224 | wxCursor::InitFromStock(wxStockCursor WXUNUSED(cursor_type)) |
eff4ffbf VZ |
225 | { |
226 | } | |
227 | ||
228 | #else // !__WXMICROWIN__ | |
229 | ||
230 | wxCursor::wxCursor(const wxString& filename, | |
dd021ce2 | 231 | wxBitmapType kind, |
eff4ffbf VZ |
232 | int hotSpotX, |
233 | int hotSpotY) | |
234 | { | |
235 | HCURSOR hcursor; | |
236 | switch ( kind ) | |
0d0512bd | 237 | { |
eff4ffbf | 238 | case wxBITMAP_TYPE_CUR_RESOURCE: |
715e4f7e | 239 | hcursor = ::LoadCursor(wxGetInstance(), filename.t_str()); |
eff4ffbf VZ |
240 | break; |
241 | ||
7f0586ef | 242 | #ifndef __WXWINCE__ |
72bd600b | 243 | case wxBITMAP_TYPE_ANI: |
eff4ffbf | 244 | case wxBITMAP_TYPE_CUR: |
715e4f7e | 245 | hcursor = ::LoadCursorFromFile(filename.t_str()); |
eff4ffbf | 246 | break; |
7f0586ef | 247 | #endif |
eff4ffbf | 248 | |
677a9e28 | 249 | case wxBITMAP_TYPE_ICO: |
7fd328a3 VZ |
250 | hcursor = wxBitmapToHCURSOR |
251 | ( | |
252 | wxIcon(filename, wxBITMAP_TYPE_ICO), | |
253 | hotSpotX, | |
254 | hotSpotY | |
255 | ); | |
eff4ffbf VZ |
256 | break; |
257 | ||
258 | case wxBITMAP_TYPE_BMP: | |
7fd328a3 VZ |
259 | hcursor = wxBitmapToHCURSOR |
260 | ( | |
261 | wxBitmap(filename, wxBITMAP_TYPE_BMP), | |
262 | hotSpotX, | |
263 | hotSpotY | |
264 | ); | |
eff4ffbf VZ |
265 | break; |
266 | ||
267 | default: | |
9a83f860 | 268 | wxLogError( wxT("unknown cursor resource type '%d'"), kind ); |
eff4ffbf VZ |
269 | |
270 | hcursor = NULL; | |
0d0512bd | 271 | } |
eff4ffbf VZ |
272 | |
273 | if ( hcursor ) | |
0d0512bd | 274 | { |
eff4ffbf | 275 | m_refData = new wxCursorRefData(hcursor, true /* delete it later */); |
eff4ffbf | 276 | } |
2bda0e17 KB |
277 | } |
278 | ||
7da60d7c | 279 | // Cursors by stock number |
0ef5b1da | 280 | void wxCursor::InitFromStock(wxStockCursor idCursor) |
7da60d7c | 281 | { |
1c6f2414 WS |
282 | // all wxWidgets standard cursors |
283 | static const struct StdCursor | |
284 | { | |
285 | // is this a standard Windows cursor? | |
286 | bool isStd; | |
287 | ||
288 | // the cursor name or id | |
289 | LPCTSTR name; | |
290 | } stdCursors[] = | |
291 | { | |
292 | { true, NULL }, // wxCURSOR_NONE | |
293 | { true, IDC_ARROW }, // wxCURSOR_ARROW | |
9a83f860 VZ |
294 | { false, wxT("WXCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW |
295 | { false, wxT("WXCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE | |
1c6f2414 | 296 | { true, IDC_ARROW }, // WXCURSOR_CHAR |
d890beb9 | 297 | { true, IDC_CROSS }, // WXCURSOR_CROSS |
fe384096 | 298 | { true, IDC_HAND }, // wxCURSOR_HAND |
1c6f2414 WS |
299 | { true, IDC_IBEAM }, // WXCURSOR_IBEAM |
300 | { true, IDC_ARROW }, // WXCURSOR_LEFT_BUTTON | |
9a83f860 | 301 | { false, wxT("WXCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER |
1c6f2414 WS |
302 | { true, IDC_ARROW }, // WXCURSOR_MIDDLE_BUTTON |
303 | { true, IDC_NO }, // WXCURSOR_NO_ENTRY | |
9a83f860 VZ |
304 | { false, wxT("WXCURSOR_PBRUSH") }, // wxCURSOR_PAINT_BRUSH |
305 | { false, wxT("WXCURSOR_PENCIL") }, // wxCURSOR_PENCIL | |
306 | { false, wxT("WXCURSOR_PLEFT") }, // wxCURSOR_POINT_LEFT | |
307 | { false, wxT("WXCURSOR_PRIGHT") }, // wxCURSOR_POINT_RIGHT | |
1c6f2414 WS |
308 | { true, IDC_HELP }, // WXCURSOR_QUESTION_ARROW |
309 | { true, IDC_ARROW }, // WXCURSOR_RIGHT_BUTTON | |
310 | { true, IDC_SIZENESW }, // WXCURSOR_SIZENESW | |
311 | { true, IDC_SIZENS }, // WXCURSOR_SIZENS | |
312 | { true, IDC_SIZENWSE }, // WXCURSOR_SIZENWSE | |
313 | { true, IDC_SIZEWE }, // WXCURSOR_SIZEWE | |
314 | { true, IDC_SIZEALL }, // WXCURSOR_SIZING | |
9a83f860 | 315 | { false, wxT("WXCURSOR_PBRUSH") }, // wxCURSOR_SPRAYCAN |
1c6f2414 | 316 | { true, IDC_WAIT }, // WXCURSOR_WAIT |
97000852 | 317 | { true, IDC_WAIT }, // WXCURSOR_WATCH |
9a83f860 | 318 | { false, wxT("WXCURSOR_BLANK") }, // wxCURSOR_BLANK |
1c6f2414 WS |
319 | { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT |
320 | ||
321 | // no entry for wxCURSOR_MAX | |
322 | }; | |
323 | ||
324 | wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX, | |
325 | CursorsIdArrayMismatch ); | |
326 | ||
eff4ffbf | 327 | wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors), |
9a83f860 | 328 | wxT("invalid cursor id in wxCursor() ctor") ); |
eff4ffbf VZ |
329 | |
330 | const StdCursor& stdCursor = stdCursors[idCursor]; | |
fe384096 | 331 | bool deleteLater = !stdCursor.isStd; |
eff4ffbf VZ |
332 | |
333 | HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(), | |
334 | stdCursor.name); | |
335 | ||
fe384096 RD |
336 | // IDC_HAND may not be available on some versions of Windows. |
337 | if ( !hcursor && idCursor == wxCURSOR_HAND) | |
338 | { | |
9a83f860 | 339 | hcursor = ::LoadCursor(wxGetInstance(), wxT("WXCURSOR_HAND")); |
fe384096 RD |
340 | deleteLater = true; |
341 | } | |
c8326d64 | 342 | |
eff4ffbf | 343 | if ( !hcursor ) |
2bda0e17 | 344 | { |
6b2f5553 VZ |
345 | if ( !stdCursor.isStd ) |
346 | { | |
347 | // it may be not obvious to the programmer why did loading fail, | |
348 | // try to help by pointing to the by far the most probable reason | |
349 | wxFAIL_MSG(wxT("Loading a cursor defined by wxWidgets failed, ") | |
350 | wxT("did you include include/wx/msw/wx.rc file from ") | |
351 | wxT("your resource file?")); | |
352 | } | |
353 | ||
9a83f860 | 354 | wxLogLastError(wxT("LoadCursor")); |
2bda0e17 | 355 | } |
eff4ffbf | 356 | else |
15dadf31 | 357 | { |
fe384096 | 358 | m_refData = new wxCursorRefData(hcursor, deleteLater); |
15dadf31 | 359 | } |
2bda0e17 KB |
360 | } |
361 | ||
eff4ffbf VZ |
362 | #endif // __WXMICROWIN__/!__WXMICROWIN__ |
363 | ||
0d0512bd | 364 | wxCursor::~wxCursor() |
2bda0e17 | 365 | { |
2bda0e17 KB |
366 | } |
367 | ||
eff4ffbf VZ |
368 | // ---------------------------------------------------------------------------- |
369 | // other wxCursor functions | |
370 | // ---------------------------------------------------------------------------- | |
371 | ||
eff4ffbf VZ |
372 | wxGDIImageRefData *wxCursor::CreateData() const |
373 | { | |
374 | return new wxCursorRefData; | |
375 | } | |
376 | ||
0d0512bd | 377 | // ---------------------------------------------------------------------------- |
2bda0e17 | 378 | // Global cursor setting |
0d0512bd VZ |
379 | // ---------------------------------------------------------------------------- |
380 | ||
bfbd6dc1 | 381 | const wxCursor *wxGetGlobalCursor() |
2bda0e17 | 382 | { |
bfbd6dc1 VZ |
383 | return gs_globalCursor; |
384 | } | |
2bda0e17 | 385 | |
bfbd6dc1 VZ |
386 | void wxSetCursor(const wxCursor& cursor) |
387 | { | |
a1b806b9 | 388 | if ( cursor.IsOk() ) |
6bf57206 | 389 | { |
bfbd6dc1 | 390 | ::SetCursor(GetHcursorOf(cursor)); |
6bf57206 | 391 | |
bfbd6dc1 VZ |
392 | if ( gs_globalCursor ) |
393 | *gs_globalCursor = cursor; | |
6bf57206 | 394 | } |
2bda0e17 | 395 | } |