1 /////////////////////////////////////////////////////////////////////////////
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"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern void wxapp_install_idle_handler();
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 class wxCursorRefData
: public wxObjectRefData
41 wxCursorRefData::wxCursorRefData()
43 m_cursor
= (GdkCursor
*) NULL
;
46 wxCursorRefData::~wxCursorRefData()
48 if (m_cursor
) gdk_cursor_destroy( m_cursor
);
51 //-----------------------------------------------------------------------------
53 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
55 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
62 wxCursor::wxCursor( int cursorId
)
64 m_refData
= new wxCursorRefData();
66 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
71 static const gchar bits
[] = { 0 };
72 static /* const -- not in GTK1 */ GdkColor color
= { 0, 0, 0, 0 };
74 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
75 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(pixmap
,
83 case wxCURSOR_ARROW
: // fall through to default
84 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
85 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
86 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND1
; break;
87 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
88 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
89 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
90 case wxCURSOR_ARROWWAIT
:
92 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
93 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
94 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
95 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
96 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
97 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
98 case wxCURSOR_SIZENWSE
:
99 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
100 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
101 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
102 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
103 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
104 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
105 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
106 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
107 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
109 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
110 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
112 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
113 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
114 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
115 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
119 wxFAIL_MSG(wxT("unsupported cursor type"));
120 // will use the standard one
124 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
127 extern GtkWidget
*wxGetRootWindow();
129 wxCursor::wxCursor(const char bits
[], int width
, int height
,
130 int hotSpotX
, int hotSpotY
,
131 const char maskBits
[], wxColour
*fg
, wxColour
*bg
)
139 if (hotSpotX
< 0 || hotSpotX
>= width
)
141 if (hotSpotY
< 0 || hotSpotY
>= height
)
144 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
145 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
147 m_refData
= new wxCursorRefData
;
148 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
149 data
, mask
, fg
->GetColor(), bg
->GetColor(),
150 hotSpotX
, hotSpotY
);
152 gdk_bitmap_unref( data
);
153 gdk_bitmap_unref( mask
);
158 wxCursor::wxCursor( const wxImage
& image
)
160 unsigned char * rgbBits
= image
.GetData();
161 int w
= image
.GetWidth() ;
162 int h
= image
.GetHeight();
163 bool bHasMask
= image
.HasMask();
164 int imagebitcount
= (w
*h
)/8;
166 unsigned char * bits
= new unsigned char [imagebitcount
];
167 unsigned char * maskBits
= new unsigned char [imagebitcount
];
169 int i
, j
, i8
; unsigned char c
, cMask
;
170 for (i
=0; i
<imagebitcount
; i
++)
178 // possible overflow if we do the summation first ?
179 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
180 //if average value is > mid grey
182 bits
[i
] = bits
[i
] | cMask
;
187 unsigned long keyMaskColor
;
191 r
= image
.GetMaskRed(),
192 g
= image
.GetMaskGreen(),
193 b
= image
.GetMaskBlue();
195 for (i
=0; i
<imagebitcount
; i
++)
203 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
204 maskBits
[i
] = maskBits
[i
] | cMask
;
209 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
213 for (i
=0; i
<imagebitcount
; i
++)
216 // init it to avoid compiler warnings
220 // find the most frequent color(s)
221 wxImageHistogram histogram
;
222 image
.ComputeHistogram(histogram
);
228 long colMostFreq
= 0;
229 unsigned long nMost
= 0;
230 long colNextMostFreq
= 0;
231 unsigned long nNext
= 0;
232 for ( wxImageHistogram::iterator entry
= histogram
.begin();
233 entry
!= histogram
.end();
236 value
= entry
->second
.value
;
238 if ( !bHasMask
|| (key
!= keyMaskColor
) )
245 else if (value
> nNext
)
248 colNextMostFreq
= key
;
253 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
254 (unsigned char)(colMostFreq
>> 8),
255 (unsigned char)(colMostFreq
) );
257 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
258 (unsigned char)(colNextMostFreq
>> 8),
259 (unsigned char)(colNextMostFreq
) );
261 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
262 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
264 if (bg_intensity
> fg_intensity
)
275 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
276 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
280 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
281 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
285 if (hotSpotX
< 0 || hotSpotX
>= w
)
287 if (hotSpotY
< 0 || hotSpotY
>= h
)
290 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
291 (gchar
*) bits
, w
, h
);
292 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
293 (gchar
*) maskBits
, w
, h
);
295 m_refData
= new wxCursorRefData
;
296 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
300 fg
.GetColor(), bg
.GetColor(),
304 gdk_bitmap_unref( data
);
305 gdk_bitmap_unref( mask
);
310 #endif // wxUSE_IMAGE
312 wxCursor::~wxCursor()
316 bool wxCursor::operator == ( const wxCursor
& cursor
) const
318 return m_refData
== cursor
.m_refData
;
321 bool wxCursor::operator != ( const wxCursor
& cursor
) const
323 return m_refData
!= cursor
.m_refData
;
326 bool wxCursor::Ok() const
328 return (m_refData
!= NULL
);
331 GdkCursor
*wxCursor::GetCursor() const
333 return M_CURSORDATA
->m_cursor
;
336 //-----------------------------------------------------------------------------
337 // busy cursor routines
338 //-----------------------------------------------------------------------------
340 extern wxCursor g_globalCursor
;
342 static wxCursor gs_savedCursor
;
343 static int gs_busyCount
= 0;
345 const wxCursor
&wxBusyCursor::GetStoredCursor()
347 return gs_savedCursor
;
350 const wxCursor
wxBusyCursor::GetBusyCursor()
352 return wxCursor(wxCURSOR_WATCH
);
355 void wxEndBusyCursor()
357 if (--gs_busyCount
> 0)
360 wxSetCursor( gs_savedCursor
);
361 gs_savedCursor
= wxNullCursor
;
364 wxTheApp
->ProcessIdle();
367 void wxBeginBusyCursor( wxCursor
*WXUNUSED(cursor
) )
369 if (gs_busyCount
++ > 0)
372 wxASSERT_MSG( !gs_savedCursor
.Ok(),
373 wxT("forgot to call wxEndBusyCursor, will leak memory") );
375 gs_savedCursor
= g_globalCursor
;
377 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
380 wxTheApp
->ProcessIdle();
387 return gs_busyCount
> 0;
390 void wxSetCursor( const wxCursor
& cursor
)
393 wxapp_install_idle_handler();
395 g_globalCursor
= cursor
;