]> git.saurik.com Git - wxWidgets.git/blob - src/motif/cursor.cpp
Change wxNotebook selection before sending page changed event in wxMSW.
[wxWidgets.git] / src / motif / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/list.h"
17 #endif
18
19 #include "wx/cursor.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #include "wx/utils.h"
24 #include "wx/window.h"
25 #include "wx/image.h"
26 #include "wx/log.h"
27 #endif
28
29 #ifdef __VMS__
30 #pragma message disable nosimpint
31 #endif
32 #include <Xm/Xm.h>
33 #include <X11/cursorfont.h>
34 #ifdef __VMS__
35 #pragma message enable nosimpint
36 #endif
37
38 #include "wx/motif/private.h"
39
40 // Cursor for one display, so we can choose the correct one for
41 // the current display.
42 class wxXCursor
43 {
44 public:
45 WXDisplay* m_display;
46 WXCursor m_cursor;
47 };
48
49 WX_DECLARE_LIST(wxXCursor, wxXCursorList);
50 #include "wx/listimpl.cpp"
51 WX_DEFINE_LIST(wxXCursorList)
52
53 class WXDLLEXPORT wxCursorRefData: public wxGDIRefData
54 {
55 public:
56 wxCursorRefData();
57 virtual ~wxCursorRefData();
58
59 wxXCursorList m_cursors; // wxXCursor objects, one per display
60 wxStockCursor m_cursorId; // wxWidgets standard cursor id
61
62 private:
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
67 friend class wxCursor;
68 };
69
70 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
71 #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData)
72
73 IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxObject)
74
75 wxCursorRefData::wxCursorRefData()
76 {
77 m_cursorId = wxCURSOR_NONE;
78 }
79
80 wxCursorRefData::~wxCursorRefData()
81 {
82 wxXCursorList::compatibility_iterator node = m_cursors.GetFirst();
83 while (node)
84 {
85 wxXCursor* c = node->GetData();
86 XFreeCursor((Display*) c->m_display, (Cursor) c->m_cursor);
87 delete c;
88 node = node->GetNext();
89 }
90 }
91
92 wxCursor::wxCursor()
93 {
94 }
95
96 #if wxUSE_IMAGE
97 wxCursor::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
108 int i, j, i8;
109 unsigned char c, cMask;
110 for (i=0; i<imagebitcount; i++)
111 {
112 bits[i] = 0xff;
113 i8 = i * 8;
114
115 cMask = 0xfe; // 11111110
116 for (j=0; j<8; j++)
117 {
118 // possible overflow if we do the summation first ?
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
121 if (c>127)
122 bits[i] = bits[i] & cMask;
123 cMask = (unsigned char)((cMask << 1) | 1);
124 }
125 }
126
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
139 cMask = 0x1;
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;
144 cMask = (unsigned char)(cMask << 1);
145 }
146 }
147 }
148 else // no mask
149 {
150 for (i=0; i<imagebitcount; i++)
151 maskBits[i] = 0xFF;
152 }
153
154 int hotSpotX;
155 int hotSpotY;
156
157 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
158 hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
159 else
160 hotSpotX = 0;
161
162 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
163 hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
164 else
165 hotSpotY = 0;
166
167 if (hotSpotX < 0 || hotSpotX >= w)
168 hotSpotX = 0;
169 if (hotSpotY < 0 || hotSpotY >= h)
170 hotSpotY = 0;
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
180 void 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;
185
186 Display *dpy = (Display*) wxGetDisplay();
187 int screen_num = DefaultScreen (dpy);
188
189 Pixmap pixmap = XCreatePixmapFromBitmapData (dpy,
190 RootWindow (dpy, screen_num),
191 (char*) bits, width, height,
192 1 , 0 , 1);
193
194 Pixmap mask_pixmap = None;
195 if (maskBits != NULL)
196 {
197 mask_pixmap = XCreatePixmapFromBitmapData (dpy,
198 RootWindow (dpy, screen_num),
199 (char*) maskBits, width, height,
200 1 , 0 , 1);
201 }
202
203 Create( (WXPixmap)pixmap, (WXPixmap)mask_pixmap, hotSpotX, hotSpotY );
204
205 XFreePixmap( dpy, pixmap );
206 if (mask_pixmap != None)
207 {
208 XFreePixmap( dpy, mask_pixmap );
209 }
210 }
211
212 void wxCursor::Create(WXPixmap pixmap, WXPixmap mask_pixmap,
213 int hotSpotX, int hotSpotY)
214 {
215 if( !m_refData )
216 m_refData = new wxCursorRefData;
217
218 Display *dpy = (Display*) wxGetDisplay();
219 int screen_num = DefaultScreen (dpy);
220
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,
230 (Pixmap)pixmap,
231 (Pixmap)mask_pixmap,
232 &foreground_color,
233 &background_color,
234 hotSpotX ,
235 hotSpotY);
236
237 if (cursor)
238 {
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);
244 }
245 }
246
247 wxCursor::wxCursor(const char bits[], int width, int height,
248 int hotSpotX, int hotSpotY, const char maskBits[] ,
249 const wxColour* WXUNUSED(fg), const wxColour* WXUNUSED(bg) )
250 {
251 Create(bits, width, height, hotSpotX, hotSpotY, maskBits);
252 }
253
254 wxCursor::wxCursor(const wxString& name, wxBitmapType type,
255 int hotSpotX, int hotSpotY)
256 {
257 // Must be an XBM file
258 if (type != wxBITMAP_TYPE_XBM) {
259 wxLogError("Invalid cursor bitmap type '%d'", type);
260 return;
261 }
262
263 m_refData = new wxCursorRefData;
264
265 int hotX = -1, hotY = -1;
266 unsigned int w, h;
267 Pixmap pixmap = None, mask_pixmap = None;
268
269 Display *dpy = (Display*) wxGetDisplay();
270 int screen_num = DefaultScreen (dpy);
271
272 int value = XReadBitmapFile (dpy, RootWindow (dpy, screen_num),
273 name.mb_str(),
274 &w, &h, &pixmap, &hotX, &hotY);
275
276 if (value == BitmapSuccess)
277 {
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
290 Create( (WXPixmap)pixmap, (WXPixmap)mask_pixmap, hotX, hotY );
291
292 XFreePixmap( dpy, pixmap );
293 }
294 }
295
296 // Cursors by stock number
297 void wxCursor::InitFromStock(wxStockCursor id)
298 {
299 m_refData = new wxCursorRefData;
300 M_CURSORDATA->m_cursorId = id;
301 }
302
303 wxCursor::~wxCursor()
304 {
305 }
306
307 wxGDIRefData *wxCursor::CreateGDIRefData() const
308 {
309 return new wxCursorRefData;
310 }
311
312 wxGDIRefData *
313 wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
314 {
315 wxFAIL_MSG( wxS("Cloning cursors is not implemented in wxMotif.") );
316
317 return new wxCursorRefData;
318 }
319
320 // Motif-specific: create/get a cursor for the current display
321 WXCursor wxCursor::GetXCursor(WXDisplay* display) const
322 {
323 if (!M_CURSORDATA)
324 return (WXCursor) 0;
325 wxXCursorList::compatibility_iterator node = M_CURSORDATA->m_cursors.GetFirst();
326 while (node)
327 {
328 wxXCursor* c = node->GetData();
329 if (c->m_display == display)
330 return c->m_cursor;
331 node = node->GetNext();
332 }
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)
337 {
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;
349 }
350
351 // Not an id-type cursor, so we don't know how to create it.
352 return (WXCursor) 0;
353 }
354
355 // Make a cursor from standard id
356 WXCursor wxCursor::MakeCursor(WXDisplay* display, wxStockCursor id) const
357 {
358 Display* dpy = (Display*) display;
359 Cursor cursor = (Cursor) 0;
360 int x_cur = -1;
361
362 switch (id)
363 {
364 case wxCURSOR_CHAR: return (WXCursor)cursor;
365
366 case wxCURSOR_WAIT: x_cur = XC_watch; break;
367 case wxCURSOR_CROSS: x_cur = XC_crosshair; break;
368 case wxCURSOR_HAND: x_cur = XC_hand1; break;
369 case wxCURSOR_BULLSEYE: x_cur = XC_target; break;
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;
373 case wxCURSOR_NO_ENTRY: x_cur = XC_pirate; break;
374 case wxCURSOR_LEFT_BUTTON: x_cur = XC_leftbutton; break;
375 case wxCURSOR_RIGHT_BUTTON: x_cur = XC_rightbutton; break;
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;
388 // (JD Huggins) added more stock cursors for X
389 // X-only cursors BEGIN
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,
410 empty_pixmap,
411 empty_pixmap,
412 gc,
413 0, 0,
414 16, 16,
415 0, 0);
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;
428 }
429
430 if( x_cur == -1 )
431 return (WXCursor)cursor;
432
433 cursor = XCreateFontCursor (dpy, x_cur);
434 return (WXCursor) cursor;
435 }
436
437 // Global cursor setting
438 void wxSetCursor(const wxCursor& WXUNUSED(cursor))
439 {
440 // Nothing to do for Motif (no global cursor)
441 }
442
443
444 // ----------------------------------------------------------------------------
445 // busy cursor stuff
446 // ----------------------------------------------------------------------------
447
448 static int wxBusyCursorCount = 0;
449
450 // Helper function
451 static void
452 wxXSetBusyCursor (wxWindow * win, const wxCursor * cursor)
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
469 if (win->GetCursor().Ok())
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
479 for(wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst (); node;
480 node = node->GetNext())
481 {
482 wxWindow *child = node->GetData ();
483 wxXSetBusyCursor (child, cursor);
484 }
485 }
486
487 // Set the cursor to the busy cursor for all windows
488 void wxBeginBusyCursor(const wxCursor *cursor)
489 {
490 wxBusyCursorCount++;
491 if (wxBusyCursorCount == 1)
492 {
493 for(wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst (); node;
494 node = node->GetNext())
495 {
496 wxWindow *win = node->GetData ();
497 wxXSetBusyCursor (win, cursor);
498 }
499 }
500 }
501
502 // Restore cursor to normal
503 void wxEndBusyCursor()
504 {
505 if (wxBusyCursorCount == 0)
506 return;
507
508 wxBusyCursorCount--;
509 if (wxBusyCursorCount == 0)
510 {
511 for(wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst (); node;
512 node = node->GetNext())
513 {
514 wxWindow *win = node->GetData ();
515 wxXSetBusyCursor (win, NULL);
516 }
517 }
518 }
519
520 // true if we're between the above two calls
521 bool wxIsBusy()
522 {
523 return (wxBusyCursorCount > 0);
524 }