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