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