1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCursor class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "cursor.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #include "wx/cursor.h"
23 #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
];
101 int i
, j
, i8
; unsigned char c
, cMask
;
102 for (i
=0; i
<imagebitcount
; i
++)
107 cMask
= 0xfe; // 11111110
110 // possible overflow if we do the summation first ?
111 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
112 //if average value is > mid grey
114 bits
[i
] = bits
[i
] & cMask
;
115 cMask
= (cMask
<< 1) | 1;
122 r
= image
.GetMaskRed(),
123 g
= image
.GetMaskGreen(),
124 b
= image
.GetMaskBlue();
126 for (i
=0; i
<imagebitcount
; i
++)
134 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
135 maskBits
[i
] = maskBits
[i
] | cMask
;
136 cMask
= (cMask
<< 1);
142 for (i
=0; i
<imagebitcount
; i
++)
149 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
150 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
154 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
155 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
159 if (hotSpotX
< 0 || hotSpotX
>= w
)
161 if (hotSpotY
< 0 || hotSpotY
>= h
)
164 Create( (const char*)bits
, w
, h
, hotSpotX
, hotSpotY
,
165 (const char*)maskBits
);
172 void wxCursor::Create(const char bits
[], int width
, int height
,
173 int hotSpotX
, int hotSpotY
, const char maskBits
[])
176 m_refData
= new wxCursorRefData
;
178 Display
*dpy
= (Display
*) wxGetDisplay();
179 int screen_num
= DefaultScreen (dpy
);
181 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
182 RootWindow (dpy
, screen_num
),
183 (char*) bits
, width
, height
,
186 Pixmap mask_pixmap
= None
;
187 if (maskBits
!= NULL
)
189 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
190 RootWindow (dpy
, screen_num
),
191 (char*) maskBits
, width
, height
,
195 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotSpotX
, hotSpotY
);
197 XFreePixmap( dpy
, pixmap
);
198 if (mask_pixmap
!= None
)
200 XFreePixmap( dpy
, mask_pixmap
);
204 void wxCursor::Create(WXPixmap pixmap
, WXPixmap mask_pixmap
,
205 int hotSpotX
, int hotSpotY
)
208 m_refData
= new wxCursorRefData
;
210 Display
*dpy
= (Display
*) wxGetDisplay();
211 int screen_num
= DefaultScreen (dpy
);
213 XColor foreground_color
;
214 XColor background_color
;
215 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
216 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
217 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
218 XQueryColor(dpy
, cmap
, &foreground_color
);
219 XQueryColor(dpy
, cmap
, &background_color
);
221 Cursor cursor
= XCreatePixmapCursor (dpy
,
231 wxXCursor
*c
= new wxXCursor
;
233 c
->m_cursor
= (WXCursor
) cursor
;
234 c
->m_display
= (WXDisplay
*) dpy
;
235 M_CURSORDATA
->m_cursors
.Append(c
);
239 wxCursor::wxCursor(const char bits
[], int width
, int height
,
240 int hotSpotX
, int hotSpotY
, const char maskBits
[])
242 Create(bits
, width
, height
, hotSpotX
, hotSpotY
, maskBits
);
245 wxCursor::wxCursor(const wxString
& name
, long flags
, int hotSpotX
, int hotSpotY
)
247 // Must be an XBM file
248 if (flags
!= wxBITMAP_TYPE_XBM
)
251 m_refData
= new wxCursorRefData
;
253 int hotX
= -1, hotY
= -1;
255 Pixmap pixmap
= None
, mask_pixmap
= None
;
257 Display
*dpy
= (Display
*) wxGetDisplay();
258 int screen_num
= DefaultScreen (dpy
);
260 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, screen_num
),
261 wxConstCast(name
.c_str(), char),
262 &w
, &h
, &pixmap
, &hotX
, &hotY
);
264 if (value
== BitmapSuccess
)
266 // TODO: how do we determine whether hotX, hotY were read correctly?
267 if (hotX
< 0 || hotY
< 0)
272 if (hotX
< 0 || hotY
< 0)
278 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotX
, hotY
);
280 XFreePixmap( dpy
, pixmap
);
284 // Cursors by stock number
285 wxCursor::wxCursor(wxStockCursor id
)
287 m_refData
= new wxCursorRefData
;
288 M_CURSORDATA
->m_cursorId
= id
;
291 wxCursor::~wxCursor()
295 bool wxCursor::Ok() const
297 return m_refData
!= NULL
;
300 // Motif-specific: create/get a cursor for the current display
301 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
)
305 wxXCursorList::compatibility_iterator node
= M_CURSORDATA
->m_cursors
.GetFirst();
308 wxXCursor
* c
= node
->GetData();
309 if (c
->m_display
== display
)
311 node
= node
->GetNext();
314 // No cursor for this display, so let's see if we're an id-type cursor.
316 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
318 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
321 wxXCursor
* c
= new wxXCursor
;
322 c
->m_cursor
= cursor
;
323 c
->m_display
= display
;
324 M_CURSORDATA
->m_cursors
.Append(c
);
331 // Not an id-type cursor, so we don't know how to create it.
335 // Make a cursor from standard id
336 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
)
338 Display
* dpy
= (Display
*) display
;
339 Cursor cursor
= (Cursor
) 0;
344 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
345 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
346 case wxCURSOR_CHAR
: return (WXCursor
)cursor
; break;
347 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
348 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
349 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
350 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
351 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
352 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
353 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
354 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
355 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
356 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
357 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
358 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
359 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
360 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
361 case wxCURSOR_SIZENWSE
:
362 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
363 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
364 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
365 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
366 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
367 // (JD Huggins) added more stock cursors for X
368 // X-only cursors BEGIN
369 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
370 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
371 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
372 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
381 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
383 gcv
.function
= GXxor
;
396 cursor
= XCreatePixmapCursor (dpy
,
406 default: x_cur
= XC_top_left_arrow
; break;
410 return (WXCursor
)cursor
;
412 cursor
= XCreateFontCursor (dpy
, x_cur
);
413 return (WXCursor
) cursor
;
416 // Global cursor setting
417 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
419 // Nothing to do for Motif (no global cursor)
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 static int wxBusyCursorCount
= 0;
431 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
433 Display
*display
= (Display
*) win
->GetXDisplay();
435 Window xwin
= (Window
) win
->GetXWindow();
439 XSetWindowAttributes attrs
;
443 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
447 // Restore old cursor
448 if (win
->GetCursor().Ok())
449 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
454 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
458 for(wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst (); node
;
459 node
= node
->GetNext())
461 wxWindow
*child
= node
->GetData ();
462 wxXSetBusyCursor (child
, cursor
);
466 // Set the cursor to the busy cursor for all windows
467 void wxBeginBusyCursor(wxCursor
*cursor
)
470 if (wxBusyCursorCount
== 1)
472 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
473 node
= node
->GetNext())
475 wxWindow
*win
= node
->GetData ();
476 wxXSetBusyCursor (win
, cursor
);
481 // Restore cursor to normal
482 void wxEndBusyCursor()
484 if (wxBusyCursorCount
== 0)
488 if (wxBusyCursorCount
== 0)
490 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
491 node
= node
->GetNext())
493 wxWindow
*win
= node
->GetData ();
494 wxXSetBusyCursor (win
, NULL
);
499 // TRUE if we're between the above two calls
502 return (wxBusyCursorCount
> 0);