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