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
;
340 WXDisplay
* display
= wxGetDisplay();
344 WXCursor cursor
= GetXCursor(display
);
347 wxCursor
::~wxCursor()
351 bool wxCursor
::Ok() const
353 return m_refData
!= NULL
;
356 // Motif-specific: create/get a cursor for the current display
357 WXCursor wxCursor
::GetXCursor(WXDisplay
* display
)
361 wxXCursorList
::Node
* node
= M_CURSORDATA
->m_cursors
.GetFirst();
364 wxXCursor
* c
= node
->GetData();
365 if (c
->m_display
== display
)
367 node
= node
->GetNext();
370 // No cursor for this display, so let's see if we're an id-type cursor.
372 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
374 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
377 wxXCursor
* c
= new wxXCursor
;
378 c
->m_cursor
= cursor
;
379 c
->m_display
= display
;
380 M_CURSORDATA
->m_cursors
.Append(c
);
387 // Not an id-type cursor, so we don't know how to create it.
391 // Make a cursor from standard id
392 WXCursor wxCursor
::MakeCursor(WXDisplay
* display
, wxStockCursor id
)
394 Display
* dpy
= (Display
*) display
;
395 Cursor cursor
= (Cursor
) 0;
400 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
401 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
402 case wxCURSOR_CHAR
: return (WXCursor
)cursor
; break;
403 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
404 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
405 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
406 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
407 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
408 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
409 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
410 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
411 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
412 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
413 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
414 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
415 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
416 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
417 case wxCURSOR_SIZENWSE
:
418 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
419 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
420 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
421 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
422 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
423 // (JD Huggins) added more stock cursors for X
424 // X-only cursors BEGIN
425 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
426 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
427 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
428 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
437 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
439 gcv
.function
= GXxor
;
452 cursor
= XCreatePixmapCursor (dpy
,
462 default: x_cur
= XC_top_left_arrow
; break;
466 return (WXCursor
)cursor
;
468 cursor
= XCreateFontCursor (dpy
, x_cur
);
469 return (WXCursor
) cursor
;
472 // Global cursor setting
473 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
475 // Nothing to do for Motif (no global cursor)
479 // ----------------------------------------------------------------------------
481 // ----------------------------------------------------------------------------
483 static int wxBusyCursorCount
= 0;
487 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
489 Display
*display
= (Display
*) win
->GetXDisplay();
491 Window xwin
= (Window
) win
->GetXWindow();
495 XSetWindowAttributes attrs
;
499 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
503 // Restore old cursor
504 if (win
->GetCursor().Ok())
505 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
510 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
514 for(wxWindowList
::Node
*node
= win
->GetChildren().GetFirst (); node
;
515 node
= node
->GetNext())
517 wxWindow
*child
= node
->GetData ();
518 wxXSetBusyCursor (child
, cursor
);
522 // Set the cursor to the busy cursor for all windows
523 void wxBeginBusyCursor(wxCursor
*cursor
)
526 if (wxBusyCursorCount
== 1)
528 for(wxWindowList
::Node
*node
= wxTopLevelWindows
.GetFirst (); node
;
529 node
= node
->GetNext())
531 wxWindow
*win
= node
->GetData ();
532 wxXSetBusyCursor (win
, cursor
);
537 // Restore cursor to normal
538 void wxEndBusyCursor()
540 if (wxBusyCursorCount
== 0)
544 if (wxBusyCursorCount
== 0)
546 for(wxWindowList
::Node
*node
= wxTopLevelWindows
.GetFirst (); node
;
547 node
= node
->GetNext())
549 wxWindow
*win
= node
->GetData ();
550 wxXSetBusyCursor (win
, NULL
);
555 // TRUE if we're between the above two calls
558 return (wxBusyCursorCount
> 0);