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