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