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