]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/cursor.cpp
missing reversed orientation assignments
[wxWidgets.git] / src / gtk / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 #include "wx/image.h"
19 #include "wx/colour.h"
20 #endif // WX_PRECOMP
21
22 #include "wx/gtk/private.h" //for idle stuff
23
24 //-----------------------------------------------------------------------------
25 // wxCursor
26 //-----------------------------------------------------------------------------
27
28 class wxCursorRefData: public wxObjectRefData
29 {
30 public:
31
32 wxCursorRefData();
33 virtual ~wxCursorRefData();
34
35 GdkCursor *m_cursor;
36 };
37
38 wxCursorRefData::wxCursorRefData()
39 {
40 m_cursor = (GdkCursor *) NULL;
41 }
42
43 wxCursorRefData::~wxCursorRefData()
44 {
45 if (m_cursor) gdk_cursor_unref( m_cursor );
46 }
47
48 //-----------------------------------------------------------------------------
49
50 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
51
52 IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
53
54 wxCursor::wxCursor()
55 {
56
57 }
58
59 wxCursor::wxCursor( int cursorId )
60 {
61 m_refData = new wxCursorRefData();
62
63 GdkCursorType gdk_cur = GDK_LEFT_PTR;
64 switch (cursorId)
65 {
66 case wxCURSOR_BLANK:
67 {
68 static const gchar bits[] = { 0 };
69 static /* const -- not in GTK1 */ GdkColor color = { 0, 0, 0, 0 };
70
71 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
72 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
73 pixmap,
74 &color,
75 &color,
76 0, 0);
77 }
78 return;
79
80 case wxCURSOR_ARROW: // fall through to default
81 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
82 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
83 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
84 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
85 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
86 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
87 case wxCURSOR_ARROWWAIT:
88 case wxCURSOR_WAIT:
89 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
90 case wxCURSOR_SIZING: gdk_cur = GDK_FLEUR; break;
91 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
92 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
93 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
94 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
95 case wxCURSOR_SIZENWSE:
96 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
97 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
98 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
99 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
100 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
101 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
102 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
103 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
104 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
105
106 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
107 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
108 /*
109 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
110 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
111 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
112 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
113 */
114
115 default:
116 wxFAIL_MSG(wxT("unsupported cursor type"));
117 // will use the standard one
118 break;
119 }
120
121 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
122 }
123
124 extern GtkWidget *wxGetRootWindow();
125
126 wxCursor::wxCursor(const char bits[], int width, int height,
127 int hotSpotX, int hotSpotY,
128 const char maskBits[], const wxColour *fg, const wxColour *bg)
129 {
130 if (!maskBits)
131 maskBits = bits;
132 if (!fg)
133 fg = wxBLACK;
134 if (!bg)
135 bg = wxWHITE;
136 if (hotSpotX < 0 || hotSpotX >= width)
137 hotSpotX = 0;
138 if (hotSpotY < 0 || hotSpotY >= height)
139 hotSpotY = 0;
140
141 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
142 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
143
144 m_refData = new wxCursorRefData;
145 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
146 data, mask, fg->GetColor(), bg->GetColor(),
147 hotSpotX, hotSpotY );
148
149 g_object_unref (data);
150 g_object_unref (mask);
151 }
152
153 #if wxUSE_IMAGE
154
155 wxCursor::wxCursor( const wxImage & image )
156 {
157 unsigned char * rgbBits = image.GetData();
158 int w = image.GetWidth() ;
159 int h = image.GetHeight();
160 bool bHasMask = image.HasMask();
161 int imagebitcount = (w*h)/8;
162
163 unsigned char * bits = new unsigned char [imagebitcount];
164 unsigned char * maskBits = new unsigned char [imagebitcount];
165
166 int i, j, i8; unsigned char c, cMask;
167 for (i=0; i<imagebitcount; i++)
168 {
169 bits[i] = 0;
170 i8 = i * 8;
171
172 cMask = 1;
173 for (j=0; j<8; j++)
174 {
175 // possible overflow if we do the summation first ?
176 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3;
177 //if average value is > mid grey
178 if (c>127)
179 bits[i] = bits[i] | cMask;
180 cMask = cMask * 2;
181 }
182 }
183
184 unsigned long keyMaskColor;
185 if (bHasMask)
186 {
187 unsigned char
188 r = image.GetMaskRed(),
189 g = image.GetMaskGreen(),
190 b = image.GetMaskBlue();
191
192 for (i=0; i<imagebitcount; i++)
193 {
194 maskBits[i] = 0x0;
195 i8 = i * 8;
196
197 cMask = 1;
198 for (j=0; j<8; j++)
199 {
200 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
201 maskBits[i] = maskBits[i] | cMask;
202 cMask = cMask * 2;
203 }
204 }
205
206 keyMaskColor = (r << 16) | (g << 8) | b;
207 }
208 else // no mask
209 {
210 for (i=0; i<imagebitcount; i++)
211 maskBits[i] = 0xFF;
212
213 // init it to avoid compiler warnings
214 keyMaskColor = 0;
215 }
216
217 // find the most frequent color(s)
218 wxImageHistogram histogram;
219 image.ComputeHistogram(histogram);
220
221 // colors as rrggbb
222 unsigned long key;
223 unsigned long value;
224
225 long colMostFreq = 0;
226 unsigned long nMost = 0;
227 long colNextMostFreq = 0;
228 unsigned long nNext = 0;
229 for ( wxImageHistogram::iterator entry = histogram.begin();
230 entry != histogram.end();
231 ++entry )
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 colMostFreq = key;
241 }
242 else if (value > nNext)
243 {
244 nNext = value;
245 colNextMostFreq = key;
246 }
247 }
248 }
249
250 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
251 (unsigned char)(colMostFreq >> 8),
252 (unsigned char)(colMostFreq) );
253
254 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
255 (unsigned char)(colNextMostFreq >> 8),
256 (unsigned char)(colNextMostFreq) );
257
258 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
259 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
260
261 if (bg_intensity > fg_intensity)
262 {
263 //swap fg and bg
264 wxColour tmp = fg;
265 fg = bg;
266 bg = tmp;
267 }
268
269 int hotSpotX;
270 int hotSpotY;
271
272 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
273 hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
274 else
275 hotSpotX = 0;
276
277 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
278 hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
279 else
280 hotSpotY = 0;
281
282 if (hotSpotX < 0 || hotSpotX >= w)
283 hotSpotX = 0;
284 if (hotSpotY < 0 || hotSpotY >= h)
285 hotSpotY = 0;
286
287 GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
288 (gchar *) bits, w, h);
289 GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window,
290 (gchar *) maskBits, w, h);
291
292 m_refData = new wxCursorRefData;
293 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
294 (
295 data,
296 mask,
297 fg.GetColor(), bg.GetColor(),
298 hotSpotX, hotSpotY
299 );
300
301 g_object_unref (data);
302 g_object_unref (mask);
303 delete [] bits;
304 delete [] maskBits;
305 }
306
307 #endif // wxUSE_IMAGE
308
309 wxCursor::~wxCursor()
310 {
311 }
312
313 bool wxCursor::IsOk() 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->ProcessIdle();
352 }
353
354 void wxBeginBusyCursor( const 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->ProcessIdle();
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 }