]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
af25572696ca394ae61a3e32075a19ff1a627bac
[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/image.h"
18 #include "wx/bitmap.h"
19 #include "wx/log.h"
20 #endif // WX_PRECOMP
21
22 #include <gtk/gtk.h>
23 #include "wx/gtk/private/object.h"
24 #include "wx/gtk/private/gtk2-compat.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)
54 {
55 #ifdef __WXGTK3__
56 g_object_unref(m_cursor);
57 #else
58 gdk_cursor_unref(m_cursor);
59 #endif
60 }
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxCursor
65 //-----------------------------------------------------------------------------
66
67 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
68
69 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
70
71 // used in the following two ctors
72 extern GtkWidget *wxGetRootWindow();
73
74 wxCursor::wxCursor()
75 {
76 }
77
78 #if wxUSE_IMAGE
79 wxCursor::wxCursor(const wxString& cursor_file,
80 wxBitmapType type,
81 int hotSpotX, int hotSpotY)
82 {
83 wxImage img;
84 if (!img.LoadFile(cursor_file, type))
85 return;
86
87 // eventually set the hotspot:
88 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
89 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
90 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
91 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
92
93 InitFromImage(img);
94 }
95
96 wxCursor::wxCursor(const wxImage& img)
97 {
98 InitFromImage(img);
99 }
100 #endif
101
102 wxCursor::wxCursor(const char bits[], int width, int height,
103 int hotSpotX, int hotSpotY,
104 const char maskBits[], const wxColour *fg, const wxColour *bg)
105 {
106 m_refData = new wxCursorRefData;
107 if (hotSpotX < 0 || hotSpotX >= width)
108 hotSpotX = 0;
109 if (hotSpotY < 0 || hotSpotY >= height)
110 hotSpotY = 0;
111 #ifdef __WXGTK3__
112 wxBitmap bitmap(bits, width, height);
113 if (maskBits)
114 bitmap.SetMask(new wxMask(wxBitmap(maskBits, width, height)));
115 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
116 if (fg || bg)
117 {
118 const int stride = gdk_pixbuf_get_rowstride(pixbuf);
119 const int n_channels = gdk_pixbuf_get_n_channels(pixbuf);
120 guchar* data = gdk_pixbuf_get_pixels(pixbuf);
121 for (int j = 0; j < height; j++, data += stride)
122 {
123 guchar* p = data;
124 for (int i = 0; i < width; i++, p += n_channels)
125 {
126 if (p[0])
127 {
128 if (fg)
129 {
130 p[0] = fg->Red();
131 p[1] = fg->Green();
132 p[2] = fg->Blue();
133 }
134 }
135 else
136 {
137 if (bg)
138 {
139 p[0] = bg->Red();
140 p[1] = bg->Green();
141 p[2] = bg->Blue();
142 }
143 }
144 }
145 }
146 }
147 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(wxGetRootWindow()), pixbuf, hotSpotX, hotSpotY);
148 #else
149 if (!maskBits)
150 maskBits = bits;
151 if (!fg)
152 fg = wxBLACK;
153 if (!bg)
154 bg = wxWHITE;
155
156 GdkBitmap* data = gdk_bitmap_create_from_data(
157 gtk_widget_get_window(wxGetRootWindow()), const_cast<char*>(bits), width, height);
158 GdkBitmap* mask = gdk_bitmap_create_from_data(
159 gtk_widget_get_window(wxGetRootWindow()), const_cast<char*>(maskBits), width, height);
160
161 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
162 data, mask, fg->GetColor(), bg->GetColor(),
163 hotSpotX, hotSpotY );
164
165 g_object_unref (data);
166 g_object_unref (mask);
167 #endif
168 }
169
170 wxCursor::~wxCursor()
171 {
172 }
173
174 void wxCursor::InitFromStock( wxStockCursor cursorId )
175 {
176 m_refData = new wxCursorRefData();
177
178 GdkCursorType gdk_cur = GDK_LEFT_PTR;
179 switch (cursorId)
180 {
181 #ifdef __WXGTK3__
182 case wxCURSOR_BLANK: gdk_cur = GDK_BLANK_CURSOR; break;
183 #else
184 case wxCURSOR_BLANK:
185 {
186 const char bits[] = { 0 };
187 const GdkColor color = { 0, 0, 0, 0 };
188
189 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
190 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
191 pixmap,
192 &color,
193 &color,
194 0, 0);
195 g_object_unref(pixmap);
196 }
197 return;
198 #endif
199 case wxCURSOR_ARROW: // fall through to default
200 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
201 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
202 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
203 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
204 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
205 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
206 case wxCURSOR_ARROWWAIT:
207 case wxCURSOR_WAIT:
208 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
209 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
210 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
211 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
212 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
213 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
214 case wxCURSOR_SIZENWSE:
215 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
216 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
217 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
218 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
219 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
220 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
221 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
222 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
223 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
224
225 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
226 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
227 /*
228 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
229 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
230 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
231 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
232 */
233
234 default:
235 wxFAIL_MSG(wxT("unsupported cursor type"));
236 // will use the standard one
237 break;
238 }
239
240 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
241 }
242
243 #if wxUSE_IMAGE
244
245 void wxCursor::InitFromImage( const wxImage & image )
246 {
247 const int w = image.GetWidth();
248 const int h = image.GetHeight();
249 const guchar* alpha = image.GetAlpha();
250 const bool hasMask = image.HasMask();
251 int hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
252 int hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
253 if (hotSpotX < 0 || hotSpotX > w) hotSpotX = 0;
254 if (hotSpotY < 0 || hotSpotY > h) hotSpotY = 0;
255 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(image.GetData(), GDK_COLORSPACE_RGB, false, 8, w, h, w * 3, NULL, NULL);
256 if (alpha || hasMask)
257 {
258 guchar r = 0, g = 0, b = 0;
259 if (hasMask)
260 {
261 r = image.GetMaskRed();
262 g = image.GetMaskGreen();
263 b = image.GetMaskBlue();
264 }
265 GdkPixbuf* pixbuf0 = pixbuf;
266 pixbuf = gdk_pixbuf_add_alpha(pixbuf, hasMask, r, g, b);
267 g_object_unref(pixbuf0);
268 if (alpha)
269 {
270 guchar* d = gdk_pixbuf_get_pixels(pixbuf);
271 const int stride = gdk_pixbuf_get_rowstride(pixbuf);
272 for (int j = 0; j < h; j++, d += stride)
273 for (int i = 0; i < w; i++, alpha++)
274 if (d[4 * i + 3])
275 d[4 * i + 3] = *alpha;
276 }
277 }
278 m_refData = new wxCursorRefData;
279 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf(gtk_widget_get_display(wxGetRootWindow()), pixbuf, hotSpotX, hotSpotY);
280 g_object_unref(pixbuf);
281 }
282
283 #endif // wxUSE_IMAGE
284
285 GdkCursor *wxCursor::GetCursor() const
286 {
287 return M_CURSORDATA->m_cursor;
288 }
289
290 wxGDIRefData *wxCursor::CreateGDIRefData() const
291 {
292 return new wxCursorRefData;
293 }
294
295 wxGDIRefData *
296 wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
297 {
298 // TODO: We can't clone GDK cursors at the moment. To do this we'd need
299 // to remember the original data from which the cursor was created
300 // (i.e. standard cursor type or the bitmap) or use
301 // gdk_cursor_get_cursor_type() (which is in 2.22+ only) and
302 // gdk_cursor_get_image().
303 wxFAIL_MSG( wxS("Cloning cursors is not implemented in wxGTK.") );
304
305 return new wxCursorRefData;
306 }
307
308 //-----------------------------------------------------------------------------
309 // busy cursor routines
310 //-----------------------------------------------------------------------------
311
312 /* Current cursor, in order to hang on to
313 * cursor handle when setting the cursor globally */
314 wxCursor g_globalCursor;
315
316 static wxCursor gs_savedCursor;
317 static int gs_busyCount = 0;
318
319 const wxCursor &wxBusyCursor::GetStoredCursor()
320 {
321 return gs_savedCursor;
322 }
323
324 const wxCursor wxBusyCursor::GetBusyCursor()
325 {
326 return wxCursor(wxCURSOR_WATCH);
327 }
328
329 static void UpdateCursors(GdkDisplay** display)
330 {
331 wxWindowList::const_iterator i = wxTopLevelWindows.begin();
332 for (size_t n = wxTopLevelWindows.size(); n--; ++i)
333 {
334 wxWindow* win = *i;
335 win->GTKUpdateCursor();
336 if (display && *display == NULL && win->m_widget)
337 *display = gtk_widget_get_display(win->m_widget);
338 }
339 }
340
341 void wxEndBusyCursor()
342 {
343 if (--gs_busyCount > 0)
344 return;
345
346 g_globalCursor = gs_savedCursor;
347 gs_savedCursor = wxNullCursor;
348 UpdateCursors(NULL);
349 }
350
351 void wxBeginBusyCursor(const wxCursor* cursor)
352 {
353 if (gs_busyCount++ > 0)
354 return;
355
356 wxASSERT_MSG( !gs_savedCursor.IsOk(),
357 wxT("forgot to call wxEndBusyCursor, will leak memory") );
358
359 gs_savedCursor = g_globalCursor;
360 g_globalCursor = *cursor;
361 GdkDisplay* display = NULL;
362 UpdateCursors(&display);
363 if (display)
364 gdk_display_flush(display);
365 }
366
367 bool wxIsBusy()
368 {
369 return gs_busyCount > 0;
370 }
371
372 void wxSetCursor( const wxCursor& cursor )
373 {
374 g_globalCursor = cursor;
375 UpdateCursors(NULL);
376 }