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