]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/cursor.cpp
a5f0b6618c843a9b47ffe171026cab4c54d7334a
[wxWidgets.git] / src / mac / classic / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "cursor.h"
14 #endif
15
16 #include "wx/defs.h"
17
18 #include "wx/app.h"
19 #include "wx/cursor.h"
20 #include "wx/icon.h"
21 #include "wx/image.h"
22 #include "wx/xpmdecod.h"
23
24 #include "wx/mac/private.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxBitmap)
27
28 const short kwxCursorBullseye = 10 ;
29 const short kwxCursorBlank = 11 ;
30 const short kwxCursorPencil = 12 ;
31 const short kwxCursorMagnifier = 13 ;
32 const short kwxCursorNoEntry = 14 ;
33 const short kwxCursorPaintBrush = 15 ;
34 const short kwxCursorPointRight = 16 ;
35 const short kwxCursorPointLeft = 17 ;
36 const short kwxCursorQuestionArrow = 18 ;
37 const short kwxCursorRightArrow = 19 ;
38 const short kwxCursorSizeNS = 20 ;
39 const short kwxCursorSize = 21 ;
40 const short kwxCursorSizeNESW = 22 ;
41 const short kwxCursorSizeNWSE = 23 ;
42 const short kwxCursorRoller = 24 ;
43
44 wxCursor gMacCurrentCursor ;
45
46 wxCursorRefData::wxCursorRefData()
47 {
48 m_width = 16;
49 m_height = 16;
50 m_hCursor = NULL ;
51 m_disposeHandle = false ;
52 m_releaseHandle = false ;
53 m_isColorCursor = false ;
54 m_themeCursor = -1 ;
55 }
56
57 wxCursorRefData::~wxCursorRefData()
58 {
59 if ( m_isColorCursor )
60 {
61 ::DisposeCCursor( (CCrsrHandle) m_hCursor ) ;
62 }
63 else if ( m_disposeHandle )
64 {
65 ::DisposeHandle( (Handle ) m_hCursor ) ;
66 }
67 else if ( m_releaseHandle )
68 {
69 // we don't release the resource since it may already
70 // be in use again
71 }
72 }
73
74 // Cursors
75 wxCursor::wxCursor()
76 {
77 }
78
79 wxCursor::wxCursor(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height),
80 int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), const char WXUNUSED(maskBits)[])
81 {
82 }
83
84 wxCursor::wxCursor( const wxImage &image )
85 {
86 CreateFromImage( image ) ;
87 }
88
89 wxCursor::wxCursor(const char **bits)
90 {
91 (void) CreateFromXpm(bits);
92 }
93
94 wxCursor::wxCursor(char **bits)
95 {
96 (void) CreateFromXpm((const char **)bits);
97 }
98
99 bool wxCursor::CreateFromXpm(const char **bits)
100 {
101 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid cursor data") )
102 wxXPMDecoder decoder;
103 wxImage img = decoder.ReadData(bits);
104 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid cursor data") )
105 CreateFromImage( img ) ;
106 return TRUE;
107 }
108
109 short GetCTabIndex( CTabHandle colors , RGBColor *col )
110 {
111 short retval = 0 ;
112 unsigned long bestdiff = 0xFFFF ;
113 for ( int i = 0 ; i < (**colors).ctSize ; ++i )
114 {
115 unsigned long diff = abs(col->red - (**colors).ctTable[i].rgb.red ) +
116 abs(col->green - (**colors).ctTable[i].rgb.green ) +
117 abs(col->blue - (**colors).ctTable[i].rgb.blue ) ;
118 if ( diff < bestdiff )
119 {
120 bestdiff = diff ;
121 retval = (**colors).ctTable[i].value ;
122 }
123 }
124 return retval ;
125 }
126
127 void wxCursor::CreateFromImage(const wxImage & image)
128 {
129 m_refData = new wxCursorRefData;
130
131 int w = 16;
132 int h = 16;
133
134 int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
135 int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
136 int image_w = image.GetWidth();
137 int image_h = image.GetHeight();
138
139 wxASSERT_MSG( hotSpotX >= 0 && hotSpotX < image_w &&
140 hotSpotY >= 0 && hotSpotY < image_h,
141 _T("invalid cursor hot spot coordinates") );
142
143 wxImage image16(image); // final image of correct size
144
145 // if image is too small then place it in the center, resize it if too big
146 if ((w > image_w) && (h > image_h))
147 {
148 wxPoint offset((w - image_w)/2, (h - image_h)/2);
149 hotSpotX = hotSpotX + offset.x;
150 hotSpotY = hotSpotY + offset.y;
151
152 image16 = image.Size(wxSize(w, h), offset);
153 }
154 else if ((w != image_w) || (h != image_h))
155 {
156 hotSpotX = int(hotSpotX * double(w) / double(image_w));
157 hotSpotY = int(hotSpotY * double(h) / double(image_h));
158
159 image16 = image.Scale(w, h);
160 }
161
162 unsigned char * rgbBits = image16.GetData();
163 bool bHasMask = image16.HasMask() ;
164
165 #if 0
166 // monochrome implementation
167 M_CURSORDATA->m_hCursor = NewHandle( sizeof( Cursor ) ) ;
168 M_CURSORDATA->m_disposeHandle = true ;
169 HLock( (Handle) M_CURSORDATA->m_hCursor ) ;
170 CursPtr cp = *(CursHandle)M_CURSORDATA->m_hCursor ;
171 memset( cp->data , 0 , sizeof( Bits16 ) ) ;
172 memset( cp->mask , 0 , sizeof( Bits16 ) ) ;
173
174 unsigned char mr = image16.GetMaskRed() ;
175 unsigned char mg = image16.GetMaskGreen() ;
176 unsigned char mb = image16.GetMaskBlue() ;
177 for ( int y = 0 ; y < h ; ++y )
178 {
179 short rowbits = 0 ;
180 short maskbits = 0 ;
181
182 for ( int x = 0 ; x < w ; ++x )
183 {
184 long pos = (y * w + x) * 3;
185
186 unsigned char r = rgbBits[pos] ;
187 unsigned char g = rgbBits[pos+1] ;
188 unsigned char b = rgbBits[pos+2] ;
189 if ( bHasMask && r==mr && g==mg && b==mb )
190 {
191 // masked area, does not appear anywhere
192 }
193 else
194 {
195 if ( (int)r + (int)g + (int)b < 0x0200 )
196 {
197 rowbits |= ( 1 << (15-x) ) ;
198 }
199 maskbits |= ( 1 << (15-x) ) ;
200 }
201 }
202 cp->data[y] = rowbits ;
203 cp->mask[y] = maskbits ;
204 }
205 if ( !bHasMask )
206 {
207 memcpy( cp->mask , cp->data , sizeof( Bits16) ) ;
208 }
209 cp->hotSpot.h = hotSpotX ;
210 cp->hotSpot.v = hotSpotY ;
211 HUnlock( (Handle) M_CURSORDATA->m_hCursor ) ;
212 #else
213 PixMapHandle pm = (PixMapHandle) NewHandleClear( sizeof (PixMap)) ;
214 short extent = 16 ;
215 short bytesPerPixel = 1 ;
216 short depth = 8 ;
217 Rect bounds = { 0 , 0 , extent , extent } ;
218 CCrsrHandle ch = (CCrsrHandle) NewHandleClear ( sizeof( CCrsr ) ) ;
219 CTabHandle newColors = GetCTable( 8 ) ;
220 HandToHand((Handle *) &newColors);
221 // set the values to the indices
222 for ( int i = 0 ; i < (**newColors).ctSize ; ++i )
223 {
224 (**newColors).ctTable[i].value = i ;
225 }
226 HLock( (Handle) ch) ;
227 (**ch).crsrType = 0x8001 ; // color cursors
228 (**ch).crsrMap = pm ;
229 short bytesPerRow = bytesPerPixel * extent ;
230
231 (**pm).baseAddr = 0;
232 (**pm).rowBytes = bytesPerRow | 0x8000;
233 (**pm).bounds = bounds;
234 (**pm).pmVersion = 0;
235 (**pm).packType = 0;
236 (**pm).packSize = 0;
237 (**pm).hRes = 0x00480000; /* 72 DPI default res */
238 (**pm).vRes = 0x00480000; /* 72 DPI default res */
239 (**pm).pixelSize = depth;
240 (**pm).pixelType = 0;
241 (**pm).cmpCount = 1;
242 (**pm).cmpSize = depth;
243 (**pm).pmTable = newColors;
244
245 (**ch).crsrData = NewHandleClear( extent * bytesPerRow ) ;
246 (**ch).crsrXData = NULL ;
247 (**ch).crsrXValid = 0;
248 (**ch).crsrXHandle = NULL;
249
250 (**ch).crsrHotSpot.h = hotSpotX ;
251 (**ch).crsrHotSpot.v = hotSpotY ;
252 (**ch).crsrXTable = NULL ;
253 (**ch).crsrID = GetCTSeed() ;
254
255 memset( (**ch).crsr1Data , 0 , sizeof( Bits16 ) ) ;
256 memset( (**ch).crsrMask , 0 , sizeof( Bits16 ) ) ;
257
258 unsigned char mr = image16.GetMaskRed() ;
259 unsigned char mg = image16.GetMaskGreen() ;
260 unsigned char mb = image16.GetMaskBlue() ;
261 for ( int y = 0 ; y < h ; ++y )
262 {
263 short rowbits = 0 ;
264 short maskbits = 0 ;
265
266 for ( int x = 0 ; x < w ; ++x )
267 {
268 long pos = (y * w + x) * 3;
269
270 unsigned char r = rgbBits[pos] ;
271 unsigned char g = rgbBits[pos+1] ;
272 unsigned char b = rgbBits[pos+2] ;
273 RGBColor col = { 0xFFFF ,0xFFFF, 0xFFFF } ;
274
275 if ( bHasMask && r==mr && g==mg && b==mb )
276 {
277 // masked area, does not appear anywhere
278 }
279 else
280 {
281 if ( (int)r + (int)g + (int)b < 0x0200 )
282 {
283 rowbits |= ( 1 << (15-x) ) ;
284 }
285 maskbits |= ( 1 << (15-x) ) ;
286
287 col = *((RGBColor*) wxColor( r , g , b ).GetPixel()) ;
288 }
289 *((*(**ch).crsrData) + y * bytesPerRow + x) =
290 GetCTabIndex( newColors , &col) ;
291 }
292 (**ch).crsr1Data[y] = rowbits ;
293 (**ch).crsrMask[y] = maskbits ;
294 }
295 if ( !bHasMask )
296 {
297 memcpy( (**ch).crsrMask , (**ch).crsr1Data , sizeof( Bits16) ) ;
298 }
299
300 HUnlock((Handle) ch) ;
301 M_CURSORDATA->m_hCursor = ch ;
302 M_CURSORDATA->m_isColorCursor = true ;
303 #endif
304 }
305
306 wxCursor::wxCursor(const wxString& cursor_file, long flags, int hotSpotX, int hotSpotY)
307 {
308 m_refData = new wxCursorRefData;
309 if ( flags == wxBITMAP_TYPE_MACCURSOR_RESOURCE )
310 {
311 Str255 theName ;
312 wxMacStringToPascal( cursor_file , theName ) ;
313
314 wxStAppResource resload ;
315 Handle resHandle = ::GetNamedResource( 'crsr' , theName ) ;
316 if ( resHandle )
317 {
318 short theId = -1 ;
319 OSType theType ;
320 GetResInfo( resHandle , &theId , &theType , theName ) ;
321 ReleaseResource( resHandle ) ;
322 M_CURSORDATA->m_hCursor = GetCCursor( theId ) ;
323 if ( M_CURSORDATA->m_hCursor )
324 M_CURSORDATA->m_isColorCursor = true ;
325 }
326 else
327 {
328 Handle resHandle = ::GetNamedResource( 'CURS' , theName ) ;
329 if ( resHandle )
330 {
331 short theId = -1 ;
332 OSType theType ;
333 GetResInfo( resHandle , &theId , &theType , theName ) ;
334 ReleaseResource( resHandle ) ;
335 M_CURSORDATA->m_hCursor = GetCursor( theId ) ;
336 if ( M_CURSORDATA->m_hCursor )
337 M_CURSORDATA->m_releaseHandle = true ;
338 }
339 }
340 }
341 else
342 {
343 wxImage image ;
344 image.LoadFile( cursor_file , flags ) ;
345 if( image.Ok() )
346 {
347 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X,hotSpotX ) ;
348 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y,hotSpotY ) ;
349 delete m_refData ;
350 CreateFromImage(image) ;
351 }
352 }
353 }
354
355 // Cursors by stock number
356 wxCursor::wxCursor(int cursor_type)
357 {
358 m_refData = new wxCursorRefData;
359
360 switch (cursor_type)
361 {
362 case wxCURSOR_COPY_ARROW:
363 M_CURSORDATA->m_themeCursor = kThemeCopyArrowCursor ;
364 break;
365 case wxCURSOR_WAIT:
366 M_CURSORDATA->m_themeCursor = kThemeWatchCursor ;
367 break;
368 case wxCURSOR_IBEAM:
369 M_CURSORDATA->m_themeCursor = kThemeIBeamCursor ;
370 break;
371 case wxCURSOR_CROSS:
372 M_CURSORDATA->m_themeCursor = kThemeCrossCursor;
373 break;
374 case wxCURSOR_SIZENWSE:
375 {
376 wxStAppResource resload ;
377 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNWSE);
378 }
379 break;
380 case wxCURSOR_SIZENESW:
381 {
382 wxStAppResource resload ;
383 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNESW);
384 }
385 break;
386 case wxCURSOR_SIZEWE:
387 {
388 M_CURSORDATA->m_themeCursor = kThemeResizeLeftRightCursor;
389 }
390 break;
391 case wxCURSOR_SIZENS:
392 {
393 wxStAppResource resload ;
394 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSizeNS);
395 }
396 break;
397 case wxCURSOR_SIZING:
398 {
399 wxStAppResource resload ;
400 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorSize);
401 }
402 break;
403 case wxCURSOR_HAND:
404 {
405 M_CURSORDATA->m_themeCursor = kThemePointingHandCursor;
406 }
407 break;
408 case wxCURSOR_BULLSEYE:
409 {
410 wxStAppResource resload ;
411 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorBullseye);
412 }
413 break;
414 case wxCURSOR_PENCIL:
415 {
416 wxStAppResource resload ;
417 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPencil);
418 }
419 break;
420 case wxCURSOR_MAGNIFIER:
421 {
422 wxStAppResource resload ;
423 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorMagnifier);
424 }
425 break;
426 case wxCURSOR_NO_ENTRY:
427 {
428 wxStAppResource resload ;
429 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorNoEntry);
430 }
431 break;
432 case wxCURSOR_WATCH:
433 {
434 M_CURSORDATA->m_themeCursor = kThemeWatchCursor;
435 break;
436 }
437 case wxCURSOR_PAINT_BRUSH:
438 {
439 wxStAppResource resload ;
440 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPaintBrush);
441 break;
442 }
443 case wxCURSOR_POINT_LEFT:
444 {
445 wxStAppResource resload ;
446 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPointLeft);
447 break;
448 }
449 case wxCURSOR_POINT_RIGHT:
450 {
451 wxStAppResource resload ;
452 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorPointRight);
453 break;
454 }
455 case wxCURSOR_QUESTION_ARROW:
456 {
457 wxStAppResource resload ;
458 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorQuestionArrow);
459 break;
460 }
461 case wxCURSOR_BLANK:
462 {
463 wxStAppResource resload ;
464 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorBlank);
465 break;
466 }
467 case wxCURSOR_RIGHT_ARROW:
468 {
469 wxStAppResource resload ;
470 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorRightArrow);
471 break;
472 }
473 case wxCURSOR_SPRAYCAN:
474 {
475 wxStAppResource resload ;
476 M_CURSORDATA->m_hCursor = ::GetCursor(kwxCursorRoller);
477 break;
478 }
479 case wxCURSOR_CHAR:
480 case wxCURSOR_ARROW:
481 case wxCURSOR_LEFT_BUTTON:
482 case wxCURSOR_RIGHT_BUTTON:
483 case wxCURSOR_MIDDLE_BUTTON:
484 default:
485 M_CURSORDATA->m_themeCursor = kThemeArrowCursor ;
486 break;
487 }
488 if ( M_CURSORDATA->m_themeCursor == -1 )
489 M_CURSORDATA->m_releaseHandle = true ;
490 }
491
492 void wxCursor::MacInstall() const
493 {
494 gMacCurrentCursor = *this ;
495 if ( m_refData && M_CURSORDATA->m_themeCursor != -1 )
496 {
497 SetThemeCursor( M_CURSORDATA->m_themeCursor ) ;
498 }
499 else if ( m_refData && M_CURSORDATA->m_hCursor )
500 {
501 if ( M_CURSORDATA->m_isColorCursor )
502 ::SetCCursor( (CCrsrHandle) M_CURSORDATA->m_hCursor ) ;
503 else
504 ::SetCursor( * (CursHandle) M_CURSORDATA->m_hCursor ) ;
505 }
506 else
507 {
508 SetThemeCursor( kThemeArrowCursor ) ;
509 }
510 }
511
512 wxCursor::~wxCursor()
513 {
514 }
515
516 // Global cursor setting
517 void wxSetCursor(const wxCursor& cursor)
518 {
519 cursor.MacInstall() ;
520 }
521
522