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