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 wxCursorRefData::wxCursorRefData()
46 wxCursorRefData::~wxCursorRefData()
48 if (m_cursor
) gdk_cursor_unref( m_cursor
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
58 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxGDIObject
)
60 // used in the following two ctors
61 extern GtkWidget
*wxGetRootWindow();
69 wxCursor::wxCursor(const wxString
& cursor_file
,
71 int hotSpotX
, int hotSpotY
)
74 if (!img
.LoadFile(cursor_file
, type
))
77 // eventually set the hotspot:
78 if (!img
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
79 img
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, hotSpotX
);
80 if (!img
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
81 img
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, hotSpotY
);
86 wxCursor::wxCursor(const wxImage
& img
)
92 wxCursor::wxCursor(const char bits
[], int width
, int height
,
93 int hotSpotX
, int hotSpotY
,
94 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
102 if (hotSpotX
< 0 || hotSpotX
>= width
)
104 if (hotSpotY
< 0 || hotSpotY
>= height
)
107 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
108 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
110 m_refData
= new wxCursorRefData
;
111 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
112 data
, mask
, fg
->GetColor(), bg
->GetColor(),
113 hotSpotX
, hotSpotY
);
115 g_object_unref (data
);
116 g_object_unref (mask
);
119 wxCursor::~wxCursor()
123 void wxCursor::InitFromStock( wxStockCursor cursorId
)
125 m_refData
= new wxCursorRefData();
127 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
132 const char bits
[] = { 0 };
133 const GdkColor color
= { 0, 0, 0, 0 };
135 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
136 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(pixmap
,
141 g_object_unref(pixmap
);
145 case wxCURSOR_ARROW
: // fall through to default
146 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
147 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
148 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND2
; break;
149 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
150 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
151 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
152 case wxCURSOR_ARROWWAIT
:
154 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
155 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
156 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
157 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
158 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
159 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
160 case wxCURSOR_SIZENWSE
:
161 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
162 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
163 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
164 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
165 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
166 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
167 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
168 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
169 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
171 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
172 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
174 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
175 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
176 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
177 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
181 wxFAIL_MSG(wxT("unsupported cursor type"));
182 // will use the standard one
186 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
191 static void GetHotSpot(const wxImage
& image
, int& x
, int& y
)
193 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
194 x
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
198 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
199 y
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
203 if (x
< 0 || x
>= image
.GetWidth())
205 if (y
< 0 || y
>= image
.GetHeight())
209 void wxCursor::InitFromImage( const wxImage
& image
)
211 int w
= image
.GetWidth() ;
212 int h
= image
.GetHeight();
213 bool bHasMask
= image
.HasMask();
214 int hotSpotX
, hotSpotY
;
215 GetHotSpot(image
, hotSpotX
, hotSpotY
);
216 m_refData
= new wxCursorRefData
;
217 wxImage
image_copy(image
);
219 GdkDisplay
* display
= gdk_drawable_get_display(wxGetRootWindow()->window
);
220 if (gdk_display_supports_cursor_color(display
))
222 if (!image
.HasAlpha())
224 // add alpha, so wxBitmap will convert to pixbuf format
225 image_copy
.InitAlpha();
227 wxBitmap
bitmap(image_copy
);
228 wxASSERT(bitmap
.HasPixbuf());
229 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixbuf
236 else // no colour cursor support
238 unsigned long keyMaskColor
= 0;
242 keyMaskColor
= wxImageHistogram::MakeKey(
243 image
.GetMaskRed(), image
.GetMaskGreen(), image
.GetMaskBlue());
244 // get mask before image is modified
245 wxBitmap
bitmap(image
, 1);
246 maskRaw
= bitmap
.GetMask()->GetBitmap();
247 g_object_ref(maskRaw
);
251 const int size
= ((w
+ 7) / 8) * h
;
252 char* bits
= new char[size
];
253 memset(bits
, 0xff, size
);
254 maskRaw
= gdk_bitmap_create_from_data(
255 wxGetRootWindow()->window
, bits
, w
, h
);
259 // assign the raw pointer to wxGtkObject to ensure it is unref'd later
260 wxGtkObject
<GdkPixmap
> mask(maskRaw
);
262 // modify image so wxBitmap can be used to convert to pixmap
263 image_copy
.SetMask(false);
264 wxByte
* data
= image_copy
.GetData();
265 for (int j
= 0; j
< h
; j
++)
267 for (int i
= 0; i
< w
; i
++, data
+= 3)
269 // if average value of the pixel is > mid grey, convert it to
270 // background (0), otherwise to foreground (255, using wxBitmap
274 data
[2] = int(data
[0]) + data
[1] + data
[2] >= 3 * 128 ? 0 : 255;
277 wxBitmap
bitmap(image_copy
, 1);
279 // find the most frequent color(s)
280 wxImageHistogram histogram
;
281 image
.ComputeHistogram(histogram
);
283 long colMostFreq
= 0;
284 unsigned long nMost
= 0;
285 long colNextMostFreq
= 0;
286 unsigned long nNext
= 0;
287 for ( wxImageHistogram::iterator entry
= histogram
.begin();
288 entry
!= histogram
.end();
291 unsigned long key
= entry
->first
;
292 if ( !bHasMask
|| (key
!= keyMaskColor
) )
294 unsigned long value
= entry
->second
.value
;
298 colNextMostFreq
= colMostFreq
;
302 else if (value
> nNext
)
305 colNextMostFreq
= key
;
310 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
311 (unsigned char)(colMostFreq
>> 8),
312 (unsigned char)(colMostFreq
) );
314 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
315 (unsigned char)(colNextMostFreq
>> 8),
316 (unsigned char)(colNextMostFreq
) );
318 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
319 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
321 if (bg_intensity
> fg_intensity
)
329 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
333 fg
.GetColor(), bg
.GetColor(),
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 UpdateCursors(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
->GTKUpdateCursor(true, false);
386 UpdateCursors(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 UpdateCursors(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 UpdateCursors(wxTopLevelWindows
, display
);
414 gdk_display_flush(display
);
419 return gs_busyCount
> 0;
422 void wxSetCursor( const wxCursor
& cursor
)
424 g_globalCursor
= cursor
;
425 GdkDisplay
* unused
= NULL
;
426 UpdateCursors(wxTopLevelWindows
, unused
);