1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCursor class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "cursor.h"
16 #include "wx/cursor.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
; // wxWindows 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::Node
* 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
];
97 int i
, j
, i8
; unsigned char c
, cMask
;
98 for (i
=0; i
<imagebitcount
; i
++)
106 // possible overflow if we do the summation first ?
107 c
= rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3;
108 //if average value is > mid grey
110 bits
[i
] = bits
[i
] | cMask
;
118 r
= image
.GetMaskRed(),
119 g
= image
.GetMaskGreen(),
120 b
= image
.GetMaskBlue();
122 for (i
=0; i
<imagebitcount
; i
++)
130 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
131 maskBits
[i
] = maskBits
[i
] | cMask
;
138 for (i
=0; i
<imagebitcount
; i
++)
145 if (image
.HasOption(wxCUR_HOTSPOT_X
))
146 hotSpotX
= image
.GetOptionInt(wxCUR_HOTSPOT_X
);
150 if (image
.HasOption(wxCUR_HOTSPOT_Y
))
151 hotSpotY
= image
.GetOptionInt(wxCUR_HOTSPOT_Y
);
155 if (hotSpotX
< 0 || hotSpotX
>= w
)
157 if (hotSpotY
< 0 || hotSpotY
>= h
)
160 m_refData
= new wxCursorRefData
;
162 Display
*dpy
= (Display
*) wxGetDisplay();
163 int screen_num
= DefaultScreen (dpy
);
165 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
166 RootWindow (dpy
, DefaultScreen(dpy
)),
170 Pixmap mask_pixmap
= None
;
171 if (maskBits
!= NULL
)
173 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
174 RootWindow (dpy
, DefaultScreen(dpy
)),
175 (char*) maskBits
, w
, h
,
179 XColor foreground_color
;
180 XColor background_color
;
181 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
182 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
183 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
184 XQueryColor(dpy
, cmap
, &foreground_color
);
185 XQueryColor(dpy
, cmap
, &background_color
);
187 Cursor cursor
= XCreatePixmapCursor (dpy
,
195 XFreePixmap( dpy
, pixmap
);
196 if (mask_pixmap
!= None
)
198 XFreePixmap( dpy
, mask_pixmap
);
203 wxXCursor
*c
= new wxXCursor
;
205 c
->m_cursor
= (WXCursor
) cursor
;
206 c
->m_display
= (WXDisplay
*) dpy
;
207 M_CURSORDATA
->m_cursors
.Append(c
);
212 wxCursor::wxCursor(const char bits
[], int width
, int height
,
213 int hotSpotX
, int hotSpotY
, const char maskBits
[])
215 m_refData
= new wxCursorRefData
;
217 Display
*dpy
= (Display
*) wxGetDisplay();
218 int screen_num
= DefaultScreen (dpy
);
220 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
221 RootWindow (dpy
, DefaultScreen(dpy
)),
222 (char*) bits
, width
, height
,
225 Pixmap mask_pixmap
= None
;
226 if (maskBits
!= NULL
)
228 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
229 RootWindow (dpy
, DefaultScreen(dpy
)),
230 (char*) maskBits
, width
, height
,
234 XColor foreground_color
;
235 XColor background_color
;
236 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
237 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
238 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
239 XQueryColor(dpy
, cmap
, &foreground_color
);
240 XQueryColor(dpy
, cmap
, &background_color
);
242 Cursor cursor
= XCreatePixmapCursor (dpy
,
250 XFreePixmap( dpy
, pixmap
);
251 if (mask_pixmap
!= None
)
253 XFreePixmap( dpy
, mask_pixmap
);
258 wxXCursor
*c
= new wxXCursor
;
260 c
->m_cursor
= (WXCursor
) cursor
;
261 c
->m_display
= (WXDisplay
*) dpy
;
262 M_CURSORDATA
->m_cursors
.Append(c
);
266 wxCursor::wxCursor(const wxString
& name
, long flags
, int hotSpotX
, int hotSpotY
)
268 // Must be an XBM file
269 if (flags
!= wxBITMAP_TYPE_XBM
)
272 m_refData
= new wxCursorRefData
;
274 int hotX
= -1, hotY
= -1;
278 Display
*dpy
= (Display
*) wxGetDisplay();
279 int screen_num
= DefaultScreen (dpy
);
281 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
282 wxConstCast(name
.c_str(), char),
283 &w
, &h
, &pixmap
, &hotX
, &hotY
);
285 if ((value
== BitmapFileInvalid
) ||
286 (value
== BitmapOpenFailed
) ||
287 (value
== BitmapNoMemory
))
292 XColor foreground_color
;
293 XColor background_color
;
294 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
295 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
296 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
297 XQueryColor(dpy
, cmap
, &foreground_color
);
298 XQueryColor(dpy
, cmap
, &background_color
);
300 // TODO: how do we determine whether hotX, hotY were read correctly?
301 if (hotX
< 0 || hotY
< 0)
306 if (hotX
< 0 || hotY
< 0)
312 Pixmap mask_pixmap
= None
;
313 Cursor cursor
= XCreatePixmapCursor (dpy
,
321 XFreePixmap( dpy
, pixmap
);
324 wxXCursor
*c
= new wxXCursor
;
326 c
->m_cursor
= (WXCursor
) cursor
;
327 c
->m_display
= (WXDisplay
*) dpy
;
328 M_CURSORDATA
->m_cursors
.Append(c
);
334 // Cursors by stock number
335 wxCursor::wxCursor(wxStockCursor id
)
337 m_refData
= new wxCursorRefData
;
338 M_CURSORDATA
->m_cursorId
= id
;
341 wxCursor::~wxCursor()
345 bool wxCursor::Ok() const
347 return m_refData
!= NULL
;
350 // Motif-specific: create/get a cursor for the current display
351 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
)
355 wxXCursorList::Node
* node
= M_CURSORDATA
->m_cursors
.GetFirst();
358 wxXCursor
* c
= node
->GetData();
359 if (c
->m_display
== display
)
361 node
= node
->GetNext();
364 // No cursor for this display, so let's see if we're an id-type cursor.
366 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
368 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
371 wxXCursor
* c
= new wxXCursor
;
372 c
->m_cursor
= cursor
;
373 c
->m_display
= display
;
374 M_CURSORDATA
->m_cursors
.Append(c
);
381 // Not an id-type cursor, so we don't know how to create it.
385 // Make a cursor from standard id
386 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
)
388 Display
* dpy
= (Display
*) display
;
389 Cursor cursor
= (Cursor
) 0;
394 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
395 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
396 case wxCURSOR_CHAR
: return (WXCursor
)cursor
; break;
397 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
398 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
399 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
400 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
401 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
402 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
403 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
404 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
405 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
406 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
407 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
408 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
409 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
410 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
411 case wxCURSOR_SIZENWSE
:
412 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
413 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
414 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
415 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
416 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
417 // (JD Huggins) added more stock cursors for X
418 // X-only cursors BEGIN
419 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
420 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
421 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
422 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
431 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
433 gcv
.function
= GXxor
;
446 cursor
= XCreatePixmapCursor (dpy
,
456 default: x_cur
= XC_top_left_arrow
; break;
460 return (WXCursor
)cursor
;
462 cursor
= XCreateFontCursor (dpy
, x_cur
);
463 return (WXCursor
) cursor
;
466 // Global cursor setting
467 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
469 // Nothing to do for Motif (no global cursor)
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 static int wxBusyCursorCount
= 0;
481 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
483 Display
*display
= (Display
*) win
->GetXDisplay();
485 Window xwin
= (Window
) win
->GetXWindow();
489 XSetWindowAttributes attrs
;
493 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
497 // Restore old cursor
498 if (win
->GetCursor().Ok())
499 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
504 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
508 for(wxWindowList::Node
*node
= win
->GetChildren().GetFirst (); node
;
509 node
= node
->GetNext())
511 wxWindow
*child
= node
->GetData ();
512 wxXSetBusyCursor (child
, cursor
);
516 // Set the cursor to the busy cursor for all windows
517 void wxBeginBusyCursor(wxCursor
*cursor
)
520 if (wxBusyCursorCount
== 1)
522 for(wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst (); node
;
523 node
= node
->GetNext())
525 wxWindow
*win
= node
->GetData ();
526 wxXSetBusyCursor (win
, cursor
);
531 // Restore cursor to normal
532 void wxEndBusyCursor()
534 if (wxBusyCursorCount
== 0)
538 if (wxBusyCursorCount
== 0)
540 for(wxWindowList::Node
*node
= wxTopLevelWindows
.GetFirst (); node
;
541 node
= node
->GetNext())
543 wxWindow
*win
= node
->GetData ();
544 wxXSetBusyCursor (win
, NULL
);
549 // TRUE if we're between the above two calls
552 return (wxBusyCursorCount
> 0);