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