]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
applied copy constructor fix to assignment operator as well
[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 : 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) it seems a waste of effort to copy the
215 // image but otherwise we need to remove the const modifier ??
216 wxImage tmpImage = image.Copy();
217 wxImageHistogram histogram;
218
219 // colors as rrggbb
220 unsigned long key;
221 unsigned long value;
222
223 tmpImage.ComputeHistogram(histogram);
224
225 long MostFreqCol = 0;
226 unsigned long nMost = 0;
227 long NextFreqCol = 0;
228 unsigned long nNext = 0;
229 wxImageHistogram::iterator entry = histogram.begin();
230
231 while ( entry != histogram.end() )
232 {
233 value = entry->second.value;
234 key = entry->first;
235 if ( !bHasMask || (key != keyMaskColor) )
236 {
237 if (value > nMost)
238 {
239 nMost = value;
240 MostFreqCol = key;
241 }
242 else if (value > nNext)
243 {
244 nNext = value;
245 NextFreqCol = key;
246 }
247 }
248 }
249
250 wxColour fg = wxColour ( (unsigned char)(MostFreqCol >> 16),
251 (unsigned char)(MostFreqCol >> 8),
252 (unsigned char)(MostFreqCol) );
253
254 wxColour bg = wxColour ( (unsigned char)(NextFreqCol >> 16),
255 (unsigned char)(NextFreqCol >> 8),
256 (unsigned char)(NextFreqCol) );
257
258 int hotSpotX=0;
259 int hotSpotY=0;
260
261 if (image.HasOption(wxCUR_HOTSPOT_X))
262 hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
263 if (image.HasOption(wxCUR_HOTSPOT_Y))
264 hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
265
266 if (hotSpotX < 0 || hotSpotX >= w)
267 hotSpotX = 0;
268 if (hotSpotY < 0 || hotSpotY >= h)
269 hotSpotY = 0;
270
271 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits,
272 w, h );
273 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits,
274 w, h );
275
276 m_refData = new wxCursorRefData;
277 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
278 data, mask, fg.GetColor(), bg.GetColor(),
279 hotSpotX, hotSpotY );
280
281 gdk_bitmap_unref( data );
282 gdk_bitmap_unref( mask );
283 delete [] bits;
284 delete [] maskBits;
285 }
286
287 #endif // wxUSE_IMAGE
288
289 wxCursor::~wxCursor()
290 {
291 }
292
293 wxCursor& wxCursor::operator = ( const wxCursor& cursor )
294 {
295 if (*this == cursor)
296 return (*this);
297
298 Ref( cursor );
299
300 return *this;
301 }
302
303 bool wxCursor::operator == ( const wxCursor& cursor ) const
304 {
305 return m_refData == cursor.m_refData;
306 }
307
308 bool wxCursor::operator != ( const wxCursor& cursor ) const
309 {
310 return m_refData != cursor.m_refData;
311 }
312
313 bool wxCursor::Ok() const
314 {
315 return (m_refData != NULL);
316 }
317
318 GdkCursor *wxCursor::GetCursor() const
319 {
320 return M_CURSORDATA->m_cursor;
321 }
322
323 //-----------------------------------------------------------------------------
324 // busy cursor routines
325 //-----------------------------------------------------------------------------
326
327 extern wxCursor g_globalCursor;
328
329 static wxCursor gs_savedCursor;
330 static int gs_busyCount = 0;
331
332 const wxCursor &wxBusyCursor::GetStoredCursor()
333 {
334 return gs_savedCursor;
335 }
336
337 const wxCursor wxBusyCursor::GetBusyCursor()
338 {
339 return wxCursor(wxCURSOR_WATCH);
340 }
341
342 void wxEndBusyCursor()
343 {
344 if (--gs_busyCount > 0)
345 return;
346
347 wxSetCursor( gs_savedCursor );
348 gs_savedCursor = wxNullCursor;
349
350 if (wxTheApp)
351 wxTheApp->SendIdleEvents();
352 }
353
354 void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
355 {
356 if (gs_busyCount++ > 0)
357 return;
358
359 wxASSERT_MSG( !gs_savedCursor.Ok(),
360 wxT("forgot to call wxEndBusyCursor, will leak memory") );
361
362 gs_savedCursor = g_globalCursor;
363
364 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
365
366 if (wxTheApp)
367 wxTheApp->SendIdleEvents();
368
369 gdk_flush();
370 }
371
372 bool wxIsBusy()
373 {
374 return gs_busyCount > 0;
375 }
376
377 void wxSetCursor( const wxCursor& cursor )
378 {
379 if (g_isIdle)
380 wxapp_install_idle_handler();
381
382 g_globalCursor = cursor;
383 }