1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/cursor.cpp
3 // Purpose: wxCursor implementation
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"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 class wxCursorRefData
: public wxGDIRefData
33 virtual ~wxCursorRefData();
35 virtual bool IsOk() const { return m_cursor
!= NULL
; }
40 wxCursorRefData::wxCursorRefData()
45 wxCursorRefData::~wxCursorRefData()
47 if (m_cursor
) gdk_cursor_unref( m_cursor
);
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
57 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
59 // used in the following two ctors
60 extern GtkWidget
*wxGetRootWindow();
68 wxCursor::wxCursor(const wxString
& cursor_file
,
70 int hotSpotX
, int hotSpotY
)
73 if (!img
.LoadFile(cursor_file
, type
))
76 // eventually set the hotspot:
77 if (!img
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
78 img
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, hotSpotX
);
79 if (!img
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
80 img
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, hotSpotY
);
85 wxCursor::wxCursor(const wxImage
& img
)
91 wxCursor::wxCursor(const char bits
[], int width
, int height
,
92 int hotSpotX
, int hotSpotY
,
93 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
101 if (hotSpotX
< 0 || hotSpotX
>= width
)
103 if (hotSpotY
< 0 || hotSpotY
>= height
)
106 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
107 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
109 m_refData
= new wxCursorRefData
;
110 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
111 data
, mask
, fg
->GetColor(), bg
->GetColor(),
112 hotSpotX
, hotSpotY
);
114 g_object_unref (data
);
115 g_object_unref (mask
);
118 wxCursor::~wxCursor()
122 void wxCursor::InitFromStock( wxStockCursor cursorId
)
124 m_refData
= new wxCursorRefData();
126 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
131 const char bits
[] = { 0 };
132 const GdkColor color
= { 0, 0, 0, 0 };
134 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
135 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(pixmap
,
140 g_object_unref(pixmap
);
144 case wxCURSOR_ARROW
: // fall through to default
145 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
146 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
147 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND2
; break;
148 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
149 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
150 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
151 case wxCURSOR_ARROWWAIT
:
153 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
154 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
155 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
156 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
157 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
158 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
159 case wxCURSOR_SIZENWSE
:
160 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
161 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
162 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
163 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
164 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
165 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
166 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
167 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
168 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
170 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
171 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
173 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
174 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
175 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
176 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
180 wxFAIL_MSG(wxT("unsupported cursor type"));
181 // will use the standard one
185 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
190 static void GetHotSpot(const wxImage
& image
, int& x
, int& y
)
192 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
193 x
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
197 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
198 y
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
202 if (x
< 0 || x
>= image
.GetWidth())
204 if (y
< 0 || y
>= image
.GetHeight())
208 void wxCursor::InitFromImage( const wxImage
& image
)
210 int w
= image
.GetWidth() ;
211 int h
= image
.GetHeight();
212 bool bHasMask
= image
.HasMask();
213 int hotSpotX
, hotSpotY
;
214 GetHotSpot(image
, hotSpotX
, hotSpotY
);
215 m_refData
= new wxCursorRefData
;
216 wxImage
image_copy(image
);
218 GdkDisplay
* display
= gdk_drawable_get_display(wxGetRootWindow()->window
);
219 if (gdk_display_supports_cursor_color(display
))
221 if (!image
.HasAlpha())
223 // add alpha, so wxBitmap will convert to pixbuf format
224 image_copy
.InitAlpha();
226 wxBitmap
bitmap(image_copy
);
227 wxASSERT(bitmap
.HasPixbuf());
228 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixbuf
237 unsigned long keyMaskColor
= 0;
241 keyMaskColor
= wxImageHistogram::MakeKey(
242 image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
243 // get mask before image is modified
244 wxBitmap
bitmap(image
, 1);
245 mask
= bitmap
.GetMask()->GetBitmap();
250 const int size
= ((w
+ 7) / 8) * h
;
251 char* bits
= new char[size
];
252 memset(bits
, 0xff, size
);
253 mask
= gdk_bitmap_create_from_data(
254 wxGetRootWindow()->window
, bits
, w
, h
);
258 // modify image so wxBitmap can be used to convert to pixmap
259 image_copy
.SetMask(false);
261 wxByte
* data
= image_copy
.GetData();
262 for (j
= 0; j
< h
; j
++)
264 for (i
= 0; i
< w
; i
++, data
+= 3)
266 //if average value is > mid grey
267 if (int(data
[0]) + data
[1] + data
[2] >= 3 * 128)
269 // wxBitmap only converts (255,255,255) to white
276 wxBitmap
bitmap(image_copy
, 1);
278 // find the most frequent color(s)
279 wxImageHistogram histogram
;
280 image
.ComputeHistogram(histogram
);
282 long colMostFreq
= 0;
283 unsigned long nMost
= 0;
284 long colNextMostFreq
= 0;
285 unsigned long nNext
= 0;
286 for ( wxImageHistogram::iterator entry
= histogram
.begin();
287 entry
!= histogram
.end();
290 unsigned long key
= entry
->first
;
291 if ( !bHasMask
|| (key
!= keyMaskColor
) )
293 unsigned long value
= entry
->second
.value
;
297 colNextMostFreq
= colMostFreq
;
301 else if (value
> nNext
)
304 colNextMostFreq
= key
;
309 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
310 (unsigned char)(colMostFreq
>> 8),
311 (unsigned char)(colMostFreq
) );
313 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
314 (unsigned char)(colNextMostFreq
>> 8),
315 (unsigned char)(colNextMostFreq
) );
317 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
318 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
320 if (bg_intensity
> fg_intensity
)
328 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
332 fg
.GetColor(), bg
.GetColor(),
336 g_object_unref (mask
);
339 #endif // wxUSE_IMAGE
341 GdkCursor
*wxCursor::GetCursor() const
343 return M_CURSORDATA
->m_cursor
;
346 wxGDIRefData
*wxCursor::CreateGDIRefData() const
348 return new wxCursorRefData
;
351 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
353 return new wxCursorRefData(*static_cast<const wxCursorRefData
*>(data
));
356 //-----------------------------------------------------------------------------
357 // busy cursor routines
358 //-----------------------------------------------------------------------------
360 /* Current cursor, in order to hang on to
361 * cursor handle when setting the cursor globally */
362 wxCursor g_globalCursor
;
364 static wxCursor gs_savedCursor
;
365 static int gs_busyCount
= 0;
367 const wxCursor
&wxBusyCursor::GetStoredCursor()
369 return gs_savedCursor
;
372 const wxCursor
wxBusyCursor::GetBusyCursor()
374 return wxCursor(wxCURSOR_WATCH
);
377 static void InternalIdle(const wxWindowList
& list
, GdkDisplay
*& display
)
379 wxWindowList::const_iterator i
= list
.begin();
380 for (size_t n
= list
.size(); n
--; ++i
)
383 if (display
== NULL
&& win
->m_widget
&& win
->m_widget
->window
)
384 display
= gdk_drawable_get_display(win
->m_widget
->window
);
385 win
->OnInternalIdle();
386 InternalIdle(win
->GetChildren(), display
);
390 void wxEndBusyCursor()
392 if (--gs_busyCount
> 0)
395 g_globalCursor
= gs_savedCursor
;
396 gs_savedCursor
= wxNullCursor
;
397 GdkDisplay
* unused
= NULL
;
398 InternalIdle(wxTopLevelWindows
, unused
);
401 void wxBeginBusyCursor(const wxCursor
* cursor
)
403 if (gs_busyCount
++ > 0)
406 wxASSERT_MSG( !gs_savedCursor
.Ok(),
407 wxT("forgot to call wxEndBusyCursor, will leak memory") );
409 gs_savedCursor
= g_globalCursor
;
410 g_globalCursor
= *cursor
;
411 GdkDisplay
* display
= NULL
;
412 InternalIdle(wxTopLevelWindows
, display
);
414 gdk_display_flush(display
);
419 return gs_busyCount
> 0;
422 void wxSetCursor( const wxCursor
& cursor
)
424 g_globalCursor
= cursor
;
425 wxTheApp
->WakeUpIdle();