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