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