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"
26 #include "wx/window.h"
33 #pragma message disable nosimpint
36 #include <X11/cursorfont.h>
38 #pragma message enable nosimpint
41 #include "wx/motif/private.h"
43 // Cursor for one display, so we can choose the correct one for
44 // the current display.
52 WX_DECLARE_LIST(wxXCursor
, wxXCursorList
);
53 #include "wx/listimpl.cpp"
54 WX_DEFINE_LIST(wxXCursorList
)
56 class WXDLLEXPORT wxCursorRefData
: public wxObjectRefData
58 friend class WXDLLEXPORT wxCursor
;
63 wxXCursorList m_cursors
; // wxXCursor objects, one per display
64 wxStockCursor m_cursorId
; // wxWidgets standard cursor id
67 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
68 #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData)
70 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxObject
)
72 wxCursorRefData::wxCursorRefData()
74 m_cursorId
= wxCURSOR_NONE
;
77 wxCursorRefData::~wxCursorRefData()
79 wxXCursorList::compatibility_iterator node
= m_cursors
.GetFirst();
82 wxXCursor
* c
= node
->GetData();
83 XFreeCursor((Display
*) c
->m_display
, (Cursor
) c
->m_cursor
);
85 node
= node
->GetNext();
94 wxCursor::wxCursor(const wxImage
& image
)
96 unsigned char * rgbBits
= image
.GetData();
97 int w
= image
.GetWidth() ;
98 int h
= image
.GetHeight();
99 bool bHasMask
= image
.HasMask();
100 int imagebitcount
= (w
*h
)/8;
102 unsigned char * bits
= new unsigned char [imagebitcount
];
103 unsigned char * maskBits
= new unsigned char [imagebitcount
];
106 unsigned char c
, cMask
;
107 for (i
=0; i
<imagebitcount
; i
++)
112 cMask
= 0xfe; // 11111110
115 // possible overflow if we do the summation first ?
116 c
= (unsigned char)(rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3);
117 // if average value is > mid grey
119 bits
[i
] = bits
[i
] & cMask
;
120 cMask
= (unsigned char)((cMask
<< 1) | 1);
127 r
= image
.GetMaskRed(),
128 g
= image
.GetMaskGreen(),
129 b
= image
.GetMaskBlue();
131 for (i
=0; i
<imagebitcount
; i
++)
139 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
140 maskBits
[i
] = maskBits
[i
] | cMask
;
141 cMask
= (unsigned char)(cMask
<< 1);
147 for (i
=0; i
<imagebitcount
; i
++)
154 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
155 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
159 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
160 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
164 if (hotSpotX
< 0 || hotSpotX
>= w
)
166 if (hotSpotY
< 0 || hotSpotY
>= h
)
169 Create( (const char*)bits
, w
, h
, hotSpotX
, hotSpotY
,
170 (const char*)maskBits
);
177 void wxCursor::Create(const char bits
[], int width
, int height
,
178 int hotSpotX
, int hotSpotY
, const char maskBits
[])
181 m_refData
= new wxCursorRefData
;
183 Display
*dpy
= (Display
*) wxGetDisplay();
184 int screen_num
= DefaultScreen (dpy
);
186 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
187 RootWindow (dpy
, screen_num
),
188 (char*) bits
, width
, height
,
191 Pixmap mask_pixmap
= None
;
192 if (maskBits
!= NULL
)
194 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
195 RootWindow (dpy
, screen_num
),
196 (char*) maskBits
, width
, height
,
200 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotSpotX
, hotSpotY
);
202 XFreePixmap( dpy
, pixmap
);
203 if (mask_pixmap
!= None
)
205 XFreePixmap( dpy
, mask_pixmap
);
209 void wxCursor::Create(WXPixmap pixmap
, WXPixmap mask_pixmap
,
210 int hotSpotX
, int hotSpotY
)
213 m_refData
= new wxCursorRefData
;
215 Display
*dpy
= (Display
*) wxGetDisplay();
216 int screen_num
= DefaultScreen (dpy
);
218 XColor foreground_color
;
219 XColor background_color
;
220 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
221 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
222 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
223 XQueryColor(dpy
, cmap
, &foreground_color
);
224 XQueryColor(dpy
, cmap
, &background_color
);
226 Cursor cursor
= XCreatePixmapCursor (dpy
,
236 wxXCursor
*c
= new wxXCursor
;
238 c
->m_cursor
= (WXCursor
) cursor
;
239 c
->m_display
= (WXDisplay
*) dpy
;
240 M_CURSORDATA
->m_cursors
.Append(c
);
244 wxCursor::wxCursor(const char bits
[], int width
, int height
,
245 int hotSpotX
, int hotSpotY
, const char maskBits
[])
247 Create(bits
, width
, height
, hotSpotX
, hotSpotY
, maskBits
);
250 wxCursor::wxCursor(const wxString
& name
, long flags
, int hotSpotX
, int hotSpotY
)
252 // Must be an XBM file
253 if (flags
!= wxBITMAP_TYPE_XBM
)
256 m_refData
= new wxCursorRefData
;
258 int hotX
= -1, hotY
= -1;
260 Pixmap pixmap
= None
, mask_pixmap
= None
;
262 Display
*dpy
= (Display
*) wxGetDisplay();
263 int screen_num
= DefaultScreen (dpy
);
265 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, screen_num
),
266 wxConstCast(name
.c_str(), char),
267 &w
, &h
, &pixmap
, &hotX
, &hotY
);
269 if (value
== BitmapSuccess
)
271 // TODO: how do we determine whether hotX, hotY were read correctly?
272 if (hotX
< 0 || hotY
< 0)
277 if (hotX
< 0 || hotY
< 0)
283 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotX
, hotY
);
285 XFreePixmap( dpy
, pixmap
);
289 // Cursors by stock number
290 wxCursor::wxCursor(wxStockCursor id
)
292 m_refData
= new wxCursorRefData
;
293 M_CURSORDATA
->m_cursorId
= id
;
296 wxCursor::~wxCursor()
300 bool wxCursor::Ok() const
302 return m_refData
!= NULL
;
305 // Motif-specific: create/get a cursor for the current display
306 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
) const
310 wxXCursorList::compatibility_iterator node
= M_CURSORDATA
->m_cursors
.GetFirst();
313 wxXCursor
* c
= node
->GetData();
314 if (c
->m_display
== display
)
316 node
= node
->GetNext();
319 // No cursor for this display, so let's see if we're an id-type cursor.
321 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
323 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
326 wxXCursor
* c
= new wxXCursor
;
327 c
->m_cursor
= cursor
;
328 c
->m_display
= display
;
329 M_CURSORDATA
->m_cursors
.Append(c
);
336 // Not an id-type cursor, so we don't know how to create it.
340 // Make a cursor from standard id
341 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
) const
343 Display
* dpy
= (Display
*) display
;
344 Cursor cursor
= (Cursor
) 0;
349 case wxCURSOR_CHAR
: return (WXCursor
)cursor
;
351 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
352 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
353 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
354 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
355 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
356 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
357 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
358 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
359 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
360 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
361 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
362 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
363 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
364 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
365 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
366 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
367 case wxCURSOR_SIZENWSE
:
368 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
369 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
370 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
371 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
372 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
373 // (JD Huggins) added more stock cursors for X
374 // X-only cursors BEGIN
375 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
376 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
377 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
378 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
387 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
389 gcv
.function
= GXxor
;
402 cursor
= XCreatePixmapCursor (dpy
,
412 default: x_cur
= XC_top_left_arrow
; break;
416 return (WXCursor
)cursor
;
418 cursor
= XCreateFontCursor (dpy
, x_cur
);
419 return (WXCursor
) cursor
;
422 // Global cursor setting
423 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
425 // Nothing to do for Motif (no global cursor)
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 static int wxBusyCursorCount
= 0;
437 wxXSetBusyCursor (wxWindow
* win
, const wxCursor
* cursor
)
439 Display
*display
= (Display
*) win
->GetXDisplay();
441 Window xwin
= (Window
) win
->GetXWindow();
445 XSetWindowAttributes attrs
;
449 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
453 // Restore old cursor
454 if (win
->GetCursor().Ok())
455 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
460 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
464 for(wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst (); node
;
465 node
= node
->GetNext())
467 wxWindow
*child
= node
->GetData ();
468 wxXSetBusyCursor (child
, cursor
);
472 // Set the cursor to the busy cursor for all windows
473 void wxBeginBusyCursor(const wxCursor
*cursor
)
476 if (wxBusyCursorCount
== 1)
478 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
479 node
= node
->GetNext())
481 wxWindow
*win
= node
->GetData ();
482 wxXSetBusyCursor (win
, cursor
);
487 // Restore cursor to normal
488 void wxEndBusyCursor()
490 if (wxBusyCursorCount
== 0)
494 if (wxBusyCursorCount
== 0)
496 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
497 node
= node
->GetNext())
499 wxWindow
*win
= node
->GetData ();
500 wxXSetBusyCursor (win
, NULL
);
505 // true if we're between the above two calls
508 return (wxBusyCursorCount
> 0);