1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "cursor.h"
15 #include "wx/cursor.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 class wxCursorRefData
: public wxObjectRefData
43 wxCursorRefData::wxCursorRefData()
45 m_cursor
= (GdkCursor
*) NULL
;
48 wxCursorRefData::~wxCursorRefData()
50 if (m_cursor
) gdk_cursor_destroy( m_cursor
);
53 //-----------------------------------------------------------------------------
55 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
57 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
64 wxCursor::wxCursor( int cursorId
)
66 m_refData
= new wxCursorRefData();
68 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
71 case wxCURSOR_ARROW
: // fall through to default
72 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
73 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
74 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND1
; break;
75 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
76 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
77 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
78 case wxCURSOR_ARROWWAIT
:
80 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
81 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
82 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
83 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
84 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
85 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
86 case wxCURSOR_SIZENWSE
:
87 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
88 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
89 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
90 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
91 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
92 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
93 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
94 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
95 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
97 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
98 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
100 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
101 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
102 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
103 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
106 wxFAIL_MSG(wxT("unsupported cursor type"));
107 // will use the standard one
111 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
114 extern GtkWidget
*wxGetRootWindow();
116 wxCursor::wxCursor(const char bits
[], int width
, int height
,
117 int hotSpotX
, int hotSpotY
,
118 const char maskBits
[], wxColour
*fg
, wxColour
*bg
)
126 if (hotSpotX
< 0 || hotSpotX
>= width
)
128 if (hotSpotY
< 0 || hotSpotY
>= height
)
131 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
132 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
134 m_refData
= new wxCursorRefData
;
135 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
136 data
, mask
, fg
->GetColor(), bg
->GetColor(),
137 hotSpotX
, hotSpotY
);
139 gdk_bitmap_unref( data
);
140 gdk_bitmap_unref( mask
);
144 wxCursor::wxCursor( const wxCursor
&cursor
)
152 wxCursor::wxCursor( const wxImage
& image
)
154 unsigned char * rgbBits
= image
.GetData();
155 int w
= image
.GetWidth() ;
156 int h
= image
.GetHeight();
157 bool bHasMask
= image
.HasMask();
158 int imagebitcount
= (w
*h
)/8;
160 unsigned char * bits
= new unsigned char [imagebitcount
];
161 unsigned char * maskBits
= new unsigned char [imagebitcount
];
163 int i
, j
, i8
; unsigned char c
, cMask
;
164 for (i
=0; i
<imagebitcount
; i
++)
172 // possible overflow if we do the summation first ?
173 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
174 //if average value is > mid grey
176 bits
[i
] = bits
[i
] | cMask
;
181 unsigned long keyMaskColor
;
185 r
= image
.GetMaskRed(),
186 g
= image
.GetMaskGreen(),
187 b
= image
.GetMaskBlue();
189 for (i
=0; i
<imagebitcount
; i
++)
197 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
198 maskBits
[i
] = maskBits
[i
] | cMask
;
203 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
207 for (i
=0; i
<imagebitcount
; i
++)
210 // init it to avoid compiler warnings
214 // find the most frequent color(s) it seems a waste of effort to copy the
215 // image but otherwise we need to remove the const modifier ??
216 wxImage tmpImage
= image
.Copy();
217 wxImageHistogram histogram
;
223 tmpImage
.ComputeHistogram(histogram
);
225 long MostFreqCol
= 0;
226 unsigned long nMost
= 0;
227 long NextFreqCol
= 0;
228 unsigned long nNext
= 0;
229 wxImageHistogram::iterator entry
= histogram
.begin();
231 while ( entry
!= histogram
.end() )
233 value
= entry
->second
.value
;
235 if ( !bHasMask
|| (key
!= keyMaskColor
) )
242 else if (value
> nNext
)
250 wxColour fg
= wxColour ( (unsigned char)(MostFreqCol
>> 16),
251 (unsigned char)(MostFreqCol
>> 8),
252 (unsigned char)(MostFreqCol
) );
254 wxColour bg
= wxColour ( (unsigned char)(NextFreqCol
>> 16),
255 (unsigned char)(NextFreqCol
>> 8),
256 (unsigned char)(NextFreqCol
) );
261 if (image
.HasOption(wxCUR_HOTSPOT_X
))
262 hotSpotX
= image
.GetOptionInt(wxCUR_HOTSPOT_X
);
263 if (image
.HasOption(wxCUR_HOTSPOT_Y
))
264 hotSpotY
= image
.GetOptionInt(wxCUR_HOTSPOT_Y
);
266 if (hotSpotX
< 0 || hotSpotX
>= w
)
268 if (hotSpotY
< 0 || hotSpotY
>= h
)
271 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
,
273 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
,
276 m_refData
= new wxCursorRefData
;
277 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
278 data
, mask
, fg
.GetColor(), bg
.GetColor(),
279 hotSpotX
, hotSpotY
);
281 gdk_bitmap_unref( data
);
282 gdk_bitmap_unref( mask
);
287 #endif // wxUSE_IMAGE
289 wxCursor::~wxCursor()
293 wxCursor
& wxCursor::operator = ( const wxCursor
& cursor
)
303 bool wxCursor::operator == ( const wxCursor
& cursor
) const
305 return m_refData
== cursor
.m_refData
;
308 bool wxCursor::operator != ( const wxCursor
& cursor
) const
310 return m_refData
!= cursor
.m_refData
;
313 bool wxCursor::Ok() const
315 return (m_refData
!= NULL
);
318 GdkCursor
*wxCursor::GetCursor() const
320 return M_CURSORDATA
->m_cursor
;
323 //-----------------------------------------------------------------------------
324 // busy cursor routines
325 //-----------------------------------------------------------------------------
327 extern wxCursor g_globalCursor
;
329 static wxCursor gs_savedCursor
;
330 static int gs_busyCount
= 0;
332 const wxCursor
&wxBusyCursor::GetStoredCursor()
334 return gs_savedCursor
;
337 const wxCursor
wxBusyCursor::GetBusyCursor()
339 return wxCursor(wxCURSOR_WATCH
);
342 void wxEndBusyCursor()
344 if (--gs_busyCount
> 0)
347 wxSetCursor( gs_savedCursor
);
348 gs_savedCursor
= wxNullCursor
;
351 wxTheApp
->SendIdleEvents();
354 void wxBeginBusyCursor( wxCursor
*WXUNUSED(cursor
) )
356 if (gs_busyCount
++ > 0)
359 wxASSERT_MSG( !gs_savedCursor
.Ok(),
360 wxT("forgot to call wxEndBusyCursor, will leak memory") );
362 gs_savedCursor
= g_globalCursor
;
364 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
367 wxTheApp
->SendIdleEvents();
374 return gs_busyCount
> 0;
377 void wxSetCursor( const wxCursor
& cursor
)
380 wxapp_install_idle_handler();
382 g_globalCursor
= cursor
;