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
)
151 wxCursor::wxCursor( const wxImage
& image
)
153 unsigned char * rgbBits
= image
.GetData();
154 int w
= image
.GetWidth() ;
155 int h
= image
.GetHeight();
156 bool bHasMask
= image
.HasMask();
157 int imagebitcount
= (w
*h
)/8;
159 unsigned char * bits
= new unsigned char [imagebitcount
];
160 unsigned char * maskBits
= new unsigned char [imagebitcount
];
162 int i
, j
, i8
; unsigned char c
, cMask
;
163 for (i
=0; i
<imagebitcount
; i
++)
171 // possible overflow if we do the summation first ?
172 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
173 //if average value is > mid grey
175 bits
[i
] = bits
[i
] | cMask
;
180 unsigned long keyMaskColor
;
184 r
= image
.GetMaskRed(),
185 g
= image
.GetMaskGreen(),
186 b
= image
.GetMaskBlue();
188 for (i
=0; i
<imagebitcount
; i
++)
196 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
197 maskBits
[i
] = maskBits
[i
] | cMask
;
202 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
206 for (i
=0; i
<imagebitcount
; i
++)
209 // init it to avoid compiler warnings
213 // find the most frequent color(s) it seems a waste of effort to copy the
214 // image but otherwise we need to remove the const modifier ??
215 wxImage tmpImage
= image
.Copy();
216 wxImageHistogram histogram
;
222 tmpImage
.ComputeHistogram(histogram
);
224 long MostFreqCol
= 0;
225 unsigned long nMost
= 0;
226 long NextFreqCol
= 0;
227 unsigned long nNext
= 0;
228 wxImageHistogram::iterator entry
= histogram
.begin();
230 while ( entry
!= histogram
.end() )
232 value
= entry
->second
.value
;
234 if ( !bHasMask
|| (key
!= keyMaskColor
) )
241 else if (value
> nNext
)
249 wxColour fg
= wxColour ( (unsigned char)(MostFreqCol
>> 16),
250 (unsigned char)(MostFreqCol
>> 8),
251 (unsigned char)(MostFreqCol
) );
253 wxColour bg
= wxColour ( (unsigned char)(NextFreqCol
>> 16),
254 (unsigned char)(NextFreqCol
>> 8),
255 (unsigned char)(NextFreqCol
) );
260 if (image
.HasOption(wxCUR_HOTSPOT_X
))
261 hotSpotX
= image
.GetOptionInt(wxCUR_HOTSPOT_X
);
262 if (image
.HasOption(wxCUR_HOTSPOT_Y
))
263 hotSpotY
= image
.GetOptionInt(wxCUR_HOTSPOT_Y
);
265 if (hotSpotX
< 0 || hotSpotX
>= w
)
267 if (hotSpotY
< 0 || hotSpotY
>= h
)
270 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
,
272 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
,
275 m_refData
= new wxCursorRefData
;
276 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
277 data
, mask
, fg
.GetColor(), bg
.GetColor(),
278 hotSpotX
, hotSpotY
);
280 gdk_bitmap_unref( data
);
281 gdk_bitmap_unref( mask
);
286 #endif // wxUSE_IMAGE
288 wxCursor::~wxCursor()
292 wxCursor
& wxCursor::operator = ( const wxCursor
& cursor
)
302 bool wxCursor::operator == ( const wxCursor
& cursor
) const
304 return m_refData
== cursor
.m_refData
;
307 bool wxCursor::operator != ( const wxCursor
& cursor
) const
309 return m_refData
!= cursor
.m_refData
;
312 bool wxCursor::Ok() const
314 return (m_refData
!= NULL
);
317 GdkCursor
*wxCursor::GetCursor() const
319 return M_CURSORDATA
->m_cursor
;
322 //-----------------------------------------------------------------------------
323 // busy cursor routines
324 //-----------------------------------------------------------------------------
326 extern wxCursor g_globalCursor
;
328 static wxCursor gs_savedCursor
;
329 static int gs_busyCount
= 0;
331 const wxCursor
&wxBusyCursor::GetStoredCursor()
333 return gs_savedCursor
;
336 const wxCursor
wxBusyCursor::GetBusyCursor()
338 return wxCursor(wxCURSOR_WATCH
);
341 void wxEndBusyCursor()
343 if (--gs_busyCount
> 0)
346 wxSetCursor( gs_savedCursor
);
347 gs_savedCursor
= wxNullCursor
;
350 wxTheApp
->SendIdleEvents();
353 void wxBeginBusyCursor( wxCursor
*WXUNUSED(cursor
) )
355 if (gs_busyCount
++ > 0)
358 wxASSERT_MSG( !gs_savedCursor
.Ok(),
359 wxT("forgot to call wxEndBusyCursor, will leak memory") );
361 gs_savedCursor
= g_globalCursor
;
363 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
366 wxTheApp
->SendIdleEvents();
373 return gs_busyCount
> 0;
376 void wxSetCursor( const wxCursor
& cursor
)
379 wxapp_install_idle_handler();
381 g_globalCursor
= cursor
;