1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 class wxCursorRefData
: public wxGDIRefData
38 virtual ~wxCursorRefData();
43 wxCursorRefData::wxCursorRefData()
45 m_cursor
= (GdkCursor
*) NULL
;
48 wxCursorRefData::~wxCursorRefData()
50 if (m_cursor
) gdk_cursor_destroy( m_cursor
);
53 //-----------------------------------------------------------------------------
55 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
57 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
64 wxCursor::wxCursor( int cursorId
)
66 m_refData
= new wxCursorRefData();
68 GdkCursorType gdk_cur
= GDK_LEFT_PTR
;
73 static const gchar bits
[] = { 0 };
74 static /* const -- not in GTK1 */ GdkColor color
= { 0, 0, 0, 0 };
76 GdkPixmap
*pixmap
= gdk_bitmap_create_from_data(NULL
, bits
, 1, 1);
77 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(pixmap
,
85 case wxCURSOR_ARROW
: // fall through to default
86 case wxCURSOR_DEFAULT
: gdk_cur
= GDK_LEFT_PTR
; break;
87 case wxCURSOR_RIGHT_ARROW
: gdk_cur
= GDK_RIGHT_PTR
; break;
88 case wxCURSOR_HAND
: gdk_cur
= GDK_HAND1
; break;
89 case wxCURSOR_CROSS
: gdk_cur
= GDK_CROSSHAIR
; break;
90 case wxCURSOR_SIZEWE
: gdk_cur
= GDK_SB_H_DOUBLE_ARROW
; break;
91 case wxCURSOR_SIZENS
: gdk_cur
= GDK_SB_V_DOUBLE_ARROW
; break;
92 case wxCURSOR_ARROWWAIT
:
94 case wxCURSOR_WATCH
: gdk_cur
= GDK_WATCH
; break;
95 case wxCURSOR_SIZING
: gdk_cur
= GDK_SIZING
; break;
96 case wxCURSOR_SPRAYCAN
: gdk_cur
= GDK_SPRAYCAN
; break;
97 case wxCURSOR_IBEAM
: gdk_cur
= GDK_XTERM
; break;
98 case wxCURSOR_PENCIL
: gdk_cur
= GDK_PENCIL
; break;
99 case wxCURSOR_NO_ENTRY
: gdk_cur
= GDK_PIRATE
; break;
100 case wxCURSOR_SIZENWSE
:
101 case wxCURSOR_SIZENESW
: gdk_cur
= GDK_FLEUR
; break;
102 case wxCURSOR_QUESTION_ARROW
: gdk_cur
= GDK_QUESTION_ARROW
; break;
103 case wxCURSOR_PAINT_BRUSH
: gdk_cur
= GDK_SPRAYCAN
; break;
104 case wxCURSOR_MAGNIFIER
: gdk_cur
= GDK_PLUS
; break;
105 case wxCURSOR_CHAR
: gdk_cur
= GDK_XTERM
; break;
106 case wxCURSOR_LEFT_BUTTON
: gdk_cur
= GDK_LEFTBUTTON
; break;
107 case wxCURSOR_MIDDLE_BUTTON
: gdk_cur
= GDK_MIDDLEBUTTON
; break;
108 case wxCURSOR_RIGHT_BUTTON
: gdk_cur
= GDK_RIGHTBUTTON
; break;
109 case wxCURSOR_BULLSEYE
: gdk_cur
= GDK_TARGET
; break;
111 case wxCURSOR_POINT_LEFT
: gdk_cur
= GDK_SB_LEFT_ARROW
; break;
112 case wxCURSOR_POINT_RIGHT
: gdk_cur
= GDK_SB_RIGHT_ARROW
; break;
114 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
115 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
116 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
117 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
121 wxFAIL_MSG(wxT("unsupported cursor type"));
122 // will use the standard one
126 M_CURSORDATA
->m_cursor
= gdk_cursor_new( gdk_cur
);
129 extern GtkWidget
*wxGetRootWindow();
131 wxCursor::wxCursor(const char bits
[], int width
, int height
,
132 int hotSpotX
, int hotSpotY
,
133 const char maskBits
[], const wxColour
*fg
, const wxColour
*bg
)
141 if (hotSpotX
< 0 || hotSpotX
>= width
)
143 if (hotSpotY
< 0 || hotSpotY
>= height
)
146 GdkBitmap
*data
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
147 GdkBitmap
*mask
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) maskBits
, width
, height
);
149 m_refData
= new wxCursorRefData
;
150 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap(
151 data
, mask
, fg
->GetColor(), bg
->GetColor(),
152 hotSpotX
, hotSpotY
);
154 gdk_bitmap_unref( data
);
155 gdk_bitmap_unref( mask
);
160 wxCursor::wxCursor( const wxImage
& image
)
162 unsigned char * rgbBits
= image
.GetData();
163 int w
= image
.GetWidth() ;
164 int h
= image
.GetHeight();
165 bool bHasMask
= image
.HasMask();
166 int imagebitcount
= (w
*h
)/8;
168 unsigned char * bits
= new unsigned char [imagebitcount
];
169 unsigned char * maskBits
= new unsigned char [imagebitcount
];
171 int i
, j
, i8
; unsigned char c
, cMask
;
172 for (i
=0; i
<imagebitcount
; i
++)
180 // possible overflow if we do the summation first ?
181 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
182 //if average value is > mid grey
184 bits
[i
] = bits
[i
] | cMask
;
189 unsigned long keyMaskColor
;
193 r
= image
.GetMaskRed(),
194 g
= image
.GetMaskGreen(),
195 b
= image
.GetMaskBlue();
197 for (i
=0; i
<imagebitcount
; i
++)
205 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
206 maskBits
[i
] = maskBits
[i
] | cMask
;
211 keyMaskColor
= (r
<< 16) | (g
<< 8) | b
;
215 for (i
=0; i
<imagebitcount
; i
++)
218 // init it to avoid compiler warnings
222 // find the most frequent color(s)
223 wxImageHistogram histogram
;
224 image
.ComputeHistogram(histogram
);
230 long colMostFreq
= 0;
231 unsigned long nMost
= 0;
232 long colNextMostFreq
= 0;
233 unsigned long nNext
= 0;
234 for ( wxImageHistogram::iterator entry
= histogram
.begin();
235 entry
!= histogram
.end();
238 value
= entry
->second
.value
;
240 if ( !bHasMask
|| (key
!= keyMaskColor
) )
247 else if (value
> nNext
)
250 colNextMostFreq
= key
;
255 wxColour fg
= wxColour ( (unsigned char)(colMostFreq
>> 16),
256 (unsigned char)(colMostFreq
>> 8),
257 (unsigned char)(colMostFreq
) );
259 wxColour bg
= wxColour ( (unsigned char)(colNextMostFreq
>> 16),
260 (unsigned char)(colNextMostFreq
>> 8),
261 (unsigned char)(colNextMostFreq
) );
263 int fg_intensity
= fg
.Red() + fg
.Green() + fg
.Blue();
264 int bg_intensity
= bg
.Red() + bg
.Green() + bg
.Blue();
266 if (bg_intensity
> fg_intensity
)
277 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
278 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
282 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
283 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
287 if (hotSpotX
< 0 || hotSpotX
>= w
)
289 if (hotSpotY
< 0 || hotSpotY
>= h
)
292 GdkBitmap
*data
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
293 (gchar
*) bits
, w
, h
);
294 GdkBitmap
*mask
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
,
295 (gchar
*) maskBits
, w
, h
);
297 m_refData
= new wxCursorRefData
;
298 M_CURSORDATA
->m_cursor
= gdk_cursor_new_from_pixmap
302 fg
.GetColor(), bg
.GetColor(),
306 gdk_bitmap_unref( data
);
307 gdk_bitmap_unref( mask
);
312 #endif // wxUSE_IMAGE
314 wxCursor::~wxCursor()
318 GdkCursor
*wxCursor::GetCursor() const
320 return M_CURSORDATA
->m_cursor
;
323 wxGDIRefData
*wxCursor::CreateGDIRefData() const
325 return new wxCursorRefData
;
328 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
330 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData
*, data
));
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
;