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);
260 wxByte
* data
= image_copy
.GetData();
261 for (int j
= 0; j
< h
; j
++)
263 for (int i
= 0; i
< w
; i
++, data
+= 3)
265 // if average value of the pixel is > mid grey, convert it to
266 // background (0), otherwise to foreground (255, using wxBitmap
270 data
[2] = int(data
[0]) + data
[1] + data
[2] >= 3 * 128 ? 0 : 255;
273 wxBitmap
bitmap(image_copy
, 1);
275 // find the most frequent color(s)
276 wxImageHistogram histogram
;
277 image
.ComputeHistogram(histogram
);
279 long colMostFreq
= 0;
280 unsigned long nMost
= 0;
281 long colNextMostFreq
= 0;
282 unsigned long nNext
= 0;
283 for ( wxImageHistogram::iterator entry
= histogram
.begin();
284 entry
!= histogram
.end();
287 unsigned long key
= entry
->first
;
288 if ( !bHasMask
|| (key
!= keyMaskColor
) )
290 unsigned long value
= entry
->second
.value
;
294 colNextMostFreq
= colMostFreq
;
298 else if (value
> nNext
)
301 colNextMostFreq
= key
;
306 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
307 (unsigned char)(colMostFreq
>> 8),
308 (unsigned char)(colMostFreq
) );
310 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
311 (unsigned char)(colNextMostFreq
>> 8),
312 (unsigned char)(colNextMostFreq
) );
314 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
315 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
317 if (bg_intensity
> fg_intensity
)
325 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
329 fg
.GetColor(), bg
.GetColor(),
333 g_object_unref (mask
);
336 #endif // wxUSE_IMAGE
338 GdkCursor
*wxCursor::GetCursor() const
340 return M_CURSORDATA
->m_cursor
;
343 wxGDIRefData
*wxCursor::CreateGDIRefData() const
345 return new wxCursorRefData
;
348 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
350 return new wxCursorRefData(*static_cast<const wxCursorRefData
*>(data
));
353 //-----------------------------------------------------------------------------
354 // busy cursor routines
355 //-----------------------------------------------------------------------------
357 /* Current cursor, in order to hang on to
358 * cursor handle when setting the cursor globally */
359 wxCursor g_globalCursor
;
361 static wxCursor gs_savedCursor
;
362 static int gs_busyCount
= 0;
364 const wxCursor
&wxBusyCursor::GetStoredCursor()
366 return gs_savedCursor
;
369 const wxCursor
wxBusyCursor::GetBusyCursor()
371 return wxCursor(wxCURSOR_WATCH
);
374 static void UpdateCursors(const wxWindowList
& list
, GdkDisplay
*& display
)
376 wxWindowList::const_iterator i
= list
.begin();
377 for (size_t n
= list
.size(); n
--; ++i
)
380 if (display
== NULL
&& win
->m_widget
&& win
->m_widget
->window
)
381 display
= gdk_drawable_get_display(win
->m_widget
->window
);
382 win
->GTKUpdateCursor(true, false);
383 UpdateCursors(win
->GetChildren(), display
);
387 void wxEndBusyCursor()
389 if (--gs_busyCount
> 0)
392 g_globalCursor
= gs_savedCursor
;
393 gs_savedCursor
= wxNullCursor
;
394 GdkDisplay
* unused
= NULL
;
395 UpdateCursors(wxTopLevelWindows
, unused
);
398 void wxBeginBusyCursor(const wxCursor
* cursor
)
400 if (gs_busyCount
++ > 0)
403 wxASSERT_MSG( !gs_savedCursor
.Ok(),
404 wxT("forgot to call wxEndBusyCursor, will leak memory") );
406 gs_savedCursor
= g_globalCursor
;
407 g_globalCursor
= *cursor
;
408 GdkDisplay
* display
= NULL
;
409 UpdateCursors(wxTopLevelWindows
, display
);
411 gdk_display_flush(display
);
416 return gs_busyCount
> 0;
419 void wxSetCursor( const wxCursor
& cursor
)
421 g_globalCursor
= cursor
;
422 GdkDisplay
* unused
= NULL
;
423 UpdateCursors(wxTopLevelWindows
, unused
);