]> git.saurik.com Git - wxWidgets.git/blob - src/msw/cursor.cpp
compilation fix for WXWIN_COMPATIBILITY_2 == 1
[wxWidgets.git] / src / msw / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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) 1997-2003 Julian Smart and Vadim Zeitlin
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/ptr_scpd.h"
41
42 #include "wx/module.h"
43 #include "wx/image.h"
44 #include "wx/msw/private.h"
45
46 #ifndef __WXMICROWIN__
47 #include "wx/msw/dib.h"
48 #endif
49
50 #if wxUSE_RESOURCE_LOADING_IN_MSW
51 #include "wx/msw/curico.h"
52 #include "wx/msw/curicop.h"
53 #endif
54
55 // define functions missing in MicroWin
56 #ifdef __WXMICROWIN__
57 static inline void DestroyCursor(HCURSOR) { }
58 static inline void SetCursor(HCURSOR) { }
59 #endif // __WXMICROWIN__
60
61 // ----------------------------------------------------------------------------
62 // private classes
63 // ----------------------------------------------------------------------------
64
65 class WXDLLEXPORT wxCursorRefData : public wxGDIImageRefData
66 {
67 public:
68 // the second parameter is used to tell us to delete the cursor when we're
69 // done with it (normally we shouldn't call DestroyCursor() this is why it
70 // doesn't happen by default)
71 wxCursorRefData(HCURSOR hcursor = 0, bool takeOwnership = false);
72
73 virtual ~wxCursorRefData() { Free(); }
74
75 virtual void Free();
76
77
78 // return the size of the standard cursor: notice that the system only
79 // supports the cursors of this size
80 static wxCoord GetStandardWidth();
81 static wxCoord GetStandardHeight();
82
83 private:
84 bool m_destroyCursor;
85
86 // standard cursor size, computed on first use
87 static wxSize ms_sizeStd;
88 };
89
90 // ----------------------------------------------------------------------------
91 // wxWin macros
92 // ----------------------------------------------------------------------------
93
94 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
95
96 // ----------------------------------------------------------------------------
97 // globals
98 // ----------------------------------------------------------------------------
99
100 // Current cursor, in order to hang on to cursor handle when setting the cursor
101 // globally
102 static wxCursor *gs_globalCursor = NULL;
103
104 // ----------------------------------------------------------------------------
105 // private classes
106 // ----------------------------------------------------------------------------
107
108 class wxCursorModule : public wxModule
109 {
110 public:
111 virtual bool OnInit()
112 {
113 gs_globalCursor = new wxCursor;
114
115 return TRUE;
116 }
117
118 virtual void OnExit()
119 {
120 delete gs_globalCursor;
121 gs_globalCursor = (wxCursor *)NULL;
122 }
123 };
124
125 // ----------------------------------------------------------------------------
126 // other types
127 // ----------------------------------------------------------------------------
128
129 wxDECLARE_SCOPED_ARRAY(unsigned char, ByteArray);
130 wxDEFINE_SCOPED_ARRAY(unsigned char, ByteArray);
131
132 // ============================================================================
133 // implementation
134 // ============================================================================
135
136 // ----------------------------------------------------------------------------
137 // wxCursorRefData
138 // ----------------------------------------------------------------------------
139
140 wxSize wxCursorRefData::ms_sizeStd;
141
142 wxCoord wxCursorRefData::GetStandardWidth()
143 {
144 if ( !ms_sizeStd.x )
145 ms_sizeStd.x = wxSystemSettings::GetMetric(wxSYS_CURSOR_X);
146
147 return ms_sizeStd.x;
148 }
149
150 wxCoord wxCursorRefData::GetStandardHeight()
151 {
152 if ( !ms_sizeStd.y )
153 ms_sizeStd.y = wxSystemSettings::GetMetric(wxSYS_CURSOR_Y);
154
155 return ms_sizeStd.y;
156 }
157
158 wxCursorRefData::wxCursorRefData(HCURSOR hcursor, bool destroy)
159 {
160 m_hCursor = (WXHCURSOR)hcursor;
161
162 if ( m_hCursor )
163 {
164 m_width = GetStandardWidth();
165 m_height = GetStandardHeight();
166 }
167
168 m_destroyCursor = destroy;
169 }
170
171 void wxCursorRefData::Free()
172 {
173 if ( m_hCursor )
174 {
175 if ( m_destroyCursor )
176 ::DestroyCursor((HCURSOR)m_hCursor);
177
178 m_hCursor = 0;
179 }
180 }
181
182 // ----------------------------------------------------------------------------
183 // Cursors
184 // ----------------------------------------------------------------------------
185
186 wxCursor::wxCursor()
187 {
188 }
189
190 wxCursor::wxCursor(const wxImage& image)
191 {
192 // image has to be of the standard cursor size, otherwise we won't be able
193 // to create it
194 const int w = wxCursorRefData::GetStandardWidth();
195 const int h = wxCursorRefData::GetStandardHeight();
196
197 wxImage image32 = image.Scale(w, h);
198
199 const int imagebitcount = (w*h)/8;
200
201 ByteArray bits(new unsigned char [imagebitcount]),
202 maskBits(new unsigned char [imagebitcount]);
203
204 int i, j, i8;
205 unsigned char c, cMask;
206
207 const unsigned char * const rgbBits = image32.GetData();
208
209 // first create the XOR mask
210 for ( i = 0; i < imagebitcount; i++ )
211 {
212 bits[i] = 0;
213 i8 = i * 8;
214 // unlike gtk, the pixels go in the opposite order in the bytes
215 cMask = 128;
216 for ( j = 0; j < 8; j++ )
217 {
218 // possible overflow if we do the summation first ?
219 c = rgbBits[(i8+j)*3]/3 +
220 rgbBits[(i8+j)*3+1]/3 +
221 rgbBits[(i8+j)*3+2]/3;
222
223 // if average value is > mid grey
224 if ( c > 127 )
225 bits[i] = bits[i] | cMask;
226 cMask = cMask / 2;
227 }
228 }
229
230 // now the AND one
231 if ( image32.HasMask() )
232 {
233 unsigned char r = image32.GetMaskRed(),
234 g = image32.GetMaskGreen(),
235 b = image32.GetMaskBlue();
236
237 for ( i = 0; i < imagebitcount; i++ )
238 {
239 maskBits[i] = 0x0;
240 i8 = i * 8;
241
242 cMask = 128;
243 for ( j = 0; j < 8; j++ )
244 {
245 if ( rgbBits[(i8+j)*3] == r &&
246 rgbBits[(i8+j)*3+1] == g &&
247 rgbBits[(i8+j)*3+2] == b )
248 {
249 maskBits[i] = maskBits[i] | cMask;
250 }
251
252 cMask = cMask / 2;
253 }
254 }
255 }
256 else // no mask in the image
257 {
258 memset(maskBits.get(), 0, sizeof(unsigned char)*imagebitcount);
259 }
260
261 // determine where should the cursors hot spot be
262 int hotSpotX = image32.GetOptionInt(wxCUR_HOTSPOT_X);
263 int hotSpotY = image32.GetOptionInt(wxCUR_HOTSPOT_Y);
264 if (hotSpotX < 0 || hotSpotX >= w)
265 hotSpotX = 0;
266 if (hotSpotY < 0 || hotSpotY >= h)
267 hotSpotY = 0;
268
269 // do create cursor now
270 HCURSOR hcursor = ::CreateCursor
271 (
272 wxGetInstance(),
273 hotSpotX, hotSpotY,
274 w, h,
275 /* AND */ maskBits.get(),
276 /* XOR */ bits.get()
277 );
278
279 if ( !hcursor )
280 {
281 wxLogLastError(_T("CreateCursor"));
282 }
283 else
284 {
285 m_refData = new wxCursorRefData(hcursor, true /* delete it */);
286 }
287 }
288
289 wxCursor::wxCursor(const char WXUNUSED(bits)[],
290 int WXUNUSED(width),
291 int WXUNUSED(height),
292 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY),
293 const char WXUNUSED(maskBits)[])
294 {
295 }
296
297 // MicroWin doesn't have support needed for the other ctors
298 #ifdef __WXMICROWIN__
299
300 wxCursor::wxCursor(const wxString& WXUNUSED(filename),
301 long WXUNUSED(kind),
302 int WXUNUSED(hotSpotX),
303 int WXUNUSED(hotSpotY))
304 {
305 }
306
307 wxCursor::wxCursor(int WXUNUSED(cursor_type))
308 {
309 }
310
311 #else // !__WXMICROWIN__
312
313 wxCursor::wxCursor(const wxString& filename,
314 long kind,
315 int hotSpotX,
316 int hotSpotY)
317 {
318 HCURSOR hcursor;
319 switch ( kind )
320 {
321 case wxBITMAP_TYPE_CUR_RESOURCE:
322 hcursor = ::LoadCursor(wxGetInstance(), filename);
323 break;
324
325 case wxBITMAP_TYPE_CUR:
326 hcursor = ::LoadCursorFromFile(filename);
327 break;
328
329 #if wxUSE_RESOURCE_LOADING_IN_MSW
330 case wxBITMAP_TYPE_ICO:
331 hcursor = IconToCursor((wxChar *)filename.c_str(),
332 wxGetInstance(),
333 hotSpotX, hotSpotY,
334 NULL, NULL);
335 break;
336
337 case wxBITMAP_TYPE_BMP:
338 {
339 HBITMAP hBitmap = 0;
340 HPALETTE hPalette = 0;
341 if ( wxReadDIB((wxChar *)filename.c_str(), &hBitmap, &hPalette) )
342 {
343 if (hPalette)
344 DeleteObject(hPalette);
345
346 POINT pt;
347 pt.x = hotSpotX;
348 pt.y = hotSpotY;
349 hcursor = MakeCursorFromBitmap(wxGetInstance(), hBitmap, &pt);
350 DeleteObject(hBitmap);
351 }
352 else
353 {
354 hcursor = NULL;
355 }
356 }
357 break;
358 #endif // wxUSE_RESOURCE_LOADING_IN_MSW
359
360 default:
361 wxFAIL_MSG( _T("unknown cursor resource type") );
362
363 hcursor = NULL;
364 }
365
366 if ( hcursor )
367 {
368 m_refData = new wxCursorRefData(hcursor, true /* delete it later */);
369
370 #if WXWIN_COMPATIBILITY_2
371 ((wxCursorRefData *)m_refData)->SetOk();
372 #endif // WXWIN_COMPATIBILITY_2
373 }
374 }
375
376 // Cursors by stock number
377 wxCursor::wxCursor(int idCursor)
378 {
379 // all wxWindows standard cursors
380 static const struct StdCursor
381 {
382 // is this a standard Windows cursor?
383 bool isStd;
384
385 // the cursor name or id
386 LPCTSTR name;
387 } stdCursors[] =
388 {
389 { true, NULL }, // wxCURSOR_NONE
390 { true, IDC_ARROW }, // wxCURSOR_ARROW
391 { false, _T("wxCURSOR_RIGHT_ARROW") }, // wxCURSOR_RIGHT_ARROW
392 { false, _T("wxCURSOR_BULLSEYE") }, // wxCURSOR_BULLSEYE
393 { true, IDC_ARROW }, // wxCURSOR_CHAR
394 { true, IDC_CROSS }, // wxCURSOR_CROSS
395 { false, _T("wxCURSOR_HAND") }, // wxCURSOR_HAND
396 { true, IDC_IBEAM }, // wxCURSOR_IBEAM
397 { true, IDC_ARROW }, // wxCURSOR_LEFT_BUTTON
398 { false, _T("wxCURSOR_MAGNIFIER") }, // wxCURSOR_MAGNIFIER
399 { true, IDC_ARROW }, // wxCURSOR_MIDDLE_BUTTON
400 { true, IDC_NO }, // wxCURSOR_NO_ENTRY
401 { false, _T("wxCURSOR_PAINT_BRUSH") }, // wxCURSOR_PAINT_BRUSH
402 { false, _T("wxCURSOR_PENCIL") }, // wxCURSOR_PENCIL
403 { false, _T("wxCURSOR_POINT_LEFT") }, // wxCURSOR_POINT_LEFT
404 { false, _T("wxCURSOR_POINT_RIGHT") }, // wxCURSOR_POINT_RIGHT
405 { true, IDC_HELP }, // wxCURSOR_QUESTION_ARROW
406 { true, IDC_ARROW }, // wxCURSOR_RIGHT_BUTTON
407 { true, IDC_SIZENESW }, // wxCURSOR_SIZENESW
408 { true, IDC_SIZENS }, // wxCURSOR_SIZENS
409 { true, IDC_SIZENWSE }, // wxCURSOR_SIZENWSE
410 { true, IDC_SIZEWE }, // wxCURSOR_SIZEWE
411 { true, IDC_SIZEALL }, // wxCURSOR_SIZING
412 { false, _T("wxCURSOR_SPRAYCAN") }, // wxCURSOR_SPRAYCAN
413 { true, IDC_WAIT }, // wxCURSOR_WAIT
414 { true, IDC_WAIT }, // wxCURSOR_WATCH
415 { false, _T("wxCURSOR_BLANK") }, // wxCURSOR_BLANK
416 { true, IDC_APPSTARTING }, // wxCURSOR_ARROWWAIT
417
418 // no entry for wxCURSOR_MAX
419 };
420
421 wxCOMPILE_TIME_ASSERT( WXSIZEOF(stdCursors) == wxCURSOR_MAX,
422 CursorsIdArrayMismatch );
423
424 wxCHECK_RET( idCursor > 0 && (size_t)idCursor < WXSIZEOF(stdCursors),
425 _T("invalid cursor id in wxCursor() ctor") );
426
427 const StdCursor& stdCursor = stdCursors[idCursor];
428
429 HCURSOR hcursor = ::LoadCursor(stdCursor.isStd ? NULL : wxGetInstance(),
430 stdCursor.name);
431
432 if ( !hcursor )
433 {
434 wxLogLastError(_T("LoadCursor"));
435 }
436 else
437 {
438 m_refData = new wxCursorRefData(hcursor);
439 }
440 }
441
442 #endif // __WXMICROWIN__/!__WXMICROWIN__
443
444 wxCursor::~wxCursor()
445 {
446 }
447
448 // ----------------------------------------------------------------------------
449 // other wxCursor functions
450 // ----------------------------------------------------------------------------
451
452 bool wxCursor::operator==(const wxCursor& cursor) const
453 {
454 if ( !m_refData )
455 return !cursor.m_refData;
456
457 return cursor.m_refData &&
458 ((wxCursorRefData *)m_refData)->m_hCursor ==
459 ((wxCursorRefData *)cursor.m_refData)->m_hCursor;
460 }
461
462 wxGDIImageRefData *wxCursor::CreateData() const
463 {
464 return new wxCursorRefData;
465 }
466
467 // ----------------------------------------------------------------------------
468 // Global cursor setting
469 // ----------------------------------------------------------------------------
470
471 const wxCursor *wxGetGlobalCursor()
472 {
473 return gs_globalCursor;
474 }
475
476 void wxSetCursor(const wxCursor& cursor)
477 {
478 if ( cursor.Ok() )
479 {
480 ::SetCursor(GetHcursorOf(cursor));
481
482 if ( gs_globalCursor )
483 *gs_globalCursor = cursor;
484 }
485 }
486
487