1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/cursor.cpp
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #include "wx/cursor.h"
18 #include "wx/module.h"
22 #include "wx/hashmap.h"
24 #include "wx/mgl/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 class wxCursorRefData
: public wxObjectRefData
41 wxCursorRefData::wxCursorRefData()
43 m_cursor
= (MGLCursor
*) NULL
;
46 wxCursorRefData::~wxCursorRefData()
51 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
53 //-----------------------------------------------------------------------------
55 WX_DECLARE_HASH_MAP(int, wxCursor
, wxIntegerHash
, wxIntegerEqual
, wxCursorsHash
);
57 static wxCursorsHash
*gs_cursorsHash
= NULL
;
59 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
65 wxCursor::wxCursor(int cursorId
)
67 if ( !gs_cursorsHash
)
68 gs_cursorsHash
= new wxCursorsHash
;
70 if ( gs_cursorsHash
->find(cursorId
) != gs_cursorsHash
->end() )
72 wxLogTrace(_T("mglcursor"), _T("cursor id %i fetched from cache"), cursorId
);
73 *this = (*gs_cursorsHash
)[cursorId
];
77 const char *cursorname
= NULL
;
78 m_refData
= new wxCursorRefData();
82 case wxCURSOR_ARROW
: cursorname
= "arrow.cur"; break;
83 case wxCURSOR_RIGHT_ARROW
: cursorname
= "rightarr.cur"; break;
84 case wxCURSOR_BULLSEYE
: cursorname
= "bullseye.cur"; break;
85 case wxCURSOR_CHAR
: cursorname
= "char.cur"; break;
86 case wxCURSOR_CROSS
: cursorname
= "cross.cur"; break;
87 case wxCURSOR_HAND
: cursorname
= "hand.cur"; break;
88 case wxCURSOR_IBEAM
: cursorname
= "ibeam.cur"; break;
89 case wxCURSOR_LEFT_BUTTON
: cursorname
= "leftbtn.cur"; break;
90 case wxCURSOR_MAGNIFIER
: cursorname
= "magnif1.cur"; break;
91 case wxCURSOR_MIDDLE_BUTTON
: cursorname
= "midbtn.cur"; break;
92 case wxCURSOR_NO_ENTRY
: cursorname
= "noentry.cur"; break;
93 case wxCURSOR_PAINT_BRUSH
: cursorname
= "pbrush.cur"; break;
94 case wxCURSOR_PENCIL
: cursorname
= "pencil.cur"; break;
95 case wxCURSOR_POINT_LEFT
: cursorname
= "pntleft.cur"; break;
96 case wxCURSOR_POINT_RIGHT
: cursorname
= "pntright.cur"; break;
97 case wxCURSOR_QUESTION_ARROW
: cursorname
= "query.cur"; break;
98 case wxCURSOR_RIGHT_BUTTON
: cursorname
= "rightbtn.cur"; break;
99 case wxCURSOR_SIZENESW
: cursorname
= "sizenesw.cur"; break;
100 case wxCURSOR_SIZENS
: cursorname
= "sizens.cur"; break;
101 case wxCURSOR_SIZENWSE
: cursorname
= "sizenwse.cur"; break;
102 case wxCURSOR_SIZEWE
: cursorname
= "sizewe.cur"; break;
103 case wxCURSOR_SIZING
: cursorname
= "size.cur"; break;
104 case wxCURSOR_SPRAYCAN
: cursorname
= "spraycan.cur"; break;
105 case wxCURSOR_WAIT
: cursorname
= "wait.cur"; break;
106 case wxCURSOR_WATCH
: cursorname
= "clock.cur"; break;
107 case wxCURSOR_BLANK
: cursorname
= "blank.cur"; break;
110 *this = wxNullCursor
;
114 wxFAIL_MSG(wxT("unsupported cursor type"));
118 M_CURSORDATA
->m_cursor
= new MGLCursor(cursorname
);
120 // if we cannot load arrow cursor, use MGL's default arrow cursor:
121 if ( !M_CURSORDATA
->m_cursor
->valid() && cursorId
== wxCURSOR_ARROW
)
123 delete M_CURSORDATA
->m_cursor
;
124 M_CURSORDATA
->m_cursor
= new MGLCursor(MGL_DEF_CURSOR
);
127 if ( !M_CURSORDATA
->m_cursor
->valid() )
129 wxLogError(_("Couldn't create cursor."));
134 (*gs_cursorsHash
)[cursorId
] = *this;
135 wxLogTrace(_T("mglcursor"), _T("cursor id %i added to cache (%s)"),
136 cursorId
, cursorname
);
140 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
142 int WXUNUSED(height
),
143 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
144 const char WXUNUSED(maskBits
)[],
145 wxColour
* WXUNUSED(fg
), wxColour
* WXUNUSED(bg
) )
150 wxCursor::wxCursor(const wxString
& cursor_file
,
152 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
))
154 if ( flags
== wxBITMAP_TYPE_CUR
|| flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
156 m_refData
= new wxCursorRefData();
157 M_CURSORDATA
->m_cursor
= new MGLCursor(cursor_file
.mb_str());
158 if ( !M_CURSORDATA
->m_cursor
->valid() )
160 wxLogError(_("Couldn't create cursor."));
166 wxLogError(wxT("Cannot load cursor resource of this type."));
170 wxCursor::wxCursor(const wxCursor
&cursor
)
175 wxCursor::~wxCursor()
177 // wxObject unrefs data
180 wxCursor
& wxCursor::operator = (const wxCursor
& cursor
)
182 if ( *this == cursor
)
188 bool wxCursor::operator == (const wxCursor
& cursor
) const
190 return (m_refData
== cursor
.m_refData
);
193 bool wxCursor::operator != (const wxCursor
& cursor
) const
195 return (m_refData
!= cursor
.m_refData
);
198 bool wxCursor::Ok() const
200 return (m_refData
!= NULL
);
203 MGLCursor
*wxCursor::GetMGLCursor() const
205 return M_CURSORDATA
->m_cursor
;
210 // ----------------------------------------------------------------------------
211 // Global cursor setting
212 // ----------------------------------------------------------------------------
214 static wxCursor gs_globalCursor
= wxNullCursor
;
216 void wxSetCursor(const wxCursor
& cursor
)
221 MGL_wmSetGlobalCursor(g_winMng
, *cursor
.GetMGLCursor());
222 gs_globalCursor
= cursor
;
227 MGL_wmSetGlobalCursor(g_winMng
, NULL
);
228 gs_globalCursor
= wxNullCursor
;
234 //-----------------------------------------------------------------------------
235 // busy cursor routines
236 //-----------------------------------------------------------------------------
238 static wxCursor gs_savedCursor
= wxNullCursor
;
239 static int gs_busyCount
= 0;
241 const wxCursor
&wxBusyCursor::GetStoredCursor()
243 return gs_savedCursor
;
246 const wxCursor
wxBusyCursor::GetBusyCursor()
248 return gs_globalCursor
;
251 void wxEndBusyCursor()
253 if ( --gs_busyCount
> 0 ) return;
255 wxSetCursor(gs_savedCursor
);
256 gs_savedCursor
= wxNullCursor
;
259 void wxBeginBusyCursor(wxCursor
*cursor
)
261 if ( gs_busyCount
++ > 0 ) return;
263 wxASSERT_MSG( !gs_savedCursor
.Ok(),
264 wxT("forgot to call wxEndBusyCursor, will leak memory") );
266 gs_savedCursor
= gs_globalCursor
;
268 wxSetCursor(*cursor
);
270 wxSetCursor(wxCursor(wxCURSOR_WAIT
));
275 return (gs_busyCount
> 0);
280 //-----------------------------------------------------------------------------
281 // module - clean up code
282 //-----------------------------------------------------------------------------
284 class wxCursorModule
: public wxModule
287 virtual bool OnInit() { return true; }
289 virtual void OnExit()
291 wxDELETE(gs_cursorsHash
);
295 DECLARE_DYNAMIC_CLASS(wxCursorModule
)
298 IMPLEMENT_DYNAMIC_CLASS(wxCursorModule
, wxModule
)