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