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