]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
don't test for both stream.fail() and stream.bad() as the latter implies the former...
[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/window.h"
17 #include "wx/app.h"
18 #include "wx/image.h"
19 #include "wx/bitmap.h"
20 #endif // WX_PRECOMP
21
22 #include <gtk/gtk.h>
23
24 //-----------------------------------------------------------------------------
25 // wxCursor
26 //-----------------------------------------------------------------------------
27
28 class wxCursorRefData: public wxGDIRefData
29 {
30 public:
31 wxCursorRefData();
32 virtual ~wxCursorRefData();
33
34 virtual bool IsOk() const { return m_cursor != NULL; }
35
36 GdkCursor *m_cursor;
37 };
38
39 wxCursorRefData::wxCursorRefData()
40 {
41 m_cursor = (GdkCursor *) NULL;
42 }
43
44 wxCursorRefData::~wxCursorRefData()
45 {
46 if (m_cursor) gdk_cursor_unref( m_cursor );
47 }
48
49 //-----------------------------------------------------------------------------
50
51 #define M_CURSORDATA wx_static_cast(wxCursorRefData*, m_refData)
52
53 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
54
55 wxCursor::wxCursor()
56 {
57 }
58
59 wxCursor::wxCursor( wxStockCursor cursorId )
60 {
61 m_refData = new wxCursorRefData();
62
63 GdkCursorType gdk_cur = GDK_LEFT_PTR;
64 switch (cursorId)
65 {
66 case wxCURSOR_BLANK:
67 {
68 const char bits[] = { 0 };
69 const GdkColor color = { 0, 0, 0, 0 };
70
71 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
72 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
73 pixmap,
74 &color,
75 &color,
76 0, 0);
77 g_object_unref(pixmap);
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_HAND2; 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
125 extern GtkWidget *wxGetRootWindow();
126
127 wxCursor::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
156 static void GetHotSpot(const wxImage& image, int& x, int& y)
157 {
158 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
159 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
160 else
161 x = 0;
162
163 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
164 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
165 else
166 y = 0;
167
168 if (x < 0 || x >= image.GetWidth())
169 x = 0;
170 if (y < 0 || y >= image.GetHeight())
171 y = 0;
172 }
173
174 wxCursor::wxCursor( const wxImage & image )
175 {
176 int w = image.GetWidth() ;
177 int h = image.GetHeight();
178 bool bHasMask = image.HasMask();
179 int hotSpotX, hotSpotY;
180 GetHotSpot(image, hotSpotX, hotSpotY);
181 m_refData = new wxCursorRefData;
182 wxImage image_copy(image);
183
184 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
185 if (gdk_display_supports_cursor_color(display))
186 {
187 if (!image.HasAlpha())
188 {
189 // add alpha, so wxBitmap will convert to pixbuf format
190 image_copy.InitAlpha();
191 }
192 wxBitmap bitmap(image_copy);
193 wxASSERT(bitmap.HasPixbuf());
194 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
195 (
196 display,
197 bitmap.GetPixbuf(),
198 hotSpotX, hotSpotY
199 );
200 return;
201 }
202
203 unsigned long keyMaskColor = 0;
204 GdkPixmap* mask;
205 if (bHasMask)
206 {
207 keyMaskColor = wxImageHistogram::MakeKey(
208 image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
209 // get mask before image is modified
210 wxBitmap bitmap(image, 1);
211 mask = bitmap.GetMask()->GetBitmap();
212 g_object_ref(mask);
213 }
214 else
215 {
216 const int size = ((w + 7) / 8) * h;
217 char* bits = new char[size];
218 memset(bits, 0xff, size);
219 mask = gdk_bitmap_create_from_data(
220 wxGetRootWindow()->window, bits, w, h);
221 delete[] bits;
222 }
223
224 // modify image so wxBitmap can be used to convert to pixmap
225 image_copy.SetMask(false);
226 int i, j;
227 wxByte* data = image_copy.GetData();
228 for (j = 0; j < h; j++)
229 {
230 for (i = 0; i < w; i++, data += 3)
231 {
232 //if average value is > mid grey
233 if (int(data[0]) + data[1] + data[2] >= 3 * 128)
234 {
235 // wxBitmap only converts (255,255,255) to white
236 data[0] = 255;
237 data[1] = 255;
238 data[2] = 255;
239 }
240 }
241 }
242 wxBitmap bitmap(image_copy, 1);
243
244 // find the most frequent color(s)
245 wxImageHistogram histogram;
246 image.ComputeHistogram(histogram);
247
248 long colMostFreq = 0;
249 unsigned long nMost = 0;
250 long colNextMostFreq = 0;
251 unsigned long nNext = 0;
252 for ( wxImageHistogram::iterator entry = histogram.begin();
253 entry != histogram.end();
254 ++entry )
255 {
256 unsigned long key = entry->first;
257 if ( !bHasMask || (key != keyMaskColor) )
258 {
259 unsigned long value = entry->second.value;
260 if (value > nMost)
261 {
262 nNext = nMost;
263 colNextMostFreq = colMostFreq;
264 nMost = value;
265 colMostFreq = key;
266 }
267 else if (value > nNext)
268 {
269 nNext = value;
270 colNextMostFreq = key;
271 }
272 }
273 }
274
275 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
276 (unsigned char)(colMostFreq >> 8),
277 (unsigned char)(colMostFreq) );
278
279 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
280 (unsigned char)(colNextMostFreq >> 8),
281 (unsigned char)(colNextMostFreq) );
282
283 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
284 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
285
286 if (bg_intensity > fg_intensity)
287 {
288 //swap fg and bg
289 wxColour tmp = fg;
290 fg = bg;
291 bg = tmp;
292 }
293
294 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
295 (
296 bitmap.GetPixmap(),
297 mask,
298 fg.GetColor(), bg.GetColor(),
299 hotSpotX, hotSpotY
300 );
301
302 g_object_unref (mask);
303 }
304
305 #endif // wxUSE_IMAGE
306
307 wxCursor::~wxCursor()
308 {
309 }
310
311 GdkCursor *wxCursor::GetCursor() const
312 {
313 return M_CURSORDATA->m_cursor;
314 }
315
316 wxGDIRefData *wxCursor::CreateGDIRefData() const
317 {
318 return new wxCursorRefData;
319 }
320
321 wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
322 {
323 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData *, data));
324 }
325
326 //-----------------------------------------------------------------------------
327 // busy cursor routines
328 //-----------------------------------------------------------------------------
329
330 /* Current cursor, in order to hang on to
331 * cursor handle when setting the cursor globally */
332 wxCursor g_globalCursor;
333
334 static wxCursor gs_savedCursor;
335 static int gs_busyCount = 0;
336
337 const wxCursor &wxBusyCursor::GetStoredCursor()
338 {
339 return gs_savedCursor;
340 }
341
342 const wxCursor wxBusyCursor::GetBusyCursor()
343 {
344 return wxCursor(wxCURSOR_WATCH);
345 }
346
347 static void InternalIdle(const wxWindowList& list, GdkDisplay*& display)
348 {
349 wxWindowList::const_iterator i = list.begin();
350 for (size_t n = list.size(); n--; ++i)
351 {
352 wxWindow* win = *i;
353 if (display == NULL && win->m_widget && win->m_widget->window)
354 display = gdk_drawable_get_display(win->m_widget->window);
355 win->OnInternalIdle();
356 InternalIdle(win->GetChildren(), display);
357 }
358 }
359
360 void wxEndBusyCursor()
361 {
362 if (--gs_busyCount > 0)
363 return;
364
365 g_globalCursor = gs_savedCursor;
366 gs_savedCursor = wxNullCursor;
367 GdkDisplay* unused = NULL;
368 InternalIdle(wxTopLevelWindows, unused);
369 }
370
371 void wxBeginBusyCursor(const wxCursor* 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 g_globalCursor = *cursor;
381 GdkDisplay* display = NULL;
382 InternalIdle(wxTopLevelWindows, display);
383 if (display)
384 gdk_display_flush(display);
385 }
386
387 bool wxIsBusy()
388 {
389 return gs_busyCount > 0;
390 }
391
392 void wxSetCursor( const wxCursor& cursor )
393 {
394 g_globalCursor = cursor;
395 wxTheApp->WakeUpIdle();
396 }