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