]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
Some doc tweaks
[wxWidgets.git] / src / msw / cursor.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "cursor.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/list.h"
33 #include "wx/utils.h"
34 #include "wx/app.h"
35 #include "wx/bitmap.h"
36 #include "wx/icon.h"
37 #include "wx/cursor.h"
38 #endif
39
40 #include "wx/module.h"
41 #include "wx/msw/private.h"
42 #ifndef __WXMICROWIN__
43 #include "wx/msw/dib.h"
44 #endif
45
46 #if wxUSE_RESOURCE_LOADING_IN_MSW
47 #include "wx/msw/curico.h"
48 #include "wx/msw/curicop.h"
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // wxWin macros
53 // ----------------------------------------------------------------------------
54
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 // ============================================================================
89
90 // ----------------------------------------------------------------------------
91 // wxCursorRefData
92 // ----------------------------------------------------------------------------
93
94 wxCursorRefData::wxCursorRefData()
95 {
96 m_width = 32;
97 m_height = 32;
98
99 m_destroyCursor = TRUE;
100 }
101
102 void wxCursorRefData::Free()
103 {
104 if ( m_hCursor )
105 {
106 #ifndef __WXMICROWIN__
107 if ( m_destroyCursor )
108 ::DestroyCursor((HCURSOR)m_hCursor);
109 #endif
110
111 m_hCursor = 0;
112 }
113 }
114
115 // ----------------------------------------------------------------------------
116 // Cursors
117 // ----------------------------------------------------------------------------
118
119 wxCursor::wxCursor()
120 {
121 }
122
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)[])
128 {
129 }
130
131 wxCursor::wxCursor(const wxString& cursor_file,
132 long flags,
133 int hotSpotX, int hotSpotY)
134 {
135 #ifdef __WXMICROWIN__
136 m_refData = NULL;
137 #else
138 wxCursorRefData *refData = new wxCursorRefData;
139 m_refData = refData;
140
141 if (flags == wxBITMAP_TYPE_CUR_RESOURCE)
142 {
143 #ifdef __WIN95__
144 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, 0);
145 #else
146 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), cursor_file);
147 #endif
148 }
149 else if (flags == wxBITMAP_TYPE_CUR)
150 {
151 #ifdef __WIN95__
152 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
153 #else
154 #if wxUSE_RESOURCE_LOADING_IN_MSW
155 refData->m_hCursor = (WXHCURSOR) ReadCursorFile(WXSTRINGCAST cursor_file, wxGetInstance(), &refData->m_width, &refData->m_height);
156 #endif
157 #endif
158 }
159 else if (flags == wxBITMAP_TYPE_ICO)
160 {
161 #if wxUSE_RESOURCE_LOADING_IN_MSW
162 refData->m_hCursor = (WXHCURSOR) IconToCursor(WXSTRINGCAST cursor_file, wxGetInstance(), hotSpotX, hotSpotY, &refData->m_width, &refData->m_height);
163 #endif
164 }
165 else if (flags == wxBITMAP_TYPE_BMP)
166 {
167 #if wxUSE_RESOURCE_LOADING_IN_MSW
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);
179 DeleteObject(hBitmap);
180 #endif
181 }
182
183 #if WXWIN_COMPATIBILITY_2
184 refData->SetOk();
185 #endif // WXWIN_COMPATIBILITY_2
186
187 #endif
188 }
189
190 // Cursors by stock number
191 wxCursor::wxCursor(int cursor_type)
192 {
193 #ifdef __WXMICROWIN__
194 m_refData = NULL;
195 #else
196 wxCursorRefData *refData = new wxCursorRefData;
197 m_refData = refData;
198
199 switch (cursor_type)
200 {
201 case wxCURSOR_ARROWWAIT:
202 #ifndef __WIN16__
203 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_APPSTARTING);
204 break;
205 #endif
206 case wxCURSOR_WAIT:
207 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_WAIT);
208 break;
209 case wxCURSOR_IBEAM:
210 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_IBEAM);
211 break;
212 case wxCURSOR_CROSS:
213 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_CROSS);
214 break;
215 case wxCURSOR_SIZENWSE:
216 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENWSE);
217 break;
218 case wxCURSOR_SIZENESW:
219 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENESW);
220 break;
221 case wxCURSOR_SIZEWE:
222 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZEWE);
223 break;
224 case wxCURSOR_SIZENS:
225 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENS);
226 break;
227 case wxCURSOR_CHAR:
228 {
229 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
230 break;
231 }
232 case wxCURSOR_HAND:
233 {
234 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
235 break;
236 }
237 case wxCURSOR_BULLSEYE:
238 {
239 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
240 break;
241 }
242 case wxCURSOR_PENCIL:
243 {
244 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
245 break;
246 }
247 case wxCURSOR_MAGNIFIER:
248 {
249 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
250 break;
251 }
252 case wxCURSOR_NO_ENTRY:
253 {
254 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
255 break;
256 }
257 case wxCURSOR_LEFT_BUTTON:
258 {
259 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
260 break;
261 }
262 case wxCURSOR_RIGHT_BUTTON:
263 {
264 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
265 break;
266 }
267 case wxCURSOR_MIDDLE_BUTTON:
268 {
269 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
270 break;
271 }
272 case wxCURSOR_SIZING:
273 {
274 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
275 break;
276 }
277 case wxCURSOR_WATCH:
278 {
279 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
280 break;
281 }
282 case wxCURSOR_SPRAYCAN:
283 {
284 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
285 break;
286 }
287 case wxCURSOR_PAINT_BRUSH:
288 {
289 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
290 break;
291 }
292 case wxCURSOR_POINT_LEFT:
293 {
294 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
295 break;
296 }
297 case wxCURSOR_POINT_RIGHT:
298 {
299 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
300 break;
301 }
302 case wxCURSOR_QUESTION_ARROW:
303 {
304 // refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), wxT("wxCURSOR_QARROW"), IMAGE_CURSOR, 16, 16, LR_MONOCHROME);
305 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
306 break;
307 }
308 case wxCURSOR_BLANK:
309 {
310 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
311 break;
312 }
313 default:
314 case wxCURSOR_ARROW:
315 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
316 break;
317 }
318
319 // no need to destroy the stock cursors
320 // TODO: check this
321 //m_refData->m_destroyCursor = FALSE;
322 #endif
323 }
324
325 wxCursor::~wxCursor()
326 {
327 }
328
329 // ----------------------------------------------------------------------------
330 // Global cursor setting
331 // ----------------------------------------------------------------------------
332
333 const wxCursor *wxGetGlobalCursor()
334 {
335 return gs_globalCursor;
336 }
337
338 void wxSetCursor(const wxCursor& cursor)
339 {
340 if ( cursor.Ok() )
341 {
342 #ifndef __WXMICROWIN__
343 ::SetCursor(GetHcursorOf(cursor));
344 #endif
345
346 if ( gs_globalCursor )
347 *gs_globalCursor = cursor;
348 }
349 }
350
351