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