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"
24 #include "wx/gtk/private/object.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 class wxCursorRefData
: public wxGDIRefData
34 virtual ~wxCursorRefData();
36 virtual bool IsOk() const { return m_cursor
!= NULL
; }
41 // There is no way to copy m_cursor so we can't implement a copy ctor
43 wxDECLARE_NO_COPY_CLASS(wxCursorRefData
);
46 wxCursorRefData::wxCursorRefData()
51 wxCursorRefData::~wxCursorRefData()
53 if (m_cursor
) gdk_cursor_unref( m_cursor
);
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
63 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
65 // used in the following two ctors
66 extern GtkWidget
*wxGetRootWindow();
74 wxCursor::wxCursor(const wxString
& cursor_file
,
76 int hotSpotX
, int hotSpotY
)
79 if (!img
.LoadFile(cursor_file
, type
))
82 // eventually set the hotspot:
83 if (!img
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
84 img
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, hotSpotX
);
85 if (!img
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
86 img
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, hotSpotY
);
91 wxCursor::wxCursor(const wxImage
& img
)
97 wxCursor::wxCursor(const char bits
[], int width
, int height
,
98 int hotSpotX
, int hotSpotY
,
99 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
107 if (hotSpotX
< 0 || hotSpotX
>= width
)
109 if (hotSpotY
< 0 || hotSpotY
>= height
)
112 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
113 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
115 m_refData
= new wxCursorRefData
;
116 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
117 data
, mask
, fg
->GetColor(), bg
->GetColor(),
118 hotSpotX
, hotSpotY
);
120 g_object_unref (data
);
121 g_object_unref (mask
);
124 wxCursor::~wxCursor()
128 void wxCursor::InitFromStock( wxStockCursor cursorId
)
130 m_refData
= new wxCursorRefData();
132 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
137 const char bits
[] = { 0 };
138 const GdkColor color
= { 0, 0, 0, 0 };
140 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
141 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(pixmap
,
146 g_object_unref(pixmap
);
150 case wxCURSOR_ARROW
: // fall through to default
151 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
152 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
153 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND2
; break;
154 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
155 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
156 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
157 case wxCURSOR_ARROWWAIT
:
159 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
160 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
161 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
162 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
163 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
164 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
165 case wxCURSOR_SIZENWSE
:
166 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
167 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
168 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
169 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
170 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
171 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
172 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
173 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
174 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
176 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
177 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
179 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
180 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
181 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
182 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
186 wxFAIL_MSG(wxT("unsupported cursor type"));
187 // will use the standard one
191 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
196 static void GetHotSpot(const wxImage
& image
, int& x
, int& y
)
198 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
199 x
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
203 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
204 y
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
208 if (x
< 0 || x
>= image
.GetWidth())
210 if (y
< 0 || y
>= image
.GetHeight())
214 void wxCursor::InitFromImage( const wxImage
& image
)
216 int w
= image
.GetWidth() ;
217 int h
= image
.GetHeight();
218 bool bHasMask
= image
.HasMask();
219 int hotSpotX
, hotSpotY
;
220 GetHotSpot(image
, hotSpotX
, hotSpotY
);
221 m_refData
= new wxCursorRefData
;
222 wxImage
image_copy(image
);
224 GdkDisplay
* display
= gdk_drawable_get_display(wxGetRootWindow()->window
);
225 if (gdk_display_supports_cursor_color(display
))
227 if (!image
.HasAlpha())
229 // add alpha, so wxBitmap will convert to pixbuf format
230 image_copy
.InitAlpha();
232 wxBitmap
bitmap(image_copy
);
233 wxASSERT(bitmap
.HasPixbuf());
234 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixbuf
241 else // no colour cursor support
243 unsigned long keyMaskColor
= 0;
247 keyMaskColor
= wxImageHistogram::MakeKey(
248 image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
249 // get mask before image is modified
250 wxBitmap
bitmap(image
, 1);
251 maskRaw
= bitmap
.GetMask()->GetBitmap();
252 g_object_ref(maskRaw
);
256 const int size
= ((w
+ 7) / 8) * h
;
257 char* bits
= new char[size
];
258 memset(bits
, 0xff, size
);
259 maskRaw
= gdk_bitmap_create_from_data(
260 wxGetRootWindow()->window
, bits
, w
, h
);
264 // assign the raw pointer to wxGtkObject to ensure it is unref'd later
265 wxGtkObject
<GdkPixmap
> mask(maskRaw
);
267 // modify image so wxBitmap can be used to convert to pixmap
268 image_copy
.SetMask(false);
269 wxByte
* data
= image_copy
.GetData();
270 for (int j
= 0; j
< h
; j
++)
272 for (int i
= 0; i
< w
; i
++, data
+= 3)
274 // if average value of the pixel is > mid grey, convert it to
275 // background (0), otherwise to foreground (255, using wxBitmap
279 data
[2] = int(data
[0]) + data
[1] + data
[2] >= 3 * 128 ? 0 : 255;
282 wxBitmap
bitmap(image_copy
, 1);
284 // find the most frequent color(s)
285 wxImageHistogram histogram
;
286 image
.ComputeHistogram(histogram
);
288 long colMostFreq
= 0;
289 unsigned long nMost
= 0;
290 long colNextMostFreq
= 0;
291 unsigned long nNext
= 0;
292 for ( wxImageHistogram::iterator entry
= histogram
.begin();
293 entry
!= histogram
.end();
296 unsigned long key
= entry
->first
;
297 if ( !bHasMask
|| (key
!= keyMaskColor
) )
299 unsigned long value
= entry
->second
.value
;
303 colNextMostFreq
= colMostFreq
;
307 else if (value
> nNext
)
310 colNextMostFreq
= key
;
315 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
316 (unsigned char)(colMostFreq
>> 8),
317 (unsigned char)(colMostFreq
) );
319 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
320 (unsigned char)(colNextMostFreq
>> 8),
321 (unsigned char)(colNextMostFreq
) );
323 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
324 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
326 if (bg_intensity
> fg_intensity
)
334 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
338 fg
.GetColor(), bg
.GetColor(),
344 #endif // wxUSE_IMAGE
346 GdkCursor
*wxCursor::GetCursor() const
348 return M_CURSORDATA
->m_cursor
;
351 wxGDIRefData
*wxCursor::CreateGDIRefData() const
353 return new wxCursorRefData
;
357 wxCursor::CloneGDIRefData(const wxGDIRefData
* WXUNUSED(data
)) const
359 // TODO: We can't clone GDK cursors at the moment. To do this we'd need
360 // to remember the original data from which the cursor was created
361 // (i.e. standard cursor type or the bitmap) or use
362 // gdk_cursor_get_cursor_type() (which is in 2.22+ only) and
363 // gdk_cursor_get_image().
364 wxFAIL_MSG( wxS("Cloning cursors is not implemented in wxGTK.") );
366 return new wxCursorRefData
;
369 //-----------------------------------------------------------------------------
370 // busy cursor routines
371 //-----------------------------------------------------------------------------
373 /* Current cursor, in order to hang on to
374 * cursor handle when setting the cursor globally */
375 wxCursor g_globalCursor
;
377 static wxCursor gs_savedCursor
;
378 static int gs_busyCount
= 0;
380 const wxCursor
&wxBusyCursor::GetStoredCursor()
382 return gs_savedCursor
;
385 const wxCursor
wxBusyCursor::GetBusyCursor()
387 return wxCursor(wxCURSOR_WATCH
);
390 static void UpdateCursors(const wxWindowList
& list
, GdkDisplay
*& display
)
392 wxWindowList::const_iterator i
= list
.begin();
393 for (size_t n
= list
.size(); n
--; ++i
)
396 if (display
== NULL
&& win
->m_widget
&& win
->m_widget
->window
)
397 display
= gdk_drawable_get_display(win
->m_widget
->window
);
398 win
->GTKUpdateCursor(true, false);
399 UpdateCursors(win
->GetChildren(), display
);
403 void wxEndBusyCursor()
405 if (--gs_busyCount
> 0)
408 g_globalCursor
= gs_savedCursor
;
409 gs_savedCursor
= wxNullCursor
;
410 GdkDisplay
* unused
= NULL
;
411 UpdateCursors(wxTopLevelWindows
, unused
);
414 void wxBeginBusyCursor(const wxCursor
* cursor
)
416 if (gs_busyCount
++ > 0)
419 wxASSERT_MSG( !gs_savedCursor
.Ok(),
420 wxT("forgot to call wxEndBusyCursor, will leak memory") );
422 gs_savedCursor
= g_globalCursor
;
423 g_globalCursor
= *cursor
;
424 GdkDisplay
* display
= NULL
;
425 UpdateCursors(wxTopLevelWindows
, display
);
427 gdk_display_flush(display
);
432 return gs_busyCount
> 0;
435 void wxSetCursor( const wxCursor
& cursor
)
437 g_globalCursor
= cursor
;
438 GdkDisplay
* unused
= NULL
;
439 UpdateCursors(wxTopLevelWindows
, unused
);