And gdk_drawable_unref is in turn deprecated in favor of g_object_unref
[wxWidgets.git] / src / gtk / cursor.cpp
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 "wx/gtk/private.h" //for idle stuff
18
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21
22 //-----------------------------------------------------------------------------
23 // wxCursor
24 //-----------------------------------------------------------------------------
25
26 class wxCursorRefData: public wxObjectRefData
27 {
28 public:
29
30 wxCursorRefData();
31 ~wxCursorRefData();
32
33 GdkCursor *m_cursor;
34 };
35
36 wxCursorRefData::wxCursorRefData()
37 {
38 m_cursor = (GdkCursor *) NULL;
39 }
40
41 wxCursorRefData::~wxCursorRefData()
42 {
43 if (m_cursor) gdk_cursor_destroy( m_cursor );
44 }
45
46 //-----------------------------------------------------------------------------
47
48 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
49
50 IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
51
52 wxCursor::wxCursor()
53 {
54
55 }
56
57 wxCursor::wxCursor( int cursorId )
58 {
59 m_refData = new wxCursorRefData();
60
61 GdkCursorType gdk_cur = GDK_LEFT_PTR;
62 switch (cursorId)
63 {
64 case wxCURSOR_BLANK:
65 {
66 static const gchar bits[] = { 0 };
67 static /* const -- not in GTK1 */ GdkColor color = { 0, 0, 0, 0 };
68
69 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
70 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
71 pixmap,
72 &color,
73 &color,
74 0, 0);
75 }
76 return;
77
78 case wxCURSOR_ARROW: // fall through to default
79 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
80 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
81 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
82 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
83 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
84 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
85 case wxCURSOR_ARROWWAIT:
86 case wxCURSOR_WAIT:
87 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
88 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
89 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
90 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
91 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
92 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
93 case wxCURSOR_SIZENWSE:
94 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
95 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
96 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
97 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
98 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
99 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
100 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
101 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
102 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
103
104 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
105 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
106 /*
107 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
108 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
109 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
110 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
111 */
112
113 default:
114 wxFAIL_MSG(wxT("unsupported cursor type"));
115 // will use the standard one
116 break;
117 }
118
119 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
120 }
121
122 extern GtkWidget *wxGetRootWindow();
123
124 wxCursor::wxCursor(const char bits[], int width, int height,
125 int hotSpotX, int hotSpotY,
126 const char maskBits[], wxColour *fg, wxColour *bg)
127 {
128 if (!maskBits)
129 maskBits = bits;
130 if (!fg)
131 fg = wxBLACK;
132 if (!bg)
133 bg = wxWHITE;
134 if (hotSpotX < 0 || hotSpotX >= width)
135 hotSpotX = 0;
136 if (hotSpotY < 0 || hotSpotY >= height)
137 hotSpotY = 0;
138
139 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
140 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
141
142 m_refData = new wxCursorRefData;
143 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
144 data, mask, fg->GetColor(), bg->GetColor(),
145 hotSpotX, hotSpotY );
146
147 g_object_unref (G_OBJECT (data));
148 g_object_unref (G_OBJECT (mask));
149 }
150
151 #if wxUSE_IMAGE
152
153 wxCursor::wxCursor( const wxImage & image )
154 {
155 unsigned char * rgbBits = image.GetData();
156 int w = image.GetWidth() ;
157 int h = image.GetHeight();
158 bool bHasMask = image.HasMask();
159 int imagebitcount = (w*h)/8;
160
161 unsigned char * bits = new unsigned char [imagebitcount];
162 unsigned char * maskBits = new unsigned char [imagebitcount];
163
164 int i, j, i8; unsigned char c, cMask;
165 for (i=0; i<imagebitcount; i++)
166 {
167 bits[i] = 0;
168 i8 = i * 8;
169
170 cMask = 1;
171 for (j=0; j<8; j++)
172 {
173 // possible overflow if we do the summation first ?
174 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
175 //if average value is > mid grey
176 if (c>127)
177 bits[i] = bits[i] | cMask;
178 cMask = cMask * 2;
179 }
180 }
181
182 unsigned long keyMaskColor;
183 if (bHasMask)
184 {
185 unsigned char
186 r = image.GetMaskRed(),
187 g = image.GetMaskGreen(),
188 b = image.GetMaskBlue();
189
190 for (i=0; i<imagebitcount; i++)
191 {
192 maskBits[i] = 0x0;
193 i8 = i * 8;
194
195 cMask = 1;
196 for (j=0; j<8; j++)
197 {
198 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
199 maskBits[i] = maskBits[i] | cMask;
200 cMask = cMask * 2;
201 }
202 }
203
204 keyMaskColor = (r << 16) | (g << 8) | b;
205 }
206 else // no mask
207 {
208 for (i=0; i<imagebitcount; i++)
209 maskBits[i] = 0xFF;
210
211 // init it to avoid compiler warnings
212 keyMaskColor = 0;
213 }
214
215 // find the most frequent color(s)
216 wxImageHistogram histogram;
217 image.ComputeHistogram(histogram);
218
219 // colors as rrggbb
220 unsigned long key;
221 unsigned long value;
222
223 long colMostFreq = 0;
224 unsigned long nMost = 0;
225 long colNextMostFreq = 0;
226 unsigned long nNext = 0;
227 for ( wxImageHistogram::iterator entry = histogram.begin();
228 entry != histogram.end();
229 ++entry )
230 {
231 value = entry->second.value;
232 key = entry->first;
233 if ( !bHasMask || (key != keyMaskColor) )
234 {
235 if (value > nMost)
236 {
237 nMost = value;
238 colMostFreq = key;
239 }
240 else if (value > nNext)
241 {
242 nNext = value;
243 colNextMostFreq = key;
244 }
245 }
246 }
247
248 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
249 (unsigned char)(colMostFreq >> 8),
250 (unsigned char)(colMostFreq) );
251
252 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
253 (unsigned char)(colNextMostFreq >> 8),
254 (unsigned char)(colNextMostFreq) );
255
256 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
257 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
258
259 if (bg_intensity > fg_intensity)
260 {
261 //swap fg and bg
262 wxColour tmp = fg;
263 fg = bg;
264 bg = tmp;
265 }
266
267 int hotSpotX;
268 int hotSpotY;
269
270 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
271 hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
272 else
273 hotSpotX = 0;
274
275 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
276 hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
277 else
278 hotSpotY = 0;
279
280 if (hotSpotX < 0 || hotSpotX >= w)
281 hotSpotX = 0;
282 if (hotSpotY < 0 || hotSpotY >= h)
283 hotSpotY = 0;
284
285 GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
286 (gchar *) bits, w, h);
287 GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
288 (gchar *) maskBits, w, h);
289
290 m_refData = new wxCursorRefData;
291 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
292 (
293 data,
294 mask,
295 fg.GetColor(), bg.GetColor(),
296 hotSpotX, hotSpotY
297 );
298
299 g_object_unref (G_OBJECT (data));
300 g_object_unref (G_OBJECT (mask));
301 delete [] bits;
302 delete [] maskBits;
303 }
304
305 #endif // wxUSE_IMAGE
306
307 wxCursor::~wxCursor()
308 {
309 }
310
311 bool wxCursor::operator == ( const wxCursor& cursor ) const
312 {
313 return m_refData == cursor.m_refData;
314 }
315
316 bool wxCursor::operator != ( const wxCursor& cursor ) const
317 {
318 return m_refData != cursor.m_refData;
319 }
320
321 bool wxCursor::Ok() const
322 {
323 return (m_refData != NULL);
324 }
325
326 GdkCursor *wxCursor::GetCursor() const
327 {
328 return M_CURSORDATA->m_cursor;
329 }
330
331 //-----------------------------------------------------------------------------
332 // busy cursor routines
333 //-----------------------------------------------------------------------------
334
335 extern wxCursor g_globalCursor;
336
337 static wxCursor gs_savedCursor;
338 static int gs_busyCount = 0;
339
340 const wxCursor &wxBusyCursor::GetStoredCursor()
341 {
342 return gs_savedCursor;
343 }
344
345 const wxCursor wxBusyCursor::GetBusyCursor()
346 {
347 return wxCursor(wxCURSOR_WATCH);
348 }
349
350 void wxEndBusyCursor()
351 {
352 if (--gs_busyCount > 0)
353 return;
354
355 wxSetCursor( gs_savedCursor );
356 gs_savedCursor = wxNullCursor;
357
358 if (wxTheApp)
359 wxTheApp->ProcessIdle();
360 }
361
362 void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
363 {
364 if (gs_busyCount++ > 0)
365 return;
366
367 wxASSERT_MSG( !gs_savedCursor.Ok(),
368 wxT("forgot to call wxEndBusyCursor, will leak memory") );
369
370 gs_savedCursor = g_globalCursor;
371
372 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
373
374 if (wxTheApp)
375 wxTheApp->ProcessIdle();
376
377 gdk_flush();
378 }
379
380 bool wxIsBusy()
381 {
382 return gs_busyCount > 0;
383 }
384
385 void wxSetCursor( const wxCursor& cursor )
386 {
387 if (g_isIdle)
388 wxapp_install_idle_handler();
389
390 g_globalCursor = cursor;
391 }