]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
make sure NSScrollers are always having the desired orientation, fixes #10803
[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>
84833214 24
c801d85f 25//-----------------------------------------------------------------------------
d6764050 26// wxCursorRefData
c801d85f
KB
27//-----------------------------------------------------------------------------
28
8f884a0d 29class wxCursorRefData: public wxGDIRefData
c801d85f 30{
8f884a0d 31public:
8bbe427f 32 wxCursorRefData();
d3c7fc99 33 virtual ~wxCursorRefData();
8bbe427f 34
8f884a0d
VZ
35 virtual bool IsOk() const { return m_cursor != NULL; }
36
c801d85f
KB
37 GdkCursor *m_cursor;
38};
39
8bbe427f 40wxCursorRefData::wxCursorRefData()
c801d85f 41{
d3b9f782 42 m_cursor = NULL;
ff7b1510 43}
c801d85f 44
8bbe427f 45wxCursorRefData::~wxCursorRefData()
c801d85f 46{
bc8b8220 47 if (m_cursor) gdk_cursor_unref( m_cursor );
ff7b1510 48}
c801d85f 49
d6764050
FM
50
51//-----------------------------------------------------------------------------
52// wxCursor
c801d85f
KB
53//-----------------------------------------------------------------------------
54
5c33522f 55#define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
c801d85f 56
0c0f973f 57IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
c801d85f 58
d6764050
FM
59// used in the following two ctors
60extern GtkWidget *wxGetRootWindow();
61
62
8bbe427f 63wxCursor::wxCursor()
c801d85f 64{
ff7b1510 65}
c801d85f 66
d6764050
FM
67#if wxUSE_IMAGE
68wxCursor::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:
a4afdcf3 77 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
d6764050 78 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
a4afdcf3 79 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
d6764050
FM
80 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
81
82 InitFromImage(img);
83}
84
85wxCursor::wxCursor(const wxImage& img)
86{
87 InitFromImage(img);
88}
89#endif
90
91wxCursor::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
118wxCursor::~wxCursor()
119{
120}
121
0ef5b1da 122void wxCursor::InitFromStock( wxStockCursor cursorId )
c801d85f 123{
2d17d68f
RR
124 m_refData = new wxCursorRefData();
125
126 GdkCursorType gdk_cur = GDK_LEFT_PTR;
127 switch (cursorId)
128 {
76471ff7
VZ
129 case wxCURSOR_BLANK:
130 {
0c0f973f
PC
131 const char bits[] = { 0 };
132 const GdkColor color = { 0, 0, 0, 0 };
76471ff7
VZ
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);
0c0f973f 140 g_object_unref(pixmap);
76471ff7
VZ
141 }
142 return;
143
15dadf31 144 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 145 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 146 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
6e63b5c9 147 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
2d17d68f
RR
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;
83f96286 151 case wxCURSOR_ARROWWAIT:
7c39369e 152 case wxCURSOR_WAIT:
2d17d68f 153 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
57b10cb4 154 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
2d17d68f
RR
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;
7c39369e 159 case wxCURSOR_SIZENWSE:
2d17d68f
RR
160 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
161 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
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;
2d17d68f
RR
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;
13971833
RD
168 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
169
957d856c
VZ
170 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
171 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 172/*
7c39369e
VZ
173 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
174 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
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;
c801d85f 177*/
76471ff7 178
7c39369e 179 default:
223d09f6 180 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 181 // will use the standard one
7c39369e 182 break;
2d17d68f 183 }
8bbe427f 184
2d17d68f 185 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 186}
c801d85f 187
7eff657c 188#if wxUSE_IMAGE
0fc5dbf5 189
9f556d1d
VZ
190static 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
d6764050 208void wxCursor::InitFromImage( const wxImage & image )
7eff657c 209{
0fc5dbf5
VZ
210 int w = image.GetWidth() ;
211 int h = image.GetHeight();
212 bool bHasMask = image.HasMask();
88051436
PC
213 int hotSpotX, hotSpotY;
214 GetHotSpot(image, hotSpotX, hotSpotY);
215 m_refData = new wxCursorRefData;
216 wxImage image_copy(image);
7eff657c 217
88051436
PC
218 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
219 if (gdk_display_supports_cursor_color(display))
9f556d1d 220 {
88051436 221 if (!image.HasAlpha())
ff654490 222 {
88051436
PC
223 // add alpha, so wxBitmap will convert to pixbuf format
224 image_copy.InitAlpha();
cb738b8b 225 }
88051436
PC
226 wxBitmap bitmap(image_copy);
227 wxASSERT(bitmap.HasPixbuf());
ff654490
VZ
228 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
229 (
88051436
PC
230 display,
231 bitmap.GetPixbuf(),
ff654490
VZ
232 hotSpotX, hotSpotY
233 );
ff654490 234 return;
9f556d1d 235 }
9f556d1d 236
e60f8377
PC
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
88051436 258 // modify image so wxBitmap can be used to convert to pixmap
e60f8377 259 image_copy.SetMask(false);
88051436
PC
260 int i, j;
261 wxByte* data = image_copy.GetData();
262 for (j = 0; j < h; j++)
0fc5dbf5 263 {
88051436 264 for (i = 0; i < w; i++, data += 3)
0fc5dbf5 265 {
0fc5dbf5 266 //if average value is > mid grey
88051436
PC
267 if (int(data[0]) + data[1] + data[2] >= 3 * 128)
268 {
269 // wxBitmap only converts (255,255,255) to white
270 data[0] = 255;
271 data[1] = 255;
272 data[2] = 255;
273 }
7eff657c 274 }
0fc5dbf5 275 }
88051436
PC
276 wxBitmap bitmap(image_copy, 1);
277
fa1fcc66 278 // find the most frequent color(s)
952ae1e8 279 wxImageHistogram histogram;
fa1fcc66 280 image.ComputeHistogram(histogram);
7eff657c 281
fa1fcc66 282 long colMostFreq = 0;
952ae1e8 283 unsigned long nMost = 0;
fa1fcc66 284 long colNextMostFreq = 0;
952ae1e8 285 unsigned long nNext = 0;
fa1fcc66
VZ
286 for ( wxImageHistogram::iterator entry = histogram.begin();
287 entry != histogram.end();
288 ++entry )
7eff657c 289 {
88051436 290 unsigned long key = entry->first;
952ae1e8 291 if ( !bHasMask || (key != keyMaskColor) )
7eff657c 292 {
88051436 293 unsigned long value = entry->second.value;
952ae1e8 294 if (value > nMost)
7eff657c 295 {
88051436
PC
296 nNext = nMost;
297 colNextMostFreq = colMostFreq;
952ae1e8 298 nMost = value;
fa1fcc66 299 colMostFreq = key;
7eff657c 300 }
952ae1e8 301 else if (value > nNext)
7eff657c 302 {
952ae1e8 303 nNext = value;
fa1fcc66 304 colNextMostFreq = key;
7eff657c
JS
305 }
306 }
307 }
308
fa1fcc66
VZ
309 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
310 (unsigned char)(colMostFreq >> 8),
311 (unsigned char)(colMostFreq) );
7eff657c 312
fa1fcc66
VZ
313 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
314 (unsigned char)(colNextMostFreq >> 8),
315 (unsigned char)(colNextMostFreq) );
7eff657c 316
624d506b
CE
317 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
318 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
86e69fa5
MR
319
320 if (bg_intensity > fg_intensity)
321 {
624d506b
CE
322 //swap fg and bg
323 wxColour tmp = fg;
324 fg = bg;
325 bg = tmp;
86e69fa5 326 }
624d506b 327
fa1fcc66
VZ
328 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
329 (
88051436 330 bitmap.GetPixmap(),
fa1fcc66
VZ
331 mask,
332 fg.GetColor(), bg.GetColor(),
333 hotSpotX, hotSpotY
334 );
7eff657c 335
3fe39b0c 336 g_object_unref (mask);
7eff657c 337}
0fc5dbf5
VZ
338
339#endif // wxUSE_IMAGE
7eff657c 340
8f884a0d 341GdkCursor *wxCursor::GetCursor() const
c801d85f 342{
8f884a0d 343 return M_CURSORDATA->m_cursor;
ff7b1510 344}
c801d85f 345
8f884a0d 346wxGDIRefData *wxCursor::CreateGDIRefData() const
c801d85f 347{
8f884a0d
VZ
348 return new wxCursorRefData;
349}
350
351wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
352{
5c33522f 353 return new wxCursorRefData(*static_cast<const wxCursorRefData *>(data));
ff7b1510 354}
c801d85f
KB
355
356//-----------------------------------------------------------------------------
357// busy cursor routines
358//-----------------------------------------------------------------------------
359
b541538f
PC
360/* Current cursor, in order to hang on to
361 * cursor handle when setting the cursor globally */
362wxCursor g_globalCursor;
7c39369e 363
238d735d 364static wxCursor gs_savedCursor;
89a43902 365static int gs_busyCount = 0;
c801d85f 366
f6bcfd97
BP
367const wxCursor &wxBusyCursor::GetStoredCursor()
368{
369 return gs_savedCursor;
370}
371
372const wxCursor wxBusyCursor::GetBusyCursor()
373{
374 return wxCursor(wxCURSOR_WATCH);
375}
376
df8d50a0
PC
377static void InternalIdle(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->OnInternalIdle();
386 InternalIdle(win->GetChildren(), display);
387 }
388}
389
8bbe427f 390void wxEndBusyCursor()
c801d85f 391{
238d735d 392 if (--gs_busyCount > 0)
89a43902
VZ
393 return;
394
df8d50a0 395 g_globalCursor = gs_savedCursor;
238d735d 396 gs_savedCursor = wxNullCursor;
df8d50a0
PC
397 GdkDisplay* unused = NULL;
398 InternalIdle(wxTopLevelWindows, unused);
ff7b1510 399}
c801d85f 400
df8d50a0 401void wxBeginBusyCursor(const wxCursor* cursor)
c801d85f 402{
238d735d 403 if (gs_busyCount++ > 0)
89a43902
VZ
404 return;
405
238d735d 406 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 407 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 408
238d735d 409 gs_savedCursor = g_globalCursor;
df8d50a0
PC
410 g_globalCursor = *cursor;
411 GdkDisplay* display = NULL;
412 InternalIdle(wxTopLevelWindows, display);
413 if (display)
414 gdk_display_flush(display);
ff7b1510 415}
c801d85f 416
8bbe427f 417bool wxIsBusy()
c801d85f 418{
89a43902 419 return gs_busyCount > 0;
ff7b1510 420}
c801d85f
KB
421
422void wxSetCursor( const wxCursor& cursor )
423{
238d735d 424 g_globalCursor = cursor;
a1abca32 425 wxTheApp->WakeUpIdle();
ff7b1510 426}