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"
24 #include "wx/window.h"
30 #pragma message disable nosimpint
33 #include <X11/cursorfont.h>
35 #pragma message enable nosimpint
38 #include "wx/motif/private.h"
40 // Cursor for one display, so we can choose the correct one for
41 // the current display.
49 WX_DECLARE_LIST(wxXCursor
, wxXCursorList
);
50 #include "wx/listimpl.cpp"
51 WX_DEFINE_LIST(wxXCursorList
)
53 class WXDLLEXPORT wxCursorRefData
: public wxGDIRefData
57 virtual ~wxCursorRefData();
59 wxXCursorList m_cursors
; // wxXCursor objects, one per display
60 wxStockCursor m_cursorId
; // wxWidgets standard cursor id
62 friend class wxCursor
;
65 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
66 #define M_CURSORHANDLERDATA ((wxCursorRefData *)bitmap->m_refData)
68 IMPLEMENT_DYNAMIC_CLASS(wxCursor
, wxObject
)
70 wxCursorRefData::wxCursorRefData()
72 m_cursorId
= wxCURSOR_NONE
;
75 wxCursorRefData::~wxCursorRefData()
77 wxXCursorList::compatibility_iterator node
= m_cursors
.GetFirst();
80 wxXCursor
* c
= node
->GetData();
81 XFreeCursor((Display
*) c
->m_display
, (Cursor
) c
->m_cursor
);
83 node
= node
->GetNext();
92 wxCursor::wxCursor(const wxImage
& image
)
94 unsigned char * rgbBits
= image
.GetData();
95 int w
= image
.GetWidth() ;
96 int h
= image
.GetHeight();
97 bool bHasMask
= image
.HasMask();
98 int imagebitcount
= (w
*h
)/8;
100 unsigned char * bits
= new unsigned char [imagebitcount
];
101 unsigned char * maskBits
= new unsigned char [imagebitcount
];
104 unsigned char c
, cMask
;
105 for (i
=0; i
<imagebitcount
; i
++)
110 cMask
= 0xfe; // 11111110
113 // possible overflow if we do the summation first ?
114 c
= (unsigned char)(rgbBits
[(i8
+j
)*3]/3 + rgbBits
[(i8
+j
)*3+1]/3 + rgbBits
[(i8
+j
)*3+2]/3);
115 // if average value is > mid grey
117 bits
[i
] = bits
[i
] & cMask
;
118 cMask
= (unsigned char)((cMask
<< 1) | 1);
125 r
= image
.GetMaskRed(),
126 g
= image
.GetMaskGreen(),
127 b
= image
.GetMaskBlue();
129 for (i
=0; i
<imagebitcount
; i
++)
137 if (rgbBits
[(i8
+j
)*3] != r
|| rgbBits
[(i8
+j
)*3+1] != g
|| rgbBits
[(i8
+j
)*3+2] != b
)
138 maskBits
[i
] = maskBits
[i
] | cMask
;
139 cMask
= (unsigned char)(cMask
<< 1);
145 for (i
=0; i
<imagebitcount
; i
++)
152 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
))
153 hotSpotX
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
);
157 if (image
.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
))
158 hotSpotY
= image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
);
162 if (hotSpotX
< 0 || hotSpotX
>= w
)
164 if (hotSpotY
< 0 || hotSpotY
>= h
)
167 Create( (const char*)bits
, w
, h
, hotSpotX
, hotSpotY
,
168 (const char*)maskBits
);
175 void wxCursor::Create(const char bits
[], int width
, int height
,
176 int hotSpotX
, int hotSpotY
, const char maskBits
[])
179 m_refData
= new wxCursorRefData
;
181 Display
*dpy
= (Display
*) wxGetDisplay();
182 int screen_num
= DefaultScreen (dpy
);
184 Pixmap pixmap
= XCreatePixmapFromBitmapData (dpy
,
185 RootWindow (dpy
, screen_num
),
186 (char*) bits
, width
, height
,
189 Pixmap mask_pixmap
= None
;
190 if (maskBits
!= NULL
)
192 mask_pixmap
= XCreatePixmapFromBitmapData (dpy
,
193 RootWindow (dpy
, screen_num
),
194 (char*) maskBits
, width
, height
,
198 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotSpotX
, hotSpotY
);
200 XFreePixmap( dpy
, pixmap
);
201 if (mask_pixmap
!= None
)
203 XFreePixmap( dpy
, mask_pixmap
);
207 void wxCursor::Create(WXPixmap pixmap
, WXPixmap mask_pixmap
,
208 int hotSpotX
, int hotSpotY
)
211 m_refData
= new wxCursorRefData
;
213 Display
*dpy
= (Display
*) wxGetDisplay();
214 int screen_num
= DefaultScreen (dpy
);
216 XColor foreground_color
;
217 XColor background_color
;
218 foreground_color
.pixel
= BlackPixel(dpy
, screen_num
);
219 background_color
.pixel
= WhitePixel(dpy
, screen_num
);
220 Colormap cmap
= (Colormap
) wxTheApp
->GetMainColormap((WXDisplay
*) dpy
);
221 XQueryColor(dpy
, cmap
, &foreground_color
);
222 XQueryColor(dpy
, cmap
, &background_color
);
224 Cursor cursor
= XCreatePixmapCursor (dpy
,
234 wxXCursor
*c
= new wxXCursor
;
236 c
->m_cursor
= (WXCursor
) cursor
;
237 c
->m_display
= (WXDisplay
*) dpy
;
238 M_CURSORDATA
->m_cursors
.Append(c
);
242 wxCursor::wxCursor(const char bits
[], int width
, int height
,
243 int hotSpotX
, int hotSpotY
, const char maskBits
[] ,
244 const wxColour
* WXUNUSED(fg
), const wxColour
* WXUNUSED(bg
) )
246 Create(bits
, width
, height
, hotSpotX
, hotSpotY
, maskBits
);
249 wxCursor::wxCursor(const wxString
& name
, wxBitmapType type
, int hotSpotX
,
252 // Must be an XBM file
253 if (type
!= wxBITMAP_TYPE_XBM
) {
254 wxLogError("Invalid cursor bitmap type '%d'", type
);
258 m_refData
= new wxCursorRefData
;
260 int hotX
= -1, hotY
= -1;
262 Pixmap pixmap
= None
, mask_pixmap
= None
;
264 Display
*dpy
= (Display
*) wxGetDisplay();
265 int screen_num
= DefaultScreen (dpy
);
267 int value
= XReadBitmapFile (dpy
, RootWindow (dpy
, screen_num
),
269 &w
, &h
, &pixmap
, &hotX
, &hotY
);
271 if (value
== BitmapSuccess
)
273 // TODO: how do we determine whether hotX, hotY were read correctly?
274 if (hotX
< 0 || hotY
< 0)
279 if (hotX
< 0 || hotY
< 0)
285 Create( (WXPixmap
)pixmap
, (WXPixmap
)mask_pixmap
, hotX
, hotY
);
287 XFreePixmap( dpy
, pixmap
);
291 // Cursors by stock number
292 void wxCursor::InitFromStock(wxStockCursor id
)
294 m_refData
= new wxCursorRefData
;
295 M_CURSORDATA
->m_cursorId
= id
;
298 wxCursor::~wxCursor()
302 wxGDIRefData
*wxCursor::CreateGDIRefData() const
304 return new wxCursorRefData
;
307 wxGDIRefData
*wxCursor::CloneGDIRefData(const wxGDIRefData
*data
) const
309 return new wxCursorRefData(*static_cast<const wxCursorRefData
*>(data
));
312 // Motif-specific: create/get a cursor for the current display
313 WXCursor
wxCursor::GetXCursor(WXDisplay
* display
) const
317 wxXCursorList::compatibility_iterator node
= M_CURSORDATA
->m_cursors
.GetFirst();
320 wxXCursor
* c
= node
->GetData();
321 if (c
->m_display
== display
)
323 node
= node
->GetNext();
326 // No cursor for this display, so let's see if we're an id-type cursor.
328 if (M_CURSORDATA
->m_cursorId
!= wxCURSOR_NONE
)
330 WXCursor cursor
= MakeCursor(display
, M_CURSORDATA
->m_cursorId
);
333 wxXCursor
* c
= new wxXCursor
;
334 c
->m_cursor
= cursor
;
335 c
->m_display
= display
;
336 M_CURSORDATA
->m_cursors
.Append(c
);
343 // Not an id-type cursor, so we don't know how to create it.
347 // Make a cursor from standard id
348 WXCursor
wxCursor::MakeCursor(WXDisplay
* display
, wxStockCursor id
) const
350 Display
* dpy
= (Display
*) display
;
351 Cursor cursor
= (Cursor
) 0;
356 case wxCURSOR_CHAR
: return (WXCursor
)cursor
;
358 case wxCURSOR_WAIT
: x_cur
= XC_watch
; break;
359 case wxCURSOR_CROSS
: x_cur
= XC_crosshair
; break;
360 case wxCURSOR_HAND
: x_cur
= XC_hand1
; break;
361 case wxCURSOR_BULLSEYE
: x_cur
= XC_target
; break;
362 case wxCURSOR_PENCIL
: x_cur
= XC_pencil
; break;
363 case wxCURSOR_MAGNIFIER
: x_cur
= XC_sizing
; break;
364 case wxCURSOR_IBEAM
: x_cur
= XC_xterm
; break;
365 case wxCURSOR_NO_ENTRY
: x_cur
= XC_pirate
; break;
366 case wxCURSOR_LEFT_BUTTON
: x_cur
= XC_leftbutton
; break;
367 case wxCURSOR_RIGHT_BUTTON
: x_cur
= XC_rightbutton
; break;
368 case wxCURSOR_MIDDLE_BUTTON
: x_cur
= XC_middlebutton
; break;
369 case wxCURSOR_QUESTION_ARROW
: x_cur
= XC_question_arrow
; break;
370 case wxCURSOR_SIZING
: x_cur
= XC_sizing
; break;
371 case wxCURSOR_WATCH
: x_cur
= XC_watch
; break;
372 case wxCURSOR_SPRAYCAN
: x_cur
= XC_spraycan
; break;
373 case wxCURSOR_PAINT_BRUSH
: x_cur
= XC_spraycan
; break;
374 case wxCURSOR_SIZENWSE
:
375 case wxCURSOR_SIZENESW
: x_cur
= XC_crosshair
; break;
376 case wxCURSOR_SIZEWE
: x_cur
= XC_sb_h_double_arrow
; break;
377 case wxCURSOR_SIZENS
: x_cur
= XC_sb_v_double_arrow
; break;
378 case wxCURSOR_POINT_LEFT
: x_cur
= XC_sb_left_arrow
; break;
379 case wxCURSOR_POINT_RIGHT
: x_cur
= XC_sb_right_arrow
; break;
380 // (JD Huggins) added more stock cursors for X
381 // X-only cursors BEGIN
382 case wxCURSOR_CROSS_REVERSE
: x_cur
= XC_cross_reverse
; break;
383 case wxCURSOR_DOUBLE_ARROW
: x_cur
= XC_double_arrow
; break;
384 case wxCURSOR_BASED_ARROW_UP
: x_cur
= XC_based_arrow_up
; break;
385 case wxCURSOR_BASED_ARROW_DOWN
: x_cur
= XC_based_arrow_down
; break;
394 XCreatePixmap (dpy
, RootWindow (dpy
, DefaultScreen (dpy
)),
396 gcv
.function
= GXxor
;
409 cursor
= XCreatePixmapCursor (dpy
,
419 default: x_cur
= XC_top_left_arrow
; break;
423 return (WXCursor
)cursor
;
425 cursor
= XCreateFontCursor (dpy
, x_cur
);
426 return (WXCursor
) cursor
;
429 // Global cursor setting
430 void wxSetCursor(const wxCursor
& WXUNUSED(cursor
))
432 // Nothing to do for Motif (no global cursor)
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 static int wxBusyCursorCount
= 0;
444 wxXSetBusyCursor (wxWindow
* win
, const wxCursor
* cursor
)
446 Display
*display
= (Display
*) win
->GetXDisplay();
448 Window xwin
= (Window
) win
->GetXWindow();
452 XSetWindowAttributes attrs
;
456 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
460 // Restore old cursor
461 if (win
->GetCursor().Ok())
462 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
467 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
471 for(wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst (); node
;
472 node
= node
->GetNext())
474 wxWindow
*child
= node
->GetData ();
475 wxXSetBusyCursor (child
, cursor
);
479 // Set the cursor to the busy cursor for all windows
480 void wxBeginBusyCursor(const wxCursor
*cursor
)
483 if (wxBusyCursorCount
== 1)
485 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
486 node
= node
->GetNext())
488 wxWindow
*win
= node
->GetData ();
489 wxXSetBusyCursor (win
, cursor
);
494 // Restore cursor to normal
495 void wxEndBusyCursor()
497 if (wxBusyCursorCount
== 0)
501 if (wxBusyCursorCount
== 0)
503 for(wxWindowList::compatibility_iterator node
= wxTopLevelWindows
.GetFirst (); node
;
504 node
= node
->GetNext())
506 wxWindow
*win
= node
->GetData ();
507 wxXSetBusyCursor (win
, NULL
);
512 // true if we're between the above two calls
515 return (wxBusyCursorCount
> 0);