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