fix leak when creating wxCURSOR_BLANK
[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 <gtk/gtk.h>
23
24 //-----------------------------------------------------------------------------
25 // wxCursor
26 //-----------------------------------------------------------------------------
27
28 class wxCursorRefData: public wxGDIRefData
29 {
30 public:
31 wxCursorRefData();
32 virtual ~wxCursorRefData();
33
34 virtual bool IsOk() const { return m_cursor != NULL; }
35
36 GdkCursor *m_cursor;
37 };
38
39 wxCursorRefData::wxCursorRefData()
40 {
41 m_cursor = (GdkCursor *) NULL;
42 }
43
44 wxCursorRefData::~wxCursorRefData()
45 {
46 if (m_cursor) gdk_cursor_unref( m_cursor );
47 }
48
49 //-----------------------------------------------------------------------------
50
51 #define M_CURSORDATA wx_static_cast(wxCursorRefData*, m_refData)
52
53 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
54
55 wxCursor::wxCursor()
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 const char bits[] = { 0 };
69 const 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 g_object_unref(pixmap);
78 }
79 return;
80
81 case wxCURSOR_ARROW: // fall through to default
82 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
83 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
84 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
85 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
86 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
87 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
88 case wxCURSOR_ARROWWAIT:
89 case wxCURSOR_WAIT:
90 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
91 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
92 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
93 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
94 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
95 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
96 case wxCURSOR_SIZENWSE:
97 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
98 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
99 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
100 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
101 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
102 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
103 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
104 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
105 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
106
107 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
108 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
109 /*
110 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
111 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
112 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
113 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
114 */
115
116 default:
117 wxFAIL_MSG(wxT("unsupported cursor type"));
118 // will use the standard one
119 break;
120 }
121
122 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
123 }
124
125 extern GtkWidget *wxGetRootWindow();
126
127 wxCursor::wxCursor(const char bits[], int width, int height,
128 int hotSpotX, int hotSpotY,
129 const char maskBits[], const wxColour *fg, const wxColour *bg)
130 {
131 if (!maskBits)
132 maskBits = bits;
133 if (!fg)
134 fg = wxBLACK;
135 if (!bg)
136 bg = wxWHITE;
137 if (hotSpotX < 0 || hotSpotX >= width)
138 hotSpotX = 0;
139 if (hotSpotY < 0 || hotSpotY >= height)
140 hotSpotY = 0;
141
142 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
143 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
144
145 m_refData = new wxCursorRefData;
146 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
147 data, mask, fg->GetColor(), bg->GetColor(),
148 hotSpotX, hotSpotY );
149
150 g_object_unref (data);
151 g_object_unref (mask);
152 }
153
154 #if wxUSE_IMAGE
155
156 static void GetHotSpot(const wxImage& image, int& x, int& y)
157 {
158 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
159 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
160 else
161 x = 0;
162
163 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
164 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
165 else
166 y = 0;
167
168 if (x < 0 || x >= image.GetWidth())
169 x = 0;
170 if (y < 0 || y >= image.GetHeight())
171 y = 0;
172 }
173
174 wxCursor::wxCursor( const wxImage & image )
175 {
176 unsigned char * rgbBits = image.GetData();
177 int w = image.GetWidth() ;
178 int h = image.GetHeight();
179 bool bHasMask = image.HasMask();
180 int imagebitcount = (w*h)/8;
181
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
229 unsigned char * bits = new unsigned char [imagebitcount];
230 unsigned char * maskBits = new unsigned char [imagebitcount];
231
232 int i, j, i8; unsigned char c, cMask;
233 for (i=0; i<imagebitcount; i++)
234 {
235 bits[i] = 0;
236 i8 = i * 8;
237
238 cMask = 1;
239 for (j=0; j<8; j++)
240 {
241 // possible overflow if we do the summation first ?
242 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
243 //if average value is > mid grey
244 if (c>127)
245 bits[i] = bits[i] | cMask;
246 cMask = cMask * 2;
247 }
248 }
249
250 unsigned long keyMaskColor;
251 if (bHasMask)
252 {
253 unsigned char
254 r = image.GetMaskRed(),
255 g = image.GetMaskGreen(),
256 b = image.GetMaskBlue();
257
258 for (i=0; i<imagebitcount; i++)
259 {
260 maskBits[i] = 0x0;
261 i8 = i * 8;
262
263 cMask = 1;
264 for (j=0; j<8; j++)
265 {
266 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
267 maskBits[i] = maskBits[i] | cMask;
268 cMask = cMask * 2;
269 }
270 }
271
272 keyMaskColor = (r << 16) | (g << 8) | b;
273 }
274 else // no mask
275 {
276 for (i=0; i<imagebitcount; i++)
277 maskBits[i] = 0xFF;
278
279 // init it to avoid compiler warnings
280 keyMaskColor = 0;
281 }
282
283 // find the most frequent color(s)
284 wxImageHistogram histogram;
285 image.ComputeHistogram(histogram);
286
287 // colors as rrggbb
288 unsigned long key;
289 unsigned long value;
290
291 long colMostFreq = 0;
292 unsigned long nMost = 0;
293 long colNextMostFreq = 0;
294 unsigned long nNext = 0;
295 for ( wxImageHistogram::iterator entry = histogram.begin();
296 entry != histogram.end();
297 ++entry )
298 {
299 value = entry->second.value;
300 key = entry->first;
301 if ( !bHasMask || (key != keyMaskColor) )
302 {
303 if (value > nMost)
304 {
305 nMost = value;
306 colMostFreq = key;
307 }
308 else if (value > nNext)
309 {
310 nNext = value;
311 colNextMostFreq = key;
312 }
313 }
314 }
315
316 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
317 (unsigned char)(colMostFreq >> 8),
318 (unsigned char)(colMostFreq) );
319
320 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
321 (unsigned char)(colNextMostFreq >> 8),
322 (unsigned char)(colNextMostFreq) );
323
324 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
325 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
326
327 if (bg_intensity > fg_intensity)
328 {
329 //swap fg and bg
330 wxColour tmp = fg;
331 fg = bg;
332 bg = tmp;
333 }
334
335 int hotSpotX, hotSpotY;
336 GetHotSpot(image, hotSpotX, hotSpotY);
337
338 GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
339 (gchar *) bits, w, h);
340 GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
341 (gchar *) maskBits, w, h);
342
343 m_refData = new wxCursorRefData;
344 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
345 (
346 data,
347 mask,
348 fg.GetColor(), bg.GetColor(),
349 hotSpotX, hotSpotY
350 );
351
352 g_object_unref (data);
353 g_object_unref (mask);
354 delete [] bits;
355 delete [] maskBits;
356 }
357
358 #endif // wxUSE_IMAGE
359
360 wxCursor::~wxCursor()
361 {
362 }
363
364 GdkCursor *wxCursor::GetCursor() const
365 {
366 return M_CURSORDATA->m_cursor;
367 }
368
369 wxGDIRefData *wxCursor::CreateGDIRefData() const
370 {
371 return new wxCursorRefData;
372 }
373
374 wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
375 {
376 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData *, data));
377 }
378
379 //-----------------------------------------------------------------------------
380 // busy cursor routines
381 //-----------------------------------------------------------------------------
382
383 /* Current cursor, in order to hang on to
384 * cursor handle when setting the cursor globally */
385 wxCursor g_globalCursor;
386
387 static wxCursor gs_savedCursor;
388 static int gs_busyCount = 0;
389
390 const wxCursor &wxBusyCursor::GetStoredCursor()
391 {
392 return gs_savedCursor;
393 }
394
395 const wxCursor wxBusyCursor::GetBusyCursor()
396 {
397 return wxCursor(wxCURSOR_WATCH);
398 }
399
400 void wxEndBusyCursor()
401 {
402 if (--gs_busyCount > 0)
403 return;
404
405 wxSetCursor( gs_savedCursor );
406 gs_savedCursor = wxNullCursor;
407
408 if (wxTheApp)
409 wxTheApp->ProcessIdle();
410 }
411
412 void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) )
413 {
414 if (gs_busyCount++ > 0)
415 return;
416
417 wxASSERT_MSG( !gs_savedCursor.Ok(),
418 wxT("forgot to call wxEndBusyCursor, will leak memory") );
419
420 gs_savedCursor = g_globalCursor;
421
422 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
423
424 if (wxTheApp)
425 wxTheApp->ProcessIdle();
426
427 gdk_flush();
428 }
429
430 bool wxIsBusy()
431 {
432 return gs_busyCount > 0;
433 }
434
435 void wxSetCursor( const wxCursor& cursor )
436 {
437 g_globalCursor = cursor;
438 wxTheApp->WakeUpIdle();
439 }