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
;
69 case wxCURSOR_ARROW
: // fall through to default
70 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
71 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
72 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND1
; break;
73 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
74 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
75 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
76 case wxCURSOR_ARROWWAIT
:
78 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
79 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
80 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
81 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
82 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
83 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
84 case wxCURSOR_SIZENWSE
:
85 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
86 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
87 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
88 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
89 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
90 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
91 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
92 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
93 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
95 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
96 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
98 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
99 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
100 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
101 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
104 wxFAIL_MSG(wxT("unsupported cursor type"));
105 // will use the standard one
109 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
112 extern GtkWidget
*wxGetRootWindow();
114 wxCursor::wxCursor(const char bits
[], int width
, int height
,
115 int hotSpotX
, int hotSpotY
,
116 const char maskBits
[], wxColour
*fg
, wxColour
*bg
)
124 if (hotSpotX
< 0 || hotSpotX
>= width
)
126 if (hotSpotY
< 0 || hotSpotY
>= height
)
129 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
130 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
132 m_refData
= new wxCursorRefData
;
133 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
134 data
, mask
, fg
->GetColor(), bg
->GetColor(),
135 hotSpotX
, hotSpotY
);
137 gdk_bitmap_unref( data
);
138 gdk_bitmap_unref( mask
);
142 wxCursor::wxCursor( const wxCursor
&cursor
)
150 wxCursor::wxCursor( const wxImage
& image
)
152 unsigned char * rgbBits
= image
.GetData();
153 int w
= image
.GetWidth() ;
154 int h
= image
.GetHeight();
155 bool bHasMask
= image
.HasMask();
156 int imagebitcount
= (w
*h
)/8;
158 unsigned char * bits
= new unsigned char [imagebitcount
];
159 unsigned char * maskBits
= new unsigned char [imagebitcount
];
161 int i
, j
, i8
; unsigned char c
, cMask
;
162 for (i
=0; i
<imagebitcount
; i
++)
170 // possible overflow if we do the summation first ?
171 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
172 //if average value is > mid grey
174 bits
[i
] = bits
[i
] | cMask
;
179 unsigned long keyMaskColor
;
183 r
= image
.GetMaskRed(),
184 g
= image
.GetMaskGreen(),
185 b
= image
.GetMaskBlue();
187 for (i
=0; i
<imagebitcount
; i
++)
195 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
196 maskBits
[i
] = maskBits
[i
] | cMask
;
201 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
205 for (i
=0; i
<imagebitcount
; i
++)
208 // init it to avoid compiler warnings
212 // find the most frequent color(s)
213 wxImageHistogram histogram
;
214 image
.ComputeHistogram(histogram
);
220 long colMostFreq
= 0;
221 unsigned long nMost
= 0;
222 long colNextMostFreq
= 0;
223 unsigned long nNext
= 0;
224 for ( wxImageHistogram::iterator entry
= histogram
.begin();
225 entry
!= histogram
.end();
228 value
= entry
->second
.value
;
230 if ( !bHasMask
|| (key
!= keyMaskColor
) )
237 else if (value
> nNext
)
240 colNextMostFreq
= key
;
245 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
246 (unsigned char)(colMostFreq
>> 8),
247 (unsigned char)(colMostFreq
) );
249 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
250 (unsigned char)(colNextMostFreq
>> 8),
251 (unsigned char)(colNextMostFreq
) );
253 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
254 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
256 if (bg_intensity
> fg_intensity
)
267 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
268 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
272 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
273 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
277 if (hotSpotX
< 0 || hotSpotX
>= w
)
279 if (hotSpotY
< 0 || hotSpotY
>= h
)
282 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
283 (gchar
*) bits
, w
, h
);
284 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
285 (gchar
*) maskBits
, w
, h
);
287 m_refData
= new wxCursorRefData
;
288 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
292 fg
.GetColor(), bg
.GetColor(),
296 gdk_bitmap_unref( data
);
297 gdk_bitmap_unref( mask
);
302 #endif // wxUSE_IMAGE
304 wxCursor::~wxCursor()
308 wxCursor
& wxCursor::operator = ( const wxCursor
& cursor
)
318 bool wxCursor::operator == ( const wxCursor
& cursor
) const
320 return m_refData
== cursor
.m_refData
;
323 bool wxCursor::operator != ( const wxCursor
& cursor
) const
325 return m_refData
!= cursor
.m_refData
;
328 bool wxCursor::Ok() const
330 return (m_refData
!= NULL
);
333 GdkCursor
*wxCursor::GetCursor() const
335 return M_CURSORDATA
->m_cursor
;
338 //-----------------------------------------------------------------------------
339 // busy cursor routines
340 //-----------------------------------------------------------------------------
342 extern wxCursor g_globalCursor
;
344 static wxCursor gs_savedCursor
;
345 static int gs_busyCount
= 0;
347 const wxCursor
&wxBusyCursor::GetStoredCursor()
349 return gs_savedCursor
;
352 const wxCursor
wxBusyCursor::GetBusyCursor()
354 return wxCursor(wxCURSOR_WATCH
);
357 void wxEndBusyCursor()
359 if (--gs_busyCount
> 0)
362 wxSetCursor( gs_savedCursor
);
363 gs_savedCursor
= wxNullCursor
;
366 wxTheApp
->ProcessIdle();
369 void wxBeginBusyCursor( wxCursor
*WXUNUSED(cursor
) )
371 if (gs_busyCount
++ > 0)
374 wxASSERT_MSG( !gs_savedCursor
.Ok(),
375 wxT("forgot to call wxEndBusyCursor, will leak memory") );
377 gs_savedCursor
= g_globalCursor
;
379 wxSetCursor( wxCursor(wxCURSOR_WATCH
) );
382 wxTheApp
->ProcessIdle();
389 return gs_busyCount
> 0;
392 void wxSetCursor( const wxCursor
& cursor
)
395 wxapp_install_idle_handler();
397 g_globalCursor
= cursor
;