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