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