]>
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" |
670f9935 WS |
18 | #endif // WX_PRECOMP |
19 | ||
84833214 VZ |
20 | #include "wx/gtk/private.h" //for idle stuff |
21 | ||
071a2d78 | 22 | #include <gdk/gdk.h> |
a1db952f | 23 | #include <gtk/gtk.h> |
83624f79 | 24 | |
c801d85f KB |
25 | //----------------------------------------------------------------------------- |
26 | // wxCursor | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | class wxCursorRefData: public wxObjectRefData | |
30 | { | |
31 | public: | |
8bbe427f VZ |
32 | |
33 | wxCursorRefData(); | |
34 | ~wxCursorRefData(); | |
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 RR |
90 | case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break; |
91 | case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break; | |
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 | |
7eff657c JS |
156 | wxCursor::wxCursor( const wxImage & image ) |
157 | { | |
158 | unsigned char * rgbBits = image.GetData(); | |
0fc5dbf5 VZ |
159 | int w = image.GetWidth() ; |
160 | int h = image.GetHeight(); | |
161 | bool bHasMask = image.HasMask(); | |
7eff657c JS |
162 | int imagebitcount = (w*h)/8; |
163 | ||
7eff657c JS |
164 | unsigned char * bits = new unsigned char [imagebitcount]; |
165 | unsigned char * maskBits = new unsigned char [imagebitcount]; | |
166 | ||
0fc5dbf5 | 167 | int i, j, i8; unsigned char c, cMask; |
7eff657c | 168 | for (i=0; i<imagebitcount; i++) |
0fc5dbf5 | 169 | { |
7eff657c JS |
170 | bits[i] = 0; |
171 | i8 = i * 8; | |
172 | ||
173 | cMask = 1; | |
174 | for (j=0; j<8; j++) | |
0fc5dbf5 VZ |
175 | { |
176 | // possible overflow if we do the summation first ? | |
177 | c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3; | |
178 | //if average value is > mid grey | |
179 | if (c>127) | |
180 | bits[i] = bits[i] | cMask; | |
181 | cMask = cMask * 2; | |
7eff657c | 182 | } |
0fc5dbf5 VZ |
183 | } |
184 | ||
185 | unsigned long keyMaskColor; | |
7eff657c | 186 | if (bHasMask) |
0fc5dbf5 VZ |
187 | { |
188 | unsigned char | |
189 | r = image.GetMaskRed(), | |
190 | g = image.GetMaskGreen(), | |
191 | b = image.GetMaskBlue(); | |
7eff657c JS |
192 | |
193 | for (i=0; i<imagebitcount; i++) | |
194 | { | |
0fc5dbf5 VZ |
195 | maskBits[i] = 0x0; |
196 | i8 = i * 8; | |
7eff657c | 197 | |
0fc5dbf5 VZ |
198 | cMask = 1; |
199 | for (j=0; j<8; j++) | |
200 | { | |
201 | if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b) | |
202 | maskBits[i] = maskBits[i] | cMask; | |
203 | cMask = cMask * 2; | |
204 | } | |
7eff657c | 205 | } |
0fc5dbf5 VZ |
206 | |
207 | keyMaskColor = (r << 16) | (g << 8) | b; | |
208 | } | |
209 | else // no mask | |
210 | { | |
7eff657c | 211 | for (i=0; i<imagebitcount; i++) |
0fc5dbf5 VZ |
212 | maskBits[i] = 0xFF; |
213 | ||
214 | // init it to avoid compiler warnings | |
215 | keyMaskColor = 0; | |
216 | } | |
217 | ||
fa1fcc66 | 218 | // find the most frequent color(s) |
952ae1e8 | 219 | wxImageHistogram histogram; |
fa1fcc66 | 220 | image.ComputeHistogram(histogram); |
7eff657c | 221 | |
0fc5dbf5 | 222 | // colors as rrggbb |
7eff657c | 223 | unsigned long key; |
952ae1e8 | 224 | unsigned long value; |
7eff657c | 225 | |
fa1fcc66 | 226 | long colMostFreq = 0; |
952ae1e8 | 227 | unsigned long nMost = 0; |
fa1fcc66 | 228 | long colNextMostFreq = 0; |
952ae1e8 | 229 | unsigned long nNext = 0; |
fa1fcc66 VZ |
230 | for ( wxImageHistogram::iterator entry = histogram.begin(); |
231 | entry != histogram.end(); | |
232 | ++entry ) | |
7eff657c | 233 | { |
952ae1e8 VS |
234 | value = entry->second.value; |
235 | key = entry->first; | |
236 | if ( !bHasMask || (key != keyMaskColor) ) | |
7eff657c | 237 | { |
952ae1e8 | 238 | if (value > nMost) |
7eff657c | 239 | { |
952ae1e8 | 240 | nMost = value; |
fa1fcc66 | 241 | colMostFreq = key; |
7eff657c | 242 | } |
952ae1e8 | 243 | else if (value > nNext) |
7eff657c | 244 | { |
952ae1e8 | 245 | nNext = value; |
fa1fcc66 | 246 | colNextMostFreq = key; |
7eff657c JS |
247 | } |
248 | } | |
249 | } | |
250 | ||
fa1fcc66 VZ |
251 | wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16), |
252 | (unsigned char)(colMostFreq >> 8), | |
253 | (unsigned char)(colMostFreq) ); | |
7eff657c | 254 | |
fa1fcc66 VZ |
255 | wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16), |
256 | (unsigned char)(colNextMostFreq >> 8), | |
257 | (unsigned char)(colNextMostFreq) ); | |
7eff657c | 258 | |
624d506b CE |
259 | int fg_intensity = fg.Red() + fg.Green() + fg.Blue(); |
260 | int bg_intensity = bg.Red() + bg.Green() + bg.Blue(); | |
86e69fa5 MR |
261 | |
262 | if (bg_intensity > fg_intensity) | |
263 | { | |
624d506b CE |
264 | //swap fg and bg |
265 | wxColour tmp = fg; | |
266 | fg = bg; | |
267 | bg = tmp; | |
86e69fa5 | 268 | } |
624d506b | 269 | |
fa1fcc66 VZ |
270 | int hotSpotX; |
271 | int hotSpotY; | |
7eff657c | 272 | |
671d2130 MB |
273 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X)) |
274 | hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); | |
fa1fcc66 VZ |
275 | else |
276 | hotSpotX = 0; | |
277 | ||
671d2130 MB |
278 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y)) |
279 | hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
fa1fcc66 VZ |
280 | else |
281 | hotSpotY = 0; | |
15dadf31 | 282 | |
7eff657c | 283 | if (hotSpotX < 0 || hotSpotX >= w) |
952ae1e8 | 284 | hotSpotX = 0; |
7eff657c | 285 | if (hotSpotY < 0 || hotSpotY >= h) |
952ae1e8 | 286 | hotSpotY = 0; |
7eff657c | 287 | |
fa1fcc66 VZ |
288 | GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window, |
289 | (gchar *) bits, w, h); | |
290 | GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window, | |
291 | (gchar *) maskBits, w, h); | |
7eff657c JS |
292 | |
293 | m_refData = new wxCursorRefData; | |
fa1fcc66 VZ |
294 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap |
295 | ( | |
296 | data, | |
297 | mask, | |
298 | fg.GetColor(), bg.GetColor(), | |
299 | hotSpotX, hotSpotY | |
300 | ); | |
7eff657c | 301 | |
3fe39b0c MR |
302 | g_object_unref (data); |
303 | g_object_unref (mask); | |
0fc5dbf5 | 304 | delete [] bits; |
7eff657c | 305 | delete [] maskBits; |
7eff657c | 306 | } |
0fc5dbf5 VZ |
307 | |
308 | #endif // wxUSE_IMAGE | |
7eff657c | 309 | |
8bbe427f | 310 | wxCursor::~wxCursor() |
c801d85f | 311 | { |
ff7b1510 | 312 | } |
c801d85f | 313 | |
8bbe427f | 314 | bool wxCursor::operator == ( const wxCursor& cursor ) const |
c801d85f | 315 | { |
2d17d68f | 316 | return m_refData == cursor.m_refData; |
ff7b1510 | 317 | } |
c801d85f | 318 | |
8bbe427f | 319 | bool wxCursor::operator != ( const wxCursor& cursor ) const |
c801d85f | 320 | { |
2d17d68f | 321 | return m_refData != cursor.m_refData; |
ff7b1510 | 322 | } |
c801d85f | 323 | |
8bbe427f | 324 | bool wxCursor::Ok() const |
c801d85f | 325 | { |
2d17d68f | 326 | return (m_refData != NULL); |
ff7b1510 | 327 | } |
c801d85f | 328 | |
8bbe427f | 329 | GdkCursor *wxCursor::GetCursor() const |
c801d85f | 330 | { |
2d17d68f | 331 | return M_CURSORDATA->m_cursor; |
ff7b1510 | 332 | } |
c801d85f KB |
333 | |
334 | //----------------------------------------------------------------------------- | |
335 | // busy cursor routines | |
336 | //----------------------------------------------------------------------------- | |
337 | ||
238d735d | 338 | extern wxCursor g_globalCursor; |
7c39369e | 339 | |
238d735d | 340 | static wxCursor gs_savedCursor; |
89a43902 | 341 | static int gs_busyCount = 0; |
c801d85f | 342 | |
f6bcfd97 BP |
343 | const wxCursor &wxBusyCursor::GetStoredCursor() |
344 | { | |
345 | return gs_savedCursor; | |
346 | } | |
347 | ||
348 | const wxCursor wxBusyCursor::GetBusyCursor() | |
349 | { | |
350 | return wxCursor(wxCURSOR_WATCH); | |
351 | } | |
352 | ||
8bbe427f | 353 | void wxEndBusyCursor() |
c801d85f | 354 | { |
238d735d | 355 | if (--gs_busyCount > 0) |
89a43902 VZ |
356 | return; |
357 | ||
238d735d RR |
358 | wxSetCursor( gs_savedCursor ); |
359 | gs_savedCursor = wxNullCursor; | |
13971833 | 360 | |
d1dea842 | 361 | if (wxTheApp) |
8b532f8c | 362 | wxTheApp->ProcessIdle(); |
ff7b1510 | 363 | } |
c801d85f | 364 | |
f516d986 | 365 | void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) ) |
c801d85f | 366 | { |
238d735d | 367 | if (gs_busyCount++ > 0) |
89a43902 VZ |
368 | return; |
369 | ||
238d735d | 370 | wxASSERT_MSG( !gs_savedCursor.Ok(), |
223d09f6 | 371 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); |
7c39369e | 372 | |
238d735d | 373 | gs_savedCursor = g_globalCursor; |
13971833 | 374 | |
238d735d | 375 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); |
13971833 | 376 | |
d1dea842 | 377 | if (wxTheApp) |
3352cfff | 378 | wxTheApp->ProcessIdle(); |
15dadf31 | 379 | |
7e410431 | 380 | gdk_flush(); |
ff7b1510 | 381 | } |
c801d85f | 382 | |
8bbe427f | 383 | bool wxIsBusy() |
c801d85f | 384 | { |
89a43902 | 385 | return gs_busyCount > 0; |
ff7b1510 | 386 | } |
c801d85f KB |
387 | |
388 | void wxSetCursor( const wxCursor& cursor ) | |
389 | { | |
13971833 | 390 | if (g_isIdle) |
238d735d RR |
391 | wxapp_install_idle_handler(); |
392 | ||
393 | g_globalCursor = cursor; | |
ff7b1510 | 394 | } |