| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: cursor.cpp |
| 3 | // Purpose: wxCursor class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 17/09/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #include "wx/cursor.h" |
| 13 | #include "wx/gdicmn.h" |
| 14 | #include "wx/icon.h" |
| 15 | #include "wx/app.h" |
| 16 | #include "wx/utils.h" |
| 17 | |
| 18 | #include "wx/x11/private.h" |
| 19 | |
| 20 | #if !wxUSE_NANOX |
| 21 | #include <X11/cursorfont.h> |
| 22 | #endif |
| 23 | |
| 24 | //----------------------------------------------------------------------------- |
| 25 | // wxCursor |
| 26 | //----------------------------------------------------------------------------- |
| 27 | |
| 28 | class wxCursorRefData: public wxObjectRefData |
| 29 | { |
| 30 | public: |
| 31 | |
| 32 | wxCursorRefData(); |
| 33 | ~wxCursorRefData(); |
| 34 | |
| 35 | WXCursor m_cursor; |
| 36 | WXDisplay *m_display; |
| 37 | }; |
| 38 | |
| 39 | wxCursorRefData::wxCursorRefData() |
| 40 | { |
| 41 | m_cursor = NULL; |
| 42 | m_display = NULL; |
| 43 | } |
| 44 | |
| 45 | wxCursorRefData::~wxCursorRefData() |
| 46 | { |
| 47 | if (m_cursor) |
| 48 | XFreeCursor( (Display*) m_display, (Cursor) m_cursor ); |
| 49 | } |
| 50 | |
| 51 | //----------------------------------------------------------------------------- |
| 52 | |
| 53 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) |
| 54 | |
| 55 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) |
| 56 | |
| 57 | wxCursor::wxCursor() |
| 58 | { |
| 59 | |
| 60 | } |
| 61 | |
| 62 | wxCursor::wxCursor( int cursorId ) |
| 63 | { |
| 64 | m_refData = new wxCursorRefData(); |
| 65 | |
| 66 | #if wxUSE_NANOX |
| 67 | // TODO Create some standard cursors from bitmaps. |
| 68 | |
| 69 | |
| 70 | #else |
| 71 | // !wxUSE_NANOX |
| 72 | |
| 73 | M_CURSORDATA->m_display = wxGlobalDisplay(); |
| 74 | wxASSERT_MSG( M_CURSORDATA->m_display, wxT("No display") ); |
| 75 | |
| 76 | int x_cur = XC_left_ptr; |
| 77 | switch (cursorId) |
| 78 | { |
| 79 | case wxCURSOR_DEFAULT: x_cur = XC_left_ptr; break; |
| 80 | case wxCURSOR_HAND: x_cur = XC_hand1; break; |
| 81 | case wxCURSOR_CROSS: x_cur = XC_crosshair; break; |
| 82 | case wxCURSOR_SIZEWE: x_cur = XC_sb_h_double_arrow; break; |
| 83 | case wxCURSOR_SIZENS: x_cur = XC_sb_v_double_arrow; break; |
| 84 | case wxCURSOR_ARROWWAIT: |
| 85 | case wxCURSOR_WAIT: |
| 86 | case wxCURSOR_WATCH: x_cur = XC_watch; break; |
| 87 | case wxCURSOR_SIZING: x_cur = XC_sizing; break; |
| 88 | case wxCURSOR_SPRAYCAN: x_cur = XC_spraycan; break; |
| 89 | case wxCURSOR_IBEAM: x_cur = XC_xterm; break; |
| 90 | case wxCURSOR_PENCIL: x_cur = XC_pencil; break; |
| 91 | case wxCURSOR_NO_ENTRY: x_cur = XC_pirate; break; |
| 92 | case wxCURSOR_SIZENWSE: |
| 93 | case wxCURSOR_SIZENESW: x_cur = XC_fleur; break; |
| 94 | case wxCURSOR_QUESTION_ARROW: x_cur = XC_question_arrow; break; |
| 95 | case wxCURSOR_PAINT_BRUSH: x_cur = XC_spraycan; break; |
| 96 | case wxCURSOR_MAGNIFIER: x_cur = XC_plus; break; |
| 97 | case wxCURSOR_CHAR: x_cur = XC_xterm; break; |
| 98 | case wxCURSOR_LEFT_BUTTON: x_cur = XC_leftbutton; break; |
| 99 | case wxCURSOR_MIDDLE_BUTTON: x_cur = XC_middlebutton; break; |
| 100 | case wxCURSOR_RIGHT_BUTTON: x_cur = XC_rightbutton; break; |
| 101 | case wxCURSOR_BULLSEYE: x_cur = XC_target; break; |
| 102 | |
| 103 | case wxCURSOR_POINT_LEFT: x_cur = XC_sb_left_arrow; break; |
| 104 | case wxCURSOR_POINT_RIGHT: x_cur = XC_sb_right_arrow; break; |
| 105 | /* |
| 106 | case wxCURSOR_DOUBLE_ARROW: x_cur = XC_double_arrow; break; |
| 107 | case wxCURSOR_CROSS_REVERSE: x_cur = XC_cross_reverse; break; |
| 108 | case wxCURSOR_BASED_ARROW_UP: x_cur = XC_based_arrow_up; break; |
| 109 | case wxCURSOR_BASED_ARROW_DOWN: x_cur = XC_based_arrow_down; break; |
| 110 | */ |
| 111 | default: |
| 112 | wxFAIL_MSG(wxT("unsupported cursor type")); |
| 113 | // will use the standard one |
| 114 | } |
| 115 | |
| 116 | M_CURSORDATA->m_cursor = (WXCursor) XCreateFontCursor( (Display*) M_CURSORDATA->m_display, x_cur ); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | wxCursor::wxCursor(const char bits[], int width, int height, |
| 121 | int hotSpotX, int hotSpotY, |
| 122 | const char maskBits[], wxColour *fg, wxColour *bg) |
| 123 | { |
| 124 | wxFAIL_MSG( wxT("wxCursor creation from bits not yet implemented") ); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | wxCursor::wxCursor( const wxCursor &cursor ) |
| 129 | { |
| 130 | Ref( cursor ); |
| 131 | } |
| 132 | |
| 133 | #if wxUSE_IMAGE |
| 134 | wxCursor::wxCursor( const wxImage & image ) |
| 135 | { |
| 136 | wxFAIL_MSG( wxT("wxCursor creation from wxImage not yet implemented") ); |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | wxCursor::~wxCursor() |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | wxCursor& wxCursor::operator = ( const wxCursor& cursor ) |
| 145 | { |
| 146 | if (*this == cursor) |
| 147 | return (*this); |
| 148 | |
| 149 | Ref( cursor ); |
| 150 | |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | bool wxCursor::operator == ( const wxCursor& cursor ) const |
| 155 | { |
| 156 | return m_refData == cursor.m_refData; |
| 157 | } |
| 158 | |
| 159 | bool wxCursor::operator != ( const wxCursor& cursor ) const |
| 160 | { |
| 161 | return m_refData != cursor.m_refData; |
| 162 | } |
| 163 | |
| 164 | bool wxCursor::Ok() const |
| 165 | { |
| 166 | return (m_refData != NULL); |
| 167 | } |
| 168 | |
| 169 | WXCursor wxCursor::GetCursor() const |
| 170 | { |
| 171 | return M_CURSORDATA->m_cursor; |
| 172 | } |
| 173 | |
| 174 | //----------------------------------------------------------------------------- |
| 175 | // busy cursor routines |
| 176 | //----------------------------------------------------------------------------- |
| 177 | |
| 178 | /* extern */ wxCursor g_globalCursor; |
| 179 | |
| 180 | static wxCursor gs_savedCursor; |
| 181 | static int gs_busyCount = 0; |
| 182 | |
| 183 | const wxCursor &wxBusyCursor::GetStoredCursor() |
| 184 | { |
| 185 | return gs_savedCursor; |
| 186 | } |
| 187 | |
| 188 | const wxCursor wxBusyCursor::GetBusyCursor() |
| 189 | { |
| 190 | return wxCursor(wxCURSOR_WATCH); |
| 191 | } |
| 192 | |
| 193 | void wxEndBusyCursor() |
| 194 | { |
| 195 | if (--gs_busyCount > 0) |
| 196 | return; |
| 197 | |
| 198 | wxSetCursor( gs_savedCursor ); |
| 199 | gs_savedCursor = wxNullCursor; |
| 200 | |
| 201 | if (wxTheApp) |
| 202 | wxTheApp->ProcessIdle(); |
| 203 | } |
| 204 | |
| 205 | void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) ) |
| 206 | { |
| 207 | if (gs_busyCount++ > 0) |
| 208 | return; |
| 209 | |
| 210 | wxASSERT_MSG( !gs_savedCursor.Ok(), |
| 211 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); |
| 212 | |
| 213 | gs_savedCursor = g_globalCursor; |
| 214 | |
| 215 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); |
| 216 | |
| 217 | if (wxTheApp) |
| 218 | wxTheApp->ProcessIdle(); |
| 219 | } |
| 220 | |
| 221 | bool wxIsBusy() |
| 222 | { |
| 223 | return gs_busyCount > 0; |
| 224 | } |
| 225 | |
| 226 | void wxSetCursor( const wxCursor& cursor ) |
| 227 | { |
| 228 | g_globalCursor = cursor; |
| 229 | } |