]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
Make the margin between wxSpinCtrlGeneric sub-windows compatible with MSW.
[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"
84833214 25
c801d85f 26//-----------------------------------------------------------------------------
d6764050 27// wxCursorRefData
c801d85f
KB
28//-----------------------------------------------------------------------------
29
8f884a0d 30class wxCursorRefData: public wxGDIRefData
c801d85f 31{
8f884a0d 32public:
8bbe427f 33 wxCursorRefData();
d3c7fc99 34 virtual ~wxCursorRefData();
8bbe427f 35
8f884a0d
VZ
36 virtual bool IsOk() const { return m_cursor != NULL; }
37
c801d85f 38 GdkCursor *m_cursor;
51bf928d
VZ
39
40private:
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);
c801d85f
KB
44};
45
8bbe427f 46wxCursorRefData::wxCursorRefData()
c801d85f 47{
d3b9f782 48 m_cursor = NULL;
ff7b1510 49}
c801d85f 50
8bbe427f 51wxCursorRefData::~wxCursorRefData()
c801d85f 52{
bc8b8220 53 if (m_cursor) gdk_cursor_unref( m_cursor );
ff7b1510 54}
c801d85f 55
d6764050
FM
56
57//-----------------------------------------------------------------------------
58// wxCursor
c801d85f
KB
59//-----------------------------------------------------------------------------
60
5c33522f 61#define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
c801d85f 62
0c0f973f 63IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
c801d85f 64
d6764050
FM
65// used in the following two ctors
66extern GtkWidget *wxGetRootWindow();
67
68
8bbe427f 69wxCursor::wxCursor()
c801d85f 70{
ff7b1510 71}
c801d85f 72
d6764050
FM
73#if wxUSE_IMAGE
74wxCursor::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;
03647350 81
d6764050 82 // eventually set the hotspot:
a4afdcf3 83 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
d6764050 84 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
a4afdcf3 85 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
d6764050 86 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
03647350 87
d6764050
FM
88 InitFromImage(img);
89}
90
91wxCursor::wxCursor(const wxImage& img)
92{
93 InitFromImage(img);
94}
95#endif
96
97wxCursor::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
124wxCursor::~wxCursor()
125{
126}
127
0ef5b1da 128void wxCursor::InitFromStock( wxStockCursor cursorId )
c801d85f 129{
2d17d68f
RR
130 m_refData = new wxCursorRefData();
131
132 GdkCursorType gdk_cur = GDK_LEFT_PTR;
133 switch (cursorId)
134 {
76471ff7
VZ
135 case wxCURSOR_BLANK:
136 {
0c0f973f
PC
137 const char bits[] = { 0 };
138 const GdkColor color = { 0, 0, 0, 0 };
76471ff7
VZ
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);
0c0f973f 146 g_object_unref(pixmap);
76471ff7
VZ
147 }
148 return;
149
15dadf31 150 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 151 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 152 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
6e63b5c9 153 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
2d17d68f
RR
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;
83f96286 157 case wxCURSOR_ARROWWAIT:
7c39369e 158 case wxCURSOR_WAIT:
2d17d68f 159 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
57b10cb4 160 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
2d17d68f
RR
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;
7c39369e 165 case wxCURSOR_SIZENWSE:
2d17d68f
RR
166 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
167 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
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;
2d17d68f
RR
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;
13971833
RD
174 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
175
957d856c
VZ
176 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
177 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 178/*
7c39369e
VZ
179 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
180 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
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;
c801d85f 183*/
76471ff7 184
7c39369e 185 default:
223d09f6 186 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 187 // will use the standard one
7c39369e 188 break;
2d17d68f 189 }
8bbe427f 190
2d17d68f 191 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 192}
c801d85f 193
7eff657c 194#if wxUSE_IMAGE
0fc5dbf5 195
9f556d1d
VZ
196static 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
d6764050 214void wxCursor::InitFromImage( const wxImage & image )
7eff657c 215{
0fc5dbf5
VZ
216 int w = image.GetWidth() ;
217 int h = image.GetHeight();
218 bool bHasMask = image.HasMask();
88051436
PC
219 int hotSpotX, hotSpotY;
220 GetHotSpot(image, hotSpotX, hotSpotY);
221 m_refData = new wxCursorRefData;
222 wxImage image_copy(image);
7eff657c 223
88051436
PC
224 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
225 if (gdk_display_supports_cursor_color(display))
9f556d1d 226 {
88051436 227 if (!image.HasAlpha())
ff654490 228 {
88051436
PC
229 // add alpha, so wxBitmap will convert to pixbuf format
230 image_copy.InitAlpha();
cb738b8b 231 }
88051436
PC
232 wxBitmap bitmap(image_copy);
233 wxASSERT(bitmap.HasPixbuf());
ff654490
VZ
234 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
235 (
88051436
PC
236 display,
237 bitmap.GetPixbuf(),
ff654490
VZ
238 hotSpotX, hotSpotY
239 );
9f556d1d 240 }
833fb475 241 else // no colour cursor support
e60f8377 242 {
833fb475
VZ
243 unsigned long keyMaskColor = 0;
244 GdkPixmap *maskRaw;
245 if (bHasMask)
0fc5dbf5 246 {
833fb475
VZ
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);
7eff657c 253 }
833fb475
VZ
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++)
7eff657c 271 {
833fb475 272 for (int i = 0; i < w; i++, data += 3)
7eff657c 273 {
833fb475
VZ
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;
7eff657c 280 }
833fb475
VZ
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) )
7eff657c 298 {
833fb475
VZ
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 }
7eff657c
JS
312 }
313 }
7eff657c 314
833fb475
VZ
315 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
316 (unsigned char)(colMostFreq >> 8),
317 (unsigned char)(colMostFreq) );
7eff657c 318
833fb475
VZ
319 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
320 (unsigned char)(colNextMostFreq >> 8),
321 (unsigned char)(colNextMostFreq) );
7eff657c 322
833fb475
VZ
323 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
324 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
624d506b 325
833fb475
VZ
326 if (bg_intensity > fg_intensity)
327 {
328 //swap fg and bg
329 wxColour tmp = fg;
330 fg = bg;
331 bg = tmp;
332 }
7eff657c 333
833fb475
VZ
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 }
7eff657c 342}
0fc5dbf5
VZ
343
344#endif // wxUSE_IMAGE
7eff657c 345
8f884a0d 346GdkCursor *wxCursor::GetCursor() const
c801d85f 347{
8f884a0d 348 return M_CURSORDATA->m_cursor;
ff7b1510 349}
c801d85f 350
8f884a0d 351wxGDIRefData *wxCursor::CreateGDIRefData() const
c801d85f 352{
8f884a0d
VZ
353 return new wxCursorRefData;
354}
355
51bf928d
VZ
356wxGDIRefData *
357wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
8f884a0d 358{
51bf928d
VZ
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;
ff7b1510 367}
c801d85f
KB
368
369//-----------------------------------------------------------------------------
370// busy cursor routines
371//-----------------------------------------------------------------------------
372
b541538f
PC
373/* Current cursor, in order to hang on to
374 * cursor handle when setting the cursor globally */
375wxCursor g_globalCursor;
7c39369e 376
238d735d 377static wxCursor gs_savedCursor;
89a43902 378static int gs_busyCount = 0;
c801d85f 379
f6bcfd97
BP
380const wxCursor &wxBusyCursor::GetStoredCursor()
381{
382 return gs_savedCursor;
383}
384
385const wxCursor wxBusyCursor::GetBusyCursor()
386{
387 return wxCursor(wxCURSOR_WATCH);
388}
389
c2246a38 390static void UpdateCursors(const wxWindowList& list, GdkDisplay*& display)
df8d50a0
PC
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);
c2246a38
RR
398 win->GTKUpdateCursor(true, false);
399 UpdateCursors(win->GetChildren(), display);
df8d50a0
PC
400 }
401}
402
8bbe427f 403void wxEndBusyCursor()
c801d85f 404{
238d735d 405 if (--gs_busyCount > 0)
89a43902
VZ
406 return;
407
df8d50a0 408 g_globalCursor = gs_savedCursor;
238d735d 409 gs_savedCursor = wxNullCursor;
df8d50a0 410 GdkDisplay* unused = NULL;
c2246a38 411 UpdateCursors(wxTopLevelWindows, unused);
ff7b1510 412}
c801d85f 413
df8d50a0 414void wxBeginBusyCursor(const wxCursor* cursor)
c801d85f 415{
238d735d 416 if (gs_busyCount++ > 0)
89a43902
VZ
417 return;
418
238d735d 419 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 420 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 421
238d735d 422 gs_savedCursor = g_globalCursor;
df8d50a0
PC
423 g_globalCursor = *cursor;
424 GdkDisplay* display = NULL;
c2246a38 425 UpdateCursors(wxTopLevelWindows, display);
df8d50a0
PC
426 if (display)
427 gdk_display_flush(display);
ff7b1510 428}
c801d85f 429
8bbe427f 430bool wxIsBusy()
c801d85f 431{
89a43902 432 return gs_busyCount > 0;
ff7b1510 433}
c801d85f
KB
434
435void wxSetCursor( const wxCursor& cursor )
436{
238d735d 437 g_globalCursor = cursor;
c2246a38
RR
438 GdkDisplay* unused = NULL;
439 UpdateCursors(wxTopLevelWindows, unused);
ff7b1510 440}