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