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