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