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