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