]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
provide backward-compat wxCursor(int) ctor; remove empty stubs of XBM ctor from all...
[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/window.h"
17 #include "wx/app.h"
18 #include "wx/image.h"
19 #include "wx/bitmap.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 void wxCursor::InitFromStock( wxStockCursor 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_HAND2; 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
126 // used in the following two ctors
127 extern GtkWidget *wxGetRootWindow();
128
129 wxCursor::wxCursor(const wxString& cursor_file,
130 wxBitmapType type,
131 int hotSpotX, int hotSpotY)
132 {
133 /* TODO: test this code! */
134
135 // Must be an XBM file
136 if (type != wxBITMAP_TYPE_XPM) {
137 wxLogError("Invalid cursor bitmap type '%d'", type);
138 return;
139 }
140
141 // load the XPM
142 GdkBitmap *mask = NULL;
143 GdkBitmap *data = gdk_pixmap_create_from_xpm( wxGetRootWindow()->window,
144 &mask, NULL, cursor_file.mb_str() );
145 if (!data)
146 return;
147
148 // check given hotspot
149 gint w, h;
150 gdk_drawable_get_size( data, &w, &h );
151 if (hotSpotX < 0 || hotSpotX >= w)
152 hotSpotX = 0;
153 if (hotSpotY < 0 || hotSpotY >= h)
154 hotSpotY = 0;
155
156 // create the real cursor
157 m_refData = new wxCursorRefData;
158 M_CURSORDATA->m_cursor =
159 gdk_cursor_new_from_pixmap( data, mask,
160 wxBLACK->GetColor(), wxWHITE->GetColor(),
161 hotSpotX, hotSpotY );
162
163 g_object_unref (data);
164 g_object_unref (mask);
165 }
166
167 wxCursor::wxCursor(const char bits[], int width, int height,
168 int hotSpotX, int hotSpotY,
169 const char maskBits[], const wxColour *fg, const wxColour *bg)
170 {
171 if (!maskBits)
172 maskBits = bits;
173 if (!fg)
174 fg = wxBLACK;
175 if (!bg)
176 bg = wxWHITE;
177 if (hotSpotX < 0 || hotSpotX >= width)
178 hotSpotX = 0;
179 if (hotSpotY < 0 || hotSpotY >= height)
180 hotSpotY = 0;
181
182 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
183 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
184
185 m_refData = new wxCursorRefData;
186 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
187 data, mask, fg->GetColor(), bg->GetColor(),
188 hotSpotX, hotSpotY );
189
190 g_object_unref (data);
191 g_object_unref (mask);
192 }
193
194 #if wxUSE_IMAGE
195
196 static void GetHotSpot(const wxImage& image, int& x, int& y)
197 {
198 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
199 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
200 else
201 x = 0;
202
203 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
204 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
205 else
206 y = 0;
207
208 if (x < 0 || x >= image.GetWidth())
209 x = 0;
210 if (y < 0 || y >= image.GetHeight())
211 y = 0;
212 }
213
214 wxCursor::wxCursor( const wxImage & image )
215 {
216 int w = image.GetWidth() ;
217 int h = image.GetHeight();
218 bool bHasMask = image.HasMask();
219 int hotSpotX, hotSpotY;
220 GetHotSpot(image, hotSpotX, hotSpotY);
221 m_refData = new wxCursorRefData;
222 wxImage image_copy(image);
223
224 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
225 if (gdk_display_supports_cursor_color(display))
226 {
227 if (!image.HasAlpha())
228 {
229 // add alpha, so wxBitmap will convert to pixbuf format
230 image_copy.InitAlpha();
231 }
232 wxBitmap bitmap(image_copy);
233 wxASSERT(bitmap.HasPixbuf());
234 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
235 (
236 display,
237 bitmap.GetPixbuf(),
238 hotSpotX, hotSpotY
239 );
240 return;
241 }
242
243 unsigned long keyMaskColor = 0;
244 GdkPixmap* mask;
245 if (bHasMask)
246 {
247 keyMaskColor = wxImageHistogram::MakeKey(
248 image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
249 // get mask before image is modified
250 wxBitmap bitmap(image, 1);
251 mask = bitmap.GetMask()->GetBitmap();
252 g_object_ref(mask);
253 }
254 else
255 {
256 const int size = ((w + 7) / 8) * h;
257 char* bits = new char[size];
258 memset(bits, 0xff, size);
259 mask = gdk_bitmap_create_from_data(
260 wxGetRootWindow()->window, bits, w, h);
261 delete[] bits;
262 }
263
264 // modify image so wxBitmap can be used to convert to pixmap
265 image_copy.SetMask(false);
266 int i, j;
267 wxByte* data = image_copy.GetData();
268 for (j = 0; j < h; j++)
269 {
270 for (i = 0; i < w; i++, data += 3)
271 {
272 //if average value is > mid grey
273 if (int(data[0]) + data[1] + data[2] >= 3 * 128)
274 {
275 // wxBitmap only converts (255,255,255) to white
276 data[0] = 255;
277 data[1] = 255;
278 data[2] = 255;
279 }
280 }
281 }
282 wxBitmap bitmap(image_copy, 1);
283
284 // find the most frequent color(s)
285 wxImageHistogram histogram;
286 image.ComputeHistogram(histogram);
287
288 long colMostFreq = 0;
289 unsigned long nMost = 0;
290 long colNextMostFreq = 0;
291 unsigned long nNext = 0;
292 for ( wxImageHistogram::iterator entry = histogram.begin();
293 entry != histogram.end();
294 ++entry )
295 {
296 unsigned long key = entry->first;
297 if ( !bHasMask || (key != keyMaskColor) )
298 {
299 unsigned long value = entry->second.value;
300 if (value > nMost)
301 {
302 nNext = nMost;
303 colNextMostFreq = colMostFreq;
304 nMost = value;
305 colMostFreq = key;
306 }
307 else if (value > nNext)
308 {
309 nNext = value;
310 colNextMostFreq = key;
311 }
312 }
313 }
314
315 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
316 (unsigned char)(colMostFreq >> 8),
317 (unsigned char)(colMostFreq) );
318
319 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
320 (unsigned char)(colNextMostFreq >> 8),
321 (unsigned char)(colNextMostFreq) );
322
323 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
324 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
325
326 if (bg_intensity > fg_intensity)
327 {
328 //swap fg and bg
329 wxColour tmp = fg;
330 fg = bg;
331 bg = tmp;
332 }
333
334 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
335 (
336 bitmap.GetPixmap(),
337 mask,
338 fg.GetColor(), bg.GetColor(),
339 hotSpotX, hotSpotY
340 );
341
342 g_object_unref (mask);
343 }
344
345 #endif // wxUSE_IMAGE
346
347 wxCursor::~wxCursor()
348 {
349 }
350
351 GdkCursor *wxCursor::GetCursor() const
352 {
353 return M_CURSORDATA->m_cursor;
354 }
355
356 wxGDIRefData *wxCursor::CreateGDIRefData() const
357 {
358 return new wxCursorRefData;
359 }
360
361 wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
362 {
363 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData *, data));
364 }
365
366 //-----------------------------------------------------------------------------
367 // busy cursor routines
368 //-----------------------------------------------------------------------------
369
370 /* Current cursor, in order to hang on to
371 * cursor handle when setting the cursor globally */
372 wxCursor g_globalCursor;
373
374 static wxCursor gs_savedCursor;
375 static int gs_busyCount = 0;
376
377 const wxCursor &wxBusyCursor::GetStoredCursor()
378 {
379 return gs_savedCursor;
380 }
381
382 const wxCursor wxBusyCursor::GetBusyCursor()
383 {
384 return wxCursor(wxCURSOR_WATCH);
385 }
386
387 static void InternalIdle(const wxWindowList& list, GdkDisplay*& display)
388 {
389 wxWindowList::const_iterator i = list.begin();
390 for (size_t n = list.size(); n--; ++i)
391 {
392 wxWindow* win = *i;
393 if (display == NULL && win->m_widget && win->m_widget->window)
394 display = gdk_drawable_get_display(win->m_widget->window);
395 win->OnInternalIdle();
396 InternalIdle(win->GetChildren(), display);
397 }
398 }
399
400 void wxEndBusyCursor()
401 {
402 if (--gs_busyCount > 0)
403 return;
404
405 g_globalCursor = gs_savedCursor;
406 gs_savedCursor = wxNullCursor;
407 GdkDisplay* unused = NULL;
408 InternalIdle(wxTopLevelWindows, unused);
409 }
410
411 void wxBeginBusyCursor(const wxCursor* cursor)
412 {
413 if (gs_busyCount++ > 0)
414 return;
415
416 wxASSERT_MSG( !gs_savedCursor.Ok(),
417 wxT("forgot to call wxEndBusyCursor, will leak memory") );
418
419 gs_savedCursor = g_globalCursor;
420 g_globalCursor = *cursor;
421 GdkDisplay* display = NULL;
422 InternalIdle(wxTopLevelWindows, display);
423 if (display)
424 gdk_display_flush(display);
425 }
426
427 bool wxIsBusy()
428 {
429 return gs_busyCount > 0;
430 }
431
432 void wxSetCursor( const wxCursor& cursor )
433 {
434 g_globalCursor = cursor;
435 wxTheApp->WakeUpIdle();
436 }