1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/cursor.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/cursor.h"
16 #include "wx/window.h"
19 #include "wx/bitmap.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 class wxCursorRefData
: public wxGDIRefData
32 virtual ~wxCursorRefData();
34 virtual bool IsOk() const { return m_cursor
!= NULL
; }
39 wxCursorRefData::wxCursorRefData()
41 m_cursor
= (GdkCursor
*) NULL
;
44 wxCursorRefData::~wxCursorRefData()
46 if (m_cursor
) gdk_cursor_unref( m_cursor
);
49 //-----------------------------------------------------------------------------
51 #define M_CURSORDATA wx_static_cast(wxCursorRefData*, m_refData)
53 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
59 wxCursor::wxCursor( int cursorId
)
61 m_refData
= new wxCursorRefData();
63 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
68 const char bits
[] = { 0 };
69 const GdkColor color
= { 0, 0, 0, 0 };
71 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
72 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(pixmap
,
77 g_object_unref(pixmap
);
81 case wxCURSOR_ARROW
: // fall through to default
82 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
83 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
84 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND2
; break;
85 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
86 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
87 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
88 case wxCURSOR_ARROWWAIT
:
90 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
91 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
92 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
93 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
94 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
95 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
96 case wxCURSOR_SIZENWSE
:
97 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
98 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
99 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
100 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
101 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
102 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
103 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
104 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
105 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
107 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
108 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
110 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
111 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
112 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
113 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
117 wxFAIL_MSG(wxT("unsupported cursor type"));
118 // will use the standard one
122 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
125 extern GtkWidget
*wxGetRootWindow();
127 wxCursor::wxCursor(const char bits
[], int width
, int height
,
128 int hotSpotX
, int hotSpotY
,
129 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
137 if (hotSpotX
< 0 || hotSpotX
>= width
)
139 if (hotSpotY
< 0 || hotSpotY
>= height
)
142 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
143 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
145 m_refData
= new wxCursorRefData
;
146 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
147 data
, mask
, fg
->GetColor(), bg
->GetColor(),
148 hotSpotX
, hotSpotY
);
150 g_object_unref (data
);
151 g_object_unref (mask
);
156 static void GetHotSpot(const wxImage
& image
, int& x
, int& y
)
158 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
159 x
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
163 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
164 y
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
168 if (x
< 0 || x
>= image
.GetWidth())
170 if (y
< 0 || y
>= image
.GetHeight())
174 wxCursor::wxCursor( const wxImage
& image
)
176 int w
= image
.GetWidth() ;
177 int h
= image
.GetHeight();
178 bool bHasMask
= image
.HasMask();
179 int hotSpotX
, hotSpotY
;
180 GetHotSpot(image
, hotSpotX
, hotSpotY
);
181 m_refData
= new wxCursorRefData
;
182 wxImage
image_copy(image
);
184 GdkDisplay
* display
= gdk_drawable_get_display(wxGetRootWindow()->window
);
185 if (gdk_display_supports_cursor_color(display
))
187 if (!image
.HasAlpha())
189 // add alpha, so wxBitmap will convert to pixbuf format
190 image_copy
.InitAlpha();
192 wxBitmap
bitmap(image_copy
);
193 wxASSERT(bitmap
.HasPixbuf());
194 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixbuf
203 unsigned long keyMaskColor
= 0;
207 keyMaskColor
= wxImageHistogram::MakeKey(
208 image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
209 // get mask before image is modified
210 wxBitmap
bitmap(image
, 1);
211 mask
= bitmap
.GetMask()->GetBitmap();
216 const int size
= ((w
+ 7) / 8) * h
;
217 char* bits
= new char[size
];
218 memset(bits
, 0xff, size
);
219 mask
= gdk_bitmap_create_from_data(
220 wxGetRootWindow()->window
, bits
, w
, h
);
224 // modify image so wxBitmap can be used to convert to pixmap
225 image_copy
.SetMask(false);
227 wxByte
* data
= image_copy
.GetData();
228 for (j
= 0; j
< h
; j
++)
230 for (i
= 0; i
< w
; i
++, data
+= 3)
232 //if average value is > mid grey
233 if (int(data
[0]) + data
[1] + data
[2] >= 3 * 128)
235 // wxBitmap only converts (255,255,255) to white
242 wxBitmap
bitmap(image_copy
, 1);
244 // find the most frequent color(s)
245 wxImageHistogram histogram
;
246 image
.ComputeHistogram(histogram
);
248 long colMostFreq
= 0;
249 unsigned long nMost
= 0;
250 long colNextMostFreq
= 0;
251 unsigned long nNext
= 0;
252 for ( wxImageHistogram::iterator entry
= histogram
.begin();
253 entry
!= histogram
.end();
256 unsigned long key
= entry
->first
;
257 if ( !bHasMask
|| (key
!= keyMaskColor
) )
259 unsigned long value
= entry
->second
.value
;
263 colNextMostFreq
= colMostFreq
;
267 else if (value
> nNext
)
270 colNextMostFreq
= key
;
275 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
276 (unsigned char)(colMostFreq
>> 8),
277 (unsigned char)(colMostFreq
) );
279 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
280 (unsigned char)(colNextMostFreq
>> 8),
281 (unsigned char)(colNextMostFreq
) );
283 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
284 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
286 if (bg_intensity
> fg_intensity
)
294 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
298 fg
.GetColor(), bg
.GetColor(),
302 g_object_unref (mask
);
305 #endif // wxUSE_IMAGE
307 wxCursor::~wxCursor()
311 GdkCursor
*wxCursor::GetCursor() const
313 return M_CURSORDATA
->m_cursor
;
316 wxGDIRefData
*wxCursor::CreateGDIRefData() const
318 return new wxCursorRefData
;
321 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
323 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData
*, data
));
326 //-----------------------------------------------------------------------------
327 // busy cursor routines
328 //-----------------------------------------------------------------------------
330 /* Current cursor, in order to hang on to
331 * cursor handle when setting the cursor globally */
332 wxCursor g_globalCursor
;
334 static wxCursor gs_savedCursor
;
335 static int gs_busyCount
= 0;
337 const wxCursor
&wxBusyCursor::GetStoredCursor()
339 return gs_savedCursor
;
342 const wxCursor
wxBusyCursor::GetBusyCursor()
344 return wxCursor(wxCURSOR_WATCH
);
347 static void InternalIdle(const wxWindowList
& list
, GdkDisplay
*& display
)
349 wxWindowList::const_iterator i
= list
.begin();
350 for (size_t n
= list
.size(); n
--; ++i
)
353 if (display
== NULL
&& win
->m_widget
&& win
->m_widget
->window
)
354 display
= gdk_drawable_get_display(win
->m_widget
->window
);
355 win
->OnInternalIdle();
356 InternalIdle(win
->GetChildren(), display
);
360 void wxEndBusyCursor()
362 if (--gs_busyCount
> 0)
365 g_globalCursor
= gs_savedCursor
;
366 gs_savedCursor
= wxNullCursor
;
367 GdkDisplay
* unused
= NULL
;
368 InternalIdle(wxTopLevelWindows
, unused
);
371 void wxBeginBusyCursor(const wxCursor
* cursor
)
373 if (gs_busyCount
++ > 0)
376 wxASSERT_MSG( !gs_savedCursor
.Ok(),
377 wxT("forgot to call wxEndBusyCursor, will leak memory") );
379 gs_savedCursor
= g_globalCursor
;
380 g_globalCursor
= *cursor
;
381 GdkDisplay
* display
= NULL
;
382 InternalIdle(wxTopLevelWindows
, display
);
384 gdk_display_flush(display
);
389 return gs_busyCount
> 0;
392 void wxSetCursor( const wxCursor
& cursor
)
394 g_globalCursor
= cursor
;
395 wxTheApp
->WakeUpIdle();