]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
Document wxDV_NO_HEADER style.
[wxWidgets.git] / src / gtk / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/cursor.cpp
3 // Purpose: wxCursor implementation
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 #include "wx/log.h"
21 #endif // WX_PRECOMP
22
23 #include <gtk/gtk.h>
24
25 //-----------------------------------------------------------------------------
26 // wxCursorRefData
27 //-----------------------------------------------------------------------------
28
29 class wxCursorRefData: public wxGDIRefData
30 {
31 public:
32 wxCursorRefData();
33 virtual ~wxCursorRefData();
34
35 virtual bool IsOk() const { return m_cursor != NULL; }
36
37 GdkCursor *m_cursor;
38 };
39
40 wxCursorRefData::wxCursorRefData()
41 {
42 m_cursor = NULL;
43 }
44
45 wxCursorRefData::~wxCursorRefData()
46 {
47 if (m_cursor) gdk_cursor_unref( m_cursor );
48 }
49
50
51 //-----------------------------------------------------------------------------
52 // wxCursor
53 //-----------------------------------------------------------------------------
54
55 #define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
56
57 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
58
59 // used in the following two ctors
60 extern GtkWidget *wxGetRootWindow();
61
62
63 wxCursor::wxCursor()
64 {
65 }
66
67 #if wxUSE_IMAGE
68 wxCursor::wxCursor(const wxString& cursor_file,
69 wxBitmapType type,
70 int hotSpotX, int hotSpotY)
71 {
72 wxImage img;
73 if (!img.LoadFile(cursor_file, type))
74 return;
75
76 // eventually set the hotspot:
77 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
78 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
79 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
80 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
81
82 InitFromImage(img);
83 }
84
85 wxCursor::wxCursor(const wxImage& img)
86 {
87 InitFromImage(img);
88 }
89 #endif
90
91 wxCursor::wxCursor(const char bits[], int width, int height,
92 int hotSpotX, int hotSpotY,
93 const char maskBits[], const wxColour *fg, const wxColour *bg)
94 {
95 if (!maskBits)
96 maskBits = bits;
97 if (!fg)
98 fg = wxBLACK;
99 if (!bg)
100 bg = wxWHITE;
101 if (hotSpotX < 0 || hotSpotX >= width)
102 hotSpotX = 0;
103 if (hotSpotY < 0 || hotSpotY >= height)
104 hotSpotY = 0;
105
106 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
107 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
108
109 m_refData = new wxCursorRefData;
110 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
111 data, mask, fg->GetColor(), bg->GetColor(),
112 hotSpotX, hotSpotY );
113
114 g_object_unref (data);
115 g_object_unref (mask);
116 }
117
118 wxCursor::~wxCursor()
119 {
120 }
121
122 void wxCursor::InitFromStock( wxStockCursor cursorId )
123 {
124 m_refData = new wxCursorRefData();
125
126 GdkCursorType gdk_cur = GDK_LEFT_PTR;
127 switch (cursorId)
128 {
129 case wxCURSOR_BLANK:
130 {
131 const char bits[] = { 0 };
132 const GdkColor color = { 0, 0, 0, 0 };
133
134 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
135 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
136 pixmap,
137 &color,
138 &color,
139 0, 0);
140 g_object_unref(pixmap);
141 }
142 return;
143
144 case wxCURSOR_ARROW: // fall through to default
145 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
146 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
147 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
148 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
149 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
150 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
151 case wxCURSOR_ARROWWAIT:
152 case wxCURSOR_WAIT:
153 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
154 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
155 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
156 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
157 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
158 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
159 case wxCURSOR_SIZENWSE:
160 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
161 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
162 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
163 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
164 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
165 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
166 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
167 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
168 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
169
170 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
171 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
172 /*
173 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
174 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
175 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
176 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
177 */
178
179 default:
180 wxFAIL_MSG(wxT("unsupported cursor type"));
181 // will use the standard one
182 break;
183 }
184
185 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
186 }
187
188 #if wxUSE_IMAGE
189
190 static void GetHotSpot(const wxImage& image, int& x, int& y)
191 {
192 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
193 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
194 else
195 x = 0;
196
197 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
198 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
199 else
200 y = 0;
201
202 if (x < 0 || x >= image.GetWidth())
203 x = 0;
204 if (y < 0 || y >= image.GetHeight())
205 y = 0;
206 }
207
208 void wxCursor::InitFromImage( const wxImage & image )
209 {
210 int w = image.GetWidth() ;
211 int h = image.GetHeight();
212 bool bHasMask = image.HasMask();
213 int hotSpotX, hotSpotY;
214 GetHotSpot(image, hotSpotX, hotSpotY);
215 m_refData = new wxCursorRefData;
216 wxImage image_copy(image);
217
218 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
219 if (gdk_display_supports_cursor_color(display))
220 {
221 if (!image.HasAlpha())
222 {
223 // add alpha, so wxBitmap will convert to pixbuf format
224 image_copy.InitAlpha();
225 }
226 wxBitmap bitmap(image_copy);
227 wxASSERT(bitmap.HasPixbuf());
228 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
229 (
230 display,
231 bitmap.GetPixbuf(),
232 hotSpotX, hotSpotY
233 );
234 return;
235 }
236
237 unsigned long keyMaskColor = 0;
238 GdkPixmap* mask;
239 if (bHasMask)
240 {
241 keyMaskColor = wxImageHistogram::MakeKey(
242 image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
243 // get mask before image is modified
244 wxBitmap bitmap(image, 1);
245 mask = bitmap.GetMask()->GetBitmap();
246 g_object_ref(mask);
247 }
248 else
249 {
250 const int size = ((w + 7) / 8) * h;
251 char* bits = new char[size];
252 memset(bits, 0xff, size);
253 mask = gdk_bitmap_create_from_data(
254 wxGetRootWindow()->window, bits, w, h);
255 delete[] bits;
256 }
257
258 // modify image so wxBitmap can be used to convert to pixmap
259 image_copy.SetMask(false);
260 int i, j;
261 wxByte* data = image_copy.GetData();
262 for (j = 0; j < h; j++)
263 {
264 for (i = 0; i < w; i++, data += 3)
265 {
266 //if average value is > mid grey
267 if (int(data[0]) + data[1] + data[2] >= 3 * 128)
268 {
269 // wxBitmap only converts (255,255,255) to white
270 data[0] = 255;
271 data[1] = 255;
272 data[2] = 255;
273 }
274 }
275 }
276 wxBitmap bitmap(image_copy, 1);
277
278 // find the most frequent color(s)
279 wxImageHistogram histogram;
280 image.ComputeHistogram(histogram);
281
282 long colMostFreq = 0;
283 unsigned long nMost = 0;
284 long colNextMostFreq = 0;
285 unsigned long nNext = 0;
286 for ( wxImageHistogram::iterator entry = histogram.begin();
287 entry != histogram.end();
288 ++entry )
289 {
290 unsigned long key = entry->first;
291 if ( !bHasMask || (key != keyMaskColor) )
292 {
293 unsigned long value = entry->second.value;
294 if (value > nMost)
295 {
296 nNext = nMost;
297 colNextMostFreq = colMostFreq;
298 nMost = value;
299 colMostFreq = key;
300 }
301 else if (value > nNext)
302 {
303 nNext = value;
304 colNextMostFreq = key;
305 }
306 }
307 }
308
309 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
310 (unsigned char)(colMostFreq >> 8),
311 (unsigned char)(colMostFreq) );
312
313 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
314 (unsigned char)(colNextMostFreq >> 8),
315 (unsigned char)(colNextMostFreq) );
316
317 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
318 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
319
320 if (bg_intensity > fg_intensity)
321 {
322 //swap fg and bg
323 wxColour tmp = fg;
324 fg = bg;
325 bg = tmp;
326 }
327
328 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
329 (
330 bitmap.GetPixmap(),
331 mask,
332 fg.GetColor(), bg.GetColor(),
333 hotSpotX, hotSpotY
334 );
335
336 g_object_unref (mask);
337 }
338
339 #endif // wxUSE_IMAGE
340
341 GdkCursor *wxCursor::GetCursor() const
342 {
343 return M_CURSORDATA->m_cursor;
344 }
345
346 wxGDIRefData *wxCursor::CreateGDIRefData() const
347 {
348 return new wxCursorRefData;
349 }
350
351 wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
352 {
353 return new wxCursorRefData(*static_cast<const wxCursorRefData *>(data));
354 }
355
356 //-----------------------------------------------------------------------------
357 // busy cursor routines
358 //-----------------------------------------------------------------------------
359
360 /* Current cursor, in order to hang on to
361 * cursor handle when setting the cursor globally */
362 wxCursor g_globalCursor;
363
364 static wxCursor gs_savedCursor;
365 static int gs_busyCount = 0;
366
367 const wxCursor &wxBusyCursor::GetStoredCursor()
368 {
369 return gs_savedCursor;
370 }
371
372 const wxCursor wxBusyCursor::GetBusyCursor()
373 {
374 return wxCursor(wxCURSOR_WATCH);
375 }
376
377 static void InternalIdle(const wxWindowList& list, GdkDisplay*& display)
378 {
379 wxWindowList::const_iterator i = list.begin();
380 for (size_t n = list.size(); n--; ++i)
381 {
382 wxWindow* win = *i;
383 if (display == NULL && win->m_widget && win->m_widget->window)
384 display = gdk_drawable_get_display(win->m_widget->window);
385 win->OnInternalIdle();
386 InternalIdle(win->GetChildren(), display);
387 }
388 }
389
390 void wxEndBusyCursor()
391 {
392 if (--gs_busyCount > 0)
393 return;
394
395 g_globalCursor = gs_savedCursor;
396 gs_savedCursor = wxNullCursor;
397 GdkDisplay* unused = NULL;
398 InternalIdle(wxTopLevelWindows, unused);
399 }
400
401 void wxBeginBusyCursor(const wxCursor* cursor)
402 {
403 if (gs_busyCount++ > 0)
404 return;
405
406 wxASSERT_MSG( !gs_savedCursor.Ok(),
407 wxT("forgot to call wxEndBusyCursor, will leak memory") );
408
409 gs_savedCursor = g_globalCursor;
410 g_globalCursor = *cursor;
411 GdkDisplay* display = NULL;
412 InternalIdle(wxTopLevelWindows, display);
413 if (display)
414 gdk_display_flush(display);
415 }
416
417 bool wxIsBusy()
418 {
419 return gs_busyCount > 0;
420 }
421
422 void wxSetCursor( const wxCursor& cursor )
423 {
424 g_globalCursor = cursor;
425 wxTheApp->WakeUpIdle();
426 }