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