]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
added wx/msw/missing.h
[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
KB
144wxCursor::wxCursor( const wxCursor &cursor )
145{
2d17d68f 146 Ref( cursor );
ff7b1510 147}
c801d85f 148
7eff657c 149#if wxUSE_IMAGE
0fc5dbf5 150
7eff657c
JS
151wxCursor::wxCursor( const wxImage & image )
152{
153 unsigned char * rgbBits = image.GetData();
0fc5dbf5
VZ
154 int w = image.GetWidth() ;
155 int h = image.GetHeight();
156 bool bHasMask = image.HasMask();
7eff657c
JS
157 int imagebitcount = (w*h)/8;
158
7eff657c
JS
159 unsigned char * bits = new unsigned char [imagebitcount];
160 unsigned char * maskBits = new unsigned char [imagebitcount];
161
0fc5dbf5 162 int i, j, i8; unsigned char c, cMask;
7eff657c 163 for (i=0; i<imagebitcount; i++)
0fc5dbf5 164 {
7eff657c
JS
165 bits[i] = 0;
166 i8 = i * 8;
167
168 cMask = 1;
169 for (j=0; j<8; j++)
0fc5dbf5
VZ
170 {
171 // possible overflow if we do the summation first ?
172 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
173 //if average value is > mid grey
174 if (c>127)
175 bits[i] = bits[i] | cMask;
176 cMask = cMask * 2;
7eff657c 177 }
0fc5dbf5
VZ
178 }
179
180 unsigned long keyMaskColor;
7eff657c 181 if (bHasMask)
0fc5dbf5
VZ
182 {
183 unsigned char
184 r = image.GetMaskRed(),
185 g = image.GetMaskGreen(),
186 b = image.GetMaskBlue();
7eff657c
JS
187
188 for (i=0; i<imagebitcount; i++)
189 {
0fc5dbf5
VZ
190 maskBits[i] = 0x0;
191 i8 = i * 8;
7eff657c 192
0fc5dbf5
VZ
193 cMask = 1;
194 for (j=0; j<8; j++)
195 {
196 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
197 maskBits[i] = maskBits[i] | cMask;
198 cMask = cMask * 2;
199 }
7eff657c 200 }
0fc5dbf5
VZ
201
202 keyMaskColor = (r << 16) | (g << 8) | b;
203 }
204 else // no mask
205 {
7eff657c 206 for (i=0; i<imagebitcount; i++)
0fc5dbf5
VZ
207 maskBits[i] = 0xFF;
208
209 // init it to avoid compiler warnings
210 keyMaskColor = 0;
211 }
212
213 // find the most frequent color(s) it seems a waste of effort to copy the
214 // image but otherwise we need to remove the const modifier ??
7eff657c 215 wxImage tmpImage = image.Copy();
952ae1e8 216 wxImageHistogram histogram;
7eff657c 217
0fc5dbf5 218 // colors as rrggbb
7eff657c 219 unsigned long key;
952ae1e8 220 unsigned long value;
7eff657c 221
952ae1e8 222 tmpImage.ComputeHistogram(histogram);
7eff657c 223
952ae1e8
VS
224 long MostFreqCol = 0;
225 unsigned long nMost = 0;
226 long NextFreqCol = 0;
227 unsigned long nNext = 0;
228 wxImageHistogram::iterator entry = histogram.begin();
229
230 while ( entry != histogram.end() )
7eff657c 231 {
952ae1e8
VS
232 value = entry->second.value;
233 key = entry->first;
234 if ( !bHasMask || (key != keyMaskColor) )
7eff657c 235 {
952ae1e8 236 if (value > nMost)
7eff657c 237 {
952ae1e8
VS
238 nMost = value;
239 MostFreqCol = key;
7eff657c 240 }
952ae1e8 241 else if (value > nNext)
7eff657c 242 {
952ae1e8
VS
243 nNext = value;
244 NextFreqCol = key;
7eff657c
JS
245 }
246 }
247 }
248
7eff657c
JS
249 wxColour fg = wxColour ( (unsigned char)(MostFreqCol >> 16),
250 (unsigned char)(MostFreqCol >> 8),
0fc5dbf5 251 (unsigned char)(MostFreqCol) );
7eff657c
JS
252
253 wxColour bg = wxColour ( (unsigned char)(NextFreqCol >> 16),
254 (unsigned char)(NextFreqCol >> 8),
0fc5dbf5 255 (unsigned char)(NextFreqCol) );
7eff657c 256
15dadf31 257 int hotSpotX=0;
7eff657c
JS
258 int hotSpotY=0;
259
260 if (image.HasOption(wxCUR_HOTSPOT_X))
952ae1e8 261 hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
7eff657c 262 if (image.HasOption(wxCUR_HOTSPOT_Y))
952ae1e8 263 hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
15dadf31 264
7eff657c 265 if (hotSpotX < 0 || hotSpotX >= w)
952ae1e8 266 hotSpotX = 0;
7eff657c 267 if (hotSpotY < 0 || hotSpotY >= h)
952ae1e8 268 hotSpotY = 0;
7eff657c
JS
269
270 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits,
271 w, h );
272 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits,
273 w, h );
274
275 m_refData = new wxCursorRefData;
276 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
277 data, mask, fg.GetColor(), bg.GetColor(),
278 hotSpotX, hotSpotY );
279
280 gdk_bitmap_unref( data );
281 gdk_bitmap_unref( mask );
0fc5dbf5 282 delete [] bits;
7eff657c 283 delete [] maskBits;
7eff657c 284}
0fc5dbf5
VZ
285
286#endif // wxUSE_IMAGE
7eff657c 287
8bbe427f 288wxCursor::~wxCursor()
c801d85f 289{
ff7b1510 290}
c801d85f
KB
291
292wxCursor& wxCursor::operator = ( const wxCursor& cursor )
293{
7c39369e
VZ
294 if (*this == cursor)
295 return (*this);
296
2d17d68f 297 Ref( cursor );
7c39369e 298
2d17d68f 299 return *this;
ff7b1510 300}
c801d85f 301
8bbe427f 302bool wxCursor::operator == ( const wxCursor& cursor ) const
c801d85f 303{
2d17d68f 304 return m_refData == cursor.m_refData;
ff7b1510 305}
c801d85f 306
8bbe427f 307bool wxCursor::operator != ( const wxCursor& cursor ) const
c801d85f 308{
2d17d68f 309 return m_refData != cursor.m_refData;
ff7b1510 310}
c801d85f 311
8bbe427f 312bool wxCursor::Ok() const
c801d85f 313{
2d17d68f 314 return (m_refData != NULL);
ff7b1510 315}
c801d85f 316
8bbe427f 317GdkCursor *wxCursor::GetCursor() const
c801d85f 318{
2d17d68f 319 return M_CURSORDATA->m_cursor;
ff7b1510 320}
c801d85f
KB
321
322//-----------------------------------------------------------------------------
323// busy cursor routines
324//-----------------------------------------------------------------------------
325
238d735d 326extern wxCursor g_globalCursor;
7c39369e 327
238d735d 328static wxCursor gs_savedCursor;
89a43902 329static int gs_busyCount = 0;
c801d85f 330
f6bcfd97
BP
331const wxCursor &wxBusyCursor::GetStoredCursor()
332{
333 return gs_savedCursor;
334}
335
336const wxCursor wxBusyCursor::GetBusyCursor()
337{
338 return wxCursor(wxCURSOR_WATCH);
339}
340
8bbe427f 341void wxEndBusyCursor()
c801d85f 342{
238d735d 343 if (--gs_busyCount > 0)
89a43902
VZ
344 return;
345
238d735d
RR
346 wxSetCursor( gs_savedCursor );
347 gs_savedCursor = wxNullCursor;
13971833 348
d1dea842
RR
349 if (wxTheApp)
350 wxTheApp->SendIdleEvents();
ff7b1510 351}
c801d85f
KB
352
353void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
354{
238d735d 355 if (gs_busyCount++ > 0)
89a43902
VZ
356 return;
357
238d735d 358 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 359 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 360
238d735d 361 gs_savedCursor = g_globalCursor;
13971833 362
238d735d 363 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
13971833 364
d1dea842
RR
365 if (wxTheApp)
366 wxTheApp->SendIdleEvents();
15dadf31 367
7e410431 368 gdk_flush();
ff7b1510 369}
c801d85f 370
8bbe427f 371bool wxIsBusy()
c801d85f 372{
89a43902 373 return gs_busyCount > 0;
ff7b1510 374}
c801d85f
KB
375
376void wxSetCursor( const wxCursor& cursor )
377{
13971833 378 if (g_isIdle)
238d735d
RR
379 wxapp_install_idle_handler();
380
381 g_globalCursor = cursor;
ff7b1510 382}