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)
215 wxImageHistogram histogram
;
216 image
.ComputeHistogram(histogram
);
222 long colMostFreq
= 0;
223 unsigned long nMost
= 0;
224 long colNextMostFreq
= 0;
225 unsigned long nNext
= 0;
226 for ( wxImageHistogram::iterator entry
= histogram
.begin();
227 entry
!= histogram
.end();
230 value
= entry
->second
.value
;
232 if ( !bHasMask
|| (key
!= keyMaskColor
) )
239 else if (value
> nNext
)
242 colNextMostFreq
= key
;
247 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
248 (unsigned char)(colMostFreq
>> 8),
249 (unsigned char)(colMostFreq
) );
251 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
252 (unsigned char)(colNextMostFreq
>> 8),
253 (unsigned char)(colNextMostFreq
) );
258 if (image
.HasOption(wxCUR_HOTSPOT_X
))
259 hotSpotX
= image
.GetOptionInt(wxCUR_HOTSPOT_X
);
263 if (image
.HasOption(wxCUR_HOTSPOT_Y
))
264 hotSpotY
= image
.GetOptionInt(wxCUR_HOTSPOT_Y
);
268 if (hotSpotX
< 0 || hotSpotX
>= w
)
270 if (hotSpotY
< 0 || hotSpotY
>= h
)
273 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
274 (gchar
*) bits
, w
, h
);
275 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
276 (gchar
*) maskBits
, w
, h
);
278 m_refData
= new wxCursorRefData
;
279 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
283 fg
.GetColor(), bg
.GetColor(),
287 gdk_bitmap_unref( data
);
288 gdk_bitmap_unref( mask
);
293 #endif // wxUSE_IMAGE
295 wxCursor::~wxCursor()
299 wxCursor
& wxCursor::operator = ( const wxCursor
& cursor
)
309 bool wxCursor::operator == ( const wxCursor
& cursor
) const
311 return m_refData
== cursor
.m_refData
;
314 bool wxCursor::operator != ( const wxCursor
& cursor
) const
316 return m_refData
!= cursor
.m_refData
;
319 bool wxCursor::Ok() const
321 return (m_refData
!= NULL
);
324 GdkCursor
*wxCursor::GetCursor() const
326 return M_CURSORDATA
->m_cursor
;
329 //-----------------------------------------------------------------------------
330 // busy cursor routines
331 //-----------------------------------------------------------------------------
333 extern wxCursor g_globalCursor
;
335 static wxCursor gs_savedCursor
;
336 static int gs_busyCount
= 0;
338 const wxCursor
&wxBusyCursor::GetStoredCursor()
340 return gs_savedCursor
;
343 const wxCursor
wxBusyCursor::GetBusyCursor()
345 return wxCursor(wxCURSOR_WATCH
);
348 void wxEndBusyCursor()
350 if (--gs_busyCount
> 0)
353 wxSetCursor( gs_savedCursor
);
354 gs_savedCursor
= wxNullCursor
;
357 wxTheApp
->SendIdleEvents();
360 void wxBeginBusyCursor( wxCursor
*WXUNUSED(cursor
) )
362 if (gs_busyCount
++ > 0)
365 wxASSERT_MSG( !gs_savedCursor
.Ok(),
366 wxT("forgot to call wxEndBusyCursor, will leak memory") );
368 gs_savedCursor
= g_globalCursor
;
370 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
373 wxTheApp
->SendIdleEvents();
380 return gs_busyCount
> 0;
383 void wxSetCursor( const wxCursor
& cursor
)
386 wxapp_install_idle_handler();
388 g_globalCursor
= cursor
;