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