Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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) Julian Smart and Markus Holzem | |
6bf57206 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
0d0512bd VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
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/list.h" |
33 | #include "wx/utils.h" | |
34 | #include "wx/app.h" | |
fef15b42 | 35 | #include "wx/bitmap.h" |
0d0512bd | 36 | #include "wx/icon.h" |
fef15b42 | 37 | #include "wx/cursor.h" |
2bda0e17 KB |
38 | #endif |
39 | ||
2b0b4c55 | 40 | #include "wx/module.h" |
2bda0e17 | 41 | #include "wx/msw/private.h" |
04ef50df | 42 | #ifndef __WXMICROWIN__ |
2bda0e17 | 43 | #include "wx/msw/dib.h" |
04ef50df | 44 | #endif |
2bda0e17 | 45 | |
47d67540 | 46 | #if wxUSE_RESOURCE_LOADING_IN_MSW |
0d0512bd VZ |
47 | #include "wx/msw/curico.h" |
48 | #include "wx/msw/curicop.h" | |
2bda0e17 KB |
49 | #endif |
50 | ||
0d0512bd VZ |
51 | // ---------------------------------------------------------------------------- |
52 | // wxWin macros | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
bfbd6dc1 VZ |
55 | IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxCursorBase) |
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | // globals | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // Current cursor, in order to hang on to cursor handle when setting the cursor | |
62 | // globally | |
63 | static wxCursor *gs_globalCursor = NULL; | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // private classes | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | class wxCursorModule : public wxModule | |
70 | { | |
71 | public: | |
72 | virtual bool OnInit() | |
73 | { | |
74 | gs_globalCursor = new wxCursor; | |
75 | ||
76 | return TRUE; | |
77 | } | |
78 | ||
79 | virtual void OnExit() | |
80 | { | |
81 | delete gs_globalCursor; | |
82 | gs_globalCursor = (wxCursor *)NULL; | |
83 | } | |
84 | }; | |
85 | ||
86 | // ============================================================================ | |
87 | // implementation | |
88 | // ============================================================================ | |
2bda0e17 | 89 | |
0d0512bd VZ |
90 | // ---------------------------------------------------------------------------- |
91 | // wxCursorRefData | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | wxCursorRefData::wxCursorRefData() | |
2bda0e17 | 95 | { |
0d0512bd VZ |
96 | m_width = 32; |
97 | m_height = 32; | |
98 | ||
f20ad12c | 99 | m_destroyCursor = TRUE; |
2bda0e17 KB |
100 | } |
101 | ||
0d0512bd | 102 | void wxCursorRefData::Free() |
2bda0e17 | 103 | { |
032af30f VZ |
104 | if ( m_hCursor ) |
105 | { | |
04ef50df | 106 | #ifndef __WXMICROWIN__ |
032af30f VZ |
107 | if ( m_destroyCursor ) |
108 | ::DestroyCursor((HCURSOR)m_hCursor); | |
04ef50df | 109 | #endif |
032af30f VZ |
110 | |
111 | m_hCursor = 0; | |
112 | } | |
2bda0e17 KB |
113 | } |
114 | ||
0d0512bd | 115 | // ---------------------------------------------------------------------------- |
2bda0e17 | 116 | // Cursors |
0d0512bd VZ |
117 | // ---------------------------------------------------------------------------- |
118 | ||
119 | wxCursor::wxCursor() | |
2bda0e17 KB |
120 | { |
121 | } | |
122 | ||
0d0512bd VZ |
123 | wxCursor::wxCursor(const char WXUNUSED(bits)[], |
124 | int WXUNUSED(width), | |
125 | int WXUNUSED(height), | |
126 | int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), | |
127 | const char WXUNUSED(maskBits)[]) | |
2bda0e17 KB |
128 | { |
129 | } | |
130 | ||
0d0512bd VZ |
131 | wxCursor::wxCursor(const wxString& cursor_file, |
132 | long flags, | |
133 | int hotSpotX, int hotSpotY) | |
2bda0e17 | 134 | { |
04ef50df JS |
135 | #ifdef __WXMICROWIN__ |
136 | m_refData = NULL; | |
137 | #else | |
0d0512bd VZ |
138 | wxCursorRefData *refData = new wxCursorRefData; |
139 | m_refData = refData; | |
2bda0e17 | 140 | |
0d0512bd VZ |
141 | if (flags == wxBITMAP_TYPE_CUR_RESOURCE) |
142 | { | |
2f851133 | 143 | #ifdef __WIN95__ |
0d0512bd | 144 | refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, 0); |
2f851133 | 145 | #else |
0d0512bd | 146 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), cursor_file); |
2f851133 | 147 | #endif |
0d0512bd VZ |
148 | } |
149 | else if (flags == wxBITMAP_TYPE_CUR) | |
150 | { | |
2f851133 | 151 | #ifdef __WIN95__ |
0d0512bd | 152 | refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE); |
2f851133 | 153 | #else |
47d67540 | 154 | #if wxUSE_RESOURCE_LOADING_IN_MSW |
0d0512bd | 155 | refData->m_hCursor = (WXHCURSOR) ReadCursorFile(WXSTRINGCAST cursor_file, wxGetInstance(), &refData->m_width, &refData->m_height); |
2f851133 | 156 | #endif |
2bda0e17 | 157 | #endif |
0d0512bd VZ |
158 | } |
159 | else if (flags == wxBITMAP_TYPE_ICO) | |
160 | { | |
47d67540 | 161 | #if wxUSE_RESOURCE_LOADING_IN_MSW |
0d0512bd | 162 | refData->m_hCursor = (WXHCURSOR) IconToCursor(WXSTRINGCAST cursor_file, wxGetInstance(), hotSpotX, hotSpotY, &refData->m_width, &refData->m_height); |
2bda0e17 | 163 | #endif |
0d0512bd VZ |
164 | } |
165 | else if (flags == wxBITMAP_TYPE_BMP) | |
166 | { | |
47d67540 | 167 | #if wxUSE_RESOURCE_LOADING_IN_MSW |
0d0512bd VZ |
168 | HBITMAP hBitmap = 0; |
169 | HPALETTE hPalette = 0; | |
170 | bool success = wxReadDIB(WXSTRINGCAST cursor_file, &hBitmap, &hPalette) != 0; | |
171 | if (!success) | |
172 | return; | |
173 | if (hPalette) | |
174 | DeleteObject(hPalette); | |
175 | POINT pnt; | |
176 | pnt.x = hotSpotX; | |
177 | pnt.y = hotSpotY; | |
178 | refData->m_hCursor = (WXHCURSOR) MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pnt); | |
0d0512bd | 179 | DeleteObject(hBitmap); |
2bda0e17 | 180 | #endif |
0d0512bd VZ |
181 | } |
182 | ||
183 | #if WXWIN_COMPATIBILITY_2 | |
184 | refData->SetOk(); | |
185 | #endif // WXWIN_COMPATIBILITY_2 | |
04ef50df JS |
186 | |
187 | #endif | |
2bda0e17 KB |
188 | } |
189 | ||
190 | // Cursors by stock number | |
debe6624 | 191 | wxCursor::wxCursor(int cursor_type) |
2bda0e17 | 192 | { |
04ef50df JS |
193 | #ifdef __WXMICROWIN__ |
194 | m_refData = NULL; | |
195 | #else | |
0d0512bd VZ |
196 | wxCursorRefData *refData = new wxCursorRefData; |
197 | m_refData = refData; | |
2bda0e17 KB |
198 | |
199 | switch (cursor_type) | |
200 | { | |
83f96286 | 201 | case wxCURSOR_ARROWWAIT: |
788722ac | 202 | #ifndef __WIN16__ |
83f96286 RD |
203 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_APPSTARTING); |
204 | break; | |
788722ac | 205 | #endif |
2bda0e17 | 206 | case wxCURSOR_WAIT: |
0d0512bd | 207 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_WAIT); |
2bda0e17 KB |
208 | break; |
209 | case wxCURSOR_IBEAM: | |
0d0512bd | 210 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_IBEAM); |
2bda0e17 KB |
211 | break; |
212 | case wxCURSOR_CROSS: | |
0d0512bd | 213 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_CROSS); |
2bda0e17 KB |
214 | break; |
215 | case wxCURSOR_SIZENWSE: | |
0d0512bd | 216 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENWSE); |
2bda0e17 KB |
217 | break; |
218 | case wxCURSOR_SIZENESW: | |
0d0512bd | 219 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENESW); |
2bda0e17 KB |
220 | break; |
221 | case wxCURSOR_SIZEWE: | |
0d0512bd | 222 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZEWE); |
2bda0e17 KB |
223 | break; |
224 | case wxCURSOR_SIZENS: | |
0d0512bd | 225 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENS); |
2bda0e17 KB |
226 | break; |
227 | case wxCURSOR_CHAR: | |
228 | { | |
0d0512bd | 229 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW); |
2bda0e17 KB |
230 | break; |
231 | } | |
232 | case wxCURSOR_HAND: | |
233 | { | |
0d0512bd | 234 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND")); |
2bda0e17 KB |
235 | break; |
236 | } | |
237 | case wxCURSOR_BULLSEYE: | |
238 | { | |
0d0512bd | 239 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE")); |
2bda0e17 KB |
240 | break; |
241 | } | |
242 | case wxCURSOR_PENCIL: | |
243 | { | |
0d0512bd | 244 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL")); |
2bda0e17 KB |
245 | break; |
246 | } | |
247 | case wxCURSOR_MAGNIFIER: | |
248 | { | |
0d0512bd | 249 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER")); |
2bda0e17 KB |
250 | break; |
251 | } | |
252 | case wxCURSOR_NO_ENTRY: | |
253 | { | |
0d0512bd | 254 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY")); |
2bda0e17 KB |
255 | break; |
256 | } | |
257 | case wxCURSOR_LEFT_BUTTON: | |
258 | { | |
0d0512bd | 259 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW); |
2bda0e17 KB |
260 | break; |
261 | } | |
262 | case wxCURSOR_RIGHT_BUTTON: | |
263 | { | |
0d0512bd | 264 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW); |
2bda0e17 KB |
265 | break; |
266 | } | |
267 | case wxCURSOR_MIDDLE_BUTTON: | |
268 | { | |
0d0512bd | 269 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW); |
2bda0e17 KB |
270 | break; |
271 | } | |
272 | case wxCURSOR_SIZING: | |
273 | { | |
0d0512bd | 274 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING")); |
2bda0e17 KB |
275 | break; |
276 | } | |
277 | case wxCURSOR_WATCH: | |
278 | { | |
0d0512bd | 279 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH")); |
2bda0e17 KB |
280 | break; |
281 | } | |
282 | case wxCURSOR_SPRAYCAN: | |
283 | { | |
0d0512bd | 284 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER")); |
2bda0e17 KB |
285 | break; |
286 | } | |
287 | case wxCURSOR_PAINT_BRUSH: | |
288 | { | |
0d0512bd | 289 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH")); |
2bda0e17 KB |
290 | break; |
291 | } | |
292 | case wxCURSOR_POINT_LEFT: | |
293 | { | |
0d0512bd | 294 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT")); |
2bda0e17 KB |
295 | break; |
296 | } | |
297 | case wxCURSOR_POINT_RIGHT: | |
298 | { | |
0d0512bd | 299 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT")); |
2bda0e17 KB |
300 | break; |
301 | } | |
302 | case wxCURSOR_QUESTION_ARROW: | |
303 | { | |
b96340e6 | 304 | // refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), wxT("wxCURSOR_QARROW"), IMAGE_CURSOR, 16, 16, LR_MONOCHROME); |
0d0512bd | 305 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW")); |
2bda0e17 KB |
306 | break; |
307 | } | |
308 | case wxCURSOR_BLANK: | |
309 | { | |
0d0512bd | 310 | refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK")); |
2bda0e17 KB |
311 | break; |
312 | } | |
313 | default: | |
314 | case wxCURSOR_ARROW: | |
0d0512bd | 315 | refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW); |
2bda0e17 KB |
316 | break; |
317 | } | |
04ef50df | 318 | #endif |
771a855f VZ |
319 | |
320 | // no need to destroy the stock cursors | |
321 | refData->m_destroyCursor = FALSE; | |
2bda0e17 KB |
322 | } |
323 | ||
0d0512bd | 324 | wxCursor::~wxCursor() |
2bda0e17 | 325 | { |
2bda0e17 KB |
326 | } |
327 | ||
0d0512bd | 328 | // ---------------------------------------------------------------------------- |
2bda0e17 | 329 | // Global cursor setting |
0d0512bd VZ |
330 | // ---------------------------------------------------------------------------- |
331 | ||
bfbd6dc1 | 332 | const wxCursor *wxGetGlobalCursor() |
2bda0e17 | 333 | { |
bfbd6dc1 VZ |
334 | return gs_globalCursor; |
335 | } | |
2bda0e17 | 336 | |
bfbd6dc1 VZ |
337 | void wxSetCursor(const wxCursor& cursor) |
338 | { | |
339 | if ( cursor.Ok() ) | |
6bf57206 | 340 | { |
04ef50df | 341 | #ifndef __WXMICROWIN__ |
bfbd6dc1 | 342 | ::SetCursor(GetHcursorOf(cursor)); |
04ef50df | 343 | #endif |
6bf57206 | 344 | |
bfbd6dc1 VZ |
345 | if ( gs_globalCursor ) |
346 | *gs_globalCursor = cursor; | |
6bf57206 | 347 | } |
2bda0e17 KB |
348 | } |
349 | ||
350 |