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 wxCursor::wxCursor( const wxImage
& image
)
157 unsigned char * rgbBits
= image
.GetData();
158 int w
= image
.GetWidth() ;
159 int h
= image
.GetHeight();
160 bool bHasMask
= image
.HasMask();
161 int imagebitcount
= (w
*h
)/8;
163 unsigned char * bits
= new unsigned char [imagebitcount
];
164 unsigned char * maskBits
= new unsigned char [imagebitcount
];
166 int i
, j
, i8
; unsigned char c
, cMask
;
167 for (i
=0; i
<imagebitcount
; i
++)
175 // possible overflow if we do the summation first ?
176 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
177 //if average value is > mid grey
179 bits
[i
] = bits
[i
] | cMask
;
184 unsigned long keyMaskColor
;
188 r
= image
.GetMaskRed(),
189 g
= image
.GetMaskGreen(),
190 b
= image
.GetMaskBlue();
192 for (i
=0; i
<imagebitcount
; i
++)
200 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
201 maskBits
[i
] = maskBits
[i
] | cMask
;
206 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
210 for (i
=0; i
<imagebitcount
; i
++)
213 // init it to avoid compiler warnings
217 // find the most frequent color(s)
218 wxImageHistogram histogram
;
219 image
.ComputeHistogram(histogram
);
225 long colMostFreq
= 0;
226 unsigned long nMost
= 0;
227 long colNextMostFreq
= 0;
228 unsigned long nNext
= 0;
229 for ( wxImageHistogram::iterator entry
= histogram
.begin();
230 entry
!= histogram
.end();
233 value
= entry
->second
.value
;
235 if ( !bHasMask
|| (key
!= keyMaskColor
) )
242 else if (value
> nNext
)
245 colNextMostFreq
= key
;
250 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
251 (unsigned char)(colMostFreq
>> 8),
252 (unsigned char)(colMostFreq
) );
254 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
255 (unsigned char)(colNextMostFreq
>> 8),
256 (unsigned char)(colNextMostFreq
) );
258 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
259 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
261 if (bg_intensity
> fg_intensity
)
272 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
273 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
277 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
278 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
282 if (hotSpotX
< 0 || hotSpotX
>= w
)
284 if (hotSpotY
< 0 || hotSpotY
>= h
)
287 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
288 (gchar
*) bits
, w
, h
);
289 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
290 (gchar
*) maskBits
, w
, h
);
292 m_refData
= new wxCursorRefData
;
293 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
297 fg
.GetColor(), bg
.GetColor(),
301 g_object_unref (data
);
302 g_object_unref (mask
);
307 #endif // wxUSE_IMAGE
309 wxCursor::~wxCursor()
313 bool wxCursor::operator == ( const wxCursor
& cursor
) const
315 return m_refData
== cursor
.m_refData
;
318 bool wxCursor::operator != ( const wxCursor
& cursor
) const
320 return m_refData
!= cursor
.m_refData
;
323 bool wxCursor::IsOk() const
325 return (m_refData
!= NULL
);
328 GdkCursor
*wxCursor::GetCursor() const
330 return M_CURSORDATA
->m_cursor
;
333 //-----------------------------------------------------------------------------
334 // busy cursor routines
335 //-----------------------------------------------------------------------------
337 extern wxCursor g_globalCursor
;
339 static wxCursor gs_savedCursor
;
340 static int gs_busyCount
= 0;
342 const wxCursor
&wxBusyCursor::GetStoredCursor()
344 return gs_savedCursor
;
347 const wxCursor
wxBusyCursor::GetBusyCursor()
349 return wxCursor(wxCURSOR_WATCH
);
352 void wxEndBusyCursor()
354 if (--gs_busyCount
> 0)
357 wxSetCursor( gs_savedCursor
);
358 gs_savedCursor
= wxNullCursor
;
361 wxTheApp
->ProcessIdle();
364 void wxBeginBusyCursor( const wxCursor
*WXUNUSED(cursor
) )
366 if (gs_busyCount
++ > 0)
369 wxASSERT_MSG( !gs_savedCursor
.Ok(),
370 wxT("forgot to call wxEndBusyCursor, will leak memory") );
372 gs_savedCursor
= g_globalCursor
;
374 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
377 wxTheApp
->ProcessIdle();
384 return gs_busyCount
> 0;
387 void wxSetCursor( const wxCursor
& cursor
)
390 wxapp_install_idle_handler();
392 g_globalCursor
= cursor
;