]> git.saurik.com Git - wxWidgets.git/blob - src/motif/cursor.cpp
Applied bug fix [ 684949 ] Dialup Sample With Borland C++ 5.5.1 Free Compiler
[wxWidgets.git] / src / motif / cursor.cpp
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"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/list.h"
20 #include "wx/window.h"
21 #if wxUSE_IMAGE
22 #include "wx/image.h"
23 #endif
24
25 #ifdef __VMS__
26 #pragma message disable nosimpint
27 #endif
28 #include <Xm/Xm.h>
29 #include <X11/cursorfont.h>
30 #ifdef __VMS__
31 #pragma message enable nosimpint
32 #endif
33
34 #include "wx/motif/private.h"
35
36 // Cursor for one display, so we can choose the correct one for
37 // the current display.
38 class wxXCursor
39 {
40 public:
41 WXDisplay* m_display;
42 WXCursor m_cursor;
43 };
44
45 WX_DECLARE_LIST(wxXCursor, wxXCursorList);
46 #include "wx/listimpl.cpp"
47 WX_DEFINE_LIST(wxXCursorList);
48
49 class WXDLLEXPORT wxCursorRefData: public wxObjectRefData
50 {
51 friend class WXDLLEXPORT wxCursor;
52 public:
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
63 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxObject)
64
65 wxCursorRefData::wxCursorRefData()
66 {
67 m_cursorId = wxCURSOR_NONE;
68 }
69
70 wxCursorRefData::~wxCursorRefData()
71 {
72 wxXCursorList::Node* node = m_cursors.GetFirst();
73 while (node)
74 {
75 wxXCursor* c = node->GetData();
76 XFreeCursor((Display*) c->m_display, (Cursor) c->m_cursor);
77 delete c;
78 node = node->GetNext();
79 }
80 }
81
82 wxCursor::wxCursor()
83 {
84 }
85
86 #if wxUSE_IMAGE
87 wxCursor::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] = 0xff;
102 i8 = i * 8;
103
104 cMask = 0xfe; // 11111110
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 << 1) | 1;
113 }
114 }
115
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 = 0x1;
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 << 1);
134 }
135 }
136 }
137 else // no mask
138 {
139 for (i=0; i<imagebitcount; i++)
140 maskBits[i] = 0xFF;
141 }
142
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 Create( (const char*)bits, w, h, hotSpotX, hotSpotY,
162 (const char*)maskBits );
163
164 delete[] bits;
165 delete[] maskBits;
166 }
167 #endif
168
169 void wxCursor::Create(const char bits[], int width, int height,
170 int hotSpotX, int hotSpotY, const char maskBits[])
171 {
172 if( !m_refData )
173 m_refData = new wxCursorRefData;
174
175 Display *dpy = (Display*) wxGetDisplay();
176 int screen_num = DefaultScreen (dpy);
177
178 Pixmap pixmap = XCreatePixmapFromBitmapData (dpy,
179 RootWindow (dpy, screen_num),
180 (char*) bits, width, height,
181 1 , 0 , 1);
182
183 Pixmap mask_pixmap = None;
184 if (maskBits != NULL)
185 {
186 mask_pixmap = XCreatePixmapFromBitmapData (dpy,
187 RootWindow (dpy, screen_num),
188 (char*) maskBits, width, height,
189 1 , 0 , 1);
190 }
191
192 Create( (WXPixmap)pixmap, (WXPixmap)mask_pixmap, hotSpotX, hotSpotY );
193
194 XFreePixmap( dpy, pixmap );
195 if (mask_pixmap != None)
196 {
197 XFreePixmap( dpy, mask_pixmap );
198 }
199 }
200
201 void wxCursor::Create(WXPixmap pixmap, WXPixmap mask_pixmap,
202 int hotSpotX, int hotSpotY)
203 {
204 if( !m_refData )
205 m_refData = new wxCursorRefData;
206
207 Display *dpy = (Display*) wxGetDisplay();
208 int screen_num = DefaultScreen (dpy);
209
210 XColor foreground_color;
211 XColor background_color;
212 foreground_color.pixel = BlackPixel(dpy, screen_num);
213 background_color.pixel = WhitePixel(dpy, screen_num);
214 Colormap cmap = (Colormap) wxTheApp->GetMainColormap((WXDisplay*) dpy);
215 XQueryColor(dpy, cmap, &foreground_color);
216 XQueryColor(dpy, cmap, &background_color);
217
218 Cursor cursor = XCreatePixmapCursor (dpy,
219 (Pixmap)pixmap,
220 (Pixmap)mask_pixmap,
221 &foreground_color,
222 &background_color,
223 hotSpotX ,
224 hotSpotY);
225
226 if (cursor)
227 {
228 wxXCursor *c = new wxXCursor;
229
230 c->m_cursor = (WXCursor) cursor;
231 c->m_display = (WXDisplay*) dpy;
232 M_CURSORDATA->m_cursors.Append(c);
233 }
234 }
235
236 wxCursor::wxCursor(const char bits[], int width, int height,
237 int hotSpotX, int hotSpotY, const char maskBits[])
238 {
239 Create(bits, width, height, hotSpotX, hotSpotY, maskBits);
240 }
241
242 wxCursor::wxCursor(const wxString& name, long flags, int hotSpotX, int hotSpotY)
243 {
244 // Must be an XBM file
245 if (flags != wxBITMAP_TYPE_XBM)
246 return;
247
248 m_refData = new wxCursorRefData;
249
250 int hotX = -1, hotY = -1;
251 unsigned int w, h;
252 Pixmap pixmap = None, mask_pixmap = None;
253
254 Display *dpy = (Display*) wxGetDisplay();
255 int screen_num = DefaultScreen (dpy);
256
257 int value = XReadBitmapFile (dpy, RootWindow (dpy, screen_num),
258 wxConstCast(name.c_str(), char),
259 &w, &h, &pixmap, &hotX, &hotY);
260
261 if (value == BitmapSuccess)
262 {
263 // TODO: how do we determine whether hotX, hotY were read correctly?
264 if (hotX < 0 || hotY < 0)
265 {
266 hotX = hotSpotX;
267 hotY = hotSpotY;
268 }
269 if (hotX < 0 || hotY < 0)
270 {
271 hotX = 0;
272 hotY = 0;
273 }
274
275 Create( (WXPixmap)pixmap, (WXPixmap)mask_pixmap, hotX, hotY );
276
277 XFreePixmap( dpy, pixmap );
278 }
279 }
280
281 // Cursors by stock number
282 wxCursor::wxCursor(wxStockCursor id)
283 {
284 m_refData = new wxCursorRefData;
285 M_CURSORDATA->m_cursorId = id;
286 }
287
288 wxCursor::~wxCursor()
289 {
290 }
291
292 bool wxCursor::Ok() const
293 {
294 return m_refData != NULL;
295 }
296
297 // Motif-specific: create/get a cursor for the current display
298 WXCursor wxCursor::GetXCursor(WXDisplay* display)
299 {
300 if (!M_CURSORDATA)
301 return (WXCursor) 0;
302 wxXCursorList::Node* node = M_CURSORDATA->m_cursors.GetFirst();
303 while (node)
304 {
305 wxXCursor* c = node->GetData();
306 if (c->m_display == display)
307 return c->m_cursor;
308 node = node->GetNext();
309 }
310
311 // No cursor for this display, so let's see if we're an id-type cursor.
312
313 if (M_CURSORDATA->m_cursorId != wxCURSOR_NONE)
314 {
315 WXCursor cursor = MakeCursor(display, M_CURSORDATA->m_cursorId);
316 if (cursor)
317 {
318 wxXCursor* c = new wxXCursor;
319 c->m_cursor = cursor;
320 c->m_display = display;
321 M_CURSORDATA->m_cursors.Append(c);
322 return cursor;
323 }
324 else
325 return (WXCursor) 0;
326 }
327
328 // Not an id-type cursor, so we don't know how to create it.
329 return (WXCursor) 0;
330 }
331
332 // Make a cursor from standard id
333 WXCursor wxCursor::MakeCursor(WXDisplay* display, wxStockCursor id)
334 {
335 Display* dpy = (Display*) display;
336 Cursor cursor = (Cursor) 0;
337 int x_cur = -1;
338
339 switch (id)
340 {
341 case wxCURSOR_WAIT: x_cur = XC_watch; break;
342 case wxCURSOR_CROSS: x_cur = XC_crosshair; break;
343 case wxCURSOR_CHAR: return (WXCursor)cursor; break;
344 case wxCURSOR_HAND: x_cur = XC_hand1; break;
345 case wxCURSOR_BULLSEYE: x_cur = XC_target; break;
346 case wxCURSOR_PENCIL: x_cur = XC_pencil; break;
347 case wxCURSOR_MAGNIFIER: x_cur = XC_sizing; break;
348 case wxCURSOR_IBEAM: x_cur = XC_xterm; break;
349 case wxCURSOR_NO_ENTRY: x_cur = XC_pirate; break;
350 case wxCURSOR_LEFT_BUTTON: x_cur = XC_leftbutton; break;
351 case wxCURSOR_RIGHT_BUTTON: x_cur = XC_rightbutton; break;
352 case wxCURSOR_MIDDLE_BUTTON: x_cur = XC_middlebutton; break;
353 case wxCURSOR_QUESTION_ARROW: x_cur = XC_question_arrow; break;
354 case wxCURSOR_SIZING: x_cur = XC_sizing; break;
355 case wxCURSOR_WATCH: x_cur = XC_watch; break;
356 case wxCURSOR_SPRAYCAN: x_cur = XC_spraycan; break;
357 case wxCURSOR_PAINT_BRUSH: x_cur = XC_spraycan; break;
358 case wxCURSOR_SIZENWSE:
359 case wxCURSOR_SIZENESW: x_cur = XC_crosshair; break;
360 case wxCURSOR_SIZEWE: x_cur = XC_sb_h_double_arrow; break;
361 case wxCURSOR_SIZENS: x_cur = XC_sb_v_double_arrow; break;
362 case wxCURSOR_POINT_LEFT: x_cur = XC_sb_left_arrow; break;
363 case wxCURSOR_POINT_RIGHT: x_cur = XC_sb_right_arrow; break;
364 // (JD Huggins) added more stock cursors for X
365 // X-only cursors BEGIN
366 case wxCURSOR_CROSS_REVERSE: x_cur = XC_cross_reverse; break;
367 case wxCURSOR_DOUBLE_ARROW: x_cur = XC_double_arrow; break;
368 case wxCURSOR_BASED_ARROW_UP: x_cur = XC_based_arrow_up; break;
369 case wxCURSOR_BASED_ARROW_DOWN: x_cur = XC_based_arrow_down; break;
370 case wxCURSOR_BLANK:
371 {
372 GC gc;
373 XGCValues gcv;
374 Pixmap empty_pixmap;
375 XColor blank_color;
376
377 empty_pixmap =
378 XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
379 16, 16, 1);
380 gcv.function = GXxor;
381 gc = XCreateGC (dpy,
382 empty_pixmap,
383 GCFunction,
384 &gcv);
385 XCopyArea (dpy,
386 empty_pixmap,
387 empty_pixmap,
388 gc,
389 0, 0,
390 16, 16,
391 0, 0);
392 XFreeGC (dpy, gc);
393 cursor = XCreatePixmapCursor (dpy,
394 empty_pixmap,
395 empty_pixmap,
396 &blank_color,
397 &blank_color,
398 8, 8);
399
400 break;
401 }
402 case wxCURSOR_ARROW:
403 default: x_cur = XC_top_left_arrow; break;
404 }
405
406 if( x_cur == -1 )
407 return (WXCursor)cursor;
408
409 cursor = XCreateFontCursor (dpy, x_cur);
410 return (WXCursor) cursor;
411 }
412
413 // Global cursor setting
414 void wxSetCursor(const wxCursor& WXUNUSED(cursor))
415 {
416 // Nothing to do for Motif (no global cursor)
417 }
418
419
420 // ----------------------------------------------------------------------------
421 // busy cursor stuff
422 // ----------------------------------------------------------------------------
423
424 static int wxBusyCursorCount = 0;
425
426 // Helper function
427 static void
428 wxXSetBusyCursor (wxWindow * win, wxCursor * cursor)
429 {
430 Display *display = (Display*) win->GetXDisplay();
431
432 Window xwin = (Window) win->GetXWindow();
433 if (!xwin)
434 return;
435
436 XSetWindowAttributes attrs;
437
438 if (cursor)
439 {
440 attrs.cursor = (Cursor) cursor->GetXCursor(display);
441 }
442 else
443 {
444 // Restore old cursor
445 if (win->GetCursor().Ok())
446 attrs.cursor = (Cursor) win->GetCursor().GetXCursor(display);
447 else
448 attrs.cursor = None;
449 }
450 if (xwin)
451 XChangeWindowAttributes (display, xwin, CWCursor, &attrs);
452
453 XFlush (display);
454
455 for(wxWindowList::Node *node = win->GetChildren().GetFirst (); node;
456 node = node->GetNext())
457 {
458 wxWindow *child = node->GetData ();
459 wxXSetBusyCursor (child, cursor);
460 }
461 }
462
463 // Set the cursor to the busy cursor for all windows
464 void wxBeginBusyCursor(wxCursor *cursor)
465 {
466 wxBusyCursorCount++;
467 if (wxBusyCursorCount == 1)
468 {
469 for(wxWindowList::Node *node = wxTopLevelWindows.GetFirst (); node;
470 node = node->GetNext())
471 {
472 wxWindow *win = node->GetData ();
473 wxXSetBusyCursor (win, cursor);
474 }
475 }
476 }
477
478 // Restore cursor to normal
479 void wxEndBusyCursor()
480 {
481 if (wxBusyCursorCount == 0)
482 return;
483
484 wxBusyCursorCount--;
485 if (wxBusyCursorCount == 0)
486 {
487 for(wxWindowList::Node *node = wxTopLevelWindows.GetFirst (); node;
488 node = node->GetNext())
489 {
490 wxWindow *win = node->GetData ();
491 wxXSetBusyCursor (win, NULL);
492 }
493 }
494 }
495
496 // TRUE if we're between the above two calls
497 bool wxIsBusy()
498 {
499 return (wxBusyCursorCount > 0);
500 }