1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCursor class
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "cursor.h"
19 #include "wx/cursor.h"
22 #include "wx/mac/private.h"
24 #if !USE_SHARED_LIBRARIES
25 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxBitmap
)
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 ;
44 wxCursor gMacCurrentCursor
;
46 wxCursorRefData::wxCursorRefData()
51 m_disposeHandle
= false ;
52 m_releaseHandle
= false ;
53 m_isColorCursor
= false ;
57 wxCursorRefData::~wxCursorRefData()
59 if ( m_isColorCursor
)
61 ::DisposeCCursor( (CCrsrHandle
) m_hCursor
) ;
63 else if ( m_disposeHandle
)
65 ::DisposeHandle( (Handle
) m_hCursor
) ;
66 } else if ( m_releaseHandle
)
68 ::ReleaseResource( (Handle
) m_hCursor
) ;
77 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
78 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
82 wxCursor::wxCursor( const wxImage
&image
)
84 CreateFromImage( image
) ;
87 void wxCursor::CreateFromImage(const wxImage
& image
)
89 m_refData
= new wxCursorRefData
;
91 wxImage image16
= image
.Scale(16,16) ;
92 unsigned char * rgbBits
= image16
.GetData();
95 int w
= image16
.GetWidth() ;
96 int h
= image16
.GetHeight() ;
97 bool bHasMask
= image16
.HasMask() ;
99 int hotSpotX
= image16
.GetOptionInt(wxCUR_HOTSPOT_X
);
100 int hotSpotY
= image16
.GetOptionInt(wxCUR_HOTSPOT_Y
);
101 if (hotSpotX
< 0 || hotSpotX
>= w
)
103 if (hotSpotY
< 0 || hotSpotY
>= h
)
106 M_CURSORDATA
->m_hCursor
= NewHandle( sizeof( Cursor
) ) ;
107 M_CURSORDATA
->m_disposeHandle
= true ;
108 HLock( (Handle
) M_CURSORDATA
->m_hCursor
) ;
109 CursPtr cp
= *(CursHandle
)M_CURSORDATA
->m_hCursor
;
110 memset( cp
->data
, 0 , sizeof( Bits16
) ) ;
111 memset( cp
->mask
, 0 , sizeof( Bits16
) ) ;
113 unsigned char mr
= image16
.GetMaskRed() ;
114 unsigned char mg
= image16
.GetMaskGreen() ;
115 unsigned char mb
= image16
.GetMaskBlue() ;
116 for ( int y
= 0 ; y
< h
; ++y
)
121 for ( int x
= 0 ; x
< w
; ++x
)
123 long pos
= (y
* w
+ x
) * 3;
125 unsigned char r
= rgbBits
[pos
] ;
126 unsigned char g
= rgbBits
[pos
+1] ;
127 unsigned char b
= rgbBits
[pos
+2] ;
128 if ( bHasMask
&& r
==mr
&& g
==mg
&& b
==mb
)
130 // masked area, does not appear anywhere
134 if ( (int)r
+ (int)g
+ (int)b
< 0x60 )
136 rowbits
|= ( 1 << (15-x
) ) ;
138 maskbits
|= ( 1 << (15-x
) ) ;
141 cp
->data
[y
] = rowbits
;
142 cp
->mask
[y
] = maskbits
;
146 memcpy( cp
->mask
, cp
->data
, sizeof( Bits16
) ) ;
149 cp
->hotSpot
.h
= hotSpotX
;
150 cp
->hotSpot
.v
= hotSpotY
;
151 HUnlock( (Handle
) M_CURSORDATA
->m_hCursor
) ;
154 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
156 m_refData
= new wxCursorRefData
;
157 if ( flags
== wxBITMAP_TYPE_MACCURSOR_RESOURCE
)
162 c2pstrcpy( (StringPtr
) theName
, cursor_file
) ;
164 strcpy( (char *) theName
, cursor_file
) ;
165 c2pstr( (char *) theName
) ;
168 wxStAppResource resload
;
169 M_CURSORDATA
->m_hCursor
= ::GetNamedResource( 'crsr' , theName
) ;
170 if ( M_CURSORDATA
->m_hCursor
)
172 M_CURSORDATA
->m_isColorCursor
= true ;
176 M_CURSORDATA
->m_hCursor
= ::GetNamedResource( 'CURS' , theName
) ;
177 if ( M_CURSORDATA
->m_hCursor
)
178 M_CURSORDATA
->m_releaseHandle
= true ;
184 image
.LoadFile( cursor_file
, flags
) ;
187 image
.SetOption(wxCUR_HOTSPOT_X
,hotSpotX
) ;
188 image
.SetOption(wxCUR_HOTSPOT_Y
,hotSpotY
) ;
190 CreateFromImage(image
) ;
195 // Cursors by stock number
196 wxCursor::wxCursor(int cursor_type
)
198 m_refData
= new wxCursorRefData
;
203 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
206 M_CURSORDATA
->m_themeCursor
= kThemeIBeamCursor
;
209 M_CURSORDATA
->m_themeCursor
= kThemeCrossCursor
;
211 case wxCURSOR_SIZENWSE
:
213 wxStAppResource resload
;
214 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSizeNWSE
);
217 case wxCURSOR_SIZENESW
:
219 wxStAppResource resload
;
220 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSizeNESW
);
223 case wxCURSOR_SIZEWE
:
225 M_CURSORDATA
->m_themeCursor
= kThemeResizeLeftRightCursor
;
228 case wxCURSOR_SIZENS
:
230 wxStAppResource resload
;
231 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSizeNS
);
234 case wxCURSOR_SIZING
:
236 wxStAppResource resload
;
237 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSize
);
242 M_CURSORDATA
->m_themeCursor
= kThemePointingHandCursor
;
245 case wxCURSOR_BULLSEYE
:
247 wxStAppResource resload
;
248 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorBullseye
);
251 case wxCURSOR_PENCIL
:
253 wxStAppResource resload
;
254 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPencil
);
257 case wxCURSOR_MAGNIFIER
:
259 wxStAppResource resload
;
260 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorMagnifier
);
263 case wxCURSOR_NO_ENTRY
:
265 wxStAppResource resload
;
266 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorNoEntry
);
271 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
274 case wxCURSOR_PAINT_BRUSH
:
276 wxStAppResource resload
;
277 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPaintBrush
);
280 case wxCURSOR_POINT_LEFT
:
282 wxStAppResource resload
;
283 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPointLeft
);
286 case wxCURSOR_POINT_RIGHT
:
288 wxStAppResource resload
;
289 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPointRight
);
292 case wxCURSOR_QUESTION_ARROW
:
294 wxStAppResource resload
;
295 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorQuestionArrow
);
300 wxStAppResource resload
;
301 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorBlank
);
304 case wxCURSOR_RIGHT_ARROW
:
306 wxStAppResource resload
;
307 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorRightArrow
);
310 case wxCURSOR_SPRAYCAN
:
312 wxStAppResource resload
;
313 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorRoller
);
318 case wxCURSOR_LEFT_BUTTON
:
319 case wxCURSOR_RIGHT_BUTTON
:
320 case wxCURSOR_MIDDLE_BUTTON
:
322 M_CURSORDATA
->m_themeCursor
= kThemeArrowCursor
;
325 if ( M_CURSORDATA
->m_themeCursor
== -1 )
326 M_CURSORDATA
->m_releaseHandle
= true ;
329 void wxCursor::MacInstall() const
331 gMacCurrentCursor
= *this ;
332 if ( m_refData
&& M_CURSORDATA
->m_themeCursor
!= -1 )
334 SetThemeCursor( M_CURSORDATA
->m_themeCursor
) ;
336 else if ( m_refData
&& M_CURSORDATA
->m_hCursor
)
338 if ( M_CURSORDATA
->m_isColorCursor
)
339 ::SetCCursor( (CCrsrHandle
) M_CURSORDATA
->m_hCursor
) ;
341 ::SetCursor( * (CursHandle
) M_CURSORDATA
->m_hCursor
) ;
345 SetThemeCursor( kThemeArrowCursor
) ;
349 wxCursor::~wxCursor()
353 // Global cursor setting
354 void wxSetCursor(const wxCursor
& cursor
)
356 cursor
.MacInstall() ;