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