]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
use typed containers in wxHtmlParser instead of type-unsafe wxList/wxHashTable
[wxWidgets.git] / src / gtk / cursor.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/gtk/cursor.cpp
c801d85f
KB
3// Purpose:
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
16 #include "wx/app.h"
de6185e2 17 #include "wx/utils.h"
0416c418 18 #include "wx/image.h"
5ff14574 19 #include "wx/colour.h"
670f9935
WS
20#endif // WX_PRECOMP
21
a1abca32 22#include <gtk/gtk.h>
84833214 23
c801d85f
KB
24//-----------------------------------------------------------------------------
25// wxCursor
26//-----------------------------------------------------------------------------
27
28class wxCursorRefData: public wxObjectRefData
29{
30 public:
8bbe427f
VZ
31
32 wxCursorRefData();
d3c7fc99 33 virtual ~wxCursorRefData();
8bbe427f 34
c801d85f
KB
35 GdkCursor *m_cursor;
36};
37
8bbe427f 38wxCursorRefData::wxCursorRefData()
c801d85f 39{
2d17d68f 40 m_cursor = (GdkCursor *) NULL;
ff7b1510 41}
c801d85f 42
8bbe427f 43wxCursorRefData::~wxCursorRefData()
c801d85f 44{
bc8b8220 45 if (m_cursor) gdk_cursor_unref( m_cursor );
ff7b1510 46}
c801d85f
KB
47
48//-----------------------------------------------------------------------------
49
50#define M_CURSORDATA ((wxCursorRefData *)m_refData)
51
52IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
53
8bbe427f 54wxCursor::wxCursor()
c801d85f 55{
7eff657c 56
ff7b1510 57}
c801d85f 58
debe6624 59wxCursor::wxCursor( int cursorId )
c801d85f 60{
2d17d68f
RR
61 m_refData = new wxCursorRefData();
62
63 GdkCursorType gdk_cur = GDK_LEFT_PTR;
64 switch (cursorId)
65 {
76471ff7
VZ
66 case wxCURSOR_BLANK:
67 {
68 static const gchar bits[] = { 0 };
defdd888 69 static /* const -- not in GTK1 */ GdkColor color = { 0, 0, 0, 0 };
76471ff7
VZ
70
71 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
72 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
73 pixmap,
74 &color,
75 &color,
76 0, 0);
77 }
78 return;
79
15dadf31 80 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 81 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 82 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
2d17d68f
RR
83 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
84 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
85 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
86 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
83f96286 87 case wxCURSOR_ARROWWAIT:
7c39369e 88 case wxCURSOR_WAIT:
2d17d68f 89 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
302dc518 90 case wxCURSOR_SIZING: gdk_cur = GDK_FLEUR; break;
2d17d68f
RR
91 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
92 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
93 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
94 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
7c39369e 95 case wxCURSOR_SIZENWSE:
2d17d68f
RR
96 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
97 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
98 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
99 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
100 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
2d17d68f
RR
101 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
102 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
103 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
13971833
RD
104 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
105
957d856c
VZ
106 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
107 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 108/*
7c39369e
VZ
109 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
110 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
111 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
112 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
c801d85f 113*/
76471ff7 114
7c39369e 115 default:
223d09f6 116 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 117 // will use the standard one
7c39369e 118 break;
2d17d68f 119 }
8bbe427f 120
2d17d68f 121 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 122}
c801d85f 123
c2fa61e8 124extern GtkWidget *wxGetRootWindow();
a1db952f
VS
125
126wxCursor::wxCursor(const char bits[], int width, int height,
127 int hotSpotX, int hotSpotY,
f516d986 128 const char maskBits[], const wxColour *fg, const wxColour *bg)
a1db952f
VS
129{
130 if (!maskBits)
131 maskBits = bits;
132 if (!fg)
133 fg = wxBLACK;
134 if (!bg)
135 bg = wxWHITE;
136 if (hotSpotX < 0 || hotSpotX >= width)
137 hotSpotX = 0;
138 if (hotSpotY < 0 || hotSpotY >= height)
139 hotSpotY = 0;
140
c2fa61e8
RD
141 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
142 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
a1db952f
VS
143
144 m_refData = new wxCursorRefData;
145 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
4f558e23 146 data, mask, fg->GetColor(), bg->GetColor(),
a1db952f
VS
147 hotSpotX, hotSpotY );
148
3fe39b0c
MR
149 g_object_unref (data);
150 g_object_unref (mask);
a1db952f
VS
151}
152
7eff657c 153#if wxUSE_IMAGE
0fc5dbf5 154
9f556d1d
VZ
155static void GetHotSpot(const wxImage& image, int& x, int& y)
156{
157 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
158 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
159 else
160 x = 0;
161
162 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
163 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
164 else
165 y = 0;
166
167 if (x < 0 || x >= image.GetWidth())
168 x = 0;
169 if (y < 0 || y >= image.GetHeight())
170 y = 0;
171}
172
7eff657c
JS
173wxCursor::wxCursor( const wxImage & image )
174{
175 unsigned char * rgbBits = image.GetData();
0fc5dbf5
VZ
176 int w = image.GetWidth() ;
177 int h = image.GetHeight();
178 bool bHasMask = image.HasMask();
7eff657c
JS
179 int imagebitcount = (w*h)/8;
180
cb738b8b
RR
181#ifdef __WXGTK24__
182 if (!gtk_check_version(2,4,0))
9f556d1d 183 {
cb738b8b 184 if ( gdk_display_supports_cursor_color(gdk_display_get_default()) )
9f556d1d 185 {
cb738b8b
RR
186 unsigned char rMask = 0,
187 gMask = 0,
188 bMask = 0;
189 if (bHasMask)
190 {
191 rMask = image.GetMaskRed();
192 gMask = image.GetMaskGreen();
193 bMask = image.GetMaskBlue();
194 }
9f556d1d 195
cb738b8b
RR
196 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, w, h);
197 unsigned char *alpha = image.HasAlpha() ? image.GetAlpha() : NULL;
198 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
199 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
200 for ( int y = 0; y < h; y++, out += rowpad )
9f556d1d 201 {
cb738b8b
RR
202 for ( int x = 0; x < w; x++, out += 4, rgbBits += 3 )
203 {
204 out[0] = rgbBits[0];
205 out[1] = rgbBits[1];
206 out[2] = rgbBits[2];
207 if (bHasMask &&
208 out[0] == rMask && out[1] == gMask && out[2] == bMask)
209 out[3] = 0;
210 else
211 out[3] = alpha ? *alpha : 255;
212 if ( alpha )
213 ++alpha;
214 }
9f556d1d 215 }
9f556d1d 216
cb738b8b
RR
217 int hotSpotX, hotSpotY;
218 GetHotSpot(image, hotSpotX, hotSpotY);
9f556d1d 219
cb738b8b
RR
220 m_refData = new wxCursorRefData;
221 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
9f556d1d
VZ
222 (
223 gdk_display_get_default(),
224 pixbuf,
225 hotSpotX, hotSpotY
226 );
cb738b8b
RR
227 g_object_unref (pixbuf);
228 return;
229 }
9f556d1d 230 }
cb738b8b 231#endif // GTK+ 2.4+
9f556d1d 232
7eff657c
JS
233 unsigned char * bits = new unsigned char [imagebitcount];
234 unsigned char * maskBits = new unsigned char [imagebitcount];
235
0fc5dbf5 236 int i, j, i8; unsigned char c, cMask;
7eff657c 237 for (i=0; i<imagebitcount; i++)
0fc5dbf5 238 {
7eff657c
JS
239 bits[i] = 0;
240 i8 = i * 8;
241
242 cMask = 1;
243 for (j=0; j<8; j++)
0fc5dbf5
VZ
244 {
245 // possible overflow if we do the summation first ?
246 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
247 //if average value is > mid grey
248 if (c>127)
249 bits[i] = bits[i] | cMask;
250 cMask = cMask * 2;
7eff657c 251 }
0fc5dbf5
VZ
252 }
253
254 unsigned long keyMaskColor;
7eff657c 255 if (bHasMask)
0fc5dbf5
VZ
256 {
257 unsigned char
258 r = image.GetMaskRed(),
259 g = image.GetMaskGreen(),
260 b = image.GetMaskBlue();
7eff657c
JS
261
262 for (i=0; i<imagebitcount; i++)
263 {
0fc5dbf5
VZ
264 maskBits[i] = 0x0;
265 i8 = i * 8;
7eff657c 266
0fc5dbf5
VZ
267 cMask = 1;
268 for (j=0; j<8; j++)
269 {
270 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
271 maskBits[i] = maskBits[i] | cMask;
272 cMask = cMask * 2;
273 }
7eff657c 274 }
0fc5dbf5
VZ
275
276 keyMaskColor = (r << 16) | (g << 8) | b;
277 }
278 else // no mask
279 {
7eff657c 280 for (i=0; i<imagebitcount; i++)
0fc5dbf5
VZ
281 maskBits[i] = 0xFF;
282
283 // init it to avoid compiler warnings
284 keyMaskColor = 0;
285 }
286
fa1fcc66 287 // find the most frequent color(s)
952ae1e8 288 wxImageHistogram histogram;
fa1fcc66 289 image.ComputeHistogram(histogram);
7eff657c 290
0fc5dbf5 291 // colors as rrggbb
7eff657c 292 unsigned long key;
952ae1e8 293 unsigned long value;
7eff657c 294
fa1fcc66 295 long colMostFreq = 0;
952ae1e8 296 unsigned long nMost = 0;
fa1fcc66 297 long colNextMostFreq = 0;
952ae1e8 298 unsigned long nNext = 0;
fa1fcc66
VZ
299 for ( wxImageHistogram::iterator entry = histogram.begin();
300 entry != histogram.end();
301 ++entry )
7eff657c 302 {
952ae1e8
VS
303 value = entry->second.value;
304 key = entry->first;
305 if ( !bHasMask || (key != keyMaskColor) )
7eff657c 306 {
952ae1e8 307 if (value > nMost)
7eff657c 308 {
952ae1e8 309 nMost = value;
fa1fcc66 310 colMostFreq = key;
7eff657c 311 }
952ae1e8 312 else if (value > nNext)
7eff657c 313 {
952ae1e8 314 nNext = value;
fa1fcc66 315 colNextMostFreq = key;
7eff657c
JS
316 }
317 }
318 }
319
fa1fcc66
VZ
320 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
321 (unsigned char)(colMostFreq >> 8),
322 (unsigned char)(colMostFreq) );
7eff657c 323
fa1fcc66
VZ
324 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
325 (unsigned char)(colNextMostFreq >> 8),
326 (unsigned char)(colNextMostFreq) );
7eff657c 327
624d506b
CE
328 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
329 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
86e69fa5
MR
330
331 if (bg_intensity > fg_intensity)
332 {
624d506b
CE
333 //swap fg and bg
334 wxColour tmp = fg;
335 fg = bg;
336 bg = tmp;
86e69fa5 337 }
624d506b 338
9f556d1d
VZ
339 int hotSpotX, hotSpotY;
340 GetHotSpot(image, hotSpotX, hotSpotY);
7eff657c 341
fa1fcc66
VZ
342 GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
343 (gchar *) bits, w, h);
344 GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
345 (gchar *) maskBits, w, h);
7eff657c
JS
346
347 m_refData = new wxCursorRefData;
fa1fcc66
VZ
348 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
349 (
350 data,
351 mask,
352 fg.GetColor(), bg.GetColor(),
353 hotSpotX, hotSpotY
354 );
7eff657c 355
3fe39b0c
MR
356 g_object_unref (data);
357 g_object_unref (mask);
0fc5dbf5 358 delete [] bits;
7eff657c 359 delete [] maskBits;
7eff657c 360}
0fc5dbf5
VZ
361
362#endif // wxUSE_IMAGE
7eff657c 363
8bbe427f 364wxCursor::~wxCursor()
c801d85f 365{
ff7b1510 366}
c801d85f 367
b7cacb43 368bool wxCursor::IsOk() const
c801d85f 369{
2d17d68f 370 return (m_refData != NULL);
ff7b1510 371}
c801d85f 372
8bbe427f 373GdkCursor *wxCursor::GetCursor() const
c801d85f 374{
2d17d68f 375 return M_CURSORDATA->m_cursor;
ff7b1510 376}
c801d85f
KB
377
378//-----------------------------------------------------------------------------
379// busy cursor routines
380//-----------------------------------------------------------------------------
381
b541538f
PC
382/* Current cursor, in order to hang on to
383 * cursor handle when setting the cursor globally */
384wxCursor g_globalCursor;
7c39369e 385
238d735d 386static wxCursor gs_savedCursor;
89a43902 387static int gs_busyCount = 0;
c801d85f 388
f6bcfd97
BP
389const wxCursor &wxBusyCursor::GetStoredCursor()
390{
391 return gs_savedCursor;
392}
393
394const wxCursor wxBusyCursor::GetBusyCursor()
395{
396 return wxCursor(wxCURSOR_WATCH);
397}
398
8bbe427f 399void wxEndBusyCursor()
c801d85f 400{
238d735d 401 if (--gs_busyCount > 0)
89a43902
VZ
402 return;
403
238d735d
RR
404 wxSetCursor( gs_savedCursor );
405 gs_savedCursor = wxNullCursor;
13971833 406
d1dea842 407 if (wxTheApp)
8b532f8c 408 wxTheApp->ProcessIdle();
ff7b1510 409}
c801d85f 410
f516d986 411void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) )
c801d85f 412{
238d735d 413 if (gs_busyCount++ > 0)
89a43902
VZ
414 return;
415
238d735d 416 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 417 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 418
238d735d 419 gs_savedCursor = g_globalCursor;
13971833 420
238d735d 421 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
13971833 422
d1dea842 423 if (wxTheApp)
3352cfff 424 wxTheApp->ProcessIdle();
15dadf31 425
7e410431 426 gdk_flush();
ff7b1510 427}
c801d85f 428
8bbe427f 429bool wxIsBusy()
c801d85f 430{
89a43902 431 return gs_busyCount > 0;
ff7b1510 432}
c801d85f
KB
433
434void wxSetCursor( const wxCursor& cursor )
435{
238d735d 436 g_globalCursor = cursor;
a1abca32 437 wxTheApp->WakeUpIdle();
ff7b1510 438}