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"
15 #include "wx/cursor.h"
19 #include "wx/window.h"
25 #pragma message disable nosimpint
28 #include <X11/cursorfont.h>
30 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 // Cursor for one display, so we can choose the correct one for
36 // the current display.
44 WX_DECLARE_LIST(wxXCursor
, wxXCursorList
);
45 #include "wx/listimpl.cpp"
46 WX_DEFINE_LIST(wxXCursorList
);
48 class WXDLLEXPORT wxCursorRefData
: public wxObjectRefData
50 friend class WXDLLEXPORT wxCursor
;
55 wxXCursorList m_cursors
; // wxXCursor objects, one per display
56 wxStockCursor m_cursorId
; // wxWidgets standard cursor id
59 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
60 #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData)
62 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxObject
)
64 wxCursorRefData::wxCursorRefData()
66 m_cursorId
= wxCURSOR_NONE
;
69 wxCursorRefData::~wxCursorRefData()
71 wxXCursorList::compatibility_iterator node
= m_cursors
.GetFirst();
74 wxXCursor
* c
= node
->GetData();
75 XFreeCursor((Display
*) c
->m_display
, (Cursor
) c
->m_cursor
);
77 node
= node
->GetNext();
86 wxCursor::wxCursor(const wxImage
& image
)
88 unsigned char * rgbBits
= image
.GetData();
89 int w
= image
.GetWidth() ;
90 int h
= image
.GetHeight();
91 bool bHasMask
= image
.HasMask();
92 int imagebitcount
= (w
*h
)/8;
94 unsigned char * bits
= new unsigned char [imagebitcount
];
95 unsigned char * maskBits
= new unsigned char [imagebitcount
];
98 unsigned char c
, cMask
;
99 for (i
=0; i
<imagebitcount
; i
++)
104 cMask
= 0xfe; // 11111110
107 // possible overflow if we do the summation first ?
108 c
= (unsigned char)(rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3);
109 // if average value is > mid grey
111 bits
[i
] = bits
[i
] & cMask
;
112 cMask
= (unsigned char)((cMask
<< 1) | 1);
119 r
= image
.GetMaskRed(),
120 g
= image
.GetMaskGreen(),
121 b
= image
.GetMaskBlue();
123 for (i
=0; i
<imagebitcount
; i
++)
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
= (unsigned char)(cMask
<< 1);
139 for (i
=0; i
<imagebitcount
; i
++)
146 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
147 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
151 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
152 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
156 if (hotSpotX
< 0 || hotSpotX
>= w
)
158 if (hotSpotY
< 0 || hotSpotY
>= h
)
161 Create( (const char*)bits
, w
, h
, hotSpotX
, hotSpotY
,
162 (const char*)maskBits
);
169 void wxCursor::Create(const char bits
[], int width
, int height
,
170 int hotSpotX
, int hotSpotY
, const char maskBits
[])
173 m_refData
= new wxCursorRefData
;
175 Display
*dpy
= (Display
*) wxGetDisplay();
176 int screen_num
= DefaultScreen (dpy
);
178 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
179 RootWindow (dpy
, screen_num
),
180 (char*) bits
, width
, height
,
183 Pixmap mask_pixmap
= None
;
184 if (maskBits
!= NULL
)
186 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
187 RootWindow (dpy
, screen_num
),
188 (char*) maskBits
, width
, height
,
192 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotSpotX
, hotSpotY
);
194 XFreePixmap( dpy
, pixmap
);
195 if (mask_pixmap
!= None
)
197 XFreePixmap( dpy
, mask_pixmap
);
201 void wxCursor::Create(WXPixmap pixmap
, WXPixmap mask_pixmap
,
202 int hotSpotX
, int hotSpotY
)
205 m_refData
= new wxCursorRefData
;
207 Display
*dpy
= (Display
*) wxGetDisplay();
208 int screen_num
= DefaultScreen (dpy
);
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
);
218 Cursor cursor
= XCreatePixmapCursor (dpy
,
228 wxXCursor
*c
= new wxXCursor
;
230 c
->m_cursor
= (WXCursor
) cursor
;
231 c
->m_display
= (WXDisplay
*) dpy
;
232 M_CURSORDATA
->m_cursors
.Append(c
);
236 wxCursor::wxCursor(const char bits
[], int width
, int height
,
237 int hotSpotX
, int hotSpotY
, const char maskBits
[])
239 Create(bits
, width
, height
, hotSpotX
, hotSpotY
, maskBits
);
242 wxCursor::wxCursor(const wxString
& name
, long flags
, int hotSpotX
, int hotSpotY
)
244 // Must be an XBM file
245 if (flags
!= wxBITMAP_TYPE_XBM
)
248 m_refData
= new wxCursorRefData
;
250 int hotX
= -1, hotY
= -1;
252 Pixmap pixmap
= None
, mask_pixmap
= None
;
254 Display
*dpy
= (Display
*) wxGetDisplay();
255 int screen_num
= DefaultScreen (dpy
);
257 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, screen_num
),
258 wxConstCast(name
.c_str(), char),
259 &w
, &h
, &pixmap
, &hotX
, &hotY
);
261 if (value
== BitmapSuccess
)
263 // TODO: how do we determine whether hotX, hotY were read correctly?
264 if (hotX
< 0 || hotY
< 0)
269 if (hotX
< 0 || hotY
< 0)
275 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotX
, hotY
);
277 XFreePixmap( dpy
, pixmap
);
281 // Cursors by stock number
282 wxCursor::wxCursor(wxStockCursor id
)
284 m_refData
= new wxCursorRefData
;
285 M_CURSORDATA
->m_cursorId
= id
;
288 wxCursor::~wxCursor()
292 bool wxCursor::Ok() const
294 return m_refData
!= NULL
;
297 // Motif-specific: create/get a cursor for the current display
298 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
) const
302 wxXCursorList::compatibility_iterator node
= M_CURSORDATA
->m_cursors
.GetFirst();
305 wxXCursor
* c
= node
->GetData();
306 if (c
->m_display
== display
)
308 node
= node
->GetNext();
311 // No cursor for this display, so let's see if we're an id-type cursor.
313 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
315 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
318 wxXCursor
* c
= new wxXCursor
;
319 c
->m_cursor
= cursor
;
320 c
->m_display
= display
;
321 M_CURSORDATA
->m_cursors
.Append(c
);
328 // Not an id-type cursor, so we don't know how to create it.
332 // Make a cursor from standard id
333 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
) const
335 Display
* dpy
= (Display
*) display
;
336 Cursor cursor
= (Cursor
) 0;
341 case wxCURSOR_CHAR
: return (WXCursor
)cursor
;
343 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
344 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
345 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
346 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
347 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
348 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
349 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
350 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
351 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
352 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
353 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
354 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
355 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
356 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
357 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
358 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
359 case wxCURSOR_SIZENWSE
:
360 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
361 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
362 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
363 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
364 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
365 // (JD Huggins) added more stock cursors for X
366 // X-only cursors BEGIN
367 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
368 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
369 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
370 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
379 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
381 gcv
.function
= GXxor
;
394 cursor
= XCreatePixmapCursor (dpy
,
404 default: x_cur
= XC_top_left_arrow
; break;
408 return (WXCursor
)cursor
;
410 cursor
= XCreateFontCursor (dpy
, x_cur
);
411 return (WXCursor
) cursor
;
414 // Global cursor setting
415 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
417 // Nothing to do for Motif (no global cursor)
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 static int wxBusyCursorCount
= 0;
429 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
431 Display
*display
= (Display
*) win
->GetXDisplay();
433 Window xwin
= (Window
) win
->GetXWindow();
437 XSetWindowAttributes attrs
;
441 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
445 // Restore old cursor
446 if (win
->GetCursor().Ok())
447 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
452 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
456 for(wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst (); node
;
457 node
= node
->GetNext())
459 wxWindow
*child
= node
->GetData ();
460 wxXSetBusyCursor (child
, cursor
);
464 // Set the cursor to the busy cursor for all windows
465 void wxBeginBusyCursor(wxCursor
*cursor
)
468 if (wxBusyCursorCount
== 1)
470 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
471 node
= node
->GetNext())
473 wxWindow
*win
= node
->GetData ();
474 wxXSetBusyCursor (win
, cursor
);
479 // Restore cursor to normal
480 void wxEndBusyCursor()
482 if (wxBusyCursorCount
== 0)
486 if (wxBusyCursorCount
== 0)
488 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
489 node
= node
->GetNext())
491 wxWindow
*win
= node
->GetData ();
492 wxXSetBusyCursor (win
, NULL
);
497 // true if we're between the above two calls
500 return (wxBusyCursorCount
> 0);