]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: cursor.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
8bbe427f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "cursor.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/cursor.h" | |
4dcaf11a | 16 | #include "wx/utils.h" |
c801d85f | 17 | |
071a2d78 | 18 | #include <gdk/gdk.h> |
a1db952f | 19 | #include <gtk/gtk.h> |
83624f79 | 20 | |
238d735d RR |
21 | //----------------------------------------------------------------------------- |
22 | // idle system | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern void wxapp_install_idle_handler(); | |
26 | extern bool g_isIdle; | |
27 | ||
c801d85f KB |
28 | //----------------------------------------------------------------------------- |
29 | // wxCursor | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | class wxCursorRefData: public wxObjectRefData | |
33 | { | |
34 | public: | |
8bbe427f VZ |
35 | |
36 | wxCursorRefData(); | |
37 | ~wxCursorRefData(); | |
38 | ||
c801d85f KB |
39 | GdkCursor *m_cursor; |
40 | }; | |
41 | ||
8bbe427f | 42 | wxCursorRefData::wxCursorRefData() |
c801d85f | 43 | { |
2d17d68f | 44 | m_cursor = (GdkCursor *) NULL; |
ff7b1510 | 45 | } |
c801d85f | 46 | |
8bbe427f | 47 | wxCursorRefData::~wxCursorRefData() |
c801d85f | 48 | { |
2d17d68f | 49 | if (m_cursor) gdk_cursor_destroy( m_cursor ); |
ff7b1510 | 50 | } |
c801d85f KB |
51 | |
52 | //----------------------------------------------------------------------------- | |
53 | ||
54 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) | |
55 | ||
56 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) | |
57 | ||
8bbe427f | 58 | wxCursor::wxCursor() |
c801d85f | 59 | { |
ff7b1510 | 60 | } |
c801d85f | 61 | |
debe6624 | 62 | wxCursor::wxCursor( int cursorId ) |
c801d85f | 63 | { |
2d17d68f RR |
64 | m_refData = new wxCursorRefData(); |
65 | ||
66 | GdkCursorType gdk_cur = GDK_LEFT_PTR; | |
67 | switch (cursorId) | |
68 | { | |
f7bdcdd7 | 69 | case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break; |
2d17d68f RR |
70 | case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break; |
71 | case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break; | |
72 | case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break; | |
73 | case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break; | |
7c39369e | 74 | case wxCURSOR_WAIT: |
2d17d68f RR |
75 | case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break; |
76 | case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break; | |
77 | case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break; | |
78 | case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break; | |
79 | case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break; | |
80 | case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break; | |
7c39369e | 81 | case wxCURSOR_SIZENWSE: |
2d17d68f RR |
82 | case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break; |
83 | case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break; | |
7c39369e VZ |
84 | case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break; |
85 | case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break; | |
86 | case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break; | |
2d17d68f RR |
87 | case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break; |
88 | case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break; | |
89 | case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break; | |
13971833 RD |
90 | case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break; |
91 | ||
957d856c VZ |
92 | case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break; |
93 | case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break; | |
c801d85f | 94 | /* |
7c39369e VZ |
95 | case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break; |
96 | case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break; | |
2d17d68f RR |
97 | case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break; |
98 | case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break; | |
c801d85f | 99 | */ |
7c39369e | 100 | default: |
223d09f6 | 101 | wxFAIL_MSG(wxT("unsupported cursor type")); |
7c39369e VZ |
102 | // will use the standard one |
103 | ||
104 | case wxCURSOR_ARROW: | |
105 | break; | |
2d17d68f | 106 | } |
8bbe427f | 107 | |
2d17d68f | 108 | M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur ); |
ff7b1510 | 109 | } |
c801d85f | 110 | |
a1db952f VS |
111 | extern GtkWidget *wxRootWindow; |
112 | ||
113 | wxCursor::wxCursor(const char bits[], int width, int height, | |
114 | int hotSpotX, int hotSpotY, | |
115 | const char maskBits[], wxColour *fg, wxColour *bg) | |
116 | { | |
117 | if (!maskBits) | |
118 | maskBits = bits; | |
119 | if (!fg) | |
120 | fg = wxBLACK; | |
121 | if (!bg) | |
122 | bg = wxWHITE; | |
123 | if (hotSpotX < 0 || hotSpotX >= width) | |
124 | hotSpotX = 0; | |
125 | if (hotSpotY < 0 || hotSpotY >= height) | |
126 | hotSpotY = 0; | |
127 | ||
128 | GdkBitmap *data = gdk_bitmap_create_from_data( wxRootWindow->window, (gchar *) bits, width, height ); | |
129 | GdkBitmap *mask = gdk_bitmap_create_from_data( wxRootWindow->window, (gchar *) maskBits, width, height); | |
130 | ||
131 | m_refData = new wxCursorRefData; | |
132 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap( | |
133 | data, mask, fg->GetColor(), bg->GetColor(), | |
134 | hotSpotX, hotSpotY ); | |
135 | ||
136 | gdk_bitmap_unref( data ); | |
137 | gdk_bitmap_unref( mask ); | |
138 | } | |
139 | ||
140 | ||
c801d85f KB |
141 | wxCursor::wxCursor( const wxCursor &cursor ) |
142 | { | |
2d17d68f | 143 | Ref( cursor ); |
ff7b1510 | 144 | } |
c801d85f | 145 | |
8bbe427f | 146 | wxCursor::~wxCursor() |
c801d85f | 147 | { |
ff7b1510 | 148 | } |
c801d85f KB |
149 | |
150 | wxCursor& wxCursor::operator = ( const wxCursor& cursor ) | |
151 | { | |
7c39369e VZ |
152 | if (*this == cursor) |
153 | return (*this); | |
154 | ||
2d17d68f | 155 | Ref( cursor ); |
7c39369e | 156 | |
2d17d68f | 157 | return *this; |
ff7b1510 | 158 | } |
c801d85f | 159 | |
8bbe427f | 160 | bool wxCursor::operator == ( const wxCursor& cursor ) const |
c801d85f | 161 | { |
2d17d68f | 162 | return m_refData == cursor.m_refData; |
ff7b1510 | 163 | } |
c801d85f | 164 | |
8bbe427f | 165 | bool wxCursor::operator != ( const wxCursor& cursor ) const |
c801d85f | 166 | { |
2d17d68f | 167 | return m_refData != cursor.m_refData; |
ff7b1510 | 168 | } |
c801d85f | 169 | |
8bbe427f | 170 | bool wxCursor::Ok() const |
c801d85f | 171 | { |
2d17d68f | 172 | return (m_refData != NULL); |
ff7b1510 | 173 | } |
c801d85f | 174 | |
8bbe427f | 175 | GdkCursor *wxCursor::GetCursor() const |
c801d85f | 176 | { |
2d17d68f | 177 | return M_CURSORDATA->m_cursor; |
ff7b1510 | 178 | } |
c801d85f KB |
179 | |
180 | //----------------------------------------------------------------------------- | |
181 | // busy cursor routines | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
238d735d | 184 | extern wxCursor g_globalCursor; |
7c39369e | 185 | |
238d735d | 186 | static wxCursor gs_savedCursor; |
89a43902 | 187 | static int gs_busyCount = 0; |
c801d85f | 188 | |
f6bcfd97 BP |
189 | const wxCursor &wxBusyCursor::GetStoredCursor() |
190 | { | |
191 | return gs_savedCursor; | |
192 | } | |
193 | ||
194 | const wxCursor wxBusyCursor::GetBusyCursor() | |
195 | { | |
196 | return wxCursor(wxCURSOR_WATCH); | |
197 | } | |
198 | ||
8bbe427f | 199 | void wxEndBusyCursor() |
c801d85f | 200 | { |
238d735d | 201 | if (--gs_busyCount > 0) |
89a43902 VZ |
202 | return; |
203 | ||
238d735d RR |
204 | wxSetCursor( gs_savedCursor ); |
205 | gs_savedCursor = wxNullCursor; | |
13971833 | 206 | |
e65cc56a | 207 | wxYield(); |
ff7b1510 | 208 | } |
c801d85f KB |
209 | |
210 | void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) ) | |
211 | { | |
238d735d | 212 | if (gs_busyCount++ > 0) |
89a43902 VZ |
213 | return; |
214 | ||
238d735d | 215 | wxASSERT_MSG( !gs_savedCursor.Ok(), |
223d09f6 | 216 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); |
7c39369e | 217 | |
238d735d | 218 | gs_savedCursor = g_globalCursor; |
13971833 | 219 | |
238d735d | 220 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); |
13971833 | 221 | |
4dcaf11a | 222 | wxYield(); |
ff7b1510 | 223 | } |
c801d85f | 224 | |
8bbe427f | 225 | bool wxIsBusy() |
c801d85f | 226 | { |
89a43902 | 227 | return gs_busyCount > 0; |
ff7b1510 | 228 | } |
c801d85f KB |
229 | |
230 | void wxSetCursor( const wxCursor& cursor ) | |
231 | { | |
13971833 | 232 | if (g_isIdle) |
238d735d RR |
233 | wxapp_install_idle_handler(); |
234 | ||
235 | g_globalCursor = cursor; | |
ff7b1510 | 236 | } |