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