1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/cursor.cpp
3 // Purpose: wxCursor class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/cursor.h"
22 #include "wx/window.h"
29 #pragma message disable nosimpint
32 #include <X11/cursorfont.h>
34 #pragma message enable nosimpint
37 #include "wx/motif/private.h"
39 // Cursor for one display, so we can choose the correct one for
40 // the current display.
48 WX_DECLARE_LIST(wxXCursor
, wxXCursorList
);
49 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST(wxXCursorList
)
52 class WXDLLEXPORT wxCursorRefData
: public wxObjectRefData
54 friend class WXDLLEXPORT wxCursor
;
59 wxXCursorList m_cursors
; // wxXCursor objects, one per display
60 wxStockCursor m_cursorId
; // wxWidgets standard cursor id
63 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
64 #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData)
66 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxObject
)
68 wxCursorRefData::wxCursorRefData()
70 m_cursorId
= wxCURSOR_NONE
;
73 wxCursorRefData::~wxCursorRefData()
75 wxXCursorList::compatibility_iterator node
= m_cursors
.GetFirst();
78 wxXCursor
* c
= node
->GetData();
79 XFreeCursor((Display
*) c
->m_display
, (Cursor
) c
->m_cursor
);
81 node
= node
->GetNext();
90 wxCursor::wxCursor(const wxImage
& image
)
92 unsigned char * rgbBits
= image
.GetData();
93 int w
= image
.GetWidth() ;
94 int h
= image
.GetHeight();
95 bool bHasMask
= image
.HasMask();
96 int imagebitcount
= (w
*h
)/8;
98 unsigned char * bits
= new unsigned char [imagebitcount
];
99 unsigned char * maskBits
= new unsigned char [imagebitcount
];
102 unsigned char c
, cMask
;
103 for (i
=0; i
<imagebitcount
; i
++)
108 cMask
= 0xfe; // 11111110
111 // possible overflow if we do the summation first ?
112 c
= (unsigned char)(rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3);
113 // if average value is > mid grey
115 bits
[i
] = bits
[i
] & cMask
;
116 cMask
= (unsigned char)((cMask
<< 1) | 1);
123 r
= image
.GetMaskRed(),
124 g
= image
.GetMaskGreen(),
125 b
= image
.GetMaskBlue();
127 for (i
=0; i
<imagebitcount
; i
++)
135 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
136 maskBits
[i
] = maskBits
[i
] | cMask
;
137 cMask
= (unsigned char)(cMask
<< 1);
143 for (i
=0; i
<imagebitcount
; i
++)
150 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
151 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
155 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
156 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
160 if (hotSpotX
< 0 || hotSpotX
>= w
)
162 if (hotSpotY
< 0 || hotSpotY
>= h
)
165 Create( (const char*)bits
, w
, h
, hotSpotX
, hotSpotY
,
166 (const char*)maskBits
);
173 void wxCursor::Create(const char bits
[], int width
, int height
,
174 int hotSpotX
, int hotSpotY
, const char maskBits
[])
177 m_refData
= new wxCursorRefData
;
179 Display
*dpy
= (Display
*) wxGetDisplay();
180 int screen_num
= DefaultScreen (dpy
);
182 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
183 RootWindow (dpy
, screen_num
),
184 (char*) bits
, width
, height
,
187 Pixmap mask_pixmap
= None
;
188 if (maskBits
!= NULL
)
190 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
191 RootWindow (dpy
, screen_num
),
192 (char*) maskBits
, width
, height
,
196 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotSpotX
, hotSpotY
);
198 XFreePixmap( dpy
, pixmap
);
199 if (mask_pixmap
!= None
)
201 XFreePixmap( dpy
, mask_pixmap
);
205 void wxCursor::Create(WXPixmap pixmap
, WXPixmap mask_pixmap
,
206 int hotSpotX
, int hotSpotY
)
209 m_refData
= new wxCursorRefData
;
211 Display
*dpy
= (Display
*) wxGetDisplay();
212 int screen_num
= DefaultScreen (dpy
);
214 XColor foreground_color
;
215 XColor background_color
;
216 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
217 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
218 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
219 XQueryColor(dpy
, cmap
, &foreground_color
);
220 XQueryColor(dpy
, cmap
, &background_color
);
222 Cursor cursor
= XCreatePixmapCursor (dpy
,
232 wxXCursor
*c
= new wxXCursor
;
234 c
->m_cursor
= (WXCursor
) cursor
;
235 c
->m_display
= (WXDisplay
*) dpy
;
236 M_CURSORDATA
->m_cursors
.Append(c
);
240 wxCursor::wxCursor(const char bits
[], int width
, int height
,
241 int hotSpotX
, int hotSpotY
, const char maskBits
[])
243 Create(bits
, width
, height
, hotSpotX
, hotSpotY
, maskBits
);
246 wxCursor::wxCursor(const wxString
& name
, long flags
, int hotSpotX
, int hotSpotY
)
248 // Must be an XBM file
249 if (flags
!= wxBITMAP_TYPE_XBM
)
252 m_refData
= new wxCursorRefData
;
254 int hotX
= -1, hotY
= -1;
256 Pixmap pixmap
= None
, mask_pixmap
= None
;
258 Display
*dpy
= (Display
*) wxGetDisplay();
259 int screen_num
= DefaultScreen (dpy
);
261 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, screen_num
),
262 wxConstCast(name
.c_str(), char),
263 &w
, &h
, &pixmap
, &hotX
, &hotY
);
265 if (value
== BitmapSuccess
)
267 // TODO: how do we determine whether hotX, hotY were read correctly?
268 if (hotX
< 0 || hotY
< 0)
273 if (hotX
< 0 || hotY
< 0)
279 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotX
, hotY
);
281 XFreePixmap( dpy
, pixmap
);
285 // Cursors by stock number
286 wxCursor::wxCursor(wxStockCursor id
)
288 m_refData
= new wxCursorRefData
;
289 M_CURSORDATA
->m_cursorId
= id
;
292 wxCursor::~wxCursor()
296 bool wxCursor::Ok() const
298 return m_refData
!= NULL
;
301 // Motif-specific: create/get a cursor for the current display
302 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
) const
306 wxXCursorList::compatibility_iterator node
= M_CURSORDATA
->m_cursors
.GetFirst();
309 wxXCursor
* c
= node
->GetData();
310 if (c
->m_display
== display
)
312 node
= node
->GetNext();
315 // No cursor for this display, so let's see if we're an id-type cursor.
317 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
319 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
322 wxXCursor
* c
= new wxXCursor
;
323 c
->m_cursor
= cursor
;
324 c
->m_display
= display
;
325 M_CURSORDATA
->m_cursors
.Append(c
);
332 // Not an id-type cursor, so we don't know how to create it.
336 // Make a cursor from standard id
337 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
) const
339 Display
* dpy
= (Display
*) display
;
340 Cursor cursor
= (Cursor
) 0;
345 case wxCURSOR_CHAR
: return (WXCursor
)cursor
;
347 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
348 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
349 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
350 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
351 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
352 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
353 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
354 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
355 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
356 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
357 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
358 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
359 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
360 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
361 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
362 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
363 case wxCURSOR_SIZENWSE
:
364 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
365 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
366 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
367 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
368 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
369 // (JD Huggins) added more stock cursors for X
370 // X-only cursors BEGIN
371 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
372 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
373 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
374 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
383 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
385 gcv
.function
= GXxor
;
398 cursor
= XCreatePixmapCursor (dpy
,
408 default: x_cur
= XC_top_left_arrow
; break;
412 return (WXCursor
)cursor
;
414 cursor
= XCreateFontCursor (dpy
, x_cur
);
415 return (WXCursor
) cursor
;
418 // Global cursor setting
419 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
421 // Nothing to do for Motif (no global cursor)
425 // ----------------------------------------------------------------------------
427 // ----------------------------------------------------------------------------
429 static int wxBusyCursorCount
= 0;
433 wxXSetBusyCursor (wxWindow
* win
, const wxCursor
* cursor
)
435 Display
*display
= (Display
*) win
->GetXDisplay();
437 Window xwin
= (Window
) win
->GetXWindow();
441 XSetWindowAttributes attrs
;
445 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
449 // Restore old cursor
450 if (win
->GetCursor().Ok())
451 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
456 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
460 for(wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst (); node
;
461 node
= node
->GetNext())
463 wxWindow
*child
= node
->GetData ();
464 wxXSetBusyCursor (child
, cursor
);
468 // Set the cursor to the busy cursor for all windows
469 void wxBeginBusyCursor(const wxCursor
*cursor
)
472 if (wxBusyCursorCount
== 1)
474 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
475 node
= node
->GetNext())
477 wxWindow
*win
= node
->GetData ();
478 wxXSetBusyCursor (win
, cursor
);
483 // Restore cursor to normal
484 void wxEndBusyCursor()
486 if (wxBusyCursorCount
== 0)
490 if (wxBusyCursorCount
== 0)
492 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
493 node
= node
->GetNext())
495 wxWindow
*win
= node
->GetData ();
496 wxXSetBusyCursor (win
, NULL
);
501 // true if we're between the above two calls
504 return (wxBusyCursorCount
> 0);