]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/gtk/cursor.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
c801d85f | 13 | #include "wx/cursor.h" |
670f9935 WS |
14 | |
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/app.h" | |
de6185e2 | 17 | #include "wx/utils.h" |
0416c418 | 18 | #include "wx/image.h" |
5ff14574 | 19 | #include "wx/colour.h" |
670f9935 WS |
20 | #endif // WX_PRECOMP |
21 | ||
a1abca32 | 22 | #include <gtk/gtk.h> |
84833214 | 23 | |
c801d85f KB |
24 | //----------------------------------------------------------------------------- |
25 | // wxCursor | |
26 | //----------------------------------------------------------------------------- | |
27 | ||
8f884a0d | 28 | class wxCursorRefData: public wxGDIRefData |
c801d85f | 29 | { |
8f884a0d | 30 | public: |
8bbe427f | 31 | wxCursorRefData(); |
d3c7fc99 | 32 | virtual ~wxCursorRefData(); |
8bbe427f | 33 | |
8f884a0d VZ |
34 | virtual bool IsOk() const { return m_cursor != NULL; } |
35 | ||
c801d85f KB |
36 | GdkCursor *m_cursor; |
37 | }; | |
38 | ||
8bbe427f | 39 | wxCursorRefData::wxCursorRefData() |
c801d85f | 40 | { |
2d17d68f | 41 | m_cursor = (GdkCursor *) NULL; |
ff7b1510 | 42 | } |
c801d85f | 43 | |
8bbe427f | 44 | wxCursorRefData::~wxCursorRefData() |
c801d85f | 45 | { |
bc8b8220 | 46 | if (m_cursor) gdk_cursor_unref( m_cursor ); |
ff7b1510 | 47 | } |
c801d85f KB |
48 | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) | |
52 | ||
53 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) | |
54 | ||
8bbe427f | 55 | wxCursor::wxCursor() |
c801d85f | 56 | { |
7eff657c | 57 | |
ff7b1510 | 58 | } |
c801d85f | 59 | |
debe6624 | 60 | wxCursor::wxCursor( int cursorId ) |
c801d85f | 61 | { |
2d17d68f RR |
62 | m_refData = new wxCursorRefData(); |
63 | ||
64 | GdkCursorType gdk_cur = GDK_LEFT_PTR; | |
65 | switch (cursorId) | |
66 | { | |
76471ff7 VZ |
67 | case wxCURSOR_BLANK: |
68 | { | |
69 | static const gchar bits[] = { 0 }; | |
defdd888 | 70 | static /* const -- not in GTK1 */ GdkColor color = { 0, 0, 0, 0 }; |
76471ff7 VZ |
71 | |
72 | GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1); | |
73 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap, | |
74 | pixmap, | |
75 | &color, | |
76 | &color, | |
77 | 0, 0); | |
78 | } | |
79 | return; | |
80 | ||
15dadf31 | 81 | case wxCURSOR_ARROW: // fall through to default |
f7bdcdd7 | 82 | case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break; |
15dadf31 | 83 | case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break; |
2d17d68f RR |
84 | case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break; |
85 | case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break; | |
86 | case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break; | |
87 | case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break; | |
83f96286 | 88 | case wxCURSOR_ARROWWAIT: |
7c39369e | 89 | case wxCURSOR_WAIT: |
2d17d68f | 90 | case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break; |
57b10cb4 | 91 | case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break; |
2d17d68f RR |
92 | case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break; |
93 | case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break; | |
94 | case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break; | |
95 | case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break; | |
7c39369e | 96 | case wxCURSOR_SIZENWSE: |
2d17d68f RR |
97 | case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break; |
98 | case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break; | |
7c39369e VZ |
99 | case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break; |
100 | case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break; | |
101 | case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break; | |
2d17d68f RR |
102 | case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break; |
103 | case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break; | |
104 | case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break; | |
13971833 RD |
105 | case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break; |
106 | ||
957d856c VZ |
107 | case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break; |
108 | case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break; | |
c801d85f | 109 | /* |
7c39369e VZ |
110 | case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break; |
111 | case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break; | |
2d17d68f RR |
112 | case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break; |
113 | case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break; | |
c801d85f | 114 | */ |
76471ff7 | 115 | |
7c39369e | 116 | default: |
223d09f6 | 117 | wxFAIL_MSG(wxT("unsupported cursor type")); |
7c39369e | 118 | // will use the standard one |
7c39369e | 119 | break; |
2d17d68f | 120 | } |
8bbe427f | 121 | |
2d17d68f | 122 | M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur ); |
ff7b1510 | 123 | } |
c801d85f | 124 | |
c2fa61e8 | 125 | extern GtkWidget *wxGetRootWindow(); |
a1db952f VS |
126 | |
127 | wxCursor::wxCursor(const char bits[], int width, int height, | |
128 | int hotSpotX, int hotSpotY, | |
f516d986 | 129 | const char maskBits[], const wxColour *fg, const wxColour *bg) |
a1db952f VS |
130 | { |
131 | if (!maskBits) | |
132 | maskBits = bits; | |
133 | if (!fg) | |
134 | fg = wxBLACK; | |
135 | if (!bg) | |
136 | bg = wxWHITE; | |
137 | if (hotSpotX < 0 || hotSpotX >= width) | |
138 | hotSpotX = 0; | |
139 | if (hotSpotY < 0 || hotSpotY >= height) | |
140 | hotSpotY = 0; | |
141 | ||
c2fa61e8 RD |
142 | GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height ); |
143 | GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height); | |
a1db952f VS |
144 | |
145 | m_refData = new wxCursorRefData; | |
146 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap( | |
4f558e23 | 147 | data, mask, fg->GetColor(), bg->GetColor(), |
a1db952f VS |
148 | hotSpotX, hotSpotY ); |
149 | ||
3fe39b0c MR |
150 | g_object_unref (data); |
151 | g_object_unref (mask); | |
a1db952f VS |
152 | } |
153 | ||
7eff657c | 154 | #if wxUSE_IMAGE |
0fc5dbf5 | 155 | |
9f556d1d VZ |
156 | static void GetHotSpot(const wxImage& image, int& x, int& y) |
157 | { | |
158 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X)) | |
159 | x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); | |
160 | else | |
161 | x = 0; | |
162 | ||
163 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y)) | |
164 | y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
165 | else | |
166 | y = 0; | |
167 | ||
168 | if (x < 0 || x >= image.GetWidth()) | |
169 | x = 0; | |
170 | if (y < 0 || y >= image.GetHeight()) | |
171 | y = 0; | |
172 | } | |
173 | ||
7eff657c JS |
174 | wxCursor::wxCursor( const wxImage & image ) |
175 | { | |
176 | unsigned char * rgbBits = image.GetData(); | |
0fc5dbf5 VZ |
177 | int w = image.GetWidth() ; |
178 | int h = image.GetHeight(); | |
179 | bool bHasMask = image.HasMask(); | |
7eff657c JS |
180 | int imagebitcount = (w*h)/8; |
181 | ||
ff654490 | 182 | if ( gdk_display_supports_cursor_color(gdk_display_get_default()) ) |
9f556d1d | 183 | { |
ff654490 VZ |
184 | unsigned char rMask = 0, |
185 | gMask = 0, | |
186 | bMask = 0; | |
187 | if (bHasMask) | |
9f556d1d | 188 | { |
ff654490 VZ |
189 | rMask = image.GetMaskRed(); |
190 | gMask = image.GetMaskGreen(); | |
191 | bMask = image.GetMaskBlue(); | |
192 | } | |
9f556d1d | 193 | |
ff654490 VZ |
194 | GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, w, h); |
195 | unsigned char *alpha = image.HasAlpha() ? image.GetAlpha() : NULL; | |
196 | unsigned char *out = gdk_pixbuf_get_pixels(pixbuf); | |
197 | int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w; | |
198 | for ( int y = 0; y < h; y++, out += rowpad ) | |
199 | { | |
200 | for ( int x = 0; x < w; x++, out += 4, rgbBits += 3 ) | |
9f556d1d | 201 | { |
ff654490 VZ |
202 | out[0] = rgbBits[0]; |
203 | out[1] = rgbBits[1]; | |
204 | out[2] = rgbBits[2]; | |
205 | if (bHasMask && | |
206 | out[0] == rMask && out[1] == gMask && out[2] == bMask) | |
207 | out[3] = 0; | |
208 | else | |
209 | out[3] = alpha ? *alpha : 255; | |
210 | if ( alpha ) | |
211 | ++alpha; | |
9f556d1d | 212 | } |
cb738b8b | 213 | } |
ff654490 VZ |
214 | |
215 | int hotSpotX, hotSpotY; | |
216 | GetHotSpot(image, hotSpotX, hotSpotY); | |
217 | ||
218 | m_refData = new wxCursorRefData; | |
219 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf | |
220 | ( | |
221 | gdk_display_get_default(), | |
222 | pixbuf, | |
223 | hotSpotX, hotSpotY | |
224 | ); | |
225 | g_object_unref (pixbuf); | |
226 | return; | |
9f556d1d | 227 | } |
9f556d1d | 228 | |
7eff657c JS |
229 | unsigned char * bits = new unsigned char [imagebitcount]; |
230 | unsigned char * maskBits = new unsigned char [imagebitcount]; | |
231 | ||
0fc5dbf5 | 232 | int i, j, i8; unsigned char c, cMask; |
7eff657c | 233 | for (i=0; i<imagebitcount; i++) |
0fc5dbf5 | 234 | { |
7eff657c JS |
235 | bits[i] = 0; |
236 | i8 = i * 8; | |
237 | ||
238 | cMask = 1; | |
239 | for (j=0; j<8; j++) | |
0fc5dbf5 VZ |
240 | { |
241 | // possible overflow if we do the summation first ? | |
242 | c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3; | |
243 | //if average value is > mid grey | |
244 | if (c>127) | |
245 | bits[i] = bits[i] | cMask; | |
246 | cMask = cMask * 2; | |
7eff657c | 247 | } |
0fc5dbf5 VZ |
248 | } |
249 | ||
250 | unsigned long keyMaskColor; | |
7eff657c | 251 | if (bHasMask) |
0fc5dbf5 VZ |
252 | { |
253 | unsigned char | |
254 | r = image.GetMaskRed(), | |
255 | g = image.GetMaskGreen(), | |
256 | b = image.GetMaskBlue(); | |
7eff657c JS |
257 | |
258 | for (i=0; i<imagebitcount; i++) | |
259 | { | |
0fc5dbf5 VZ |
260 | maskBits[i] = 0x0; |
261 | i8 = i * 8; | |
7eff657c | 262 | |
0fc5dbf5 VZ |
263 | cMask = 1; |
264 | for (j=0; j<8; j++) | |
265 | { | |
266 | if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b) | |
267 | maskBits[i] = maskBits[i] | cMask; | |
268 | cMask = cMask * 2; | |
269 | } | |
7eff657c | 270 | } |
0fc5dbf5 VZ |
271 | |
272 | keyMaskColor = (r << 16) | (g << 8) | b; | |
273 | } | |
274 | else // no mask | |
275 | { | |
7eff657c | 276 | for (i=0; i<imagebitcount; i++) |
0fc5dbf5 VZ |
277 | maskBits[i] = 0xFF; |
278 | ||
279 | // init it to avoid compiler warnings | |
280 | keyMaskColor = 0; | |
281 | } | |
282 | ||
fa1fcc66 | 283 | // find the most frequent color(s) |
952ae1e8 | 284 | wxImageHistogram histogram; |
fa1fcc66 | 285 | image.ComputeHistogram(histogram); |
7eff657c | 286 | |
0fc5dbf5 | 287 | // colors as rrggbb |
7eff657c | 288 | unsigned long key; |
952ae1e8 | 289 | unsigned long value; |
7eff657c | 290 | |
fa1fcc66 | 291 | long colMostFreq = 0; |
952ae1e8 | 292 | unsigned long nMost = 0; |
fa1fcc66 | 293 | long colNextMostFreq = 0; |
952ae1e8 | 294 | unsigned long nNext = 0; |
fa1fcc66 VZ |
295 | for ( wxImageHistogram::iterator entry = histogram.begin(); |
296 | entry != histogram.end(); | |
297 | ++entry ) | |
7eff657c | 298 | { |
952ae1e8 VS |
299 | value = entry->second.value; |
300 | key = entry->first; | |
301 | if ( !bHasMask || (key != keyMaskColor) ) | |
7eff657c | 302 | { |
952ae1e8 | 303 | if (value > nMost) |
7eff657c | 304 | { |
952ae1e8 | 305 | nMost = value; |
fa1fcc66 | 306 | colMostFreq = key; |
7eff657c | 307 | } |
952ae1e8 | 308 | else if (value > nNext) |
7eff657c | 309 | { |
952ae1e8 | 310 | nNext = value; |
fa1fcc66 | 311 | colNextMostFreq = key; |
7eff657c JS |
312 | } |
313 | } | |
314 | } | |
315 | ||
fa1fcc66 VZ |
316 | wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16), |
317 | (unsigned char)(colMostFreq >> 8), | |
318 | (unsigned char)(colMostFreq) ); | |
7eff657c | 319 | |
fa1fcc66 VZ |
320 | wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16), |
321 | (unsigned char)(colNextMostFreq >> 8), | |
322 | (unsigned char)(colNextMostFreq) ); | |
7eff657c | 323 | |
624d506b CE |
324 | int fg_intensity = fg.Red() + fg.Green() + fg.Blue(); |
325 | int bg_intensity = bg.Red() + bg.Green() + bg.Blue(); | |
86e69fa5 MR |
326 | |
327 | if (bg_intensity > fg_intensity) | |
328 | { | |
624d506b CE |
329 | //swap fg and bg |
330 | wxColour tmp = fg; | |
331 | fg = bg; | |
332 | bg = tmp; | |
86e69fa5 | 333 | } |
624d506b | 334 | |
9f556d1d VZ |
335 | int hotSpotX, hotSpotY; |
336 | GetHotSpot(image, hotSpotX, hotSpotY); | |
7eff657c | 337 | |
fa1fcc66 VZ |
338 | GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window, |
339 | (gchar *) bits, w, h); | |
340 | GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window, | |
341 | (gchar *) maskBits, w, h); | |
7eff657c JS |
342 | |
343 | m_refData = new wxCursorRefData; | |
fa1fcc66 VZ |
344 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap |
345 | ( | |
346 | data, | |
347 | mask, | |
348 | fg.GetColor(), bg.GetColor(), | |
349 | hotSpotX, hotSpotY | |
350 | ); | |
7eff657c | 351 | |
3fe39b0c MR |
352 | g_object_unref (data); |
353 | g_object_unref (mask); | |
0fc5dbf5 | 354 | delete [] bits; |
7eff657c | 355 | delete [] maskBits; |
7eff657c | 356 | } |
0fc5dbf5 VZ |
357 | |
358 | #endif // wxUSE_IMAGE | |
7eff657c | 359 | |
8bbe427f | 360 | wxCursor::~wxCursor() |
c801d85f | 361 | { |
ff7b1510 | 362 | } |
c801d85f | 363 | |
8f884a0d | 364 | GdkCursor *wxCursor::GetCursor() const |
c801d85f | 365 | { |
8f884a0d | 366 | return M_CURSORDATA->m_cursor; |
ff7b1510 | 367 | } |
c801d85f | 368 | |
8f884a0d | 369 | wxGDIRefData *wxCursor::CreateGDIRefData() const |
c801d85f | 370 | { |
8f884a0d VZ |
371 | return new wxCursorRefData; |
372 | } | |
373 | ||
374 | wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const | |
375 | { | |
376 | return new wxCursorRefData(*wx_static_cast(const wxCursorRefData *, data)); | |
ff7b1510 | 377 | } |
c801d85f KB |
378 | |
379 | //----------------------------------------------------------------------------- | |
380 | // busy cursor routines | |
381 | //----------------------------------------------------------------------------- | |
382 | ||
b541538f PC |
383 | /* Current cursor, in order to hang on to |
384 | * cursor handle when setting the cursor globally */ | |
385 | wxCursor g_globalCursor; | |
7c39369e | 386 | |
238d735d | 387 | static wxCursor gs_savedCursor; |
89a43902 | 388 | static int gs_busyCount = 0; |
c801d85f | 389 | |
f6bcfd97 BP |
390 | const wxCursor &wxBusyCursor::GetStoredCursor() |
391 | { | |
392 | return gs_savedCursor; | |
393 | } | |
394 | ||
395 | const wxCursor wxBusyCursor::GetBusyCursor() | |
396 | { | |
397 | return wxCursor(wxCURSOR_WATCH); | |
398 | } | |
399 | ||
8bbe427f | 400 | void wxEndBusyCursor() |
c801d85f | 401 | { |
238d735d | 402 | if (--gs_busyCount > 0) |
89a43902 VZ |
403 | return; |
404 | ||
238d735d RR |
405 | wxSetCursor( gs_savedCursor ); |
406 | gs_savedCursor = wxNullCursor; | |
13971833 | 407 | |
d1dea842 | 408 | if (wxTheApp) |
8b532f8c | 409 | wxTheApp->ProcessIdle(); |
ff7b1510 | 410 | } |
c801d85f | 411 | |
f516d986 | 412 | void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) ) |
c801d85f | 413 | { |
238d735d | 414 | if (gs_busyCount++ > 0) |
89a43902 VZ |
415 | return; |
416 | ||
238d735d | 417 | wxASSERT_MSG( !gs_savedCursor.Ok(), |
223d09f6 | 418 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); |
7c39369e | 419 | |
238d735d | 420 | gs_savedCursor = g_globalCursor; |
13971833 | 421 | |
238d735d | 422 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); |
13971833 | 423 | |
d1dea842 | 424 | if (wxTheApp) |
3352cfff | 425 | wxTheApp->ProcessIdle(); |
15dadf31 | 426 | |
7e410431 | 427 | gdk_flush(); |
ff7b1510 | 428 | } |
c801d85f | 429 | |
8bbe427f | 430 | bool wxIsBusy() |
c801d85f | 431 | { |
89a43902 | 432 | return gs_busyCount > 0; |
ff7b1510 | 433 | } |
c801d85f KB |
434 | |
435 | void wxSetCursor( const wxCursor& cursor ) | |
436 | { | |
238d735d | 437 | g_globalCursor = cursor; |
a1abca32 | 438 | wxTheApp->WakeUpIdle(); |
ff7b1510 | 439 | } |