]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
added a check which should prevent the crash of bug 555111
[wxWidgets.git] / src / gtk / cursor.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: cursor.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
8bbe427f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "cursor.h"
13#endif
14
15#include "wx/cursor.h"
4dcaf11a 16#include "wx/utils.h"
d1dea842 17#include "wx/app.h"
c801d85f 18
071a2d78 19#include <gdk/gdk.h>
a1db952f 20#include <gtk/gtk.h>
83624f79 21
238d735d
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
c801d85f
KB
29//-----------------------------------------------------------------------------
30// wxCursor
31//-----------------------------------------------------------------------------
32
33class wxCursorRefData: public wxObjectRefData
34{
35 public:
8bbe427f
VZ
36
37 wxCursorRefData();
38 ~wxCursorRefData();
39
c801d85f
KB
40 GdkCursor *m_cursor;
41};
42
8bbe427f 43wxCursorRefData::wxCursorRefData()
c801d85f 44{
2d17d68f 45 m_cursor = (GdkCursor *) NULL;
ff7b1510 46}
c801d85f 47
8bbe427f 48wxCursorRefData::~wxCursorRefData()
c801d85f 49{
2d17d68f 50 if (m_cursor) gdk_cursor_destroy( m_cursor );
ff7b1510 51}
c801d85f
KB
52
53//-----------------------------------------------------------------------------
54
55#define M_CURSORDATA ((wxCursorRefData *)m_refData)
56
57IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
58
8bbe427f 59wxCursor::wxCursor()
c801d85f 60{
7eff657c 61
ff7b1510 62}
c801d85f 63
debe6624 64wxCursor::wxCursor( int cursorId )
c801d85f 65{
2d17d68f
RR
66 m_refData = new wxCursorRefData();
67
68 GdkCursorType gdk_cur = GDK_LEFT_PTR;
69 switch (cursorId)
70 {
15dadf31 71 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 72 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 73 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
2d17d68f
RR
74 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
75 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
76 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
77 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
83f96286 78 case wxCURSOR_ARROWWAIT:
7c39369e 79 case wxCURSOR_WAIT:
2d17d68f
RR
80 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
81 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
82 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
83 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
84 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
85 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
7c39369e 86 case wxCURSOR_SIZENWSE:
2d17d68f
RR
87 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
88 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
89 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
90 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
91 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
2d17d68f
RR
92 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
93 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
94 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
13971833
RD
95 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
96
957d856c
VZ
97 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
98 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 99/*
7c39369e
VZ
100 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
101 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
102 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
103 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
c801d85f 104*/
7c39369e 105 default:
223d09f6 106 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 107 // will use the standard one
7c39369e 108 break;
2d17d68f 109 }
8bbe427f 110
2d17d68f 111 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 112}
c801d85f 113
c2fa61e8 114extern GtkWidget *wxGetRootWindow();
a1db952f
VS
115
116wxCursor::wxCursor(const char bits[], int width, int height,
117 int hotSpotX, int hotSpotY,
118 const char maskBits[], wxColour *fg, wxColour *bg)
119{
120 if (!maskBits)
121 maskBits = bits;
122 if (!fg)
123 fg = wxBLACK;
124 if (!bg)
125 bg = wxWHITE;
126 if (hotSpotX < 0 || hotSpotX >= width)
127 hotSpotX = 0;
128 if (hotSpotY < 0 || hotSpotY >= height)
129 hotSpotY = 0;
130
c2fa61e8
RD
131 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
132 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
a1db952f
VS
133
134 m_refData = new wxCursorRefData;
135 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
136 data, mask, fg->GetColor(), bg->GetColor(),
137 hotSpotX, hotSpotY );
138
139 gdk_bitmap_unref( data );
140 gdk_bitmap_unref( mask );
141}
142
143
c801d85f 144wxCursor::wxCursor( const wxCursor &cursor )
d84afea9 145 : wxObject()
c801d85f 146{
2d17d68f 147 Ref( cursor );
ff7b1510 148}
c801d85f 149
7eff657c 150#if wxUSE_IMAGE
0fc5dbf5 151
7eff657c
JS
152wxCursor::wxCursor( const wxImage & image )
153{
154 unsigned char * rgbBits = image.GetData();
0fc5dbf5
VZ
155 int w = image.GetWidth() ;
156 int h = image.GetHeight();
157 bool bHasMask = image.HasMask();
7eff657c
JS
158 int imagebitcount = (w*h)/8;
159
7eff657c
JS
160 unsigned char * bits = new unsigned char [imagebitcount];
161 unsigned char * maskBits = new unsigned char [imagebitcount];
162
0fc5dbf5 163 int i, j, i8; unsigned char c, cMask;
7eff657c 164 for (i=0; i<imagebitcount; i++)
0fc5dbf5 165 {
7eff657c
JS
166 bits[i] = 0;
167 i8 = i * 8;
168
169 cMask = 1;
170 for (j=0; j<8; j++)
0fc5dbf5
VZ
171 {
172 // possible overflow if we do the summation first ?
173 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
174 //if average value is > mid grey
175 if (c>127)
176 bits[i] = bits[i] | cMask;
177 cMask = cMask * 2;
7eff657c 178 }
0fc5dbf5
VZ
179 }
180
181 unsigned long keyMaskColor;
7eff657c 182 if (bHasMask)
0fc5dbf5
VZ
183 {
184 unsigned char
185 r = image.GetMaskRed(),
186 g = image.GetMaskGreen(),
187 b = image.GetMaskBlue();
7eff657c
JS
188
189 for (i=0; i<imagebitcount; i++)
190 {
0fc5dbf5
VZ
191 maskBits[i] = 0x0;
192 i8 = i * 8;
7eff657c 193
0fc5dbf5
VZ
194 cMask = 1;
195 for (j=0; j<8; j++)
196 {
197 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
198 maskBits[i] = maskBits[i] | cMask;
199 cMask = cMask * 2;
200 }
7eff657c 201 }
0fc5dbf5
VZ
202
203 keyMaskColor = (r << 16) | (g << 8) | b;
204 }
205 else // no mask
206 {
7eff657c 207 for (i=0; i<imagebitcount; i++)
0fc5dbf5
VZ
208 maskBits[i] = 0xFF;
209
210 // init it to avoid compiler warnings
211 keyMaskColor = 0;
212 }
213
214 // find the most frequent color(s) it seems a waste of effort to copy the
215 // image but otherwise we need to remove the const modifier ??
7eff657c 216 wxImage tmpImage = image.Copy();
952ae1e8 217 wxImageHistogram histogram;
7eff657c 218
0fc5dbf5 219 // colors as rrggbb
7eff657c 220 unsigned long key;
952ae1e8 221 unsigned long value;
7eff657c 222
952ae1e8 223 tmpImage.ComputeHistogram(histogram);
7eff657c 224
952ae1e8
VS
225 long MostFreqCol = 0;
226 unsigned long nMost = 0;
227 long NextFreqCol = 0;
228 unsigned long nNext = 0;
229 wxImageHistogram::iterator entry = histogram.begin();
230
231 while ( entry != histogram.end() )
7eff657c 232 {
952ae1e8
VS
233 value = entry->second.value;
234 key = entry->first;
235 if ( !bHasMask || (key != keyMaskColor) )
7eff657c 236 {
952ae1e8 237 if (value > nMost)
7eff657c 238 {
952ae1e8
VS
239 nMost = value;
240 MostFreqCol = key;
7eff657c 241 }
952ae1e8 242 else if (value > nNext)
7eff657c 243 {
952ae1e8
VS
244 nNext = value;
245 NextFreqCol = key;
7eff657c
JS
246 }
247 }
248 }
249
7eff657c
JS
250 wxColour fg = wxColour ( (unsigned char)(MostFreqCol >> 16),
251 (unsigned char)(MostFreqCol >> 8),
0fc5dbf5 252 (unsigned char)(MostFreqCol) );
7eff657c
JS
253
254 wxColour bg = wxColour ( (unsigned char)(NextFreqCol >> 16),
255 (unsigned char)(NextFreqCol >> 8),
0fc5dbf5 256 (unsigned char)(NextFreqCol) );
7eff657c 257
15dadf31 258 int hotSpotX=0;
7eff657c
JS
259 int hotSpotY=0;
260
261 if (image.HasOption(wxCUR_HOTSPOT_X))
952ae1e8 262 hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
7eff657c 263 if (image.HasOption(wxCUR_HOTSPOT_Y))
952ae1e8 264 hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
15dadf31 265
7eff657c 266 if (hotSpotX < 0 || hotSpotX >= w)
952ae1e8 267 hotSpotX = 0;
7eff657c 268 if (hotSpotY < 0 || hotSpotY >= h)
952ae1e8 269 hotSpotY = 0;
7eff657c
JS
270
271 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits,
272 w, h );
273 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits,
274 w, h );
275
276 m_refData = new wxCursorRefData;
277 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
278 data, mask, fg.GetColor(), bg.GetColor(),
279 hotSpotX, hotSpotY );
280
281 gdk_bitmap_unref( data );
282 gdk_bitmap_unref( mask );
0fc5dbf5 283 delete [] bits;
7eff657c 284 delete [] maskBits;
7eff657c 285}
0fc5dbf5
VZ
286
287#endif // wxUSE_IMAGE
7eff657c 288
8bbe427f 289wxCursor::~wxCursor()
c801d85f 290{
ff7b1510 291}
c801d85f
KB
292
293wxCursor& wxCursor::operator = ( const wxCursor& cursor )
294{
7c39369e
VZ
295 if (*this == cursor)
296 return (*this);
297
2d17d68f 298 Ref( cursor );
7c39369e 299
2d17d68f 300 return *this;
ff7b1510 301}
c801d85f 302
8bbe427f 303bool wxCursor::operator == ( const wxCursor& cursor ) const
c801d85f 304{
2d17d68f 305 return m_refData == cursor.m_refData;
ff7b1510 306}
c801d85f 307
8bbe427f 308bool wxCursor::operator != ( const wxCursor& cursor ) const
c801d85f 309{
2d17d68f 310 return m_refData != cursor.m_refData;
ff7b1510 311}
c801d85f 312
8bbe427f 313bool wxCursor::Ok() const
c801d85f 314{
2d17d68f 315 return (m_refData != NULL);
ff7b1510 316}
c801d85f 317
8bbe427f 318GdkCursor *wxCursor::GetCursor() const
c801d85f 319{
2d17d68f 320 return M_CURSORDATA->m_cursor;
ff7b1510 321}
c801d85f
KB
322
323//-----------------------------------------------------------------------------
324// busy cursor routines
325//-----------------------------------------------------------------------------
326
238d735d 327extern wxCursor g_globalCursor;
7c39369e 328
238d735d 329static wxCursor gs_savedCursor;
89a43902 330static int gs_busyCount = 0;
c801d85f 331
f6bcfd97
BP
332const wxCursor &wxBusyCursor::GetStoredCursor()
333{
334 return gs_savedCursor;
335}
336
337const wxCursor wxBusyCursor::GetBusyCursor()
338{
339 return wxCursor(wxCURSOR_WATCH);
340}
341
8bbe427f 342void wxEndBusyCursor()
c801d85f 343{
238d735d 344 if (--gs_busyCount > 0)
89a43902
VZ
345 return;
346
238d735d
RR
347 wxSetCursor( gs_savedCursor );
348 gs_savedCursor = wxNullCursor;
13971833 349
d1dea842
RR
350 if (wxTheApp)
351 wxTheApp->SendIdleEvents();
ff7b1510 352}
c801d85f
KB
353
354void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
355{
238d735d 356 if (gs_busyCount++ > 0)
89a43902
VZ
357 return;
358
238d735d 359 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 360 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 361
238d735d 362 gs_savedCursor = g_globalCursor;
13971833 363
238d735d 364 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
13971833 365
d1dea842
RR
366 if (wxTheApp)
367 wxTheApp->SendIdleEvents();
15dadf31 368
7e410431 369 gdk_flush();
ff7b1510 370}
c801d85f 371
8bbe427f 372bool wxIsBusy()
c801d85f 373{
89a43902 374 return gs_busyCount > 0;
ff7b1510 375}
c801d85f
KB
376
377void wxSetCursor( const wxCursor& cursor )
378{
13971833 379 if (g_isIdle)
238d735d
RR
380 wxapp_install_idle_handler();
381
382 g_globalCursor = cursor;
ff7b1510 383}