added support for colour cursors in wxGTK (patch 1655576)
[wxWidgets.git] / src / gtk / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/utils.h"
18 #include "wx/image.h"
19 #include "wx/colour.h"
20 #endif // WX_PRECOMP
21
22 #include "wx/gtk/private.h" //for idle stuff
23
24 //-----------------------------------------------------------------------------
25 // wxCursor
26 //-----------------------------------------------------------------------------
27
28 class wxCursorRefData: public wxObjectRefData
29 {
30 public:
31
32 wxCursorRefData();
33 virtual ~wxCursorRefData();
34
35 GdkCursor *m_cursor;
36 };
37
38 wxCursorRefData::wxCursorRefData()
39 {
40 m_cursor = (GdkCursor *) NULL;
41 }
42
43 wxCursorRefData::~wxCursorRefData()
44 {
45 if (m_cursor) gdk_cursor_unref( m_cursor );
46 }
47
48 //-----------------------------------------------------------------------------
49
50 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
51
52 IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
53
54 wxCursor::wxCursor()
55 {
56
57 }
58
59 wxCursor::wxCursor( int cursorId )
60 {
61 m_refData = new wxCursorRefData();
62
63 GdkCursorType gdk_cur = GDK_LEFT_PTR;
64 switch (cursorId)
65 {
66 case wxCURSOR_BLANK:
67 {
68 static const gchar bits[] = { 0 };
69 static /* const -- not in GTK1 */ GdkColor color = { 0, 0, 0, 0 };
70
71 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
72 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
73 pixmap,
74 &color,
75 &color,
76 0, 0);
77 }
78 return;
79
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:
88 case wxCURSOR_WAIT:
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;
105
106 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
107 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
108 /*
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;
113 */
114
115 default:
116 wxFAIL_MSG(wxT("unsupported cursor type"));
117 // will use the standard one
118 break;
119 }
120
121 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
122 }
123
124 extern GtkWidget *wxGetRootWindow();
125
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)
129 {
130 if (!maskBits)
131 maskBits = bits;
132 if (!fg)
133 fg = wxBLACK;
134 if (!bg)
135 bg = wxWHITE;
136 if (hotSpotX < 0 || hotSpotX >= width)
137 hotSpotX = 0;
138 if (hotSpotY < 0 || hotSpotY >= height)
139 hotSpotY = 0;
140
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);
143
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 );
148
149 g_object_unref (data);
150 g_object_unref (mask);
151 }
152
153 #if wxUSE_IMAGE
154
155 static void GetHotSpot(const wxImage& image, int& x, int& y)
156 {
157 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
158 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
159 else
160 x = 0;
161
162 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
163 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
164 else
165 y = 0;
166
167 if (x < 0 || x >= image.GetWidth())
168 x = 0;
169 if (y < 0 || y >= image.GetHeight())
170 y = 0;
171 }
172
173 wxCursor::wxCursor( const wxImage & image )
174 {
175 unsigned char * rgbBits = image.GetData();
176 int w = image.GetWidth() ;
177 int h = image.GetHeight();
178 bool bHasMask = image.HasMask();
179 int imagebitcount = (w*h)/8;
180
181 #if GTK_CHECK_VERSION(2,2,0)
182 if ( gdk_display_supports_cursor_color(gdk_display_get_default()) )
183 {
184 unsigned char rMask = 0,
185 gMask = 0,
186 bMask = 0;
187 if (bHasMask)
188 {
189 rMask = image.GetMaskRed();
190 gMask = image.GetMaskGreen();
191 bMask = image.GetMaskBlue();
192 }
193
194 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, w, h);
195 unsigned char *alpha = image.HasAlpha() ? image.GetAlpha() : NULL;
196 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
197 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
198 for ( int y = 0; y < h; y++, out += rowpad )
199 {
200 for ( int x = 0; x < w; x++, out += 4, rgbBits += 3 )
201 {
202 out[0] = rgbBits[0];
203 out[1] = rgbBits[1];
204 out[2] = rgbBits[2];
205 if (bHasMask &&
206 out[0] == rMask && out[1] == gMask && out[2] == bMask)
207 out[3] = 0;
208 else
209 out[3] = alpha ? *alpha : 255;
210 if ( alpha )
211 ++alpha;
212 }
213 }
214
215 int hotSpotX, hotSpotY;
216 GetHotSpot(image, hotSpotX, hotSpotY);
217
218 m_refData = new wxCursorRefData;
219 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
220 (
221 gdk_display_get_default(),
222 pixbuf,
223 hotSpotX, hotSpotY
224 );
225 g_object_unref (pixbuf);
226 return;
227 }
228 #endif // GTK+ 2.2+
229
230 unsigned char * bits = new unsigned char [imagebitcount];
231 unsigned char * maskBits = new unsigned char [imagebitcount];
232
233 int i, j, i8; unsigned char c, cMask;
234 for (i=0; i<imagebitcount; i++)
235 {
236 bits[i] = 0;
237 i8 = i * 8;
238
239 cMask = 1;
240 for (j=0; j<8; j++)
241 {
242 // possible overflow if we do the summation first ?
243 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
244 //if average value is > mid grey
245 if (c>127)
246 bits[i] = bits[i] | cMask;
247 cMask = cMask * 2;
248 }
249 }
250
251 unsigned long keyMaskColor;
252 if (bHasMask)
253 {
254 unsigned char
255 r = image.GetMaskRed(),
256 g = image.GetMaskGreen(),
257 b = image.GetMaskBlue();
258
259 for (i=0; i<imagebitcount; i++)
260 {
261 maskBits[i] = 0x0;
262 i8 = i * 8;
263
264 cMask = 1;
265 for (j=0; j<8; j++)
266 {
267 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
268 maskBits[i] = maskBits[i] | cMask;
269 cMask = cMask * 2;
270 }
271 }
272
273 keyMaskColor = (r << 16) | (g << 8) | b;
274 }
275 else // no mask
276 {
277 for (i=0; i<imagebitcount; i++)
278 maskBits[i] = 0xFF;
279
280 // init it to avoid compiler warnings
281 keyMaskColor = 0;
282 }
283
284 // find the most frequent color(s)
285 wxImageHistogram histogram;
286 image.ComputeHistogram(histogram);
287
288 // colors as rrggbb
289 unsigned long key;
290 unsigned long value;
291
292 long colMostFreq = 0;
293 unsigned long nMost = 0;
294 long colNextMostFreq = 0;
295 unsigned long nNext = 0;
296 for ( wxImageHistogram::iterator entry = histogram.begin();
297 entry != histogram.end();
298 ++entry )
299 {
300 value = entry->second.value;
301 key = entry->first;
302 if ( !bHasMask || (key != keyMaskColor) )
303 {
304 if (value > nMost)
305 {
306 nMost = value;
307 colMostFreq = key;
308 }
309 else if (value > nNext)
310 {
311 nNext = value;
312 colNextMostFreq = key;
313 }
314 }
315 }
316
317 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
318 (unsigned char)(colMostFreq >> 8),
319 (unsigned char)(colMostFreq) );
320
321 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
322 (unsigned char)(colNextMostFreq >> 8),
323 (unsigned char)(colNextMostFreq) );
324
325 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
326 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
327
328 if (bg_intensity > fg_intensity)
329 {
330 //swap fg and bg
331 wxColour tmp = fg;
332 fg = bg;
333 bg = tmp;
334 }
335
336 int hotSpotX, hotSpotY;
337 GetHotSpot(image, hotSpotX, hotSpotY);
338
339 GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
340 (gchar *) bits, w, h);
341 GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
342 (gchar *) maskBits, w, h);
343
344 m_refData = new wxCursorRefData;
345 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
346 (
347 data,
348 mask,
349 fg.GetColor(), bg.GetColor(),
350 hotSpotX, hotSpotY
351 );
352
353 g_object_unref (data);
354 g_object_unref (mask);
355 delete [] bits;
356 delete [] maskBits;
357 }
358
359 #endif // wxUSE_IMAGE
360
361 wxCursor::~wxCursor()
362 {
363 }
364
365 bool wxCursor::IsOk() const
366 {
367 return (m_refData != NULL);
368 }
369
370 GdkCursor *wxCursor::GetCursor() const
371 {
372 return M_CURSORDATA->m_cursor;
373 }
374
375 //-----------------------------------------------------------------------------
376 // busy cursor routines
377 //-----------------------------------------------------------------------------
378
379 extern wxCursor g_globalCursor;
380
381 static wxCursor gs_savedCursor;
382 static int gs_busyCount = 0;
383
384 const wxCursor &wxBusyCursor::GetStoredCursor()
385 {
386 return gs_savedCursor;
387 }
388
389 const wxCursor wxBusyCursor::GetBusyCursor()
390 {
391 return wxCursor(wxCURSOR_WATCH);
392 }
393
394 void wxEndBusyCursor()
395 {
396 if (--gs_busyCount > 0)
397 return;
398
399 wxSetCursor( gs_savedCursor );
400 gs_savedCursor = wxNullCursor;
401
402 if (wxTheApp)
403 wxTheApp->ProcessIdle();
404 }
405
406 void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) )
407 {
408 if (gs_busyCount++ > 0)
409 return;
410
411 wxASSERT_MSG( !gs_savedCursor.Ok(),
412 wxT("forgot to call wxEndBusyCursor, will leak memory") );
413
414 gs_savedCursor = g_globalCursor;
415
416 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
417
418 if (wxTheApp)
419 wxTheApp->ProcessIdle();
420
421 gdk_flush();
422 }
423
424 bool wxIsBusy()
425 {
426 return gs_busyCount > 0;
427 }
428
429 void wxSetCursor( const wxCursor& cursor )
430 {
431 if (g_isIdle)
432 wxapp_install_idle_handler();
433
434 g_globalCursor = cursor;
435 }