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"
19 #include "wx/colour.h"
22 #include "wx/gtk/private.h" //for idle stuff
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 class wxCursorRefData
: public wxObjectRefData
33 virtual ~wxCursorRefData();
38 wxCursorRefData::wxCursorRefData()
40 m_cursor
= (GdkCursor
*) NULL
;
43 wxCursorRefData::~wxCursorRefData()
45 if (m_cursor
) gdk_cursor_unref( m_cursor
);
48 //-----------------------------------------------------------------------------
50 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
52 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
59 wxCursor::wxCursor( int cursorId
)
61 m_refData
= new wxCursorRefData();
63 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
68 static const gchar bits
[] = { 0 };
69 static /* const -- not in GTK1 */ 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
,
80 case wxCURSOR_ARROW
: // fall through to default
81 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
82 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
83 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND1
; break;
84 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
85 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
86 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
87 case wxCURSOR_ARROWWAIT
:
89 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
90 case wxCURSOR_SIZING
: gdk_cur
= GDK_FLEUR
; break;
91 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
92 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
93 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
94 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
95 case wxCURSOR_SIZENWSE
:
96 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
97 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
98 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
99 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
100 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
101 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
102 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
103 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
104 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
106 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
107 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
109 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
110 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
111 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
112 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
116 wxFAIL_MSG(wxT("unsupported cursor type"));
117 // will use the standard one
121 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
124 extern GtkWidget
*wxGetRootWindow();
126 wxCursor::wxCursor(const char bits
[], int width
, int height
,
127 int hotSpotX
, int hotSpotY
,
128 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
136 if (hotSpotX
< 0 || hotSpotX
>= width
)
138 if (hotSpotY
< 0 || hotSpotY
>= height
)
141 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
142 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
144 m_refData
= new wxCursorRefData
;
145 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
146 data
, mask
, fg
->GetColor(), bg
->GetColor(),
147 hotSpotX
, hotSpotY
);
149 g_object_unref (data
);
150 g_object_unref (mask
);
155 static void GetHotSpot(const wxImage
& image
, int& x
, int& y
)
157 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
158 x
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
162 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
163 y
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
167 if (x
< 0 || x
>= image
.GetWidth())
169 if (y
< 0 || y
>= image
.GetHeight())
173 wxCursor::wxCursor( const wxImage
& image
)
175 unsigned char * rgbBits
= image
.GetData();
176 int w
= image
.GetWidth() ;
177 int h
= image
.GetHeight();
178 bool bHasMask
= image
.HasMask();
179 int imagebitcount
= (w
*h
)/8;
182 if (!gtk_check_version(2,4,0))
184 if ( gdk_display_supports_cursor_color(gdk_display_get_default()) )
186 unsigned char rMask
= 0,
191 rMask
= image
.GetMaskRed();
192 gMask
= image
.GetMaskGreen();
193 bMask
= image
.GetMaskBlue();
196 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, w
, h
);
197 unsigned char *alpha
= image
.HasAlpha() ? image
.GetAlpha() : NULL
;
198 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
199 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
200 for ( int y
= 0; y
< h
; y
++, out
+= rowpad
)
202 for ( int x
= 0; x
< w
; x
++, out
+= 4, rgbBits
+= 3 )
208 out
[0] == rMask
&& out
[1] == gMask
&& out
[2] == bMask
)
211 out
[3] = alpha
? *alpha
: 255;
217 int hotSpotX
, hotSpotY
;
218 GetHotSpot(image
, hotSpotX
, hotSpotY
);
220 m_refData
= new wxCursorRefData
;
221 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixbuf
223 gdk_display_get_default(),
227 g_object_unref (pixbuf
);
233 unsigned char * bits
= new unsigned char [imagebitcount
];
234 unsigned char * maskBits
= new unsigned char [imagebitcount
];
236 int i
, j
, i8
; unsigned char c
, cMask
;
237 for (i
=0; i
<imagebitcount
; i
++)
245 // possible overflow if we do the summation first ?
246 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
247 //if average value is > mid grey
249 bits
[i
] = bits
[i
] | cMask
;
254 unsigned long keyMaskColor
;
258 r
= image
.GetMaskRed(),
259 g
= image
.GetMaskGreen(),
260 b
= image
.GetMaskBlue();
262 for (i
=0; i
<imagebitcount
; i
++)
270 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
271 maskBits
[i
] = maskBits
[i
] | cMask
;
276 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
280 for (i
=0; i
<imagebitcount
; i
++)
283 // init it to avoid compiler warnings
287 // find the most frequent color(s)
288 wxImageHistogram histogram
;
289 image
.ComputeHistogram(histogram
);
295 long colMostFreq
= 0;
296 unsigned long nMost
= 0;
297 long colNextMostFreq
= 0;
298 unsigned long nNext
= 0;
299 for ( wxImageHistogram::iterator entry
= histogram
.begin();
300 entry
!= histogram
.end();
303 value
= entry
->second
.value
;
305 if ( !bHasMask
|| (key
!= keyMaskColor
) )
312 else if (value
> nNext
)
315 colNextMostFreq
= key
;
320 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
321 (unsigned char)(colMostFreq
>> 8),
322 (unsigned char)(colMostFreq
) );
324 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
325 (unsigned char)(colNextMostFreq
>> 8),
326 (unsigned char)(colNextMostFreq
) );
328 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
329 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
331 if (bg_intensity
> fg_intensity
)
339 int hotSpotX
, hotSpotY
;
340 GetHotSpot(image
, hotSpotX
, hotSpotY
);
342 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
343 (gchar
*) bits
, w
, h
);
344 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
345 (gchar
*) maskBits
, w
, h
);
347 m_refData
= new wxCursorRefData
;
348 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
352 fg
.GetColor(), bg
.GetColor(),
356 g_object_unref (data
);
357 g_object_unref (mask
);
362 #endif // wxUSE_IMAGE
364 wxCursor::~wxCursor()
368 bool wxCursor::IsOk() const
370 return (m_refData
!= NULL
);
373 GdkCursor
*wxCursor::GetCursor() const
375 return M_CURSORDATA
->m_cursor
;
378 //-----------------------------------------------------------------------------
379 // busy cursor routines
380 //-----------------------------------------------------------------------------
382 extern wxCursor g_globalCursor
;
384 static wxCursor gs_savedCursor
;
385 static int gs_busyCount
= 0;
387 const wxCursor
&wxBusyCursor::GetStoredCursor()
389 return gs_savedCursor
;
392 const wxCursor
wxBusyCursor::GetBusyCursor()
394 return wxCursor(wxCURSOR_WATCH
);
397 void wxEndBusyCursor()
399 if (--gs_busyCount
> 0)
402 wxSetCursor( gs_savedCursor
);
403 gs_savedCursor
= wxNullCursor
;
406 wxTheApp
->ProcessIdle();
409 void wxBeginBusyCursor( const wxCursor
*WXUNUSED(cursor
) )
411 if (gs_busyCount
++ > 0)
414 wxASSERT_MSG( !gs_savedCursor
.Ok(),
415 wxT("forgot to call wxEndBusyCursor, will leak memory") );
417 gs_savedCursor
= g_globalCursor
;
419 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
422 wxTheApp
->ProcessIdle();
429 return gs_busyCount
> 0;
432 void wxSetCursor( const wxCursor
& cursor
)
435 wxapp_install_idle_handler();
437 g_globalCursor
= cursor
;