]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
we do want arrows even in a read only text ctrl
[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/image.h"
42 #include "wx/msw/private.h"
43 #ifndef __WXMICROWIN__
44 #include "wx/msw/dib.h"
45 #endif
46
47 #if wxUSE_RESOURCE_LOADING_IN_MSW
48 #include "wx/msw/curico.h"
49 #include "wx/msw/curicop.h"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // wxWin macros
54 // ----------------------------------------------------------------------------
55
56 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
57
58 // ----------------------------------------------------------------------------
59 // globals
60 // ----------------------------------------------------------------------------
61
62 // Current cursor, in order to hang on to cursor handle when setting the cursor
63 // globally
64 static wxCursor *gs_globalCursor = NULL;
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 class wxCursorModule : public wxModule
71 {
72 public:
73 virtual bool OnInit()
74 {
75 gs_globalCursor = new wxCursor;
76
77 return TRUE;
78 }
79
80 virtual void OnExit()
81 {
82 delete gs_globalCursor;
83 gs_globalCursor = (wxCursor *)NULL;
84 }
85 };
86
87 // ============================================================================
88 // implementation
89 // ============================================================================
90
91 // ----------------------------------------------------------------------------
92 // wxCursorRefData
93 // ----------------------------------------------------------------------------
94
95 wxCursorRefData::wxCursorRefData()
96 {
97 m_width = 32;
98 m_height = 32;
99
100 m_destroyCursor = TRUE;
101 }
102
103 void wxCursorRefData::Free()
104 {
105 if ( m_hCursor )
106 {
107 #ifndef __WXMICROWIN__
108 if ( m_destroyCursor )
109 ::DestroyCursor((HCURSOR)m_hCursor);
110 #endif
111
112 m_hCursor = 0;
113 }
114 }
115
116 // ----------------------------------------------------------------------------
117 // Cursors
118 // ----------------------------------------------------------------------------
119
120 wxCursor::wxCursor()
121 {
122 }
123
124 wxCursor::wxCursor( const wxImage & image )
125 {
126 //image has to be 32x32
127 wxImage image32 = image.Scale(32,32);
128 unsigned char * rgbBits = image32.GetData();
129 int w = image32.GetWidth() ;
130 int h = image32.GetHeight() ;
131 bool bHasMask = image32.HasMask() ;
132 int imagebitcount = (w*h)/8;
133
134 unsigned char r, g, b ;
135 unsigned char * bits = new unsigned char [imagebitcount];
136 unsigned char * maskBits = new unsigned char [imagebitcount];
137
138 int i,j, i8; unsigned char c, cMask;
139 for (i=0; i<imagebitcount; i++)
140 {
141 bits[i] = 0;
142 i8 = i * 8;
143 //unlike gtk, the pixels go in the opposite order in the bytes
144 cMask = 128;
145 for (j=0; j<8; j++)
146 {
147 // possible overflow if we do the summation first ?
148 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3 ;
149 //if average value is > mid grey
150 if (c>127)
151 bits[i] = bits[i] | cMask ;
152 cMask = cMask / 2 ;
153 }
154 }
155 if (bHasMask)
156 {
157 r = image32.GetMaskRed() ;
158 g = image32.GetMaskGreen() ;
159 b = image32.GetMaskBlue() ;
160
161 for (i=0; i<imagebitcount; i++)
162 {
163 maskBits[i] = 0x0;
164 i8 = i * 8;
165
166 cMask = 128;
167 for (j=0; j<8; j++)
168 {
169 if (rgbBits[(i8+j)*3] == r && rgbBits[(i8+j)*3+1] == g && rgbBits[(i8+j)*3+2] == b)
170 maskBits[i] = maskBits[i] | cMask ;
171 cMask = cMask / 2 ;
172 }
173 }
174 }
175 else
176 {
177 for (i=0; i<imagebitcount; i++)
178 maskBits[i]= 0x0 ;
179 }
180
181 int hotSpotX = image32.GetOptionInt(wxCUR_HOTSPOT_X);
182 int hotSpotY = image32.GetOptionInt(wxCUR_HOTSPOT_Y);
183 if (hotSpotX < 0 || hotSpotX >= w)
184 hotSpotX = 0;
185 if (hotSpotY < 0 || hotSpotY >= h)
186 hotSpotY = 0;
187
188 wxCursorRefData *refData = new wxCursorRefData;
189 m_refData = refData;
190 refData->m_hCursor = (WXHCURSOR) CreateCursor ( wxGetInstance(), hotSpotX, hotSpotY, w, h, /*AND*/ maskBits, /*XOR*/ bits );
191
192 delete [] bits ;
193 delete [] maskBits;
194
195 }
196
197 wxCursor::wxCursor(const char WXUNUSED(bits)[],
198 int WXUNUSED(width),
199 int WXUNUSED(height),
200 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
201 const char WXUNUSED(maskBits)[])
202 {
203 }
204
205 wxCursor::wxCursor(const wxString& cursor_file,
206 long flags,
207 int hotSpotX, int hotSpotY)
208 {
209 #ifdef __WXMICROWIN__
210 m_refData = NULL;
211 #else
212 wxCursorRefData *refData = new wxCursorRefData;
213 m_refData = refData;
214
215 if (flags == wxBITMAP_TYPE_CUR_RESOURCE)
216 {
217 #ifdef __WIN95__
218 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, 0);
219 #else
220 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), cursor_file);
221 #endif
222 }
223 else if (flags == wxBITMAP_TYPE_CUR)
224 {
225 #ifdef __WIN95__
226 refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), cursor_file, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
227 #else
228 #if wxUSE_RESOURCE_LOADING_IN_MSW
229 refData->m_hCursor = (WXHCURSOR) ReadCursorFile(WXSTRINGCAST cursor_file, wxGetInstance(), &refData->m_width, &refData->m_height);
230 #endif
231 #endif
232 }
233 else if (flags == wxBITMAP_TYPE_ICO)
234 {
235 #if wxUSE_RESOURCE_LOADING_IN_MSW
236 refData->m_hCursor = (WXHCURSOR) IconToCursor(WXSTRINGCAST cursor_file, wxGetInstance(), hotSpotX, hotSpotY, &refData->m_width, &refData->m_height);
237 #endif
238 }
239 else if (flags == wxBITMAP_TYPE_BMP)
240 {
241 #if wxUSE_RESOURCE_LOADING_IN_MSW
242 HBITMAP hBitmap = 0;
243 HPALETTE hPalette = 0;
244 bool success = wxReadDIB(WXSTRINGCAST cursor_file, &hBitmap, &hPalette) != 0;
245 if (!success)
246 return;
247 if (hPalette)
248 DeleteObject(hPalette);
249 POINT pnt;
250 pnt.x = hotSpotX;
251 pnt.y = hotSpotY;
252 refData->m_hCursor = (WXHCURSOR) MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pnt);
253 DeleteObject(hBitmap);
254 #endif
255 }
256
257 #if WXWIN_COMPATIBILITY_2
258 refData->SetOk();
259 #endif // WXWIN_COMPATIBILITY_2
260
261 #endif
262 }
263
264 // Cursors by stock number
265 wxCursor::wxCursor(int cursor_type)
266 {
267 #ifdef __WXMICROWIN__
268 m_refData = NULL;
269 #else
270 wxCursorRefData *refData = new wxCursorRefData;
271 m_refData = refData;
272
273 switch (cursor_type)
274 {
275 case wxCURSOR_ARROWWAIT:
276 #ifndef __WIN16__
277 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_APPSTARTING);
278 break;
279 #endif
280 case wxCURSOR_WAIT:
281 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_WAIT);
282 break;
283 case wxCURSOR_IBEAM:
284 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_IBEAM);
285 break;
286 case wxCURSOR_CROSS:
287 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_CROSS);
288 break;
289 case wxCURSOR_SIZENWSE:
290 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENWSE);
291 break;
292 case wxCURSOR_SIZENESW:
293 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENESW);
294 break;
295 case wxCURSOR_SIZEWE:
296 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZEWE);
297 break;
298 case wxCURSOR_SIZENS:
299 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_SIZENS);
300 break;
301 case wxCURSOR_CHAR:
302 {
303 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
304 break;
305 }
306 case wxCURSOR_HAND:
307 {
308 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_HAND"));
309 break;
310 }
311 case wxCURSOR_BULLSEYE:
312 {
313 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BULLSEYE"));
314 break;
315 }
316 case wxCURSOR_PENCIL:
317 {
318 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PENCIL"));
319 break;
320 }
321 case wxCURSOR_MAGNIFIER:
322 {
323 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_MAGNIFIER"));
324 break;
325 }
326 case wxCURSOR_NO_ENTRY:
327 {
328 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_NO_ENTRY"));
329 break;
330 }
331 case wxCURSOR_LEFT_BUTTON:
332 {
333 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
334 break;
335 }
336 case wxCURSOR_RIGHT_BUTTON:
337 {
338 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
339 break;
340 }
341 case wxCURSOR_MIDDLE_BUTTON:
342 {
343 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
344 break;
345 }
346 case wxCURSOR_SIZING:
347 {
348 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_SIZING"));
349 break;
350 }
351 case wxCURSOR_WATCH:
352 {
353 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_WATCH"));
354 break;
355 }
356 case wxCURSOR_SPRAYCAN:
357 {
358 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_ROLLER"));
359 break;
360 }
361 case wxCURSOR_PAINT_BRUSH:
362 {
363 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PBRUSH"));
364 break;
365 }
366 case wxCURSOR_POINT_LEFT:
367 {
368 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PLEFT"));
369 break;
370 }
371 case wxCURSOR_POINT_RIGHT:
372 {
373 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_PRIGHT"));
374 break;
375 }
376 case wxCURSOR_QUESTION_ARROW:
377 {
378 // refData->m_hCursor = (WXHCURSOR) LoadImage(wxGetInstance(), wxT("wxCURSOR_QARROW"), IMAGE_CURSOR, 16, 16, LR_MONOCHROME);
379 // refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_QARROW"));
380 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_HELP);
381 break;
382 }
383 case wxCURSOR_BLANK:
384 {
385 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_BLANK"));
386 break;
387 }
388 case wxCURSOR_RIGHT_ARROW:
389 {
390 refData->m_hCursor = (WXHCURSOR) LoadCursor(wxGetInstance(), wxT("wxCURSOR_RIGHT_ARROW"));
391 break;
392 }
393 default:
394 case wxCURSOR_ARROW:
395 refData->m_hCursor = (WXHCURSOR) LoadCursor((HINSTANCE) NULL, IDC_ARROW);
396 break;
397 }
398
399 // no need to destroy the stock cursors
400 // TODO: check this
401 //m_refData->m_destroyCursor = FALSE;
402 #endif
403 }
404
405 wxCursor::~wxCursor()
406 {
407 }
408
409 // ----------------------------------------------------------------------------
410 // Global cursor setting
411 // ----------------------------------------------------------------------------
412
413 const wxCursor *wxGetGlobalCursor()
414 {
415 return gs_globalCursor;
416 }
417
418 void wxSetCursor(const wxCursor& cursor)
419 {
420 if ( cursor.Ok() )
421 {
422 #ifndef __WXMICROWIN__
423 ::SetCursor(GetHcursorOf(cursor));
424 #endif
425
426 if ( gs_globalCursor )
427 *gs_globalCursor = cursor;
428 }
429 }
430
431