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