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