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