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