]>
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 |
8bbe427f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "cursor.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/cursor.h" | |
4dcaf11a | 16 | #include "wx/utils.h" |
d1dea842 | 17 | #include "wx/app.h" |
c801d85f | 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 | ||
33 | class wxCursorRefData: public wxObjectRefData | |
34 | { | |
35 | public: | |
8bbe427f VZ |
36 | |
37 | wxCursorRefData(); | |
38 | ~wxCursorRefData(); | |
39 | ||
c801d85f KB |
40 | GdkCursor *m_cursor; |
41 | }; | |
42 | ||
8bbe427f | 43 | wxCursorRefData::wxCursorRefData() |
c801d85f | 44 | { |
2d17d68f | 45 | m_cursor = (GdkCursor *) NULL; |
ff7b1510 | 46 | } |
c801d85f | 47 | |
8bbe427f | 48 | wxCursorRefData::~wxCursorRefData() |
c801d85f | 49 | { |
2d17d68f | 50 | if (m_cursor) gdk_cursor_destroy( m_cursor ); |
ff7b1510 | 51 | } |
c801d85f KB |
52 | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) | |
58 | ||
8bbe427f | 59 | wxCursor::wxCursor() |
c801d85f | 60 | { |
7eff657c | 61 | |
ff7b1510 | 62 | } |
c801d85f | 63 | |
debe6624 | 64 | wxCursor::wxCursor( int cursorId ) |
c801d85f | 65 | { |
2d17d68f RR |
66 | m_refData = new wxCursorRefData(); |
67 | ||
68 | GdkCursorType gdk_cur = GDK_LEFT_PTR; | |
69 | switch (cursorId) | |
70 | { | |
f7bdcdd7 | 71 | case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_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 VZ |
105 | // will use the standard one |
106 | ||
107 | case wxCURSOR_ARROW: | |
108 | break; | |
2d17d68f | 109 | } |
8bbe427f | 110 | |
2d17d68f | 111 | M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur ); |
ff7b1510 | 112 | } |
c801d85f | 113 | |
c2fa61e8 | 114 | extern GtkWidget *wxGetRootWindow(); |
a1db952f VS |
115 | |
116 | wxCursor::wxCursor(const char bits[], int width, int height, | |
117 | int hotSpotX, int hotSpotY, | |
118 | const char maskBits[], wxColour *fg, wxColour *bg) | |
119 | { | |
120 | if (!maskBits) | |
121 | maskBits = bits; | |
122 | if (!fg) | |
123 | fg = wxBLACK; | |
124 | if (!bg) | |
125 | bg = wxWHITE; | |
126 | if (hotSpotX < 0 || hotSpotX >= width) | |
127 | hotSpotX = 0; | |
128 | if (hotSpotY < 0 || hotSpotY >= height) | |
129 | hotSpotY = 0; | |
130 | ||
c2fa61e8 RD |
131 | GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height ); |
132 | GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height); | |
a1db952f VS |
133 | |
134 | m_refData = new wxCursorRefData; | |
135 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap( | |
136 | data, mask, fg->GetColor(), bg->GetColor(), | |
137 | hotSpotX, hotSpotY ); | |
138 | ||
139 | gdk_bitmap_unref( data ); | |
140 | gdk_bitmap_unref( mask ); | |
141 | } | |
142 | ||
143 | ||
c801d85f KB |
144 | wxCursor::wxCursor( const wxCursor &cursor ) |
145 | { | |
2d17d68f | 146 | Ref( cursor ); |
ff7b1510 | 147 | } |
c801d85f | 148 | |
7eff657c JS |
149 | #if wxUSE_IMAGE |
150 | wxCursor::wxCursor( const wxImage & image ) | |
151 | { | |
152 | unsigned char * rgbBits = image.GetData(); | |
153 | int w = image.GetWidth() ; | |
154 | int h = image.GetHeight() ; | |
155 | bool bHasMask = image.HasMask() ; | |
156 | int imagebitcount = (w*h)/8; | |
157 | ||
158 | unsigned char r, g, b ; | |
159 | unsigned char * bits = new unsigned char [imagebitcount]; | |
160 | unsigned char * maskBits = new unsigned char [imagebitcount]; | |
161 | ||
162 | int i,j, i8; unsigned char c, cMask; | |
163 | for (i=0; i<imagebitcount; i++) | |
164 | { | |
165 | bits[i] = 0; | |
166 | i8 = i * 8; | |
167 | ||
168 | cMask = 1; | |
169 | for (j=0; j<8; j++) | |
170 | { | |
171 | // possible overflow if we do the summation first ? | |
172 | c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3 ; | |
173 | //if average value is > mid grey | |
174 | if (c>127) | |
175 | bits[i] = bits[i] | cMask ; | |
176 | cMask = cMask * 2 ; | |
177 | } | |
178 | } | |
179 | if (bHasMask) | |
180 | { | |
181 | r = image.GetMaskRed() ; | |
182 | g = image.GetMaskGreen() ; | |
183 | b = image.GetMaskBlue() ; | |
184 | ||
185 | for (i=0; i<imagebitcount; i++) | |
186 | { | |
187 | maskBits[i] = 0x0; | |
188 | i8 = i * 8; | |
189 | ||
190 | cMask = 1; | |
191 | for (j=0; j<8; j++) | |
192 | { | |
193 | if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b) | |
194 | maskBits[i] = maskBits[i] | cMask ; | |
195 | cMask = cMask * 2 ; | |
196 | } | |
197 | } | |
198 | } | |
199 | else | |
200 | { | |
201 | for (i=0; i<imagebitcount; i++) | |
202 | maskBits[i]= 0xFF ; | |
203 | } | |
204 | //find the most frequent color(s) | |
205 | //it seems a waste of effort to copy the image | |
206 | //but otherwise we need to remove the const modifier ?? | |
207 | wxImage tmpImage = image.Copy(); | |
952ae1e8 | 208 | wxImageHistogram histogram; |
7eff657c JS |
209 | |
210 | //colors as rrggbb | |
211 | unsigned long key; | |
952ae1e8 | 212 | unsigned long value; |
7eff657c | 213 | unsigned long keyMaskColor = 0; |
952ae1e8 VS |
214 | if (bHasMask) |
215 | keyMaskColor = (r << 16) | (g << 8) | b; | |
7eff657c | 216 | |
952ae1e8 | 217 | tmpImage.ComputeHistogram(histogram); |
7eff657c | 218 | |
952ae1e8 VS |
219 | long MostFreqCol = 0; |
220 | unsigned long nMost = 0; | |
221 | long NextFreqCol = 0; | |
222 | unsigned long nNext = 0; | |
223 | wxImageHistogram::iterator entry = histogram.begin(); | |
224 | ||
225 | while ( entry != histogram.end() ) | |
7eff657c | 226 | { |
952ae1e8 VS |
227 | value = entry->second.value; |
228 | key = entry->first; | |
229 | if ( !bHasMask || (key != keyMaskColor) ) | |
7eff657c | 230 | { |
952ae1e8 | 231 | if (value > nMost) |
7eff657c | 232 | { |
952ae1e8 VS |
233 | nMost = value; |
234 | MostFreqCol = key; | |
7eff657c | 235 | } |
952ae1e8 | 236 | else if (value > nNext) |
7eff657c | 237 | { |
952ae1e8 VS |
238 | nNext = value; |
239 | NextFreqCol = key; | |
7eff657c JS |
240 | } |
241 | } | |
242 | } | |
243 | ||
7eff657c JS |
244 | wxColour fg = wxColour ( (unsigned char)(MostFreqCol >> 16), |
245 | (unsigned char)(MostFreqCol >> 8), | |
246 | (unsigned char)(MostFreqCol) ) ; | |
247 | ||
248 | wxColour bg = wxColour ( (unsigned char)(NextFreqCol >> 16), | |
249 | (unsigned char)(NextFreqCol >> 8), | |
250 | (unsigned char)(NextFreqCol) ) ; | |
251 | ||
7eff657c JS |
252 | int hotSpotX=0; |
253 | int hotSpotY=0; | |
254 | ||
255 | if (image.HasOption(wxCUR_HOTSPOT_X)) | |
952ae1e8 | 256 | hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X); |
7eff657c | 257 | if (image.HasOption(wxCUR_HOTSPOT_Y)) |
952ae1e8 | 258 | hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y); |
7eff657c JS |
259 | |
260 | if (hotSpotX < 0 || hotSpotX >= w) | |
952ae1e8 | 261 | hotSpotX = 0; |
7eff657c | 262 | if (hotSpotY < 0 || hotSpotY >= h) |
952ae1e8 | 263 | hotSpotY = 0; |
7eff657c JS |
264 | |
265 | GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, | |
266 | w, h ); | |
267 | GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, | |
268 | w, h ); | |
269 | ||
270 | m_refData = new wxCursorRefData; | |
271 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap( | |
272 | data, mask, fg.GetColor(), bg.GetColor(), | |
273 | hotSpotX, hotSpotY ); | |
274 | ||
275 | gdk_bitmap_unref( data ); | |
276 | gdk_bitmap_unref( mask ); | |
277 | delete [] bits ; | |
278 | delete [] maskBits; | |
7eff657c JS |
279 | } |
280 | #endif | |
281 | ||
8bbe427f | 282 | wxCursor::~wxCursor() |
c801d85f | 283 | { |
ff7b1510 | 284 | } |
c801d85f KB |
285 | |
286 | wxCursor& wxCursor::operator = ( const wxCursor& cursor ) | |
287 | { | |
7c39369e VZ |
288 | if (*this == cursor) |
289 | return (*this); | |
290 | ||
2d17d68f | 291 | Ref( cursor ); |
7c39369e | 292 | |
2d17d68f | 293 | return *this; |
ff7b1510 | 294 | } |
c801d85f | 295 | |
8bbe427f | 296 | bool wxCursor::operator == ( const wxCursor& cursor ) const |
c801d85f | 297 | { |
2d17d68f | 298 | return m_refData == cursor.m_refData; |
ff7b1510 | 299 | } |
c801d85f | 300 | |
8bbe427f | 301 | bool wxCursor::operator != ( const wxCursor& cursor ) const |
c801d85f | 302 | { |
2d17d68f | 303 | return m_refData != cursor.m_refData; |
ff7b1510 | 304 | } |
c801d85f | 305 | |
8bbe427f | 306 | bool wxCursor::Ok() const |
c801d85f | 307 | { |
2d17d68f | 308 | return (m_refData != NULL); |
ff7b1510 | 309 | } |
c801d85f | 310 | |
8bbe427f | 311 | GdkCursor *wxCursor::GetCursor() const |
c801d85f | 312 | { |
2d17d68f | 313 | return M_CURSORDATA->m_cursor; |
ff7b1510 | 314 | } |
c801d85f KB |
315 | |
316 | //----------------------------------------------------------------------------- | |
317 | // busy cursor routines | |
318 | //----------------------------------------------------------------------------- | |
319 | ||
238d735d | 320 | extern wxCursor g_globalCursor; |
7c39369e | 321 | |
238d735d | 322 | static wxCursor gs_savedCursor; |
89a43902 | 323 | static int gs_busyCount = 0; |
c801d85f | 324 | |
f6bcfd97 BP |
325 | const wxCursor &wxBusyCursor::GetStoredCursor() |
326 | { | |
327 | return gs_savedCursor; | |
328 | } | |
329 | ||
330 | const wxCursor wxBusyCursor::GetBusyCursor() | |
331 | { | |
332 | return wxCursor(wxCURSOR_WATCH); | |
333 | } | |
334 | ||
8bbe427f | 335 | void wxEndBusyCursor() |
c801d85f | 336 | { |
238d735d | 337 | if (--gs_busyCount > 0) |
89a43902 VZ |
338 | return; |
339 | ||
238d735d RR |
340 | wxSetCursor( gs_savedCursor ); |
341 | gs_savedCursor = wxNullCursor; | |
13971833 | 342 | |
d1dea842 RR |
343 | if (wxTheApp) |
344 | wxTheApp->SendIdleEvents(); | |
ff7b1510 | 345 | } |
c801d85f KB |
346 | |
347 | void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) ) | |
348 | { | |
238d735d | 349 | if (gs_busyCount++ > 0) |
89a43902 VZ |
350 | return; |
351 | ||
238d735d | 352 | wxASSERT_MSG( !gs_savedCursor.Ok(), |
223d09f6 | 353 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); |
7c39369e | 354 | |
238d735d | 355 | gs_savedCursor = g_globalCursor; |
13971833 | 356 | |
238d735d | 357 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); |
13971833 | 358 | |
d1dea842 RR |
359 | if (wxTheApp) |
360 | wxTheApp->SendIdleEvents(); | |
7e410431 RR |
361 | |
362 | gdk_flush(); | |
ff7b1510 | 363 | } |
c801d85f | 364 | |
8bbe427f | 365 | bool wxIsBusy() |
c801d85f | 366 | { |
89a43902 | 367 | return gs_busyCount > 0; |
ff7b1510 | 368 | } |
c801d85f KB |
369 | |
370 | void wxSetCursor( const wxCursor& cursor ) | |
371 | { | |
13971833 | 372 | if (g_isIdle) |
238d735d RR |
373 | wxapp_install_idle_handler(); |
374 | ||
375 | g_globalCursor = cursor; | |
ff7b1510 | 376 | } |