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