1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCursor class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "cursor.h"
19 #include "wx/cursor.h"
22 #include "wx/xpmdecod.h"
24 #include "wx/mac/private.h"
26 #if !USE_SHARED_LIBRARIES
27 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxBitmap
)
30 const short kwxCursorBullseye
= 0 ;
31 const short kwxCursorBlank
= 1 ;
32 const short kwxCursorPencil
= 2 ;
33 const short kwxCursorMagnifier
= 3 ;
34 const short kwxCursorNoEntry
= 4 ;
35 const short kwxCursorPaintBrush
= 5 ;
36 const short kwxCursorPointRight
= 6 ;
37 const short kwxCursorPointLeft
= 7 ;
38 const short kwxCursorQuestionArrow
= 8 ;
39 const short kwxCursorRightArrow
= 9 ;
40 const short kwxCursorSizeNS
= 10 ;
41 const short kwxCursorSize
= 11 ;
42 const short kwxCursorSizeNESW
= 12 ;
43 const short kwxCursorSizeNWSE
= 13 ;
44 const short kwxCursorRoller
= 14 ;
45 const short kwxCursorLast
= kwxCursorRoller
;
47 Cursor gMacCursors
[kwxCursorLast
+1] =
51 {0x0000, 0x03E0, 0x0630, 0x0808, 0x1004, 0x31C6, 0x2362, 0x2222,
52 0x2362, 0x31C6, 0x1004, 0x0808, 0x0630, 0x03E0, 0x0000, 0x0000},
53 {0x0000, 0x03E0, 0x07F0, 0x0FF8, 0x1FFC, 0x3FFE, 0x3FFE, 0x3FFE,
54 0x3FFE, 0x3FFE, 0x1FFC, 0x0FF8, 0x07F0, 0x03E0, 0x0000, 0x0000},
59 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
60 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
61 {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
62 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
67 {0x00F0, 0x0088, 0x0108, 0x0190, 0x0270, 0x0220, 0x0440, 0x0440,
68 0x0880, 0x0880, 0x1100, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000},
69 {0x00F0, 0x00F8, 0x01F8, 0x01F0, 0x03F0, 0x03E0, 0x07C0, 0x07C0,
70 0x0F80, 0x0F80, 0x1F00, 0x1E00, 0x1C00, 0x1800, 0x1000, 0x0000},
75 {0x0000, 0x1E00, 0x2100, 0x4080, 0x4080, 0x4080, 0x4080, 0x2180,
76 0x1FC0, 0x00E0, 0x0070, 0x0038, 0x001C, 0x000E, 0x0006, 0x0000},
77 {0x3F00, 0x7F80, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0x7FC0,
78 0x3FE0, 0x1FF0, 0x00F8, 0x007C, 0x003E, 0x001F, 0x000F, 0x0007},
83 {0x0000, 0x07E0, 0x1FF0, 0x3838, 0x3C0C, 0x6E0E, 0x6706, 0x6386,
84 0x61C6, 0x60E6, 0x7076, 0x303C, 0x1C1C, 0x0FF8, 0x07E0, 0x0000},
85 {0x0540, 0x0FF0, 0x3FF8, 0x3C3C, 0x7E0E, 0xFF0F, 0x6F86, 0xE7C7,
86 0x63E6, 0xE1F7, 0x70FE, 0x707E, 0x3C3C, 0x1FFC, 0x0FF0, 0x0540},
91 {0x0000, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0380, 0x0FE0,
92 0x1FF0, 0x1FF0, 0x0000, 0x1FF0, 0x1FF0, 0x1550, 0x1550, 0x1550},
93 {0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x07C0, 0x0FE0, 0x1FF0,
94 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8, 0x3FF8},
99 {0x00C0, 0x0140, 0x0640, 0x08C0, 0x3180, 0x47FE, 0x8001, 0x8001,
100 0x81FE, 0x8040, 0x01C0, 0x0040, 0x03C0, 0xC080, 0x3F80, 0x0000},
101 {0x00C0, 0x01C0, 0x07C0, 0x0FC0, 0x3F80, 0x7FFE, 0xFFFF, 0xFFFF,
102 0xFFFE, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFF80, 0x3F80, 0x0000},
107 {0x0100, 0x0280, 0x0260, 0x0310, 0x018C, 0x7FE3, 0x8000, 0x8000,
108 0x7F80, 0x0200, 0x0380, 0x0200, 0x03C0, 0x0107, 0x01F8, 0x0000},
109 {0x0100, 0x0380, 0x03E0, 0x03F0, 0x01FC, 0x7FFF, 0xFFFF, 0xFFFF,
110 0xFFFF, 0x03FF, 0x03FF, 0x03FF, 0x03FF, 0x01FF, 0x01F8, 0x0000},
115 {0x0000, 0x4078, 0x60FC, 0x71CE, 0x7986, 0x7C06, 0x7E0E, 0x7F1C,
116 0x7FB8, 0x7C30, 0x6C30, 0x4600, 0x0630, 0x0330, 0x0300, 0x0000},
117 {0xC078, 0xE0FC, 0xF1FE, 0xFBFF, 0xFFCF, 0xFF8F, 0xFF1F, 0xFFBE,
118 0xFFFC, 0xFE78, 0xFF78, 0xEFF8, 0xCFF8, 0x87F8, 0x07F8, 0x0300},
123 {0x0000, 0x0002, 0x0006, 0x000E, 0x001E, 0x003E, 0x007E, 0x00FE,
124 0x01FE, 0x003E, 0x0036, 0x0062, 0x0060, 0x00C0, 0x00C0, 0x0000},
125 {0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF,
126 0x03FF, 0x07FF, 0x007F, 0x00F7, 0x00F3, 0x01E1, 0x01E0, 0x01C0},
131 {0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0080, 0x0080, 0x1FFC,
132 0x1FFC, 0x0080, 0x0080, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000},
133 {0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0FF8, 0x01C0, 0x3FFE, 0x3FFE,
134 0x3FFE, 0x3FFE, 0x01C0, 0x0FF8, 0x07F0, 0x03E0, 0x01C0, 0x0080},
139 {0x0000, 0x0080, 0x01C0, 0x03E0, 0x0080, 0x0888, 0x188C, 0x3FFE,
140 0x188C, 0x0888, 0x0080, 0x03E0, 0x01C0, 0x0080, 0x0000, 0x0000},
141 {0x0080, 0x01C0, 0x03E0, 0x07F0, 0x0BE8, 0x1DDC, 0x3FFE, 0x7FFF,
142 0x3FFE, 0x1DDC, 0x0BE8, 0x07F0, 0x03E0, 0x01C0, 0x0080, 0x0000},
147 {0x0000, 0x001E, 0x000E, 0x060E, 0x0712, 0x03A0, 0x01C0, 0x00E0,
148 0x0170, 0x1238, 0x1C18, 0x1C00, 0x1E00, 0x0000, 0x0000, 0x0000},
149 {0x007F, 0x003F, 0x0E1F, 0x0F0F, 0x0F97, 0x07E3, 0x03E1, 0x21F0,
150 0x31F8, 0x3A7C, 0x3C3C, 0x3E1C, 0x3F00, 0x3F80, 0x0000, 0x0000},
155 {0x0000, 0x7800, 0x7000, 0x7060, 0x48E0, 0x05C0, 0x0380, 0x0700,
156 0x0E80, 0x1C48, 0x1838, 0x0038, 0x0078, 0x0000, 0x0000, 0x0000},
157 {0xFE00, 0xFC00, 0xF870, 0xF0F0, 0xE9F0, 0xC7E0, 0x87C0, 0x0F84,
158 0x1F8C, 0x3E5C, 0x3C3C, 0x387C, 0x00FC, 0x01FC, 0x0000, 0x0000},
163 {0x0006, 0x000E, 0x001C, 0x0018, 0x0020, 0x0040, 0x00F8, 0x0004,
164 0x1FF4, 0x200C, 0x2AA8, 0x1FF0, 0x1F80, 0x3800, 0x6000, 0x8000},
165 {0x000F, 0x001F, 0x003E, 0x007C, 0x0070, 0x00E0, 0x01FC, 0x3FF6,
166 0x7FF6, 0x7FFE, 0x7FFC, 0x7FF8, 0x3FF0, 0x7FC0, 0xF800, 0xE000},
172 wxCursor gMacCurrentCursor
;
174 CursHandle
wxGetStockCursor( int number
)
176 wxASSERT_MSG( number
>= 0 && number
<=kwxCursorLast
, wxT("invalid stock cursor id") ) ;
177 CursHandle c
= (CursHandle
) NewHandle( sizeof(Cursor
) ) ;
178 memcpy( *c
, &gMacCursors
[number
] , sizeof( Cursor
) ) ;
182 wxCursorRefData::wxCursorRefData()
187 m_disposeHandle
= false ;
188 m_releaseHandle
= false ;
189 m_isColorCursor
= false ;
193 wxCursorRefData::~wxCursorRefData()
195 if ( m_isColorCursor
)
197 ::DisposeCCursor( (CCrsrHandle
) m_hCursor
) ;
199 else if ( m_disposeHandle
)
201 ::DisposeHandle( (Handle
) m_hCursor
) ;
203 else if ( m_releaseHandle
)
205 // we don't release the resource since it may already
215 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
216 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
220 wxCursor::wxCursor( const wxImage
&image
)
222 CreateFromImage( image
) ;
225 wxCursor::wxCursor(const char **bits
)
227 (void) CreateFromXpm(bits
);
230 wxCursor::wxCursor(char **bits
)
232 (void) CreateFromXpm((const char **)bits
);
235 bool wxCursor::CreateFromXpm(const char **bits
)
237 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid cursor data") )
238 wxXPMDecoder decoder
;
239 wxImage img
= decoder
.ReadData(bits
);
240 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid cursor data") )
241 CreateFromImage( img
) ;
245 short GetCTabIndex( CTabHandle colors
, RGBColor
*col
)
248 unsigned long bestdiff
= 0xFFFF ;
249 for ( int i
= 0 ; i
< (**colors
).ctSize
; ++i
)
251 unsigned long diff
= abs(col
->red
- (**colors
).ctTable
[i
].rgb
.red
) +
252 abs(col
->green
- (**colors
).ctTable
[i
].rgb
.green
) +
253 abs(col
->blue
- (**colors
).ctTable
[i
].rgb
.blue
) ;
254 if ( diff
< bestdiff
)
257 retval
= (**colors
).ctTable
[i
].value
;
263 void wxCursor::CreateFromImage(const wxImage
& image
)
265 m_refData
= new wxCursorRefData
;
267 wxImage image16
= image
.Scale(16,16) ;
268 unsigned char * rgbBits
= image16
.GetData();
271 int w
= image16
.GetWidth() ;
272 int h
= image16
.GetHeight() ;
273 bool bHasMask
= image16
.HasMask() ;
275 int hotSpotX
= image16
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
276 int hotSpotY
= image16
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
277 if (hotSpotX
< 0 || hotSpotX
>= w
)
279 if (hotSpotY
< 0 || hotSpotY
>= h
)
282 PixMapHandle pm
= (PixMapHandle
) NewHandleClear( sizeof (PixMap
)) ;
284 short bytesPerPixel
= 1 ;
286 Rect bounds
= { 0 , 0 , extent
, extent
} ;
287 CCrsrHandle ch
= (CCrsrHandle
) NewHandleClear ( sizeof( CCrsr
) ) ;
288 CTabHandle newColors
= GetCTable( 8 ) ;
289 HandToHand((Handle
*) &newColors
);
290 // set the values to the indices
291 for ( int i
= 0 ; i
< (**newColors
).ctSize
; ++i
)
293 (**newColors
).ctTable
[i
].value
= i
;
295 HLock( (Handle
) ch
) ;
296 (**ch
).crsrType
= 0x8001 ; // color cursors
297 (**ch
).crsrMap
= pm
;
298 short bytesPerRow
= bytesPerPixel
* extent
;
301 (**pm
).rowBytes
= bytesPerRow
| 0x8000;
302 (**pm
).bounds
= bounds
;
303 (**pm
).pmVersion
= 0;
306 (**pm
).hRes
= 0x00480000; /* 72 DPI default res */
307 (**pm
).vRes
= 0x00480000; /* 72 DPI default res */
308 (**pm
).pixelSize
= depth
;
309 (**pm
).pixelType
= 0;
311 (**pm
).cmpSize
= depth
;
312 (**pm
).pmTable
= newColors
;
314 (**ch
).crsrData
= NewHandleClear( extent
* bytesPerRow
) ;
315 (**ch
).crsrXData
= NULL
;
316 (**ch
).crsrXValid
= 0;
317 (**ch
).crsrXHandle
= NULL
;
319 (**ch
).crsrHotSpot
.h
= hotSpotX
;
320 (**ch
).crsrHotSpot
.v
= hotSpotY
;
321 (**ch
).crsrXTable
= NULL
;
322 (**ch
).crsrID
= GetCTSeed() ;
324 memset( (**ch
).crsr1Data
, 0 , sizeof( Bits16
) ) ;
325 memset( (**ch
).crsrMask
, 0 , sizeof( Bits16
) ) ;
327 unsigned char mr
= image16
.GetMaskRed() ;
328 unsigned char mg
= image16
.GetMaskGreen() ;
329 unsigned char mb
= image16
.GetMaskBlue() ;
330 for ( int y
= 0 ; y
< h
; ++y
)
335 for ( int x
= 0 ; x
< w
; ++x
)
337 long pos
= (y
* w
+ x
) * 3;
339 unsigned char r
= rgbBits
[pos
] ;
340 unsigned char g
= rgbBits
[pos
+1] ;
341 unsigned char b
= rgbBits
[pos
+2] ;
342 RGBColor col
= { 0xFFFF ,0xFFFF, 0xFFFF } ;
344 if ( bHasMask
&& r
==mr
&& g
==mg
&& b
==mb
)
346 // masked area, does not appear anywhere
350 if ( (int)r
+ (int)g
+ (int)b
< 0x0200 )
352 rowbits
|= ( 1 << (15-x
) ) ;
354 maskbits
|= ( 1 << (15-x
) ) ;
356 col
= *((RGBColor
*) wxColor( r
, g
, b
).GetPixel()) ;
358 *((*(**ch
).crsrData
) + y
* bytesPerRow
+ x
) =
359 GetCTabIndex( newColors
, &col
) ;
361 (**ch
).crsr1Data
[y
] = rowbits
;
362 (**ch
).crsrMask
[y
] = maskbits
;
366 memcpy( (**ch
).crsrMask
, (**ch
).crsr1Data
, sizeof( Bits16
) ) ;
369 HUnlock((Handle
) ch
) ;
370 M_CURSORDATA
->m_hCursor
= ch
;
371 M_CURSORDATA
->m_isColorCursor
= true ;
374 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
376 m_refData
= new wxCursorRefData
;
377 if ( flags
== wxBITMAP_TYPE_MACCURSOR_RESOURCE
)
380 wxMacStringToPascal( cursor_file
, theName
) ;
382 Handle resHandle
= ::GetNamedResource( 'crsr' , theName
) ;
387 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
388 ReleaseResource( resHandle
) ;
389 M_CURSORDATA
->m_hCursor
= GetCCursor( theId
) ;
390 if ( M_CURSORDATA
->m_hCursor
)
391 M_CURSORDATA
->m_isColorCursor
= true ;
395 Handle resHandle
= ::GetNamedResource( 'CURS' , theName
) ;
400 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
401 ReleaseResource( resHandle
) ;
402 M_CURSORDATA
->m_hCursor
= GetCursor( theId
) ;
403 if ( M_CURSORDATA
->m_hCursor
)
404 M_CURSORDATA
->m_releaseHandle
= true ;
411 image
.LoadFile( cursor_file
, flags
) ;
414 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
,hotSpotX
) ;
415 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
,hotSpotY
) ;
417 CreateFromImage(image
) ;
422 // Cursors by stock number
423 wxCursor::wxCursor(int cursor_type
)
425 m_refData
= new wxCursorRefData
;
429 case wxCURSOR_COPY_ARROW
:
430 M_CURSORDATA
->m_themeCursor
= kThemeCopyArrowCursor
;
433 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
436 M_CURSORDATA
->m_themeCursor
= kThemeIBeamCursor
;
439 M_CURSORDATA
->m_themeCursor
= kThemeCrossCursor
;
441 case wxCURSOR_SIZENWSE
:
443 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSizeNWSE
);
446 case wxCURSOR_SIZENESW
:
448 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSizeNESW
);
451 case wxCURSOR_SIZEWE
:
453 M_CURSORDATA
->m_themeCursor
= kThemeResizeLeftRightCursor
;
456 case wxCURSOR_SIZENS
:
458 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSizeNS
);
461 case wxCURSOR_SIZING
:
463 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorSize
);
468 M_CURSORDATA
->m_themeCursor
= kThemePointingHandCursor
;
471 case wxCURSOR_BULLSEYE
:
473 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorBullseye
);
476 case wxCURSOR_PENCIL
:
478 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPencil
);
481 case wxCURSOR_MAGNIFIER
:
483 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorMagnifier
);
486 case wxCURSOR_NO_ENTRY
:
488 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorNoEntry
);
493 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
496 case wxCURSOR_PAINT_BRUSH
:
498 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPaintBrush
);
501 case wxCURSOR_POINT_LEFT
:
503 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPointLeft
);
506 case wxCURSOR_POINT_RIGHT
:
508 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorPointRight
);
511 case wxCURSOR_QUESTION_ARROW
:
513 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorQuestionArrow
);
518 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorBlank
);
521 case wxCURSOR_RIGHT_ARROW
:
523 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorRightArrow
);
526 case wxCURSOR_SPRAYCAN
:
528 M_CURSORDATA
->m_hCursor
= wxGetStockCursor(kwxCursorRoller
);
533 case wxCURSOR_LEFT_BUTTON
:
534 case wxCURSOR_RIGHT_BUTTON
:
535 case wxCURSOR_MIDDLE_BUTTON
:
537 M_CURSORDATA
->m_themeCursor
= kThemeArrowCursor
;
540 if ( M_CURSORDATA
->m_themeCursor
== -1 )
541 M_CURSORDATA
->m_releaseHandle
= true ;
544 void wxCursor::MacInstall() const
546 gMacCurrentCursor
= *this ;
547 if ( m_refData
&& M_CURSORDATA
->m_themeCursor
!= -1 )
549 SetThemeCursor( M_CURSORDATA
->m_themeCursor
) ;
551 else if ( m_refData
&& M_CURSORDATA
->m_hCursor
)
553 if ( M_CURSORDATA
->m_isColorCursor
)
554 ::SetCCursor( (CCrsrHandle
) M_CURSORDATA
->m_hCursor
) ;
556 ::SetCursor( * (CursHandle
) M_CURSORDATA
->m_hCursor
) ;
560 SetThemeCursor( kThemeArrowCursor
) ;
564 wxCursor::~wxCursor()
568 // Global cursor setting
569 void wxSetCursor(const wxCursor
& cursor
)
571 cursor
.MacInstall() ;