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 void wxCursor::InitFromStock( wxStockCursor 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
);
126 // used in the following two ctors
127 extern GtkWidget
*wxGetRootWindow();
129 wxCursor::wxCursor(const wxString
& cursor_file
,
131 int hotSpotX
, int hotSpotY
)
133 /* TODO: test this code! */
135 // Must be an XBM file
136 if (type
!= wxBITMAP_TYPE_XPM
) {
137 wxLogError("Invalid cursor bitmap type '%d'", type
);
142 GdkBitmap
*mask
= NULL
;
143 GdkBitmap
*data
= gdk_pixmap_create_from_xpm( wxGetRootWindow()->window
,
144 &mask
, NULL
, cursor_file
.mb_str() );
148 // check given hotspot
150 gdk_drawable_get_size( data
, &w
, &h
);
151 if (hotSpotX
< 0 || hotSpotX
>= w
)
153 if (hotSpotY
< 0 || hotSpotY
>= h
)
156 // create the real cursor
157 m_refData
= new wxCursorRefData
;
158 M_CURSORDATA
->m_cursor
=
159 gdk_cursor_new_from_pixmap( data
, mask
,
160 wxBLACK
->GetColor(), wxWHITE
->GetColor(),
161 hotSpotX
, hotSpotY
);
163 g_object_unref (data
);
164 g_object_unref (mask
);
167 wxCursor::wxCursor(const char bits
[], int width
, int height
,
168 int hotSpotX
, int hotSpotY
,
169 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
177 if (hotSpotX
< 0 || hotSpotX
>= width
)
179 if (hotSpotY
< 0 || hotSpotY
>= height
)
182 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
183 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
185 m_refData
= new wxCursorRefData
;
186 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
187 data
, mask
, fg
->GetColor(), bg
->GetColor(),
188 hotSpotX
, hotSpotY
);
190 g_object_unref (data
);
191 g_object_unref (mask
);
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 wxCursor::wxCursor( 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
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 mask
= bitmap
.GetMask()->GetBitmap();
256 const int size
= ((w
+ 7) / 8) * h
;
257 char* bits
= new char[size
];
258 memset(bits
, 0xff, size
);
259 mask
= gdk_bitmap_create_from_data(
260 wxGetRootWindow()->window
, bits
, w
, h
);
264 // modify image so wxBitmap can be used to convert to pixmap
265 image_copy
.SetMask(false);
267 wxByte
* data
= image_copy
.GetData();
268 for (j
= 0; j
< h
; j
++)
270 for (i
= 0; i
< w
; i
++, data
+= 3)
272 //if average value is > mid grey
273 if (int(data
[0]) + data
[1] + data
[2] >= 3 * 128)
275 // wxBitmap only converts (255,255,255) to white
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(),
342 g_object_unref (mask
);
345 #endif // wxUSE_IMAGE
347 wxCursor::~wxCursor()
351 GdkCursor
*wxCursor::GetCursor() const
353 return M_CURSORDATA
->m_cursor
;
356 wxGDIRefData
*wxCursor::CreateGDIRefData() const
358 return new wxCursorRefData
;
361 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
363 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData
*, data
));
366 //-----------------------------------------------------------------------------
367 // busy cursor routines
368 //-----------------------------------------------------------------------------
370 /* Current cursor, in order to hang on to
371 * cursor handle when setting the cursor globally */
372 wxCursor g_globalCursor
;
374 static wxCursor gs_savedCursor
;
375 static int gs_busyCount
= 0;
377 const wxCursor
&wxBusyCursor::GetStoredCursor()
379 return gs_savedCursor
;
382 const wxCursor
wxBusyCursor::GetBusyCursor()
384 return wxCursor(wxCURSOR_WATCH
);
387 static void InternalIdle(const wxWindowList
& list
, GdkDisplay
*& display
)
389 wxWindowList::const_iterator i
= list
.begin();
390 for (size_t n
= list
.size(); n
--; ++i
)
393 if (display
== NULL
&& win
->m_widget
&& win
->m_widget
->window
)
394 display
= gdk_drawable_get_display(win
->m_widget
->window
);
395 win
->OnInternalIdle();
396 InternalIdle(win
->GetChildren(), display
);
400 void wxEndBusyCursor()
402 if (--gs_busyCount
> 0)
405 g_globalCursor
= gs_savedCursor
;
406 gs_savedCursor
= wxNullCursor
;
407 GdkDisplay
* unused
= NULL
;
408 InternalIdle(wxTopLevelWindows
, unused
);
411 void wxBeginBusyCursor(const wxCursor
* cursor
)
413 if (gs_busyCount
++ > 0)
416 wxASSERT_MSG( !gs_savedCursor
.Ok(),
417 wxT("forgot to call wxEndBusyCursor, will leak memory") );
419 gs_savedCursor
= g_globalCursor
;
420 g_globalCursor
= *cursor
;
421 GdkDisplay
* display
= NULL
;
422 InternalIdle(wxTopLevelWindows
, display
);
424 gdk_display_flush(display
);
429 return gs_busyCount
> 0;
432 void wxSetCursor( const wxCursor
& cursor
)
434 g_globalCursor
= cursor
;
435 wxTheApp
->WakeUpIdle();