]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/cursor.cpp
Some WinCE fixes
[wxWidgets.git] / src / gtk1 / 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 : wxObject()
146 {
147 Ref( cursor );
148 }
149
150 #if wxUSE_IMAGE
151
152 wxCursor::wxCursor( const wxImage & image )
153 {
154 unsigned char * rgbBits = image.GetData();
155 int w = image.GetWidth() ;
156 int h = image.GetHeight();
157 bool bHasMask = image.HasMask();
158 int imagebitcount = (w*h)/8;
159
160 unsigned char * bits = new unsigned char [imagebitcount];
161 unsigned char * maskBits = new unsigned char [imagebitcount];
162
163 int i, j, i8; unsigned char c, cMask;
164 for (i=0; i<imagebitcount; i++)
165 {
166 bits[i] = 0;
167 i8 = i * 8;
168
169 cMask = 1;
170 for (j=0; j<8; j++)
171 {
172 // possible overflow if we do the summation first ?
173 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
174 //if average value is > mid grey
175 if (c>127)
176 bits[i] = bits[i] | cMask;
177 cMask = cMask * 2;
178 }
179 }
180
181 unsigned long keyMaskColor;
182 if (bHasMask)
183 {
184 unsigned char
185 r = image.GetMaskRed(),
186 g = image.GetMaskGreen(),
187 b = image.GetMaskBlue();
188
189 for (i=0; i<imagebitcount; i++)
190 {
191 maskBits[i] = 0x0;
192 i8 = i * 8;
193
194 cMask = 1;
195 for (j=0; j<8; j++)
196 {
197 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
198 maskBits[i] = maskBits[i] | cMask;
199 cMask = cMask * 2;
200 }
201 }
202
203 keyMaskColor = (r << 16) | (g << 8) | b;
204 }
205 else // no mask
206 {
207 for (i=0; i<imagebitcount; i++)
208 maskBits[i] = 0xFF;
209
210 // init it to avoid compiler warnings
211 keyMaskColor = 0;
212 }
213
214 // find the most frequent color(s)
215 wxImageHistogram histogram;
216 image.ComputeHistogram(histogram);
217
218 // colors as rrggbb
219 unsigned long key;
220 unsigned long value;
221
222 long colMostFreq = 0;
223 unsigned long nMost = 0;
224 long colNextMostFreq = 0;
225 unsigned long nNext = 0;
226 for ( wxImageHistogram::iterator entry = histogram.begin();
227 entry != histogram.end();
228 ++entry )
229 {
230 value = entry->second.value;
231 key = entry->first;
232 if ( !bHasMask || (key != keyMaskColor) )
233 {
234 if (value > nMost)
235 {
236 nMost = value;
237 colMostFreq = key;
238 }
239 else if (value > nNext)
240 {
241 nNext = value;
242 colNextMostFreq = key;
243 }
244 }
245 }
246
247 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
248 (unsigned char)(colMostFreq >> 8),
249 (unsigned char)(colMostFreq) );
250
251 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
252 (unsigned char)(colNextMostFreq >> 8),
253 (unsigned char)(colNextMostFreq) );
254
255 int hotSpotX;
256 int hotSpotY;
257
258 if (image.HasOption(wxCUR_HOTSPOT_X))
259 hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
260 else
261 hotSpotX = 0;
262
263 if (image.HasOption(wxCUR_HOTSPOT_Y))
264 hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
265 else
266 hotSpotY = 0;
267
268 if (hotSpotX < 0 || hotSpotX >= w)
269 hotSpotX = 0;
270 if (hotSpotY < 0 || hotSpotY >= h)
271 hotSpotY = 0;
272
273 GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
274 (gchar *) bits, w, h);
275 GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
276 (gchar *) maskBits, w, h);
277
278 m_refData = new wxCursorRefData;
279 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
280 (
281 data,
282 mask,
283 fg.GetColor(), bg.GetColor(),
284 hotSpotX, hotSpotY
285 );
286
287 gdk_bitmap_unref( data );
288 gdk_bitmap_unref( mask );
289 delete [] bits;
290 delete [] maskBits;
291 }
292
293 #endif // wxUSE_IMAGE
294
295 wxCursor::~wxCursor()
296 {
297 }
298
299 wxCursor& wxCursor::operator = ( const wxCursor& cursor )
300 {
301 if (*this == cursor)
302 return (*this);
303
304 Ref( cursor );
305
306 return *this;
307 }
308
309 bool wxCursor::operator == ( const wxCursor& cursor ) const
310 {
311 return m_refData == cursor.m_refData;
312 }
313
314 bool wxCursor::operator != ( const wxCursor& cursor ) const
315 {
316 return m_refData != cursor.m_refData;
317 }
318
319 bool wxCursor::Ok() const
320 {
321 return (m_refData != NULL);
322 }
323
324 GdkCursor *wxCursor::GetCursor() const
325 {
326 return M_CURSORDATA->m_cursor;
327 }
328
329 //-----------------------------------------------------------------------------
330 // busy cursor routines
331 //-----------------------------------------------------------------------------
332
333 extern 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->SendIdleEvents();
358 }
359
360 void wxBeginBusyCursor( 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 if (g_isIdle)
386 wxapp_install_idle_handler();
387
388 g_globalCursor = cursor;
389 }