1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/cursor.h"
22 #include "wx/xpmdecod.h"
24 #include "wx/mac/private.h"
27 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
30 class WXDLLEXPORT wxCursorRefData
: public wxGDIRefData
32 friend class wxCursor
;
34 DECLARE_NO_COPY_CLASS(wxCursorRefData
)
38 virtual ~wxCursorRefData();
42 WX_NSCursor m_hCursor
;
52 #define M_CURSORDATA wx_static_cast(wxCursorRefData*, m_refData)
54 ClassicCursor gMacCursors
[kwxCursorLast
+1] =
58 {0x0000, 0x03E0, 0x0630, 0x0808, 0x1004, 0x31C6, 0x2362, 0x2222,
59 0x2362, 0x31C6, 0x1004, 0x0808, 0x0630, 0x03E0, 0x0000, 0x0000},
60 {0x0000, 0x03E0, 0x07F0, 0x0FF8, 0x1FFC, 0x3FFE, 0x3FFE, 0x3FFE,
61 0x3FFE, 0x3FFE, 0x1FFC, 0x0FF8, 0x07F0, 0x03E0, 0x0000, 0x0000},
66 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
67 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
68 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
69 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
74 {0x00F0, 0x0088, 0x0108, 0x0190, 0x0270, 0x0220, 0x0440, 0x0440,
75 0x0880, 0x0880, 0x1100, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000},
76 {0x00F0, 0x00F8, 0x01F8, 0x01F0, 0x03F0, 0x03E0, 0x07C0, 0x07C0,
77 0x0F80, 0x0F80, 0x1F00, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000},
82 {0x0000, 0x1E00, 0x2100, 0x4080, 0x4080, 0x4080, 0x4080, 0x2180,
83 0x1FC0, 0x00E0, 0x0070, 0x0038, 0x001C, 0x000E, 0x0006, 0x0000},
84 {0x3F00, 0x7F80, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0x7FC0,
85 0x3FE0, 0x1FF0, 0x00F8, 0x007C, 0x003E, 0x001F, 0x000F, 0x0007},
90 {0x0000, 0x07E0, 0x1FF0, 0x3838, 0x3C0C, 0x6E0E, 0x6706, 0x6386,
91 0x61C6, 0x60E6, 0x7076, 0x303C, 0x1C1C, 0x0FF8, 0x07E0, 0x0000},
92 {0x0540, 0x0FF0, 0x3FF8, 0x3C3C, 0x7E0E, 0xFF0F, 0x6F86, 0xE7C7,
93 0x63E6, 0xE1F7, 0x70FE, 0x707E, 0x3C3C, 0x1FFC, 0x0FF0, 0x0540},
98 {0x0000, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0FE0,
99 0x1FF0, 0x1FF0, 0x0000, 0x1FF0, 0x1FF0, 0x1550, 0x1550, 0x1550},
100 {0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x0FE0, 0x1FF0,
101 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8},
106 {0x00C0, 0x0140, 0x0640, 0x08C0, 0x3180, 0x47FE, 0x8001, 0x8001,
107 0x81FE, 0x8040, 0x01C0, 0x0040, 0x03C0, 0xC080, 0x3F80, 0x0000},
108 {0x00C0, 0x01C0, 0x07C0, 0x0FC0, 0x3F80, 0x7FFE, 0xFFFF, 0xFFFF,
109 0xFFFE, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFF80, 0x3F80, 0x0000},
114 {0x0100, 0x0280, 0x0260, 0x0310, 0x018C, 0x7FE3, 0x8000, 0x8000,
115 0x7F80, 0x0200, 0x0380, 0x0200, 0x03C0, 0x0107, 0x01F8, 0x0000},
116 {0x0100, 0x0380, 0x03E0, 0x03F0, 0x01FC, 0x7FFF, 0xFFFF, 0xFFFF,
117 0xFFFF, 0x03FF, 0x03FF, 0x03FF, 0x03FF, 0x01FF, 0x01F8, 0x0000},
122 {0x0000, 0x4078, 0x60FC, 0x71CE, 0x7986, 0x7C06, 0x7E0E, 0x7F1C,
123 0x7FB8, 0x7C30, 0x6C30, 0x4600, 0x0630, 0x0330, 0x0300, 0x0000},
124 {0xC078, 0xE0FC, 0xF1FE, 0xFBFF, 0xFFCF, 0xFF8F, 0xFF1F, 0xFFBE,
125 0xFFFC, 0xFE78, 0xFF78, 0xEFF8, 0xCFF8, 0x87F8, 0x07F8, 0x0300},
130 {0x0000, 0x0002, 0x0006, 0x000E, 0x001E, 0x003E, 0x007E, 0x00FE,
131 0x01FE, 0x003E, 0x0036, 0x0062, 0x0060, 0x00C0, 0x00C0, 0x0000},
132 {0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF,
133 0x03FF, 0x07FF, 0x007F, 0x00F7, 0x00F3, 0x01E1, 0x01E0, 0x01C0},
138 {0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0080, 0x0080, 0x1FFC,
139 0x1FFC, 0x0080, 0x0080, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000},
140 {0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0FF8, 0x01C0, 0x3FFE, 0x3FFE,
141 0x3FFE, 0x3FFE, 0x01C0, 0x0FF8, 0x07F0, 0x03E0, 0x01C0, 0x0080},
146 {0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0888, 0x188C, 0x3FFE,
147 0x188C, 0x0888, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000, 0x0000},
148 {0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0BE8, 0x1DDC, 0x3FFE, 0x7FFF,
149 0x3FFE, 0x1DDC, 0x0BE8, 0x07F0, 0x03E0, 0x01C0, 0x0080, 0x0000},
154 {0x0000, 0x001E, 0x000E, 0x060E, 0x0712, 0x03A0, 0x01C0, 0x00E0,
155 0x0170, 0x1238, 0x1C18, 0x1C00, 0x1E00, 0x0000, 0x0000, 0x0000},
156 {0x007F, 0x003F, 0x0E1F, 0x0F0F, 0x0F97, 0x07E3, 0x03E1, 0x21F0,
157 0x31F8, 0x3A7C, 0x3C3C, 0x3E1C, 0x3F00, 0x3F80, 0x0000, 0x0000},
162 {0x0000, 0x7800, 0x7000, 0x7060, 0x48E0, 0x05C0, 0x0380, 0x0700,
163 0x0E80, 0x1C48, 0x1838, 0x0038, 0x0078, 0x0000, 0x0000, 0x0000},
164 {0xFE00, 0xFC00, 0xF870, 0xF0F0, 0xE9F0, 0xC7E0, 0x87C0, 0x0F84,
165 0x1F8C, 0x3E5C, 0x3C3C, 0x387C, 0x00FC, 0x01FC, 0x0000, 0x0000},
170 {0x0006, 0x000E, 0x001C, 0x0018, 0x0020, 0x0040, 0x00F8, 0x0004,
171 0x1FF4, 0x200C, 0x2AA8, 0x1FF0, 0x1F80, 0x3800, 0x6000, 0x8000},
172 {0x000F, 0x001F, 0x003E, 0x007C, 0x0070, 0x00E0, 0x01FC, 0x3FF6,
173 0x7FF6, 0x7FFE, 0x7FFC, 0x7FF8, 0x3FF0, 0x7FC0, 0xF800, 0xE000},
179 wxCursor gMacCurrentCursor
;
182 CursHandle
wxGetStockCursor( int number
)
184 wxASSERT_MSG( number
>= 0 && number
<=kwxCursorLast
, wxT("invalid stock cursor id") ) ;
185 CursHandle c
= (CursHandle
) NewHandle( sizeof(Cursor
) ) ;
186 memcpy( *c
, &gMacCursors
[number
], sizeof(Cursor
) ) ;
188 #ifndef WORDS_BIGENDIAN
189 short *sptr
= (short*) *c
;
190 for ( int i
= 0 ; i
< 2 * 16 /* image and mask */ ; ++i
, ++sptr
)
192 *sptr
= CFSwapInt16( *sptr
) ;
199 wxCursorRefData::wxCursorRefData()
204 m_disposeHandle
= false;
205 m_releaseHandle
= false;
206 m_isColorCursor
= false;
211 wxCursorRefData::~wxCursorRefData()
215 wxMacCocoaRelease(m_hCursor
);
217 if ( m_isColorCursor
)
220 ::DisposeCCursor( (CCrsrHandle
) m_hCursor
) ;
223 else if ( m_disposeHandle
)
225 ::DisposeHandle( (Handle
) m_hCursor
) ;
227 else if ( m_releaseHandle
)
229 // we don't release the resource since it may already
239 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
240 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
244 wxCursor::wxCursor( const wxImage
&image
)
247 CreateFromImage( image
) ;
251 wxCursor::wxCursor(const char **bits
)
253 (void) CreateFromXpm(bits
);
256 wxCursor::wxCursor(char **bits
)
258 (void) CreateFromXpm((const char **)bits
);
261 bool wxCursor::CreateFromXpm(const char **bits
)
264 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid cursor data") );
265 wxXPMDecoder decoder
;
266 wxImage img
= decoder
.ReadData(bits
);
267 wxCHECK_MSG( img
.Ok(), false, wxT("invalid cursor data") );
268 CreateFromImage( img
) ;
275 WXHCURSOR
wxCursor::GetHCURSOR() const
277 return (M_CURSORDATA
? M_CURSORDATA
->m_hCursor
: 0);
280 bool wxCursor::IsOk() const
283 return GetHCURSOR() != NULL
;
285 return (m_refData
!= NULL
&& ( M_CURSORDATA
->m_hCursor
!= NULL
|| M_CURSORDATA
->m_themeCursor
!= -1 ) ) ;
290 short GetCTabIndex( CTabHandle colors
, RGBColor
*col
)
293 unsigned long bestdiff
= 0xFFFF ;
295 for ( int i
= 0 ; i
< (**colors
).ctSize
; ++i
)
297 unsigned long diff
= abs(col
->red
- (**colors
).ctTable
[i
].rgb
.red
) +
298 abs(col
->green
- (**colors
).ctTable
[i
].rgb
.green
) +
299 abs(col
->blue
- (**colors
).ctTable
[i
].rgb
.blue
) ;
301 if ( diff
< bestdiff
)
304 retval
= (**colors
).ctTable
[i
].value
;
314 void wxCursor::CreateFromImage(const wxImage
& image
)
316 m_refData
= new wxCursorRefData
;
317 int hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
318 int hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
320 wxBitmap
bmp( image
);
321 CGImageRef cgimage
= wxMacCreateCGImageFromBitmap(bmp
);
324 M_CURSORDATA
->m_hCursor
= wxMacCocoaCreateCursorFromCGImage( cgimage
, hotSpotX
, hotSpotY
);
325 CFRelease( cgimage
);
332 int image_w
= image
.GetWidth();
333 int image_h
= image
.GetHeight();
335 wxASSERT_MSG( hotSpotX
>= 0 && hotSpotX
< image_w
&&
336 hotSpotY
>= 0 && hotSpotY
< image_h
,
337 wxT("invalid cursor hot spot coordinates") );
339 wxImage
image16(image
); // final image of correct size
341 // if image is too small then place it in the center, resize it if too big
342 if ((w
> image_w
) && (h
> image_h
))
344 wxPoint
offset((w
- image_w
) / 2, (h
- image_h
) / 2);
345 hotSpotX
= hotSpotX
+ offset
.x
;
346 hotSpotY
= hotSpotY
+ offset
.y
;
348 image16
= image
.Size(wxSize(w
, h
), offset
);
350 else if ((w
!= image_w
) || (h
!= image_h
))
352 hotSpotX
= int(hotSpotX
* double(w
) / double(image_w
));
353 hotSpotY
= int(hotSpotY
* double(h
) / double(image_h
));
355 image16
= image
.Scale(w
, h
);
358 unsigned char * rgbBits
= image16
.GetData();
359 bool bHasMask
= image16
.HasMask() ;
361 PixMapHandle pm
= (PixMapHandle
) NewHandleClear( sizeof(PixMap
) ) ;
363 short bytesPerPixel
= 1 ;
365 Rect bounds
= { 0 , 0 , extent
, extent
} ;
366 CCrsrHandle ch
= (CCrsrHandle
) NewHandleClear( sizeof(CCrsr
) ) ;
367 CTabHandle newColors
= GetCTable( 8 ) ;
368 HandToHand( (Handle
*) &newColors
);
370 // set the values to the indices
371 for ( int i
= 0 ; i
< (**newColors
).ctSize
; ++i
)
373 (**newColors
).ctTable
[i
].value
= i
;
377 (**ch
).crsrType
= 0x8001; // color cursors
379 short bytesPerRow
= bytesPerPixel
* extent
;
382 (**pm
).rowBytes
= bytesPerRow
| 0x8000;
383 (**pm
).bounds
= bounds
;
384 (**pm
).pmVersion
= 0;
387 (**pm
).hRes
= 0x00480000; // 72 DPI default res
388 (**pm
).vRes
= 0x00480000; // 72 DPI default res
389 (**pm
).pixelSize
= depth
;
390 (**pm
).pixelType
= 0;
392 (**pm
).cmpSize
= depth
;
393 (**pm
).pmTable
= newColors
;
395 (**ch
).crsrData
= NewHandleClear( extent
* bytesPerRow
) ;
396 (**ch
).crsrXData
= NULL
;
397 (**ch
).crsrXValid
= 0;
398 (**ch
).crsrXHandle
= NULL
;
400 (**ch
).crsrHotSpot
.h
= hotSpotX
;
401 (**ch
).crsrHotSpot
.v
= hotSpotY
;
402 (**ch
).crsrXTable
= 0 ;
403 (**ch
).crsrID
= GetCTSeed() ;
405 memset( (**ch
).crsr1Data
, 0 , sizeof( Bits16
) ) ;
406 memset( (**ch
).crsrMask
, 0 , sizeof( Bits16
) ) ;
408 unsigned char mr
= image16
.GetMaskRed() ;
409 unsigned char mg
= image16
.GetMaskGreen() ;
410 unsigned char mb
= image16
.GetMaskBlue() ;
412 for ( int y
= 0 ; y
< h
; ++y
)
414 short rowbits
= 0, maskbits
= 0 ;
416 for ( int x
= 0 ; x
< w
; ++x
)
418 long pos
= (y
* w
+ x
) * 3;
420 unsigned char r
= rgbBits
[pos
] ;
421 unsigned char g
= rgbBits
[pos
+ 1] ;
422 unsigned char b
= rgbBits
[pos
+ 2] ;
423 RGBColor col
= { 0xFFFF, 0xFFFF, 0xFFFF } ;
425 if ( bHasMask
&& r
== mr
&& g
== mg
&& b
== mb
)
427 // masked area, does not appear anywhere
431 if ( (int)r
+ (int)g
+ (int)b
< 0x0200 )
432 rowbits
|= ( 1 << (15 - x
) ) ;
434 maskbits
|= ( 1 << (15 - x
) ) ;
436 col
= *((RGBColor
*) wxColor( r
, g
, b
).GetPixel()) ;
439 *((*(**ch
).crsrData
) + y
* bytesPerRow
+ x
) =
440 GetCTabIndex( newColors
, &col
) ;
442 #ifdef WORDS_BIGENDIAN
443 (**ch
).crsr1Data
[y
] = rowbits
;
444 (**ch
).crsrMask
[y
] = maskbits
;
446 (**ch
).crsr1Data
[y
] = CFSwapInt16(rowbits
) ;
447 (**ch
).crsrMask
[y
] = CFSwapInt16(maskbits
) ;
452 memcpy( (**ch
).crsrMask
, (**ch
).crsr1Data
, sizeof( Bits16
) ) ;
454 HUnlock( (Handle
)ch
) ;
455 M_CURSORDATA
->m_hCursor
= ch
;
456 M_CURSORDATA
->m_isColorCursor
= true ;
463 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
465 m_refData
= new wxCursorRefData
;
466 if ( flags
== wxBITMAP_TYPE_MACCURSOR_RESOURCE
)
469 wxFAIL_MSG( wxT("Not implemented") );
473 wxMacStringToPascal( cursor_file
, theName
) ;
475 Handle resHandle
= ::GetNamedResource( 'crsr' , theName
) ;
481 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
482 ReleaseResource( resHandle
) ;
484 M_CURSORDATA
->m_hCursor
= GetCCursor( theId
) ;
485 if ( M_CURSORDATA
->m_hCursor
)
486 M_CURSORDATA
->m_isColorCursor
= true ;
490 Handle resHandle
= ::GetNamedResource( 'CURS' , theName
) ;
496 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
497 ReleaseResource( resHandle
) ;
499 M_CURSORDATA
->m_hCursor
= GetCursor( theId
) ;
500 if ( M_CURSORDATA
->m_hCursor
)
501 M_CURSORDATA
->m_releaseHandle
= true ;
511 image
.LoadFile( cursor_file
, flags
) ;
514 image
.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X
, hotSpotX
) ;
515 image
.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y
, hotSpotY
) ;
516 m_refData
->DecRef() ;
518 CreateFromImage( image
) ;
524 // Cursors by stock number
525 wxCursor::wxCursor(int cursor_type
)
527 m_refData
= new wxCursorRefData
;
529 M_CURSORDATA
->m_hCursor
= wxMacCocoaCreateStockCursor( cursor_type
);
533 case wxCURSOR_COPY_ARROW
:
534 M_CURSORDATA
->m_themeCursor
= kThemeCopyArrowCursor
;
538 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
542 M_CURSORDATA
->m_themeCursor
= kThemeIBeamCursor
;
546 M_CURSORDATA
->m_themeCursor
= kThemeCrossCursor
;
549 case wxCURSOR_SIZENWSE
:
550 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSizeNWSE
);
553 case wxCURSOR_SIZENESW
:
554 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSizeNESW
);
557 case wxCURSOR_SIZEWE
:
558 M_CURSORDATA
->m_themeCursor
= kThemeResizeLeftRightCursor
;
561 case wxCURSOR_SIZENS
:
562 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSizeNS
);
565 case wxCURSOR_SIZING
:
566 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSize
);
570 M_CURSORDATA
->m_themeCursor
= kThemePointingHandCursor
;
573 case wxCURSOR_BULLSEYE
:
574 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorBullseye
);
577 case wxCURSOR_PENCIL
:
578 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPencil
);
581 case wxCURSOR_MAGNIFIER
:
582 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorMagnifier
);
585 case wxCURSOR_NO_ENTRY
:
586 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorNoEntry
);
590 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
593 case wxCURSOR_PAINT_BRUSH
:
594 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPaintBrush
);
597 case wxCURSOR_POINT_LEFT
:
598 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPointLeft
);
601 case wxCURSOR_POINT_RIGHT
:
602 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPointRight
);
605 case wxCURSOR_QUESTION_ARROW
:
606 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorQuestionArrow
);
610 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorBlank
);
613 case wxCURSOR_RIGHT_ARROW
:
614 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorRightArrow
);
617 case wxCURSOR_SPRAYCAN
:
618 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorRoller
);
623 case wxCURSOR_LEFT_BUTTON
:
624 case wxCURSOR_RIGHT_BUTTON
:
625 case wxCURSOR_MIDDLE_BUTTON
:
627 M_CURSORDATA
->m_themeCursor
= kThemeArrowCursor
;
631 if ( M_CURSORDATA
->m_themeCursor
== -1 )
632 M_CURSORDATA
->m_releaseHandle
= true;
636 void wxCursor::MacInstall() const
638 gMacCurrentCursor
= *this ;
641 wxMacCocoaSetCursor( M_CURSORDATA
->m_hCursor
);
643 if ( m_refData
&& M_CURSORDATA
->m_themeCursor
!= -1 )
645 SetThemeCursor( M_CURSORDATA
->m_themeCursor
) ;
647 else if ( m_refData
&& M_CURSORDATA
->m_hCursor
)
650 if ( M_CURSORDATA
->m_isColorCursor
)
651 ::SetCCursor( (CCrsrHandle
) M_CURSORDATA
->m_hCursor
) ;
653 ::SetCursor( * (CursHandle
) M_CURSORDATA
->m_hCursor
) ;
658 SetThemeCursor( kThemeArrowCursor
) ;
663 wxCursor::~wxCursor()
667 // Global cursor setting
668 void wxSetCursor(const wxCursor
& cursor
)
670 cursor
.MacInstall() ;