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"
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 ((wxCursorRefData *)m_refData)
53 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
60 wxCursor::wxCursor( int cursorId
)
62 m_refData
= new wxCursorRefData();
64 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
69 static const gchar bits
[] = { 0 };
70 static /* const -- not in GTK1 */ GdkColor color
= { 0, 0, 0, 0 };
72 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
73 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(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_HAND1
; 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
);
125 extern GtkWidget
*wxGetRootWindow();
127 wxCursor::wxCursor(const char bits
[], int width
, int height
,
128 int hotSpotX
, int hotSpotY
,
129 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
137 if (hotSpotX
< 0 || hotSpotX
>= width
)
139 if (hotSpotY
< 0 || hotSpotY
>= height
)
142 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
143 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
145 m_refData
= new wxCursorRefData
;
146 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
147 data
, mask
, fg
->GetColor(), bg
->GetColor(),
148 hotSpotX
, hotSpotY
);
150 g_object_unref (data
);
151 g_object_unref (mask
);
156 static void GetHotSpot(const wxImage
& image
, int& x
, int& y
)
158 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
159 x
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
163 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
164 y
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
168 if (x
< 0 || x
>= image
.GetWidth())
170 if (y
< 0 || y
>= image
.GetHeight())
174 wxCursor::wxCursor( const wxImage
& image
)
176 unsigned char * rgbBits
= image
.GetData();
177 int w
= image
.GetWidth() ;
178 int h
= image
.GetHeight();
179 bool bHasMask
= image
.HasMask();
180 int imagebitcount
= (w
*h
)/8;
182 if ( gdk_display_supports_cursor_color(gdk_display_get_default()) )
184 unsigned char rMask
= 0,
189 rMask
= image
.GetMaskRed();
190 gMask
= image
.GetMaskGreen();
191 bMask
= image
.GetMaskBlue();
194 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, w
, h
);
195 unsigned char *alpha
= image
.HasAlpha() ? image
.GetAlpha() : NULL
;
196 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
197 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
198 for ( int y
= 0; y
< h
; y
++, out
+= rowpad
)
200 for ( int x
= 0; x
< w
; x
++, out
+= 4, rgbBits
+= 3 )
206 out
[0] == rMask
&& out
[1] == gMask
&& out
[2] == bMask
)
209 out
[3] = alpha
? *alpha
: 255;
215 int hotSpotX
, hotSpotY
;
216 GetHotSpot(image
, hotSpotX
, hotSpotY
);
218 m_refData
= new wxCursorRefData
;
219 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixbuf
221 gdk_display_get_default(),
225 g_object_unref (pixbuf
);
229 unsigned char * bits
= new unsigned char [imagebitcount
];
230 unsigned char * maskBits
= new unsigned char [imagebitcount
];
232 int i
, j
, i8
; unsigned char c
, cMask
;
233 for (i
=0; i
<imagebitcount
; i
++)
241 // possible overflow if we do the summation first ?
242 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
243 //if average value is > mid grey
245 bits
[i
] = bits
[i
] | cMask
;
250 unsigned long keyMaskColor
;
254 r
= image
.GetMaskRed(),
255 g
= image
.GetMaskGreen(),
256 b
= image
.GetMaskBlue();
258 for (i
=0; i
<imagebitcount
; i
++)
266 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
267 maskBits
[i
] = maskBits
[i
] | cMask
;
272 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
276 for (i
=0; i
<imagebitcount
; i
++)
279 // init it to avoid compiler warnings
283 // find the most frequent color(s)
284 wxImageHistogram histogram
;
285 image
.ComputeHistogram(histogram
);
291 long colMostFreq
= 0;
292 unsigned long nMost
= 0;
293 long colNextMostFreq
= 0;
294 unsigned long nNext
= 0;
295 for ( wxImageHistogram::iterator entry
= histogram
.begin();
296 entry
!= histogram
.end();
299 value
= entry
->second
.value
;
301 if ( !bHasMask
|| (key
!= keyMaskColor
) )
308 else if (value
> nNext
)
311 colNextMostFreq
= key
;
316 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
317 (unsigned char)(colMostFreq
>> 8),
318 (unsigned char)(colMostFreq
) );
320 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
321 (unsigned char)(colNextMostFreq
>> 8),
322 (unsigned char)(colNextMostFreq
) );
324 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
325 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
327 if (bg_intensity
> fg_intensity
)
335 int hotSpotX
, hotSpotY
;
336 GetHotSpot(image
, hotSpotX
, hotSpotY
);
338 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
339 (gchar
*) bits
, w
, h
);
340 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
341 (gchar
*) maskBits
, w
, h
);
343 m_refData
= new wxCursorRefData
;
344 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
348 fg
.GetColor(), bg
.GetColor(),
352 g_object_unref (data
);
353 g_object_unref (mask
);
358 #endif // wxUSE_IMAGE
360 wxCursor::~wxCursor()
364 GdkCursor
*wxCursor::GetCursor() const
366 return M_CURSORDATA
->m_cursor
;
369 wxGDIRefData
*wxCursor::CreateGDIRefData() const
371 return new wxCursorRefData
;
374 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
376 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData
*, data
));
379 //-----------------------------------------------------------------------------
380 // busy cursor routines
381 //-----------------------------------------------------------------------------
383 /* Current cursor, in order to hang on to
384 * cursor handle when setting the cursor globally */
385 wxCursor g_globalCursor
;
387 static wxCursor gs_savedCursor
;
388 static int gs_busyCount
= 0;
390 const wxCursor
&wxBusyCursor::GetStoredCursor()
392 return gs_savedCursor
;
395 const wxCursor
wxBusyCursor::GetBusyCursor()
397 return wxCursor(wxCURSOR_WATCH
);
400 void wxEndBusyCursor()
402 if (--gs_busyCount
> 0)
405 wxSetCursor( gs_savedCursor
);
406 gs_savedCursor
= wxNullCursor
;
409 wxTheApp
->ProcessIdle();
412 void wxBeginBusyCursor( const wxCursor
*WXUNUSED(cursor
) )
414 if (gs_busyCount
++ > 0)
417 wxASSERT_MSG( !gs_savedCursor
.Ok(),
418 wxT("forgot to call wxEndBusyCursor, will leak memory") );
420 gs_savedCursor
= g_globalCursor
;
422 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
425 wxTheApp
->ProcessIdle();
432 return gs_busyCount
> 0;
435 void wxSetCursor( const wxCursor
& cursor
)
437 g_globalCursor
= cursor
;
438 wxTheApp
->WakeUpIdle();