]>
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(); | |
208 | wxHashTable hTable; | |
209 | ||
210 | //colors as rrggbb | |
211 | unsigned long key; | |
212 | unsigned long keyMaskColor = 0; | |
213 | if (bHasMask) keyMaskColor = (r << 16) | (g << 8) | b; | |
214 | ||
215 | tmpImage.ComputeHistogram( hTable ); | |
216 | ||
217 | long MostFreqCol = 0 ; long nMost = 0; | |
218 | long NextFreqCol = 0 ; long nNext = 0; | |
219 | long value ; | |
220 | hTable.BeginFind(); | |
221 | wxNode *node = NULL; | |
222 | while ((node = hTable.Next()) != NULL) | |
223 | { | |
224 | wxHNode *hnode = (wxHNode*) node->GetData(); | |
225 | value = hnode->value; | |
226 | key = node->GetKeyInteger() ; | |
227 | if (!bHasMask || (key != keyMaskColor) ) | |
228 | { | |
229 | if (value > nMost) | |
230 | { | |
231 | nMost = value; | |
232 | MostFreqCol = key; | |
233 | } | |
234 | else | |
235 | if (value > nNext) | |
236 | { | |
237 | nNext = value ; | |
238 | NextFreqCol = key; | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | ||
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 | ||
252 | ||
253 | ||
254 | int hotSpotX=0; | |
255 | int hotSpotY=0; | |
256 | ||
257 | if (image.HasOption(wxCUR_HOTSPOT_X)) | |
258 | hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X); | |
259 | if (image.HasOption(wxCUR_HOTSPOT_Y)) | |
260 | hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y); | |
261 | ||
262 | if (hotSpotX < 0 || hotSpotX >= w) | |
263 | hotSpotX = 0; | |
264 | if (hotSpotY < 0 || hotSpotY >= h) | |
265 | hotSpotY = 0; | |
266 | ||
267 | GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, | |
268 | w, h ); | |
269 | GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, | |
270 | w, h ); | |
271 | ||
272 | m_refData = new wxCursorRefData; | |
273 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap( | |
274 | data, mask, fg.GetColor(), bg.GetColor(), | |
275 | hotSpotX, hotSpotY ); | |
276 | ||
277 | gdk_bitmap_unref( data ); | |
278 | gdk_bitmap_unref( mask ); | |
279 | delete [] bits ; | |
280 | delete [] maskBits; | |
281 | ||
282 | } | |
283 | #endif | |
284 | ||
8bbe427f | 285 | wxCursor::~wxCursor() |
c801d85f | 286 | { |
ff7b1510 | 287 | } |
c801d85f KB |
288 | |
289 | wxCursor& wxCursor::operator = ( const wxCursor& cursor ) | |
290 | { | |
7c39369e VZ |
291 | if (*this == cursor) |
292 | return (*this); | |
293 | ||
2d17d68f | 294 | Ref( cursor ); |
7c39369e | 295 | |
2d17d68f | 296 | return *this; |
ff7b1510 | 297 | } |
c801d85f | 298 | |
8bbe427f | 299 | bool wxCursor::operator == ( const wxCursor& cursor ) const |
c801d85f | 300 | { |
2d17d68f | 301 | return m_refData == cursor.m_refData; |
ff7b1510 | 302 | } |
c801d85f | 303 | |
8bbe427f | 304 | bool wxCursor::operator != ( const wxCursor& cursor ) const |
c801d85f | 305 | { |
2d17d68f | 306 | return m_refData != cursor.m_refData; |
ff7b1510 | 307 | } |
c801d85f | 308 | |
8bbe427f | 309 | bool wxCursor::Ok() const |
c801d85f | 310 | { |
2d17d68f | 311 | return (m_refData != NULL); |
ff7b1510 | 312 | } |
c801d85f | 313 | |
8bbe427f | 314 | GdkCursor *wxCursor::GetCursor() const |
c801d85f | 315 | { |
2d17d68f | 316 | return M_CURSORDATA->m_cursor; |
ff7b1510 | 317 | } |
c801d85f KB |
318 | |
319 | //----------------------------------------------------------------------------- | |
320 | // busy cursor routines | |
321 | //----------------------------------------------------------------------------- | |
322 | ||
238d735d | 323 | extern wxCursor g_globalCursor; |
7c39369e | 324 | |
238d735d | 325 | static wxCursor gs_savedCursor; |
89a43902 | 326 | static int gs_busyCount = 0; |
c801d85f | 327 | |
f6bcfd97 BP |
328 | const wxCursor &wxBusyCursor::GetStoredCursor() |
329 | { | |
330 | return gs_savedCursor; | |
331 | } | |
332 | ||
333 | const wxCursor wxBusyCursor::GetBusyCursor() | |
334 | { | |
335 | return wxCursor(wxCURSOR_WATCH); | |
336 | } | |
337 | ||
8bbe427f | 338 | void wxEndBusyCursor() |
c801d85f | 339 | { |
238d735d | 340 | if (--gs_busyCount > 0) |
89a43902 VZ |
341 | return; |
342 | ||
238d735d RR |
343 | wxSetCursor( gs_savedCursor ); |
344 | gs_savedCursor = wxNullCursor; | |
13971833 | 345 | |
d1dea842 RR |
346 | if (wxTheApp) |
347 | wxTheApp->SendIdleEvents(); | |
ff7b1510 | 348 | } |
c801d85f KB |
349 | |
350 | void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) ) | |
351 | { | |
238d735d | 352 | if (gs_busyCount++ > 0) |
89a43902 VZ |
353 | return; |
354 | ||
238d735d | 355 | wxASSERT_MSG( !gs_savedCursor.Ok(), |
223d09f6 | 356 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); |
7c39369e | 357 | |
238d735d | 358 | gs_savedCursor = g_globalCursor; |
13971833 | 359 | |
238d735d | 360 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); |
13971833 | 361 | |
d1dea842 RR |
362 | if (wxTheApp) |
363 | wxTheApp->SendIdleEvents(); | |
7e410431 RR |
364 | |
365 | gdk_flush(); | |
ff7b1510 | 366 | } |
c801d85f | 367 | |
8bbe427f | 368 | bool wxIsBusy() |
c801d85f | 369 | { |
89a43902 | 370 | return gs_busyCount > 0; |
ff7b1510 | 371 | } |
c801d85f KB |
372 | |
373 | void wxSetCursor( const wxCursor& cursor ) | |
374 | { | |
13971833 | 375 | if (g_isIdle) |
238d735d RR |
376 | wxapp_install_idle_handler(); |
377 | ||
378 | g_globalCursor = cursor; | |
ff7b1510 | 379 | } |