build fixes for Universal
[wxWidgets.git] / src / gtk / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/cursor.cpp
3 // Purpose: wxCursor implementation
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 #include "wx/log.h"
21 #endif // WX_PRECOMP
22
23 #include <gtk/gtk.h>
24 #include "wx/gtk/private/object.h"
25 #include "wx/gtk/private/gtk2-compat.h"
26
27 //-----------------------------------------------------------------------------
28 // wxCursorRefData
29 //-----------------------------------------------------------------------------
30
31 class wxCursorRefData: public wxGDIRefData
32 {
33 public:
34 wxCursorRefData();
35 virtual ~wxCursorRefData();
36
37 virtual bool IsOk() const { return m_cursor != NULL; }
38
39 GdkCursor *m_cursor;
40
41 private:
42 // There is no way to copy m_cursor so we can't implement a copy ctor
43 // properly.
44 wxDECLARE_NO_COPY_CLASS(wxCursorRefData);
45 };
46
47 wxCursorRefData::wxCursorRefData()
48 {
49 m_cursor = NULL;
50 }
51
52 wxCursorRefData::~wxCursorRefData()
53 {
54 if (m_cursor) gdk_cursor_unref( m_cursor );
55 }
56
57
58 //-----------------------------------------------------------------------------
59 // wxCursor
60 //-----------------------------------------------------------------------------
61
62 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
63
64 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
65
66 // used in the following two ctors
67 extern GtkWidget *wxGetRootWindow();
68
69
70 wxCursor::wxCursor()
71 {
72 }
73
74 #if wxUSE_IMAGE
75 wxCursor::wxCursor(const wxString& cursor_file,
76 wxBitmapType type,
77 int hotSpotX, int hotSpotY)
78 {
79 wxImage img;
80 if (!img.LoadFile(cursor_file, type))
81 return;
82
83 // eventually set the hotspot:
84 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
85 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
86 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
87 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
88
89 InitFromImage(img);
90 }
91
92 wxCursor::wxCursor(const wxImage& img)
93 {
94 InitFromImage(img);
95 }
96 #endif
97
98 wxCursor::wxCursor(const char bits[], int width, int height,
99 int hotSpotX, int hotSpotY,
100 const char maskBits[], const wxColour *fg, const wxColour *bg)
101 {
102 if (!maskBits)
103 maskBits = bits;
104 if (!fg)
105 fg = wxBLACK;
106 if (!bg)
107 bg = wxWHITE;
108 if (hotSpotX < 0 || hotSpotX >= width)
109 hotSpotX = 0;
110 if (hotSpotY < 0 || hotSpotY >= height)
111 hotSpotY = 0;
112
113 GdkBitmap* data = gdk_bitmap_create_from_data(
114 gtk_widget_get_window(wxGetRootWindow()), const_cast<char*>(bits), width, height);
115 GdkBitmap* mask = gdk_bitmap_create_from_data(
116 gtk_widget_get_window(wxGetRootWindow()), const_cast<char*>(maskBits), width, height);
117
118 m_refData = new wxCursorRefData;
119 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
120 data, mask, fg->GetColor(), bg->GetColor(),
121 hotSpotX, hotSpotY );
122
123 g_object_unref (data);
124 g_object_unref (mask);
125 }
126
127 wxCursor::~wxCursor()
128 {
129 }
130
131 void wxCursor::InitFromStock( wxStockCursor cursorId )
132 {
133 m_refData = new wxCursorRefData();
134
135 GdkCursorType gdk_cur = GDK_LEFT_PTR;
136 switch (cursorId)
137 {
138 case wxCURSOR_BLANK:
139 {
140 const char bits[] = { 0 };
141 const GdkColor color = { 0, 0, 0, 0 };
142
143 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
144 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
145 pixmap,
146 &color,
147 &color,
148 0, 0);
149 g_object_unref(pixmap);
150 }
151 return;
152
153 case wxCURSOR_ARROW: // fall through to default
154 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
155 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
156 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
157 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
158 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
159 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
160 case wxCURSOR_ARROWWAIT:
161 case wxCURSOR_WAIT:
162 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
163 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
164 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
165 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
166 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
167 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
168 case wxCURSOR_SIZENWSE:
169 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
170 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
171 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
172 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
173 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
174 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
175 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
176 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
177 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
178
179 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
180 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
181 /*
182 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
183 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
184 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
185 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
186 */
187
188 default:
189 wxFAIL_MSG(wxT("unsupported cursor type"));
190 // will use the standard one
191 break;
192 }
193
194 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
195 }
196
197 #if wxUSE_IMAGE
198
199 static void GetHotSpot(const wxImage& image, int& x, int& y)
200 {
201 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
202 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
203 else
204 x = 0;
205
206 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
207 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
208 else
209 y = 0;
210
211 if (x < 0 || x >= image.GetWidth())
212 x = 0;
213 if (y < 0 || y >= image.GetHeight())
214 y = 0;
215 }
216
217 void wxCursor::InitFromImage( const wxImage & image )
218 {
219 int w = image.GetWidth() ;
220 int h = image.GetHeight();
221 bool bHasMask = image.HasMask();
222 int hotSpotX, hotSpotY;
223 GetHotSpot(image, hotSpotX, hotSpotY);
224 m_refData = new wxCursorRefData;
225 wxImage image_copy(image);
226
227 GdkDisplay* display = gdk_drawable_get_display(gtk_widget_get_window(wxGetRootWindow()));
228 if (gdk_display_supports_cursor_color(display))
229 {
230 if (!image.HasAlpha())
231 {
232 // add alpha, so wxBitmap will convert to pixbuf format
233 image_copy.InitAlpha();
234 }
235 wxBitmap bitmap(image_copy);
236 wxASSERT(bitmap.HasPixbuf());
237 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
238 (
239 display,
240 bitmap.GetPixbuf(),
241 hotSpotX, hotSpotY
242 );
243 }
244 else // no colour cursor support
245 {
246 unsigned long keyMaskColor = 0;
247 GdkPixmap *maskRaw;
248 if (bHasMask)
249 {
250 keyMaskColor = wxImageHistogram::MakeKey(
251 image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
252 // get mask before image is modified
253 wxBitmap bitmap(image, 1);
254 maskRaw = bitmap.GetMask()->GetBitmap();
255 g_object_ref(maskRaw);
256 }
257 else
258 {
259 const int size = ((w + 7) / 8) * h;
260 char* bits = new char[size];
261 memset(bits, 0xff, size);
262 maskRaw = gdk_bitmap_create_from_data(
263 gtk_widget_get_window(wxGetRootWindow()), bits, w, h);
264 delete[] bits;
265 }
266
267 // assign the raw pointer to wxGtkObject to ensure it is unref'd later
268 wxGtkObject<GdkPixmap> mask(maskRaw);
269
270 // modify image so wxBitmap can be used to convert to pixmap
271 image_copy.SetMask(false);
272 wxByte* data = image_copy.GetData();
273 for (int j = 0; j < h; j++)
274 {
275 for (int i = 0; i < w; i++, data += 3)
276 {
277 // if average value of the pixel is > mid grey, convert it to
278 // background (0), otherwise to foreground (255, using wxBitmap
279 // convention)
280 data[0] =
281 data[1] =
282 data[2] = int(data[0]) + data[1] + data[2] >= 3 * 128 ? 0 : 255;
283 }
284 }
285 wxBitmap bitmap(image_copy, 1);
286
287 // find the most frequent color(s)
288 wxImageHistogram histogram;
289 image.ComputeHistogram(histogram);
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 unsigned long key = entry->first;
300 if ( !bHasMask || (key != keyMaskColor) )
301 {
302 unsigned long value = entry->second.value;
303 if (value > nMost)
304 {
305 nNext = nMost;
306 colNextMostFreq = colMostFreq;
307 nMost = value;
308 colMostFreq = key;
309 }
310 else if (value > nNext)
311 {
312 nNext = value;
313 colNextMostFreq = key;
314 }
315 }
316 }
317
318 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
319 (unsigned char)(colMostFreq >> 8),
320 (unsigned char)(colMostFreq) );
321
322 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
323 (unsigned char)(colNextMostFreq >> 8),
324 (unsigned char)(colNextMostFreq) );
325
326 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
327 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
328
329 if (bg_intensity > fg_intensity)
330 {
331 //swap fg and bg
332 wxColour tmp = fg;
333 fg = bg;
334 bg = tmp;
335 }
336
337 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
338 (
339 bitmap.GetPixmap(),
340 mask,
341 fg.GetColor(), bg.GetColor(),
342 hotSpotX, hotSpotY
343 );
344 }
345 }
346
347 #endif // wxUSE_IMAGE
348
349 GdkCursor *wxCursor::GetCursor() const
350 {
351 return M_CURSORDATA->m_cursor;
352 }
353
354 wxGDIRefData *wxCursor::CreateGDIRefData() const
355 {
356 return new wxCursorRefData;
357 }
358
359 wxGDIRefData *
360 wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
361 {
362 // TODO: We can't clone GDK cursors at the moment. To do this we'd need
363 // to remember the original data from which the cursor was created
364 // (i.e. standard cursor type or the bitmap) or use
365 // gdk_cursor_get_cursor_type() (which is in 2.22+ only) and
366 // gdk_cursor_get_image().
367 wxFAIL_MSG( wxS("Cloning cursors is not implemented in wxGTK.") );
368
369 return new wxCursorRefData;
370 }
371
372 //-----------------------------------------------------------------------------
373 // busy cursor routines
374 //-----------------------------------------------------------------------------
375
376 /* Current cursor, in order to hang on to
377 * cursor handle when setting the cursor globally */
378 wxCursor g_globalCursor;
379
380 static wxCursor gs_savedCursor;
381 static int gs_busyCount = 0;
382
383 const wxCursor &wxBusyCursor::GetStoredCursor()
384 {
385 return gs_savedCursor;
386 }
387
388 const wxCursor wxBusyCursor::GetBusyCursor()
389 {
390 return wxCursor(wxCURSOR_WATCH);
391 }
392
393 static void UpdateCursors(GdkDisplay** display)
394 {
395 wxWindowList::const_iterator i = wxTopLevelWindows.begin();
396 for (size_t n = wxTopLevelWindows.size(); n--; ++i)
397 {
398 wxWindow* win = *i;
399 win->GTKUpdateCursor();
400 if (display && *display == NULL && win->m_widget)
401 *display = gtk_widget_get_display(win->m_widget);
402 }
403 }
404
405 void wxEndBusyCursor()
406 {
407 if (--gs_busyCount > 0)
408 return;
409
410 g_globalCursor = gs_savedCursor;
411 gs_savedCursor = wxNullCursor;
412 UpdateCursors(NULL);
413 }
414
415 void wxBeginBusyCursor(const wxCursor* cursor)
416 {
417 if (gs_busyCount++ > 0)
418 return;
419
420 wxASSERT_MSG( !gs_savedCursor.IsOk(),
421 wxT("forgot to call wxEndBusyCursor, will leak memory") );
422
423 gs_savedCursor = g_globalCursor;
424 g_globalCursor = *cursor;
425 GdkDisplay* display = NULL;
426 UpdateCursors(&display);
427 if (display)
428 gdk_display_flush(display);
429 }
430
431 bool wxIsBusy()
432 {
433 return gs_busyCount > 0;
434 }
435
436 void wxSetCursor( const wxCursor& cursor )
437 {
438 g_globalCursor = cursor;
439 UpdateCursors(NULL);
440 }