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