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/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
) ;
67 else if ( m_releaseHandle
)
69 // we don't release the resource since it may already
79 wxCursor::wxCursor(const char WXUNUSED(bits
)[], int WXUNUSED(width
), int WXUNUSED(height
),
80 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
), const char WXUNUSED(maskBits
)[])
84 wxCursor::wxCursor( const wxImage
&image
)
86 CreateFromImage( image
) ;
89 void wxCursor::CreateFromImage(const wxImage
& image
)
91 m_refData
= new wxCursorRefData
;
93 wxImage image16
= image
.Scale(16,16) ;
94 unsigned char * rgbBits
= image16
.GetData();
97 int w
= image16
.GetWidth() ;
98 int h
= image16
.GetHeight() ;
99 bool bHasMask
= image16
.HasMask() ;
101 int hotSpotX
= image16
.GetOptionInt(wxCUR_HOTSPOT_X
);
102 int hotSpotY
= image16
.GetOptionInt(wxCUR_HOTSPOT_Y
);
103 if (hotSpotX
< 0 || hotSpotX
>= w
)
105 if (hotSpotY
< 0 || hotSpotY
>= h
)
108 M_CURSORDATA
->m_hCursor
= NewHandle( sizeof( Cursor
) ) ;
109 M_CURSORDATA
->m_disposeHandle
= true ;
110 HLock( (Handle
) M_CURSORDATA
->m_hCursor
) ;
111 CursPtr cp
= *(CursHandle
)M_CURSORDATA
->m_hCursor
;
112 memset( cp
->data
, 0 , sizeof( Bits16
) ) ;
113 memset( cp
->mask
, 0 , sizeof( Bits16
) ) ;
115 unsigned char mr
= image16
.GetMaskRed() ;
116 unsigned char mg
= image16
.GetMaskGreen() ;
117 unsigned char mb
= image16
.GetMaskBlue() ;
118 for ( int y
= 0 ; y
< h
; ++y
)
123 for ( int x
= 0 ; x
< w
; ++x
)
125 long pos
= (y
* w
+ x
) * 3;
127 unsigned char r
= rgbBits
[pos
] ;
128 unsigned char g
= rgbBits
[pos
+1] ;
129 unsigned char b
= rgbBits
[pos
+2] ;
130 if ( bHasMask
&& r
==mr
&& g
==mg
&& b
==mb
)
132 // masked area, does not appear anywhere
136 if ( (int)r
+ (int)g
+ (int)b
< 0x60 )
138 rowbits
|= ( 1 << (15-x
) ) ;
140 maskbits
|= ( 1 << (15-x
) ) ;
143 cp
->data
[y
] = rowbits
;
144 cp
->mask
[y
] = maskbits
;
148 memcpy( cp
->mask
, cp
->data
, sizeof( Bits16
) ) ;
151 cp
->hotSpot
.h
= hotSpotX
;
152 cp
->hotSpot
.v
= hotSpotY
;
153 HUnlock( (Handle
) M_CURSORDATA
->m_hCursor
) ;
156 wxCursor::wxCursor(const wxString
& cursor_file
, long flags
, int hotSpotX
, int hotSpotY
)
158 m_refData
= new wxCursorRefData
;
159 if ( flags
== wxBITMAP_TYPE_MACCURSOR_RESOURCE
)
164 c2pstrcpy( (StringPtr
) theName
, cursor_file
) ;
166 strcpy( (char *) theName
, cursor_file
) ;
167 c2pstr( (char *) theName
) ;
170 wxStAppResource resload
;
171 Handle resHandle
= ::GetNamedResource( 'crsr' , theName
) ;
176 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
177 ReleaseResource( resHandle
) ;
178 M_CURSORDATA
->m_hCursor
= GetCCursor( theId
) ;
179 if ( M_CURSORDATA
->m_hCursor
)
180 M_CURSORDATA
->m_isColorCursor
= true ;
184 Handle resHandle
= ::GetNamedResource( 'CURS' , theName
) ;
189 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
190 ReleaseResource( resHandle
) ;
191 M_CURSORDATA
->m_hCursor
= GetCursor( theId
) ;
192 if ( M_CURSORDATA
->m_hCursor
)
193 M_CURSORDATA
->m_releaseHandle
= true ;
200 image
.LoadFile( cursor_file
, flags
) ;
203 image
.SetOption(wxCUR_HOTSPOT_X
,hotSpotX
) ;
204 image
.SetOption(wxCUR_HOTSPOT_Y
,hotSpotY
) ;
206 CreateFromImage(image
) ;
211 // Cursors by stock number
212 wxCursor::wxCursor(int cursor_type
)
214 m_refData
= new wxCursorRefData
;
219 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
222 M_CURSORDATA
->m_themeCursor
= kThemeIBeamCursor
;
225 M_CURSORDATA
->m_themeCursor
= kThemeCrossCursor
;
227 case wxCURSOR_SIZENWSE
:
229 wxStAppResource resload
;
230 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSizeNWSE
);
233 case wxCURSOR_SIZENESW
:
235 wxStAppResource resload
;
236 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSizeNESW
);
239 case wxCURSOR_SIZEWE
:
241 M_CURSORDATA
->m_themeCursor
= kThemeResizeLeftRightCursor
;
244 case wxCURSOR_SIZENS
:
246 wxStAppResource resload
;
247 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSizeNS
);
250 case wxCURSOR_SIZING
:
252 wxStAppResource resload
;
253 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorSize
);
258 M_CURSORDATA
->m_themeCursor
= kThemePointingHandCursor
;
261 case wxCURSOR_BULLSEYE
:
263 wxStAppResource resload
;
264 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorBullseye
);
267 case wxCURSOR_PENCIL
:
269 wxStAppResource resload
;
270 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPencil
);
273 case wxCURSOR_MAGNIFIER
:
275 wxStAppResource resload
;
276 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorMagnifier
);
279 case wxCURSOR_NO_ENTRY
:
281 wxStAppResource resload
;
282 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorNoEntry
);
287 M_CURSORDATA
->m_themeCursor
= kThemeWatchCursor
;
290 case wxCURSOR_PAINT_BRUSH
:
292 wxStAppResource resload
;
293 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPaintBrush
);
296 case wxCURSOR_POINT_LEFT
:
298 wxStAppResource resload
;
299 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPointLeft
);
302 case wxCURSOR_POINT_RIGHT
:
304 wxStAppResource resload
;
305 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorPointRight
);
308 case wxCURSOR_QUESTION_ARROW
:
310 wxStAppResource resload
;
311 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorQuestionArrow
);
316 wxStAppResource resload
;
317 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorBlank
);
320 case wxCURSOR_RIGHT_ARROW
:
322 wxStAppResource resload
;
323 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorRightArrow
);
326 case wxCURSOR_SPRAYCAN
:
328 wxStAppResource resload
;
329 M_CURSORDATA
->m_hCursor
= ::GetCursor(kwxCursorRoller
);
334 case wxCURSOR_LEFT_BUTTON
:
335 case wxCURSOR_RIGHT_BUTTON
:
336 case wxCURSOR_MIDDLE_BUTTON
:
338 M_CURSORDATA
->m_themeCursor
= kThemeArrowCursor
;
341 if ( M_CURSORDATA
->m_themeCursor
== -1 )
342 M_CURSORDATA
->m_releaseHandle
= true ;
345 void wxCursor::MacInstall() const
347 gMacCurrentCursor
= *this ;
348 if ( m_refData
&& M_CURSORDATA
->m_themeCursor
!= -1 )
350 SetThemeCursor( M_CURSORDATA
->m_themeCursor
) ;
352 else if ( m_refData
&& M_CURSORDATA
->m_hCursor
)
354 if ( M_CURSORDATA
->m_isColorCursor
)
355 ::SetCCursor( (CCrsrHandle
) M_CURSORDATA
->m_hCursor
) ;
357 ::SetCursor( * (CursHandle
) M_CURSORDATA
->m_hCursor
) ;
361 SetThemeCursor( kThemeArrowCursor
) ;
365 wxCursor::~wxCursor()
369 // Global cursor setting
370 void wxSetCursor(const wxCursor
& cursor
)
372 cursor
.MacInstall() ;