]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/cursor.cpp
Allow wx parents a chance to handle events fired by non-wx children. (i.e. wxActiveX...
[wxWidgets.git] / src / gtk / cursor.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: cursor.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#include "wx/cursor.h"
14#include "wx/utils.h"
15#include "wx/app.h"
16
17#include <gdk/gdk.h>
18#include <gtk/gtk.h>
19
20//-----------------------------------------------------------------------------
21// idle system
22//-----------------------------------------------------------------------------
23
24extern void wxapp_install_idle_handler();
25extern bool g_isIdle;
26
27//-----------------------------------------------------------------------------
28// wxCursor
29//-----------------------------------------------------------------------------
30
31class wxCursorRefData: public wxObjectRefData
32{
33 public:
34
35 wxCursorRefData();
36 ~wxCursorRefData();
37
38 GdkCursor *m_cursor;
39};
40
41wxCursorRefData::wxCursorRefData()
42{
43 m_cursor = (GdkCursor *) NULL;
44}
45
46wxCursorRefData::~wxCursorRefData()
47{
48 if (m_cursor) gdk_cursor_destroy( m_cursor );
49}
50
51//-----------------------------------------------------------------------------
52
53#define M_CURSORDATA ((wxCursorRefData *)m_refData)
54
55IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
56
57wxCursor::wxCursor()
58{
59
60}
61
62wxCursor::wxCursor( int cursorId )
63{
64 m_refData = new wxCursorRefData();
65
66 GdkCursorType gdk_cur = GDK_LEFT_PTR;
67 switch (cursorId)
68 {
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:
77 case wxCURSOR_WAIT:
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;
94
95 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
96 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
97/*
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;
102*/
103 default:
104 wxFAIL_MSG(wxT("unsupported cursor type"));
105 // will use the standard one
106 break;
107 }
108
109 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
110}
111
112extern GtkWidget *wxGetRootWindow();
113
114wxCursor::wxCursor(const char bits[], int width, int height,
115 int hotSpotX, int hotSpotY,
116 const char maskBits[], wxColour *fg, wxColour *bg)
117{
118 if (!maskBits)
119 maskBits = bits;
120 if (!fg)
121 fg = wxBLACK;
122 if (!bg)
123 bg = wxWHITE;
124 if (hotSpotX < 0 || hotSpotX >= width)
125 hotSpotX = 0;
126 if (hotSpotY < 0 || hotSpotY >= height)
127 hotSpotY = 0;
128
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);
131
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 );
136
137 gdk_bitmap_unref( data );
138 gdk_bitmap_unref( mask );
139}
140
141
142wxCursor::wxCursor( const wxCursor &cursor )
143 : wxObject()
144{
145 Ref( cursor );
146}
147
148#if wxUSE_IMAGE
149
150wxCursor::wxCursor( const wxImage & image )
151{
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;
157
158 unsigned char * bits = new unsigned char [imagebitcount];
159 unsigned char * maskBits = new unsigned char [imagebitcount];
160
161 int i, j, i8; unsigned char c, cMask;
162 for (i=0; i<imagebitcount; i++)
163 {
164 bits[i] = 0;
165 i8 = i * 8;
166
167 cMask = 1;
168 for (j=0; j<8; j++)
169 {
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
173 if (c>127)
174 bits[i] = bits[i] | cMask;
175 cMask = cMask * 2;
176 }
177 }
178
179 unsigned long keyMaskColor;
180 if (bHasMask)
181 {
182 unsigned char
183 r = image.GetMaskRed(),
184 g = image.GetMaskGreen(),
185 b = image.GetMaskBlue();
186
187 for (i=0; i<imagebitcount; i++)
188 {
189 maskBits[i] = 0x0;
190 i8 = i * 8;
191
192 cMask = 1;
193 for (j=0; j<8; j++)
194 {
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;
197 cMask = cMask * 2;
198 }
199 }
200
201 keyMaskColor = (r << 16) | (g << 8) | b;
202 }
203 else // no mask
204 {
205 for (i=0; i<imagebitcount; i++)
206 maskBits[i] = 0xFF;
207
208 // init it to avoid compiler warnings
209 keyMaskColor = 0;
210 }
211
212 // find the most frequent color(s)
213 wxImageHistogram histogram;
214 image.ComputeHistogram(histogram);
215
216 // colors as rrggbb
217 unsigned long key;
218 unsigned long value;
219
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();
226 ++entry )
227 {
228 value = entry->second.value;
229 key = entry->first;
230 if ( !bHasMask || (key != keyMaskColor) )
231 {
232 if (value > nMost)
233 {
234 nMost = value;
235 colMostFreq = key;
236 }
237 else if (value > nNext)
238 {
239 nNext = value;
240 colNextMostFreq = key;
241 }
242 }
243 }
244
245 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
246 (unsigned char)(colMostFreq >> 8),
247 (unsigned char)(colMostFreq) );
248
249 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
250 (unsigned char)(colNextMostFreq >> 8),
251 (unsigned char)(colNextMostFreq) );
252
253 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
254 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
255
256 if (bg_intensity > fg_intensity)
257 {
258 //swap fg and bg
259 wxColour tmp = fg;
260 fg = bg;
261 bg = tmp;
262 }
263
264 int hotSpotX;
265 int hotSpotY;
266
267 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
268 hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
269 else
270 hotSpotX = 0;
271
272 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
273 hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
274 else
275 hotSpotY = 0;
276
277 if (hotSpotX < 0 || hotSpotX >= w)
278 hotSpotX = 0;
279 if (hotSpotY < 0 || hotSpotY >= h)
280 hotSpotY = 0;
281
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);
286
287 m_refData = new wxCursorRefData;
288 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
289 (
290 data,
291 mask,
292 fg.GetColor(), bg.GetColor(),
293 hotSpotX, hotSpotY
294 );
295
296 gdk_bitmap_unref( data );
297 gdk_bitmap_unref( mask );
298 delete [] bits;
299 delete [] maskBits;
300}
301
302#endif // wxUSE_IMAGE
303
304wxCursor::~wxCursor()
305{
306}
307
308wxCursor& wxCursor::operator = ( const wxCursor& cursor )
309{
310 if (*this == cursor)
311 return (*this);
312
313 Ref( cursor );
314
315 return *this;
316}
317
318bool wxCursor::operator == ( const wxCursor& cursor ) const
319{
320 return m_refData == cursor.m_refData;
321}
322
323bool wxCursor::operator != ( const wxCursor& cursor ) const
324{
325 return m_refData != cursor.m_refData;
326}
327
328bool wxCursor::Ok() const
329{
330 return (m_refData != NULL);
331}
332
333GdkCursor *wxCursor::GetCursor() const
334{
335 return M_CURSORDATA->m_cursor;
336}
337
338//-----------------------------------------------------------------------------
339// busy cursor routines
340//-----------------------------------------------------------------------------
341
342extern wxCursor g_globalCursor;
343
344static wxCursor gs_savedCursor;
345static int gs_busyCount = 0;
346
347const wxCursor &wxBusyCursor::GetStoredCursor()
348{
349 return gs_savedCursor;
350}
351
352const wxCursor wxBusyCursor::GetBusyCursor()
353{
354 return wxCursor(wxCURSOR_WATCH);
355}
356
357void wxEndBusyCursor()
358{
359 if (--gs_busyCount > 0)
360 return;
361
362 wxSetCursor( gs_savedCursor );
363 gs_savedCursor = wxNullCursor;
364
365 if (wxTheApp)
366 wxTheApp->ProcessIdle();
367}
368
369void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
370{
371 if (gs_busyCount++ > 0)
372 return;
373
374 wxASSERT_MSG( !gs_savedCursor.Ok(),
375 wxT("forgot to call wxEndBusyCursor, will leak memory") );
376
377 gs_savedCursor = g_globalCursor;
378
379 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
380
381 if (wxTheApp)
382 wxTheApp->ProcessIdle();
383
384 gdk_flush();
385}
386
387bool wxIsBusy()
388{
389 return gs_busyCount > 0;
390}
391
392void wxSetCursor( const wxCursor& cursor )
393{
394 if (g_isIdle)
395 wxapp_install_idle_handler();
396
397 g_globalCursor = cursor;
398}