]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
bracketing msw enhanced metafile usage
[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 private:
41 // There is no way to copy m_cursor so we can't implement a copy ctor
42 // properly.
43 wxDECLARE_NO_COPY_CLASS(wxCursorRefData);
44 };
45
46 wxCursorRefData::wxCursorRefData()
47 {
48 m_cursor = NULL;
49 }
50
51 wxCursorRefData::~wxCursorRefData()
52 {
53 if (m_cursor) gdk_cursor_unref( m_cursor );
54 }
55
56
57 //-----------------------------------------------------------------------------
58 // wxCursor
59 //-----------------------------------------------------------------------------
60
61 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
62
63 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
64
65 // used in the following two ctors
66 extern GtkWidget *wxGetRootWindow();
67
68
69 wxCursor::wxCursor()
70 {
71 }
72
73 #if wxUSE_IMAGE
74 wxCursor::wxCursor(const wxString& cursor_file,
75 wxBitmapType type,
76 int hotSpotX, int hotSpotY)
77 {
78 wxImage img;
79 if (!img.LoadFile(cursor_file, type))
80 return;
81
82 // eventually set the hotspot:
83 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
84 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
85 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
86 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
87
88 InitFromImage(img);
89 }
90
91 wxCursor::wxCursor(const wxImage& img)
92 {
93 InitFromImage(img);
94 }
95 #endif
96
97 wxCursor::wxCursor(const char bits[], int width, int height,
98 int hotSpotX, int hotSpotY,
99 const char maskBits[], const wxColour *fg, const wxColour *bg)
100 {
101 if (!maskBits)
102 maskBits = bits;
103 if (!fg)
104 fg = wxBLACK;
105 if (!bg)
106 bg = wxWHITE;
107 if (hotSpotX < 0 || hotSpotX >= width)
108 hotSpotX = 0;
109 if (hotSpotY < 0 || hotSpotY >= height)
110 hotSpotY = 0;
111
112 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
113 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
114
115 m_refData = new wxCursorRefData;
116 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
117 data, mask, fg->GetColor(), bg->GetColor(),
118 hotSpotX, hotSpotY );
119
120 g_object_unref (data);
121 g_object_unref (mask);
122 }
123
124 wxCursor::~wxCursor()
125 {
126 }
127
128 void wxCursor::InitFromStock( wxStockCursor cursorId )
129 {
130 m_refData = new wxCursorRefData();
131
132 GdkCursorType gdk_cur = GDK_LEFT_PTR;
133 switch (cursorId)
134 {
135 case wxCURSOR_BLANK:
136 {
137 const char bits[] = { 0 };
138 const GdkColor color = { 0, 0, 0, 0 };
139
140 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
141 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
142 pixmap,
143 &color,
144 &color,
145 0, 0);
146 g_object_unref(pixmap);
147 }
148 return;
149
150 case wxCURSOR_ARROW: // fall through to default
151 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
152 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
153 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
154 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
155 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
156 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
157 case wxCURSOR_ARROWWAIT:
158 case wxCURSOR_WAIT:
159 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
160 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
161 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
162 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
163 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
164 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
165 case wxCURSOR_SIZENWSE:
166 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
167 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
168 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
169 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
170 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
171 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
172 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
173 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
174 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
175
176 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
177 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
178 /*
179 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
180 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
181 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
182 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
183 */
184
185 default:
186 wxFAIL_MSG(wxT("unsupported cursor type"));
187 // will use the standard one
188 break;
189 }
190
191 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
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 void wxCursor::InitFromImage( 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 }
241 else // no colour cursor support
242 {
243 unsigned long keyMaskColor = 0;
244 GdkPixmap *maskRaw;
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 maskRaw = bitmap.GetMask()->GetBitmap();
252 g_object_ref(maskRaw);
253 }
254 else
255 {
256 const int size = ((w + 7) / 8) * h;
257 char* bits = new char[size];
258 memset(bits, 0xff, size);
259 maskRaw = gdk_bitmap_create_from_data(
260 wxGetRootWindow()->window, bits, w, h);
261 delete[] bits;
262 }
263
264 // assign the raw pointer to wxGtkObject to ensure it is unref'd later
265 wxGtkObject<GdkPixmap> mask(maskRaw);
266
267 // modify image so wxBitmap can be used to convert to pixmap
268 image_copy.SetMask(false);
269 wxByte* data = image_copy.GetData();
270 for (int j = 0; j < h; j++)
271 {
272 for (int i = 0; i < w; i++, data += 3)
273 {
274 // if average value of the pixel is > mid grey, convert it to
275 // background (0), otherwise to foreground (255, using wxBitmap
276 // convention)
277 data[0] =
278 data[1] =
279 data[2] = int(data[0]) + data[1] + data[2] >= 3 * 128 ? 0 : 255;
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 }
343
344 #endif // wxUSE_IMAGE
345
346 GdkCursor *wxCursor::GetCursor() const
347 {
348 return M_CURSORDATA->m_cursor;
349 }
350
351 wxGDIRefData *wxCursor::CreateGDIRefData() const
352 {
353 return new wxCursorRefData;
354 }
355
356 wxGDIRefData *
357 wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
358 {
359 // TODO: We can't clone GDK cursors at the moment. To do this we'd need
360 // to remember the original data from which the cursor was created
361 // (i.e. standard cursor type or the bitmap) or use
362 // gdk_cursor_get_cursor_type() (which is in 2.22+ only) and
363 // gdk_cursor_get_image().
364 wxFAIL_MSG( wxS("Cloning cursors is not implemented in wxGTK.") );
365
366 return new wxCursorRefData;
367 }
368
369 //-----------------------------------------------------------------------------
370 // busy cursor routines
371 //-----------------------------------------------------------------------------
372
373 /* Current cursor, in order to hang on to
374 * cursor handle when setting the cursor globally */
375 wxCursor g_globalCursor;
376
377 static wxCursor gs_savedCursor;
378 static int gs_busyCount = 0;
379
380 const wxCursor &wxBusyCursor::GetStoredCursor()
381 {
382 return gs_savedCursor;
383 }
384
385 const wxCursor wxBusyCursor::GetBusyCursor()
386 {
387 return wxCursor(wxCURSOR_WATCH);
388 }
389
390 static void UpdateCursors(const wxWindowList& list, GdkDisplay*& display)
391 {
392 wxWindowList::const_iterator i = list.begin();
393 for (size_t n = list.size(); n--; ++i)
394 {
395 wxWindow* win = *i;
396 if (display == NULL && win->m_widget && win->m_widget->window)
397 display = gdk_drawable_get_display(win->m_widget->window);
398 win->GTKUpdateCursor(true, false);
399 UpdateCursors(win->GetChildren(), display);
400 }
401 }
402
403 void wxEndBusyCursor()
404 {
405 if (--gs_busyCount > 0)
406 return;
407
408 g_globalCursor = gs_savedCursor;
409 gs_savedCursor = wxNullCursor;
410 GdkDisplay* unused = NULL;
411 UpdateCursors(wxTopLevelWindows, unused);
412 }
413
414 void wxBeginBusyCursor(const wxCursor* cursor)
415 {
416 if (gs_busyCount++ > 0)
417 return;
418
419 wxASSERT_MSG( !gs_savedCursor.Ok(),
420 wxT("forgot to call wxEndBusyCursor, will leak memory") );
421
422 gs_savedCursor = g_globalCursor;
423 g_globalCursor = *cursor;
424 GdkDisplay* display = NULL;
425 UpdateCursors(wxTopLevelWindows, display);
426 if (display)
427 gdk_display_flush(display);
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 GdkDisplay* unused = NULL;
439 UpdateCursors(wxTopLevelWindows, unused);
440 }