Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: cursor.cpp | |
3 | // Purpose: | |
4 | // Author: Vaclav Slavik | |
5 | // Id: $Id$ | |
99ee04b9 | 6 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) |
32b8ec41 VZ |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "cursor.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #include "wx/cursor.h" | |
99ee04b9 | 23 | #include "wx/module.h" |
32b8ec41 VZ |
24 | #include "wx/utils.h" |
25 | #include "wx/log.h" | |
26 | #include "wx/intl.h" | |
99ee04b9 | 27 | #include "wx/hashmap.h" |
32b8ec41 | 28 | |
ef344ff8 | 29 | #include "wx/mgl/private.h" |
32b8ec41 VZ |
30 | |
31 | ||
32 | //----------------------------------------------------------------------------- | |
33 | // wxCursor | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | class wxCursorRefData: public wxObjectRefData | |
37 | { | |
38 | public: | |
39 | ||
40 | wxCursorRefData(); | |
41 | ~wxCursorRefData(); | |
42 | ||
43 | MGLCursor *m_cursor; | |
44 | }; | |
45 | ||
46 | wxCursorRefData::wxCursorRefData() | |
47 | { | |
48 | m_cursor = (MGLCursor*) NULL; | |
49 | } | |
50 | ||
51 | wxCursorRefData::~wxCursorRefData() | |
52 | { | |
53 | delete m_cursor; | |
54 | } | |
55 | ||
99ee04b9 VS |
56 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) |
57 | ||
32b8ec41 VZ |
58 | //----------------------------------------------------------------------------- |
59 | ||
99ee04b9 VS |
60 | WX_DECLARE_HASH_MAP(int, wxCursor, wxIntegerHash, wxIntegerEqual, wxCursorsHash) |
61 | ||
62 | static wxCursorsHash *gs_cursorsHash = NULL; | |
32b8ec41 VZ |
63 | |
64 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) | |
65 | ||
66 | wxCursor::wxCursor() | |
67 | { | |
68 | } | |
69 | ||
70 | wxCursor::wxCursor(int cursorId) | |
71 | { | |
c41c20a5 VS |
72 | if ( !gs_cursorsHash ) |
73 | gs_cursorsHash = new wxCursorsHash; | |
74 | ||
99ee04b9 VS |
75 | if ( gs_cursorsHash->find(cursorId) != gs_cursorsHash->end() ) |
76 | { | |
77 | wxLogTrace(_T("mglcursor"), _T("cursor id %i fetched from cache"), cursorId); | |
78 | *this = (*gs_cursorsHash)[cursorId]; | |
79 | return; | |
80 | } | |
81 | ||
32b8ec41 VZ |
82 | const char *cursorname = NULL; |
83 | m_refData = new wxCursorRefData(); | |
84 | ||
85 | switch (cursorId) | |
86 | { | |
87 | case wxCURSOR_ARROW: cursorname = "arrow.cur"; break; | |
a7c8e710 | 88 | case wxCURSOR_RIGHT_ARROW: cursorname = "rightarr.cur"; break; |
32b8ec41 VZ |
89 | case wxCURSOR_BULLSEYE: cursorname = "bullseye.cur"; break; |
90 | case wxCURSOR_CHAR: cursorname = "char.cur"; break; | |
91 | case wxCURSOR_CROSS: cursorname = "cross.cur"; break; | |
92 | case wxCURSOR_HAND: cursorname = "hand.cur"; break; | |
93 | case wxCURSOR_IBEAM: cursorname = "ibeam.cur"; break; | |
94 | case wxCURSOR_LEFT_BUTTON: cursorname = "leftbtn.cur"; break; | |
95 | case wxCURSOR_MAGNIFIER: cursorname = "magnif1.cur"; break; | |
96 | case wxCURSOR_MIDDLE_BUTTON: cursorname = "midbtn.cur"; break; | |
97 | case wxCURSOR_NO_ENTRY: cursorname = "noentry.cur"; break; | |
98 | case wxCURSOR_PAINT_BRUSH: cursorname = "pbrush.cur"; break; | |
99 | case wxCURSOR_PENCIL: cursorname = "pencil.cur"; break; | |
100 | case wxCURSOR_POINT_LEFT: cursorname = "pntleft.cur"; break; | |
101 | case wxCURSOR_POINT_RIGHT: cursorname = "pntright.cur"; break; | |
102 | case wxCURSOR_QUESTION_ARROW: cursorname = "query.cur"; break; | |
103 | case wxCURSOR_RIGHT_BUTTON: cursorname = "rightbtn.cur"; break; | |
104 | case wxCURSOR_SIZENESW: cursorname = "sizenesw.cur"; break; | |
105 | case wxCURSOR_SIZENS: cursorname = "sizens.cur"; break; | |
106 | case wxCURSOR_SIZENWSE: cursorname = "sizenwse.cur"; break; | |
107 | case wxCURSOR_SIZEWE: cursorname = "sizewe.cur"; break; | |
108 | case wxCURSOR_SIZING: cursorname = "size.cur"; break; | |
109 | case wxCURSOR_SPRAYCAN: cursorname = "spraycan.cur"; break; | |
110 | case wxCURSOR_WAIT: cursorname = "wait.cur"; break; | |
111 | case wxCURSOR_WATCH: cursorname = "clock.cur"; break; | |
112 | case wxCURSOR_BLANK: cursorname = "blank.cur"; break; | |
113 | ||
114 | case wxCURSOR_NONE: | |
32b8ec41 VZ |
115 | *this = wxNullCursor; |
116 | return; | |
117 | break; | |
118 | ||
119 | default: | |
120 | wxFAIL_MSG(wxT("unsupported cursor type")); | |
121 | break; | |
122 | } | |
123 | ||
124 | M_CURSORDATA->m_cursor = new MGLCursor(cursorname); | |
125 | ||
126 | // if we cannot load arrow cursor, use MGL's default arrow cursor: | |
127 | if ( !M_CURSORDATA->m_cursor->valid() && cursorId == wxCURSOR_ARROW ) | |
128 | { | |
129 | delete M_CURSORDATA->m_cursor; | |
130 | M_CURSORDATA->m_cursor = new MGLCursor(MGL_DEF_CURSOR); | |
131 | } | |
132 | ||
133 | if ( !M_CURSORDATA->m_cursor->valid() ) | |
134 | { | |
135 | wxLogError(_("Couldn't create cursor.")); | |
136 | UnRef(); | |
137 | } | |
99ee04b9 VS |
138 | else |
139 | { | |
140 | (*gs_cursorsHash)[cursorId] = *this; | |
141 | wxLogTrace(_T("mglcursor"), _T("cursor id %i added to cache (%s)"), | |
142 | cursorId, cursorname); | |
143 | } | |
32b8ec41 VZ |
144 | } |
145 | ||
146 | wxCursor::wxCursor(const char WXUNUSED(bits)[], | |
147 | int WXUNUSED(width), | |
148 | int WXUNUSED(height), | |
149 | int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY), | |
150 | const char WXUNUSED(maskBits)[], | |
151 | wxColour * WXUNUSED(fg), wxColour * WXUNUSED(bg) ) | |
152 | { | |
153 | //FIXME_MGL | |
154 | } | |
155 | ||
156 | wxCursor::wxCursor(const wxString& cursor_file, | |
157 | long flags, | |
158 | int hotSpotX, int hotSpotY) | |
159 | { | |
160 | if ( flags == wxBITMAP_TYPE_CUR || flags == wxBITMAP_TYPE_CUR_RESOURCE ) | |
161 | { | |
162 | m_refData = new wxCursorRefData(); | |
163 | M_CURSORDATA->m_cursor = new MGLCursor(cursor_file.mb_str()); | |
164 | if ( !M_CURSORDATA->m_cursor->valid() ) | |
165 | { | |
166 | wxLogError(_("Couldn't create cursor.")); | |
167 | UnRef(); | |
168 | } | |
169 | } | |
170 | else | |
171 | { | |
172 | wxLogError(wxT("Cannot load cursor resource of this type.")); | |
173 | } | |
174 | } | |
175 | ||
176 | wxCursor::wxCursor(const wxCursor &cursor) | |
177 | { | |
178 | Ref(cursor); | |
179 | } | |
180 | ||
181 | wxCursor::~wxCursor() | |
182 | { | |
183 | // wxObject unrefs data | |
184 | } | |
185 | ||
186 | wxCursor& wxCursor::operator = (const wxCursor& cursor) | |
187 | { | |
188 | if ( *this == cursor ) | |
189 | return (*this); | |
190 | Ref(cursor); | |
191 | return *this; | |
192 | } | |
193 | ||
194 | bool wxCursor::operator == (const wxCursor& cursor) const | |
195 | { | |
196 | return (m_refData == cursor.m_refData); | |
197 | } | |
198 | ||
199 | bool wxCursor::operator != (const wxCursor& cursor) const | |
200 | { | |
201 | return (m_refData != cursor.m_refData); | |
202 | } | |
203 | ||
204 | bool wxCursor::Ok() const | |
205 | { | |
206 | return (m_refData != NULL); | |
207 | } | |
208 | ||
209 | MGLCursor *wxCursor::GetMGLCursor() const | |
210 | { | |
211 | return M_CURSORDATA->m_cursor; | |
212 | } | |
213 | ||
214 | ||
215 | ||
216 | // ---------------------------------------------------------------------------- | |
217 | // Global cursor setting | |
218 | // ---------------------------------------------------------------------------- | |
219 | ||
5fd1ea32 | 220 | static wxCursor gs_globalCursor = wxNullCursor; |
32b8ec41 VZ |
221 | |
222 | void wxSetCursor(const wxCursor& cursor) | |
223 | { | |
224 | if ( cursor.Ok() ) | |
225 | { | |
9006f25e VS |
226 | if ( g_winMng ) |
227 | MGL_wmSetGlobalCursor(g_winMng, *cursor.GetMGLCursor()); | |
5fd1ea32 VS |
228 | gs_globalCursor = cursor; |
229 | } | |
230 | else | |
231 | { | |
9006f25e VS |
232 | if ( g_winMng ) |
233 | MGL_wmSetGlobalCursor(g_winMng, NULL); | |
5fd1ea32 | 234 | gs_globalCursor = wxNullCursor; |
32b8ec41 VZ |
235 | } |
236 | } | |
237 | ||
238 | ||
239 | ||
240 | //----------------------------------------------------------------------------- | |
241 | // busy cursor routines | |
242 | //----------------------------------------------------------------------------- | |
243 | ||
ef344ff8 | 244 | static wxCursor gs_savedCursor = wxNullCursor; |
32b8ec41 VZ |
245 | static int gs_busyCount = 0; |
246 | ||
247 | const wxCursor &wxBusyCursor::GetStoredCursor() | |
248 | { | |
249 | return gs_savedCursor; | |
250 | } | |
251 | ||
252 | const wxCursor wxBusyCursor::GetBusyCursor() | |
253 | { | |
5fd1ea32 | 254 | return gs_globalCursor; |
32b8ec41 VZ |
255 | } |
256 | ||
257 | void wxEndBusyCursor() | |
258 | { | |
259 | if ( --gs_busyCount > 0 ) return; | |
260 | ||
261 | wxSetCursor(gs_savedCursor); | |
262 | gs_savedCursor = wxNullCursor; | |
32b8ec41 VZ |
263 | } |
264 | ||
ef344ff8 | 265 | void wxBeginBusyCursor(wxCursor *cursor) |
32b8ec41 VZ |
266 | { |
267 | if ( gs_busyCount++ > 0 ) return; | |
268 | ||
269 | wxASSERT_MSG( !gs_savedCursor.Ok(), | |
270 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); | |
271 | ||
5fd1ea32 | 272 | gs_savedCursor = gs_globalCursor; |
ef344ff8 VS |
273 | if ( cursor->Ok() ) |
274 | wxSetCursor(*cursor); | |
275 | else | |
276 | wxSetCursor(wxCursor(wxCURSOR_WAIT)); | |
32b8ec41 VZ |
277 | } |
278 | ||
279 | bool wxIsBusy() | |
280 | { | |
281 | return (gs_busyCount > 0); | |
282 | } | |
283 | ||
99ee04b9 VS |
284 | |
285 | ||
286 | //----------------------------------------------------------------------------- | |
287 | // module - clean up code | |
288 | //----------------------------------------------------------------------------- | |
289 | ||
290 | class wxCursorModule : public wxModule | |
291 | { | |
292 | public: | |
c41c20a5 | 293 | virtual bool OnInit() { return TRUE; } |
99ee04b9 VS |
294 | |
295 | virtual void OnExit() | |
296 | { | |
297 | wxDELETE(gs_cursorsHash); | |
298 | } | |
299 | ||
300 | private: | |
301 | DECLARE_DYNAMIC_CLASS(wxCursorModule) | |
302 | }; | |
303 | ||
304 | IMPLEMENT_DYNAMIC_CLASS(wxCursorModule, wxModule) |