]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: cursor.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "cursor.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/cursor.h" | |
16 | #include "wx/utils.h" | |
17 | ||
18 | #include <gdk/gdk.h> | |
19 | #include <gtk/gtk.h> | |
20 | ||
21 | //----------------------------------------------------------------------------- | |
22 | // idle system | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern void wxapp_install_idle_handler(); | |
26 | extern bool g_isIdle; | |
27 | ||
28 | //----------------------------------------------------------------------------- | |
29 | // wxCursor | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | class wxCursorRefData: public wxObjectRefData | |
33 | { | |
34 | public: | |
35 | ||
36 | wxCursorRefData(); | |
37 | ~wxCursorRefData(); | |
38 | ||
39 | GdkCursor *m_cursor; | |
40 | }; | |
41 | ||
42 | wxCursorRefData::wxCursorRefData() | |
43 | { | |
44 | m_cursor = (GdkCursor *) NULL; | |
45 | } | |
46 | ||
47 | wxCursorRefData::~wxCursorRefData() | |
48 | { | |
49 | if (m_cursor) gdk_cursor_destroy( m_cursor ); | |
50 | } | |
51 | ||
52 | //----------------------------------------------------------------------------- | |
53 | ||
54 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) | |
55 | ||
56 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) | |
57 | ||
58 | wxCursor::wxCursor() | |
59 | { | |
60 | } | |
61 | ||
62 | wxCursor::wxCursor( int cursorId ) | |
63 | { | |
64 | m_refData = new wxCursorRefData(); | |
65 | ||
66 | GdkCursorType gdk_cur = GDK_LEFT_PTR; | |
67 | switch (cursorId) | |
68 | { | |
69 | case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break; | |
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; | |
74 | case wxCURSOR_WAIT: | |
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; | |
81 | case wxCURSOR_SIZENWSE: | |
82 | case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break; | |
83 | case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break; | |
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; | |
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; | |
90 | case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break; | |
91 | ||
92 | case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break; | |
93 | case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break; | |
94 | /* | |
95 | case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break; | |
96 | case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break; | |
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; | |
99 | */ | |
100 | default: | |
101 | wxFAIL_MSG(wxT("unsupported cursor type")); | |
102 | // will use the standard one | |
103 | ||
104 | case wxCURSOR_ARROW: | |
105 | break; | |
106 | } | |
107 | ||
108 | M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur ); | |
109 | } | |
110 | ||
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 | ||
141 | wxCursor::wxCursor( const wxCursor &cursor ) | |
142 | { | |
143 | Ref( cursor ); | |
144 | } | |
145 | ||
146 | wxCursor::~wxCursor() | |
147 | { | |
148 | } | |
149 | ||
150 | wxCursor& wxCursor::operator = ( const wxCursor& cursor ) | |
151 | { | |
152 | if (*this == cursor) | |
153 | return (*this); | |
154 | ||
155 | Ref( cursor ); | |
156 | ||
157 | return *this; | |
158 | } | |
159 | ||
160 | bool wxCursor::operator == ( const wxCursor& cursor ) const | |
161 | { | |
162 | return m_refData == cursor.m_refData; | |
163 | } | |
164 | ||
165 | bool wxCursor::operator != ( const wxCursor& cursor ) const | |
166 | { | |
167 | return m_refData != cursor.m_refData; | |
168 | } | |
169 | ||
170 | bool wxCursor::Ok() const | |
171 | { | |
172 | return (m_refData != NULL); | |
173 | } | |
174 | ||
175 | GdkCursor *wxCursor::GetCursor() const | |
176 | { | |
177 | return M_CURSORDATA->m_cursor; | |
178 | } | |
179 | ||
180 | //----------------------------------------------------------------------------- | |
181 | // busy cursor routines | |
182 | //----------------------------------------------------------------------------- | |
183 | ||
184 | extern wxCursor g_globalCursor; | |
185 | ||
186 | static wxCursor gs_savedCursor; | |
187 | static int gs_busyCount = 0; | |
188 | ||
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 | ||
199 | void wxEndBusyCursor() | |
200 | { | |
201 | if (--gs_busyCount > 0) | |
202 | return; | |
203 | ||
204 | wxSetCursor( gs_savedCursor ); | |
205 | gs_savedCursor = wxNullCursor; | |
206 | ||
207 | wxYield(); | |
208 | } | |
209 | ||
210 | void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) ) | |
211 | { | |
212 | if (gs_busyCount++ > 0) | |
213 | return; | |
214 | ||
215 | wxASSERT_MSG( !gs_savedCursor.Ok(), | |
216 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); | |
217 | ||
218 | gs_savedCursor = g_globalCursor; | |
219 | ||
220 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); | |
221 | ||
222 | wxYield(); | |
223 | } | |
224 | ||
225 | bool wxIsBusy() | |
226 | { | |
227 | return gs_busyCount > 0; | |
228 | } | |
229 | ||
230 | void wxSetCursor( const wxCursor& cursor ) | |
231 | { | |
232 | if (g_isIdle) | |
233 | wxapp_install_idle_handler(); | |
234 | ||
235 | g_globalCursor = cursor; | |
236 | } |