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