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"
24 #include "wx/window.h"
32 #pragma message disable nosimpint
35 #include <X11/cursorfont.h>
37 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 // Cursor for one display, so we can choose the correct one for
43 // the current display.
51 WX_DECLARE_LIST(wxXCursor
, wxXCursorList
);
52 #include "wx/listimpl.cpp"
53 WX_DEFINE_LIST(wxXCursorList
)
55 class WXDLLEXPORT wxCursorRefData
: public wxObjectRefData
57 friend class WXDLLEXPORT wxCursor
;
62 wxXCursorList m_cursors
; // wxXCursor objects, one per display
63 wxStockCursor m_cursorId
; // wxWidgets standard cursor id
66 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
67 #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData)
69 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxObject
)
71 wxCursorRefData::wxCursorRefData()
73 m_cursorId
= wxCURSOR_NONE
;
76 wxCursorRefData::~wxCursorRefData()
78 wxXCursorList::compatibility_iterator node
= m_cursors
.GetFirst();
81 wxXCursor
* c
= node
->GetData();
82 XFreeCursor((Display
*) c
->m_display
, (Cursor
) c
->m_cursor
);
84 node
= node
->GetNext();
93 wxCursor::wxCursor(const wxImage
& image
)
95 unsigned char * rgbBits
= image
.GetData();
96 int w
= image
.GetWidth() ;
97 int h
= image
.GetHeight();
98 bool bHasMask
= image
.HasMask();
99 int imagebitcount
= (w
*h
)/8;
101 unsigned char * bits
= new unsigned char [imagebitcount
];
102 unsigned char * maskBits
= new unsigned char [imagebitcount
];
105 unsigned char c
, cMask
;
106 for (i
=0; i
<imagebitcount
; i
++)
111 cMask
= 0xfe; // 11111110
114 // possible overflow if we do the summation first ?
115 c
= (unsigned char)(rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3);
116 // if average value is > mid grey
118 bits
[i
] = bits
[i
] & cMask
;
119 cMask
= (unsigned char)((cMask
<< 1) | 1);
126 r
= image
.GetMaskRed(),
127 g
= image
.GetMaskGreen(),
128 b
= image
.GetMaskBlue();
130 for (i
=0; i
<imagebitcount
; i
++)
138 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
139 maskBits
[i
] = maskBits
[i
] | cMask
;
140 cMask
= (unsigned char)(cMask
<< 1);
146 for (i
=0; i
<imagebitcount
; i
++)
153 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
154 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
158 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
159 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
163 if (hotSpotX
< 0 || hotSpotX
>= w
)
165 if (hotSpotY
< 0 || hotSpotY
>= h
)
168 Create( (const char*)bits
, w
, h
, hotSpotX
, hotSpotY
,
169 (const char*)maskBits
);
176 void wxCursor::Create(const char bits
[], int width
, int height
,
177 int hotSpotX
, int hotSpotY
, const char maskBits
[])
180 m_refData
= new wxCursorRefData
;
182 Display
*dpy
= (Display
*) wxGetDisplay();
183 int screen_num
= DefaultScreen (dpy
);
185 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
186 RootWindow (dpy
, screen_num
),
187 (char*) bits
, width
, height
,
190 Pixmap mask_pixmap
= None
;
191 if (maskBits
!= NULL
)
193 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
194 RootWindow (dpy
, screen_num
),
195 (char*) maskBits
, width
, height
,
199 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotSpotX
, hotSpotY
);
201 XFreePixmap( dpy
, pixmap
);
202 if (mask_pixmap
!= None
)
204 XFreePixmap( dpy
, mask_pixmap
);
208 void wxCursor::Create(WXPixmap pixmap
, WXPixmap mask_pixmap
,
209 int hotSpotX
, int hotSpotY
)
212 m_refData
= new wxCursorRefData
;
214 Display
*dpy
= (Display
*) wxGetDisplay();
215 int screen_num
= DefaultScreen (dpy
);
217 XColor foreground_color
;
218 XColor background_color
;
219 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
220 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
221 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
222 XQueryColor(dpy
, cmap
, &foreground_color
);
223 XQueryColor(dpy
, cmap
, &background_color
);
225 Cursor cursor
= XCreatePixmapCursor (dpy
,
235 wxXCursor
*c
= new wxXCursor
;
237 c
->m_cursor
= (WXCursor
) cursor
;
238 c
->m_display
= (WXDisplay
*) dpy
;
239 M_CURSORDATA
->m_cursors
.Append(c
);
243 wxCursor::wxCursor(const char bits
[], int width
, int height
,
244 int hotSpotX
, int hotSpotY
, const char maskBits
[])
246 Create(bits
, width
, height
, hotSpotX
, hotSpotY
, maskBits
);
249 wxCursor::wxCursor(const wxString
& name
, long flags
, int hotSpotX
, int hotSpotY
)
251 // Must be an XBM file
252 if (flags
!= wxBITMAP_TYPE_XBM
)
255 m_refData
= new wxCursorRefData
;
257 int hotX
= -1, hotY
= -1;
259 Pixmap pixmap
= None
, mask_pixmap
= None
;
261 Display
*dpy
= (Display
*) wxGetDisplay();
262 int screen_num
= DefaultScreen (dpy
);
264 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, screen_num
),
265 wxConstCast(name
.c_str(), char),
266 &w
, &h
, &pixmap
, &hotX
, &hotY
);
268 if (value
== BitmapSuccess
)
270 // TODO: how do we determine whether hotX, hotY were read correctly?
271 if (hotX
< 0 || hotY
< 0)
276 if (hotX
< 0 || hotY
< 0)
282 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotX
, hotY
);
284 XFreePixmap( dpy
, pixmap
);
288 // Cursors by stock number
289 wxCursor::wxCursor(wxStockCursor id
)
291 m_refData
= new wxCursorRefData
;
292 M_CURSORDATA
->m_cursorId
= id
;
295 wxCursor::~wxCursor()
299 bool wxCursor::Ok() const
301 return m_refData
!= NULL
;
304 // Motif-specific: create/get a cursor for the current display
305 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
) const
309 wxXCursorList::compatibility_iterator node
= M_CURSORDATA
->m_cursors
.GetFirst();
312 wxXCursor
* c
= node
->GetData();
313 if (c
->m_display
== display
)
315 node
= node
->GetNext();
318 // No cursor for this display, so let's see if we're an id-type cursor.
320 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
322 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
325 wxXCursor
* c
= new wxXCursor
;
326 c
->m_cursor
= cursor
;
327 c
->m_display
= display
;
328 M_CURSORDATA
->m_cursors
.Append(c
);
335 // Not an id-type cursor, so we don't know how to create it.
339 // Make a cursor from standard id
340 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
) const
342 Display
* dpy
= (Display
*) display
;
343 Cursor cursor
= (Cursor
) 0;
348 case wxCURSOR_CHAR
: return (WXCursor
)cursor
;
350 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
351 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
352 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
353 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
354 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
355 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
356 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
357 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
358 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
359 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
360 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
361 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
362 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
363 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
364 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
365 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
366 case wxCURSOR_SIZENWSE
:
367 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
368 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
369 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
370 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
371 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
372 // (JD Huggins) added more stock cursors for X
373 // X-only cursors BEGIN
374 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
375 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
376 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
377 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
386 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
388 gcv
.function
= GXxor
;
401 cursor
= XCreatePixmapCursor (dpy
,
411 default: x_cur
= XC_top_left_arrow
; break;
415 return (WXCursor
)cursor
;
417 cursor
= XCreateFontCursor (dpy
, x_cur
);
418 return (WXCursor
) cursor
;
421 // Global cursor setting
422 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
424 // Nothing to do for Motif (no global cursor)
428 // ----------------------------------------------------------------------------
430 // ----------------------------------------------------------------------------
432 static int wxBusyCursorCount
= 0;
436 wxXSetBusyCursor (wxWindow
* win
, const wxCursor
* cursor
)
438 Display
*display
= (Display
*) win
->GetXDisplay();
440 Window xwin
= (Window
) win
->GetXWindow();
444 XSetWindowAttributes attrs
;
448 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
452 // Restore old cursor
453 if (win
->GetCursor().Ok())
454 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
459 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
463 for(wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst (); node
;
464 node
= node
->GetNext())
466 wxWindow
*child
= node
->GetData ();
467 wxXSetBusyCursor (child
, cursor
);
471 // Set the cursor to the busy cursor for all windows
472 void wxBeginBusyCursor(const wxCursor
*cursor
)
475 if (wxBusyCursorCount
== 1)
477 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
478 node
= node
->GetNext())
480 wxWindow
*win
= node
->GetData ();
481 wxXSetBusyCursor (win
, cursor
);
486 // Restore cursor to normal
487 void wxEndBusyCursor()
489 if (wxBusyCursorCount
== 0)
493 if (wxBusyCursorCount
== 0)
495 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
496 node
= node
->GetNext())
498 wxWindow
*win
= node
->GetData ();
499 wxXSetBusyCursor (win
, NULL
);
504 // true if we're between the above two calls
507 return (wxBusyCursorCount
> 0);