]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
Fix bitmap position attribute name in XRC handler.
[wxWidgets.git] / src / gtk / cursor.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/gtk/cursor.cpp
d6764050 3// Purpose: wxCursor implementation
c801d85f 4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/cursor.h"
670f9935
WS
14
15#ifndef WX_PRECOMP
df8d50a0 16 #include "wx/window.h"
670f9935 17 #include "wx/app.h"
0416c418 18 #include "wx/image.h"
88051436 19 #include "wx/bitmap.h"
cfaf8bd1 20 #include "wx/log.h"
670f9935
WS
21#endif // WX_PRECOMP
22
a1abca32 23#include <gtk/gtk.h>
833fb475 24#include "wx/gtk/private/object.h"
385e8575 25#include "wx/gtk/private/gtk2-compat.h"
84833214 26
c801d85f 27//-----------------------------------------------------------------------------
d6764050 28// wxCursorRefData
c801d85f
KB
29//-----------------------------------------------------------------------------
30
8f884a0d 31class wxCursorRefData: public wxGDIRefData
c801d85f 32{
8f884a0d 33public:
8bbe427f 34 wxCursorRefData();
d3c7fc99 35 virtual ~wxCursorRefData();
8bbe427f 36
8f884a0d
VZ
37 virtual bool IsOk() const { return m_cursor != NULL; }
38
c801d85f 39 GdkCursor *m_cursor;
51bf928d
VZ
40
41private:
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);
c801d85f
KB
45};
46
8bbe427f 47wxCursorRefData::wxCursorRefData()
c801d85f 48{
d3b9f782 49 m_cursor = NULL;
ff7b1510 50}
c801d85f 51
8bbe427f 52wxCursorRefData::~wxCursorRefData()
c801d85f 53{
bc8b8220 54 if (m_cursor) gdk_cursor_unref( m_cursor );
ff7b1510 55}
c801d85f 56
d6764050
FM
57
58//-----------------------------------------------------------------------------
59// wxCursor
c801d85f
KB
60//-----------------------------------------------------------------------------
61
5c33522f 62#define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
c801d85f 63
0c0f973f 64IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
c801d85f 65
d6764050
FM
66// used in the following two ctors
67extern GtkWidget *wxGetRootWindow();
68
69
8bbe427f 70wxCursor::wxCursor()
c801d85f 71{
ff7b1510 72}
c801d85f 73
d6764050
FM
74#if wxUSE_IMAGE
75wxCursor::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;
03647350 82
d6764050 83 // eventually set the hotspot:
a4afdcf3 84 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
d6764050 85 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
a4afdcf3 86 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
d6764050 87 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
03647350 88
d6764050
FM
89 InitFromImage(img);
90}
91
92wxCursor::wxCursor(const wxImage& img)
93{
94 InitFromImage(img);
95}
96#endif
97
98wxCursor::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
385e8575
PC
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);
d6764050
FM
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
127wxCursor::~wxCursor()
128{
129}
130
0ef5b1da 131void wxCursor::InitFromStock( wxStockCursor cursorId )
c801d85f 132{
2d17d68f
RR
133 m_refData = new wxCursorRefData();
134
135 GdkCursorType gdk_cur = GDK_LEFT_PTR;
136 switch (cursorId)
137 {
76471ff7
VZ
138 case wxCURSOR_BLANK:
139 {
0c0f973f
PC
140 const char bits[] = { 0 };
141 const GdkColor color = { 0, 0, 0, 0 };
76471ff7
VZ
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);
0c0f973f 149 g_object_unref(pixmap);
76471ff7
VZ
150 }
151 return;
152
15dadf31 153 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 154 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 155 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
6e63b5c9 156 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
2d17d68f
RR
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;
83f96286 160 case wxCURSOR_ARROWWAIT:
7c39369e 161 case wxCURSOR_WAIT:
2d17d68f 162 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
57b10cb4 163 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
2d17d68f
RR
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;
7c39369e 168 case wxCURSOR_SIZENWSE:
2d17d68f
RR
169 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
170 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
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;
2d17d68f
RR
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;
13971833
RD
177 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
178
957d856c
VZ
179 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
180 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 181/*
7c39369e
VZ
182 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
183 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
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;
c801d85f 186*/
76471ff7 187
7c39369e 188 default:
223d09f6 189 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 190 // will use the standard one
7c39369e 191 break;
2d17d68f 192 }
8bbe427f 193
2d17d68f 194 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 195}
c801d85f 196
7eff657c 197#if wxUSE_IMAGE
0fc5dbf5 198
9f556d1d
VZ
199static 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
d6764050 217void wxCursor::InitFromImage( const wxImage & image )
7eff657c 218{
0fc5dbf5
VZ
219 int w = image.GetWidth() ;
220 int h = image.GetHeight();
221 bool bHasMask = image.HasMask();
88051436
PC
222 int hotSpotX, hotSpotY;
223 GetHotSpot(image, hotSpotX, hotSpotY);
224 m_refData = new wxCursorRefData;
225 wxImage image_copy(image);
7eff657c 226
385e8575 227 GdkDisplay* display = gdk_drawable_get_display(gtk_widget_get_window(wxGetRootWindow()));
88051436 228 if (gdk_display_supports_cursor_color(display))
9f556d1d 229 {
88051436 230 if (!image.HasAlpha())
ff654490 231 {
88051436
PC
232 // add alpha, so wxBitmap will convert to pixbuf format
233 image_copy.InitAlpha();
cb738b8b 234 }
88051436
PC
235 wxBitmap bitmap(image_copy);
236 wxASSERT(bitmap.HasPixbuf());
ff654490
VZ
237 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
238 (
88051436
PC
239 display,
240 bitmap.GetPixbuf(),
ff654490
VZ
241 hotSpotX, hotSpotY
242 );
9f556d1d 243 }
833fb475 244 else // no colour cursor support
e60f8377 245 {
833fb475
VZ
246 unsigned long keyMaskColor = 0;
247 GdkPixmap *maskRaw;
248 if (bHasMask)
0fc5dbf5 249 {
833fb475
VZ
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);
7eff657c 256 }
833fb475
VZ
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(
385e8575 263 gtk_widget_get_window(wxGetRootWindow()), bits, w, h);
833fb475
VZ
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++)
7eff657c 274 {
833fb475 275 for (int i = 0; i < w; i++, data += 3)
7eff657c 276 {
833fb475
VZ
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;
7eff657c 283 }
833fb475
VZ
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) )
7eff657c 301 {
833fb475
VZ
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 }
7eff657c
JS
315 }
316 }
7eff657c 317
833fb475
VZ
318 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
319 (unsigned char)(colMostFreq >> 8),
320 (unsigned char)(colMostFreq) );
7eff657c 321
833fb475
VZ
322 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
323 (unsigned char)(colNextMostFreq >> 8),
324 (unsigned char)(colNextMostFreq) );
7eff657c 325
833fb475
VZ
326 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
327 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
624d506b 328
833fb475
VZ
329 if (bg_intensity > fg_intensity)
330 {
331 //swap fg and bg
332 wxColour tmp = fg;
333 fg = bg;
334 bg = tmp;
335 }
7eff657c 336
833fb475
VZ
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 }
7eff657c 345}
0fc5dbf5
VZ
346
347#endif // wxUSE_IMAGE
7eff657c 348
8f884a0d 349GdkCursor *wxCursor::GetCursor() const
c801d85f 350{
8f884a0d 351 return M_CURSORDATA->m_cursor;
ff7b1510 352}
c801d85f 353
8f884a0d 354wxGDIRefData *wxCursor::CreateGDIRefData() const
c801d85f 355{
8f884a0d
VZ
356 return new wxCursorRefData;
357}
358
51bf928d
VZ
359wxGDIRefData *
360wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
8f884a0d 361{
51bf928d
VZ
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;
ff7b1510 370}
c801d85f
KB
371
372//-----------------------------------------------------------------------------
373// busy cursor routines
374//-----------------------------------------------------------------------------
375
b541538f
PC
376/* Current cursor, in order to hang on to
377 * cursor handle when setting the cursor globally */
378wxCursor g_globalCursor;
7c39369e 379
238d735d 380static wxCursor gs_savedCursor;
89a43902 381static int gs_busyCount = 0;
c801d85f 382
f6bcfd97
BP
383const wxCursor &wxBusyCursor::GetStoredCursor()
384{
385 return gs_savedCursor;
386}
387
388const wxCursor wxBusyCursor::GetBusyCursor()
389{
390 return wxCursor(wxCURSOR_WATCH);
391}
392
c2246a38 393static void UpdateCursors(const wxWindowList& list, GdkDisplay*& display)
df8d50a0
PC
394{
395 wxWindowList::const_iterator i = list.begin();
396 for (size_t n = list.size(); n--; ++i)
397 {
398 wxWindow* win = *i;
385e8575
PC
399 if (display == NULL && win->m_widget)
400 {
401 GdkWindow* w = gtk_widget_get_window(win->m_widget);
402 if (w)
403 display = gdk_drawable_get_display(w);
404 }
c2246a38
RR
405 win->GTKUpdateCursor(true, false);
406 UpdateCursors(win->GetChildren(), display);
df8d50a0
PC
407 }
408}
409
8bbe427f 410void wxEndBusyCursor()
c801d85f 411{
238d735d 412 if (--gs_busyCount > 0)
89a43902
VZ
413 return;
414
df8d50a0 415 g_globalCursor = gs_savedCursor;
238d735d 416 gs_savedCursor = wxNullCursor;
df8d50a0 417 GdkDisplay* unused = NULL;
c2246a38 418 UpdateCursors(wxTopLevelWindows, unused);
ff7b1510 419}
c801d85f 420
df8d50a0 421void wxBeginBusyCursor(const wxCursor* cursor)
c801d85f 422{
238d735d 423 if (gs_busyCount++ > 0)
89a43902
VZ
424 return;
425
a1b806b9 426 wxASSERT_MSG( !gs_savedCursor.IsOk(),
223d09f6 427 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 428
238d735d 429 gs_savedCursor = g_globalCursor;
df8d50a0
PC
430 g_globalCursor = *cursor;
431 GdkDisplay* display = NULL;
c2246a38 432 UpdateCursors(wxTopLevelWindows, display);
df8d50a0
PC
433 if (display)
434 gdk_display_flush(display);
ff7b1510 435}
c801d85f 436
8bbe427f 437bool wxIsBusy()
c801d85f 438{
89a43902 439 return gs_busyCount > 0;
ff7b1510 440}
c801d85f
KB
441
442void wxSetCursor( const wxCursor& cursor )
443{
238d735d 444 g_globalCursor = cursor;
c2246a38
RR
445 GdkDisplay* unused = NULL;
446 UpdateCursors(wxTopLevelWindows, unused);
ff7b1510 447}