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