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