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