]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: cursor.cpp | |
3 | // Purpose: wxCursor class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1248b41f MB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
4bb6408c | 15 | #include "wx/cursor.h" |
f97c9854 JS |
16 | #include "wx/app.h" |
17 | #include "wx/utils.h" | |
93e73c74 | 18 | #include "wx/list.h" |
d8d18184 | 19 | #include "wx/window.h" |
4daa4d66 CE |
20 | #if wxUSE_IMAGE |
21 | #include "wx/image.h" | |
22 | #endif | |
f97c9854 | 23 | |
338dd992 JJ |
24 | #ifdef __VMS__ |
25 | #pragma message disable nosimpint | |
26 | #endif | |
f97c9854 JS |
27 | #include <Xm/Xm.h> |
28 | #include <X11/cursorfont.h> | |
338dd992 JJ |
29 | #ifdef __VMS__ |
30 | #pragma message enable nosimpint | |
31 | #endif | |
f97c9854 JS |
32 | |
33 | #include "wx/motif/private.h" | |
4bb6408c | 34 | |
93e73c74 MB |
35 | // Cursor for one display, so we can choose the correct one for |
36 | // the current display. | |
37 | class wxXCursor | |
38 | { | |
39 | public: | |
40 | WXDisplay* m_display; | |
41 | WXCursor m_cursor; | |
42 | }; | |
43 | ||
44 | WX_DECLARE_LIST(wxXCursor, wxXCursorList); | |
45 | #include "wx/listimpl.cpp" | |
46 | WX_DEFINE_LIST(wxXCursorList); | |
47 | ||
48 | class WXDLLEXPORT wxCursorRefData: public wxObjectRefData | |
49 | { | |
50 | friend class WXDLLEXPORT wxCursor; | |
51 | public: | |
52 | wxCursorRefData(); | |
53 | ~wxCursorRefData(); | |
54 | ||
55 | wxXCursorList m_cursors; // wxXCursor objects, one per display | |
77ffb593 | 56 | wxStockCursor m_cursorId; // wxWidgets standard cursor id |
93e73c74 MB |
57 | }; |
58 | ||
59 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) | |
60 | #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData) | |
61 | ||
62 | IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxObject) | |
4bb6408c JS |
63 | |
64 | wxCursorRefData::wxCursorRefData() | |
65 | { | |
f97c9854 | 66 | m_cursorId = wxCURSOR_NONE; |
4bb6408c JS |
67 | } |
68 | ||
69 | wxCursorRefData::~wxCursorRefData() | |
70 | { | |
ac32ba44 | 71 | wxXCursorList::compatibility_iterator node = m_cursors.GetFirst(); |
f97c9854 JS |
72 | while (node) |
73 | { | |
93e73c74 MB |
74 | wxXCursor* c = node->GetData(); |
75 | XFreeCursor((Display*) c->m_display, (Cursor) c->m_cursor); | |
f97c9854 | 76 | delete c; |
fd304d98 | 77 | node = node->GetNext(); |
f97c9854 | 78 | } |
4bb6408c JS |
79 | } |
80 | ||
4bb6408c JS |
81 | wxCursor::wxCursor() |
82 | { | |
83 | } | |
84 | ||
4daa4d66 CE |
85 | #if wxUSE_IMAGE |
86 | wxCursor::wxCursor(const wxImage & image) | |
87 | { | |
88 | unsigned char * rgbBits = image.GetData(); | |
89 | int w = image.GetWidth() ; | |
90 | int h = image.GetHeight(); | |
91 | bool bHasMask = image.HasMask(); | |
92 | int imagebitcount = (w*h)/8; | |
93 | ||
94 | unsigned char * bits = new unsigned char [imagebitcount]; | |
95 | unsigned char * maskBits = new unsigned char [imagebitcount]; | |
96 | ||
97 | int i, j, i8; unsigned char c, cMask; | |
98 | for (i=0; i<imagebitcount; i++) | |
99 | { | |
ffa3d6b8 | 100 | bits[i] = 0xff; |
4daa4d66 CE |
101 | i8 = i * 8; |
102 | ||
ffa3d6b8 | 103 | cMask = 0xfe; // 11111110 |
4daa4d66 CE |
104 | for (j=0; j<8; j++) |
105 | { | |
106 | // possible overflow if we do the summation first ? | |
107 | c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3; | |
108 | //if average value is > mid grey | |
109 | if (c>127) | |
ffa3d6b8 MB |
110 | bits[i] = bits[i] & cMask; |
111 | cMask = (cMask << 1) | 1; | |
4daa4d66 CE |
112 | } |
113 | } | |
114 | ||
4daa4d66 CE |
115 | if (bHasMask) |
116 | { | |
117 | unsigned char | |
118 | r = image.GetMaskRed(), | |
119 | g = image.GetMaskGreen(), | |
120 | b = image.GetMaskBlue(); | |
121 | ||
122 | for (i=0; i<imagebitcount; i++) | |
123 | { | |
124 | maskBits[i] = 0x0; | |
125 | i8 = i * 8; | |
126 | ||
ffa3d6b8 | 127 | cMask = 0x1; |
4daa4d66 CE |
128 | for (j=0; j<8; j++) |
129 | { | |
130 | if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b) | |
131 | maskBits[i] = maskBits[i] | cMask; | |
ffa3d6b8 | 132 | cMask = (cMask << 1); |
4daa4d66 CE |
133 | } |
134 | } | |
4daa4d66 CE |
135 | } |
136 | else // no mask | |
137 | { | |
138 | for (i=0; i<imagebitcount; i++) | |
139 | maskBits[i] = 0xFF; | |
4daa4d66 | 140 | } |
4daa4d66 | 141 | |
4daa4d66 CE |
142 | int hotSpotX; |
143 | int hotSpotY; | |
144 | ||
7e91413a MB |
145 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X)) |
146 | hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); | |
4daa4d66 CE |
147 | else |
148 | hotSpotX = 0; | |
149 | ||
7e91413a MB |
150 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y)) |
151 | hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
4daa4d66 CE |
152 | else |
153 | hotSpotY = 0; | |
154 | ||
155 | if (hotSpotX < 0 || hotSpotX >= w) | |
156 | hotSpotX = 0; | |
157 | if (hotSpotY < 0 || hotSpotY >= h) | |
158 | hotSpotY = 0; | |
ffa3d6b8 MB |
159 | |
160 | Create( (const char*)bits, w, h, hotSpotX, hotSpotY, | |
161 | (const char*)maskBits ); | |
162 | ||
163 | delete[] bits; | |
164 | delete[] maskBits; | |
165 | } | |
166 | #endif | |
167 | ||
168 | void wxCursor::Create(const char bits[], int width, int height, | |
169 | int hotSpotX, int hotSpotY, const char maskBits[]) | |
170 | { | |
171 | if( !m_refData ) | |
172 | m_refData = new wxCursorRefData; | |
4daa4d66 CE |
173 | |
174 | Display *dpy = (Display*) wxGetDisplay(); | |
175 | int screen_num = DefaultScreen (dpy); | |
176 | ||
177 | Pixmap pixmap = XCreatePixmapFromBitmapData (dpy, | |
ffa3d6b8 MB |
178 | RootWindow (dpy, screen_num), |
179 | (char*) bits, width, height, | |
4daa4d66 CE |
180 | 1 , 0 , 1); |
181 | ||
182 | Pixmap mask_pixmap = None; | |
183 | if (maskBits != NULL) | |
184 | { | |
185 | mask_pixmap = XCreatePixmapFromBitmapData (dpy, | |
ffa3d6b8 MB |
186 | RootWindow (dpy, screen_num), |
187 | (char*) maskBits, width, height, | |
4daa4d66 CE |
188 | 1 , 0 , 1); |
189 | } | |
190 | ||
ffa3d6b8 | 191 | Create( (WXPixmap)pixmap, (WXPixmap)mask_pixmap, hotSpotX, hotSpotY ); |
4daa4d66 CE |
192 | |
193 | XFreePixmap( dpy, pixmap ); | |
194 | if (mask_pixmap != None) | |
195 | { | |
196 | XFreePixmap( dpy, mask_pixmap ); | |
197 | } | |
4daa4d66 | 198 | } |
4daa4d66 | 199 | |
ffa3d6b8 MB |
200 | void wxCursor::Create(WXPixmap pixmap, WXPixmap mask_pixmap, |
201 | int hotSpotX, int hotSpotY) | |
4bb6408c | 202 | { |
ffa3d6b8 MB |
203 | if( !m_refData ) |
204 | m_refData = new wxCursorRefData; | |
4bb6408c | 205 | |
f97c9854 JS |
206 | Display *dpy = (Display*) wxGetDisplay(); |
207 | int screen_num = DefaultScreen (dpy); | |
4bb6408c | 208 | |
f97c9854 JS |
209 | XColor foreground_color; |
210 | XColor background_color; | |
211 | foreground_color.pixel = BlackPixel(dpy, screen_num); | |
212 | background_color.pixel = WhitePixel(dpy, screen_num); | |
213 | Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy); | |
214 | XQueryColor(dpy, cmap, &foreground_color); | |
215 | XQueryColor(dpy, cmap, &background_color); | |
216 | ||
217 | Cursor cursor = XCreatePixmapCursor (dpy, | |
ffa3d6b8 MB |
218 | (Pixmap)pixmap, |
219 | (Pixmap)mask_pixmap, | |
f97c9854 JS |
220 | &foreground_color, |
221 | &background_color, | |
222 | hotSpotX , | |
223 | hotSpotY); | |
224 | ||
f97c9854 | 225 | if (cursor) |
4bb6408c | 226 | { |
f97c9854 JS |
227 | wxXCursor *c = new wxXCursor; |
228 | ||
229 | c->m_cursor = (WXCursor) cursor; | |
230 | c->m_display = (WXDisplay*) dpy; | |
231 | M_CURSORDATA->m_cursors.Append(c); | |
4bb6408c | 232 | } |
f97c9854 JS |
233 | } |
234 | ||
ffa3d6b8 MB |
235 | wxCursor::wxCursor(const char bits[], int width, int height, |
236 | int hotSpotX, int hotSpotY, const char maskBits[]) | |
237 | { | |
238 | Create(bits, width, height, hotSpotX, hotSpotY, maskBits); | |
239 | } | |
240 | ||
f97c9854 JS |
241 | wxCursor::wxCursor(const wxString& name, long flags, int hotSpotX, int hotSpotY) |
242 | { | |
243 | // Must be an XBM file | |
244 | if (flags != wxBITMAP_TYPE_XBM) | |
245 | return; | |
246 | ||
247 | m_refData = new wxCursorRefData; | |
248 | ||
249 | int hotX = -1, hotY = -1; | |
250 | unsigned int w, h; | |
ffa3d6b8 | 251 | Pixmap pixmap = None, mask_pixmap = None; |
f97c9854 JS |
252 | |
253 | Display *dpy = (Display*) wxGetDisplay(); | |
254 | int screen_num = DefaultScreen (dpy); | |
255 | ||
ffa3d6b8 | 256 | int value = XReadBitmapFile (dpy, RootWindow (dpy, screen_num), |
d3a80c92 MB |
257 | wxConstCast(name.c_str(), char), |
258 | &w, &h, &pixmap, &hotX, &hotY); | |
f97c9854 | 259 | |
ffa3d6b8 | 260 | if (value == BitmapSuccess) |
4bb6408c | 261 | { |
f97c9854 JS |
262 | // TODO: how do we determine whether hotX, hotY were read correctly? |
263 | if (hotX < 0 || hotY < 0) | |
264 | { | |
265 | hotX = hotSpotX; | |
266 | hotY = hotSpotY; | |
267 | } | |
268 | if (hotX < 0 || hotY < 0) | |
269 | { | |
270 | hotX = 0; | |
271 | hotY = 0; | |
272 | } | |
273 | ||
ffa3d6b8 | 274 | Create( (WXPixmap)pixmap, (WXPixmap)mask_pixmap, hotX, hotY ); |
f97c9854 JS |
275 | |
276 | XFreePixmap( dpy, pixmap ); | |
4bb6408c | 277 | } |
f97c9854 JS |
278 | } |
279 | ||
280 | // Cursors by stock number | |
281 | wxCursor::wxCursor(wxStockCursor id) | |
282 | { | |
283 | m_refData = new wxCursorRefData; | |
284 | M_CURSORDATA->m_cursorId = id; | |
f97c9854 JS |
285 | } |
286 | ||
287 | wxCursor::~wxCursor() | |
288 | { | |
289 | } | |
290 | ||
93e73c74 MB |
291 | bool wxCursor::Ok() const |
292 | { | |
293 | return m_refData != NULL; | |
294 | } | |
295 | ||
f97c9854 | 296 | // Motif-specific: create/get a cursor for the current display |
bc6e28d5 | 297 | WXCursor wxCursor::GetXCursor(WXDisplay* display) const |
f97c9854 JS |
298 | { |
299 | if (!M_CURSORDATA) | |
300 | return (WXCursor) 0; | |
ac32ba44 | 301 | wxXCursorList::compatibility_iterator node = M_CURSORDATA->m_cursors.GetFirst(); |
f97c9854 | 302 | while (node) |
4bb6408c | 303 | { |
93e73c74 | 304 | wxXCursor* c = node->GetData(); |
f97c9854 JS |
305 | if (c->m_display == display) |
306 | return c->m_cursor; | |
fd304d98 | 307 | node = node->GetNext(); |
4bb6408c | 308 | } |
f97c9854 JS |
309 | |
310 | // No cursor for this display, so let's see if we're an id-type cursor. | |
311 | ||
312 | if (M_CURSORDATA->m_cursorId != wxCURSOR_NONE) | |
4bb6408c | 313 | { |
f97c9854 JS |
314 | WXCursor cursor = MakeCursor(display, M_CURSORDATA->m_cursorId); |
315 | if (cursor) | |
316 | { | |
317 | wxXCursor* c = new wxXCursor; | |
318 | c->m_cursor = cursor; | |
319 | c->m_display = display; | |
320 | M_CURSORDATA->m_cursors.Append(c); | |
321 | return cursor; | |
322 | } | |
323 | else | |
324 | return (WXCursor) 0; | |
4bb6408c | 325 | } |
4bb6408c | 326 | |
f97c9854 JS |
327 | // Not an id-type cursor, so we don't know how to create it. |
328 | return (WXCursor) 0; | |
4bb6408c JS |
329 | } |
330 | ||
f97c9854 | 331 | // Make a cursor from standard id |
bc6e28d5 | 332 | WXCursor wxCursor::MakeCursor(WXDisplay* display, wxStockCursor id) const |
4bb6408c | 333 | { |
f97c9854 JS |
334 | Display* dpy = (Display*) display; |
335 | Cursor cursor = (Cursor) 0; | |
93e73c74 | 336 | int x_cur = -1; |
f97c9854 JS |
337 | |
338 | switch (id) | |
339 | { | |
93e73c74 MB |
340 | case wxCURSOR_WAIT: x_cur = XC_watch; break; |
341 | case wxCURSOR_CROSS: x_cur = XC_crosshair; break; | |
342 | case wxCURSOR_CHAR: return (WXCursor)cursor; break; | |
343 | case wxCURSOR_HAND: x_cur = XC_hand1; break; | |
344 | case wxCURSOR_BULLSEYE: x_cur = XC_target; break; | |
345 | case wxCURSOR_PENCIL: x_cur = XC_pencil; break; | |
346 | case wxCURSOR_MAGNIFIER: x_cur = XC_sizing; break; | |
347 | case wxCURSOR_IBEAM: x_cur = XC_xterm; break; | |
348 | case wxCURSOR_NO_ENTRY: x_cur = XC_pirate; break; | |
349 | case wxCURSOR_LEFT_BUTTON: x_cur = XC_leftbutton; break; | |
350 | case wxCURSOR_RIGHT_BUTTON: x_cur = XC_rightbutton; break; | |
351 | case wxCURSOR_MIDDLE_BUTTON: x_cur = XC_middlebutton; break; | |
352 | case wxCURSOR_QUESTION_ARROW: x_cur = XC_question_arrow; break; | |
353 | case wxCURSOR_SIZING: x_cur = XC_sizing; break; | |
354 | case wxCURSOR_WATCH: x_cur = XC_watch; break; | |
355 | case wxCURSOR_SPRAYCAN: x_cur = XC_spraycan; break; | |
356 | case wxCURSOR_PAINT_BRUSH: x_cur = XC_spraycan; break; | |
357 | case wxCURSOR_SIZENWSE: | |
358 | case wxCURSOR_SIZENESW: x_cur = XC_crosshair; break; | |
359 | case wxCURSOR_SIZEWE: x_cur = XC_sb_h_double_arrow; break; | |
360 | case wxCURSOR_SIZENS: x_cur = XC_sb_v_double_arrow; break; | |
361 | case wxCURSOR_POINT_LEFT: x_cur = XC_sb_left_arrow; break; | |
362 | case wxCURSOR_POINT_RIGHT: x_cur = XC_sb_right_arrow; break; | |
f97c9854 JS |
363 | // (JD Huggins) added more stock cursors for X |
364 | // X-only cursors BEGIN | |
93e73c74 MB |
365 | case wxCURSOR_CROSS_REVERSE: x_cur = XC_cross_reverse; break; |
366 | case wxCURSOR_DOUBLE_ARROW: x_cur = XC_double_arrow; break; | |
367 | case wxCURSOR_BASED_ARROW_UP: x_cur = XC_based_arrow_up; break; | |
368 | case wxCURSOR_BASED_ARROW_DOWN: x_cur = XC_based_arrow_down; break; | |
369 | case wxCURSOR_BLANK: | |
370 | { | |
371 | GC gc; | |
372 | XGCValues gcv; | |
373 | Pixmap empty_pixmap; | |
374 | XColor blank_color; | |
375 | ||
376 | empty_pixmap = | |
377 | XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), | |
378 | 16, 16, 1); | |
379 | gcv.function = GXxor; | |
380 | gc = XCreateGC (dpy, | |
381 | empty_pixmap, | |
382 | GCFunction, | |
383 | &gcv); | |
384 | XCopyArea (dpy, | |
f97c9854 JS |
385 | empty_pixmap, |
386 | empty_pixmap, | |
387 | gc, | |
388 | 0, 0, | |
389 | 16, 16, | |
390 | 0, 0); | |
93e73c74 MB |
391 | XFreeGC (dpy, gc); |
392 | cursor = XCreatePixmapCursor (dpy, | |
393 | empty_pixmap, | |
394 | empty_pixmap, | |
395 | &blank_color, | |
396 | &blank_color, | |
397 | 8, 8); | |
398 | ||
399 | break; | |
400 | } | |
401 | case wxCURSOR_ARROW: | |
402 | default: x_cur = XC_top_left_arrow; break; | |
f97c9854 | 403 | } |
93e73c74 MB |
404 | |
405 | if( x_cur == -1 ) | |
406 | return (WXCursor)cursor; | |
407 | ||
408 | cursor = XCreateFontCursor (dpy, x_cur); | |
f97c9854 | 409 | return (WXCursor) cursor; |
4bb6408c JS |
410 | } |
411 | ||
412 | // Global cursor setting | |
f9e02ac7 | 413 | void wxSetCursor(const wxCursor& WXUNUSED(cursor)) |
4bb6408c | 414 | { |
f97c9854 | 415 | // Nothing to do for Motif (no global cursor) |
4bb6408c JS |
416 | } |
417 | ||
418 | ||
707440dc JS |
419 | // ---------------------------------------------------------------------------- |
420 | // busy cursor stuff | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | static int wxBusyCursorCount = 0; | |
424 | ||
425 | // Helper function | |
426 | static void | |
427 | wxXSetBusyCursor (wxWindow * win, wxCursor * cursor) | |
428 | { | |
429 | Display *display = (Display*) win->GetXDisplay(); | |
430 | ||
431 | Window xwin = (Window) win->GetXWindow(); | |
432 | if (!xwin) | |
433 | return; | |
434 | ||
435 | XSetWindowAttributes attrs; | |
436 | ||
437 | if (cursor) | |
438 | { | |
439 | attrs.cursor = (Cursor) cursor->GetXCursor(display); | |
440 | } | |
441 | else | |
442 | { | |
443 | // Restore old cursor | |
444 | if (win->GetCursor().Ok()) | |
445 | attrs.cursor = (Cursor) win->GetCursor().GetXCursor(display); | |
446 | else | |
447 | attrs.cursor = None; | |
448 | } | |
449 | if (xwin) | |
450 | XChangeWindowAttributes (display, xwin, CWCursor, &attrs); | |
451 | ||
452 | XFlush (display); | |
453 | ||
ac32ba44 | 454 | for(wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst (); node; |
fd304d98 | 455 | node = node->GetNext()) |
707440dc | 456 | { |
fd304d98 | 457 | wxWindow *child = node->GetData (); |
707440dc JS |
458 | wxXSetBusyCursor (child, cursor); |
459 | } | |
460 | } | |
461 | ||
462 | // Set the cursor to the busy cursor for all windows | |
463 | void wxBeginBusyCursor(wxCursor *cursor) | |
464 | { | |
465 | wxBusyCursorCount++; | |
466 | if (wxBusyCursorCount == 1) | |
467 | { | |
ac32ba44 | 468 | for(wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst (); node; |
fd304d98 | 469 | node = node->GetNext()) |
707440dc | 470 | { |
fd304d98 | 471 | wxWindow *win = node->GetData (); |
707440dc JS |
472 | wxXSetBusyCursor (win, cursor); |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | // Restore cursor to normal | |
478 | void wxEndBusyCursor() | |
479 | { | |
480 | if (wxBusyCursorCount == 0) | |
481 | return; | |
482 | ||
483 | wxBusyCursorCount--; | |
484 | if (wxBusyCursorCount == 0) | |
485 | { | |
ac32ba44 | 486 | for(wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst (); node; |
fd304d98 | 487 | node = node->GetNext()) |
707440dc | 488 | { |
fd304d98 | 489 | wxWindow *win = node->GetData (); |
707440dc JS |
490 | wxXSetBusyCursor (win, NULL); |
491 | } | |
492 | } | |
493 | } | |
494 | ||
96be256b | 495 | // true if we're between the above two calls |
707440dc JS |
496 | bool wxIsBusy() |
497 | { | |
498 | return (wxBusyCursorCount > 0); | |
499 | } |