1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "cursor.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/cursor.h"
23 #include "wx/module.h"
27 #include "wx/hashmap.h"
29 #include "wx/mgl/private.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 class wxCursorRefData
: public wxObjectRefData
46 wxCursorRefData::wxCursorRefData()
48 m_cursor
= (MGLCursor
*) NULL
;
51 wxCursorRefData::~wxCursorRefData()
56 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
58 //-----------------------------------------------------------------------------
60 WX_DECLARE_HASH_MAP(int, wxCursor
, wxIntegerHash
, wxIntegerEqual
, wxCursorsHash
)
62 static wxCursorsHash
*gs_cursorsHash
= NULL
;
64 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
70 wxCursor::wxCursor(int cursorId
)
72 if ( !gs_cursorsHash
)
73 gs_cursorsHash
= new wxCursorsHash
;
75 if ( gs_cursorsHash
->find(cursorId
) != gs_cursorsHash
->end() )
77 wxLogTrace(_T("mglcursor"), _T("cursor id %i fetched from cache"), cursorId
);
78 *this = (*gs_cursorsHash
)[cursorId
];
82 const char *cursorname
= NULL
;
83 m_refData
= new wxCursorRefData();
87 case wxCURSOR_ARROW
: cursorname
= "arrow.cur"; break;
88 case wxCURSOR_BULLSEYE
: cursorname
= "bullseye.cur"; break;
89 case wxCURSOR_CHAR
: cursorname
= "char.cur"; break;
90 case wxCURSOR_CROSS
: cursorname
= "cross.cur"; break;
91 case wxCURSOR_HAND
: cursorname
= "hand.cur"; break;
92 case wxCURSOR_IBEAM
: cursorname
= "ibeam.cur"; break;
93 case wxCURSOR_LEFT_BUTTON
: cursorname
= "leftbtn.cur"; break;
94 case wxCURSOR_MAGNIFIER
: cursorname
= "magnif1.cur"; break;
95 case wxCURSOR_MIDDLE_BUTTON
: cursorname
= "midbtn.cur"; break;
96 case wxCURSOR_NO_ENTRY
: cursorname
= "noentry.cur"; break;
97 case wxCURSOR_PAINT_BRUSH
: cursorname
= "pbrush.cur"; break;
98 case wxCURSOR_PENCIL
: cursorname
= "pencil.cur"; break;
99 case wxCURSOR_POINT_LEFT
: cursorname
= "pntleft.cur"; break;
100 case wxCURSOR_POINT_RIGHT
: cursorname
= "pntright.cur"; break;
101 case wxCURSOR_QUESTION_ARROW
: cursorname
= "query.cur"; break;
102 case wxCURSOR_RIGHT_BUTTON
: cursorname
= "rightbtn.cur"; break;
103 case wxCURSOR_SIZENESW
: cursorname
= "sizenesw.cur"; break;
104 case wxCURSOR_SIZENS
: cursorname
= "sizens.cur"; break;
105 case wxCURSOR_SIZENWSE
: cursorname
= "sizenwse.cur"; break;
106 case wxCURSOR_SIZEWE
: cursorname
= "sizewe.cur"; break;
107 case wxCURSOR_SIZING
: cursorname
= "size.cur"; break;
108 case wxCURSOR_SPRAYCAN
: cursorname
= "spraycan.cur"; break;
109 case wxCURSOR_WAIT
: cursorname
= "wait.cur"; break;
110 case wxCURSOR_WATCH
: cursorname
= "clock.cur"; break;
111 case wxCURSOR_BLANK
: cursorname
= "blank.cur"; break;
114 *this = wxNullCursor
;
119 wxFAIL_MSG(wxT("unsupported cursor type"));
123 M_CURSORDATA
->m_cursor
= new MGLCursor(cursorname
);
125 // if we cannot load arrow cursor, use MGL's default arrow cursor:
126 if ( !M_CURSORDATA
->m_cursor
->valid() && cursorId
== wxCURSOR_ARROW
)
128 delete M_CURSORDATA
->m_cursor
;
129 M_CURSORDATA
->m_cursor
= new MGLCursor(MGL_DEF_CURSOR
);
132 if ( !M_CURSORDATA
->m_cursor
->valid() )
134 wxLogError(_("Couldn't create cursor."));
139 (*gs_cursorsHash
)[cursorId
] = *this;
140 wxLogTrace(_T("mglcursor"), _T("cursor id %i added to cache (%s)"),
141 cursorId
, cursorname
);
145 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
147 int WXUNUSED(height
),
148 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
149 const char WXUNUSED(maskBits
)[],
150 wxColour
* WXUNUSED(fg
), wxColour
* WXUNUSED(bg
) )
155 wxCursor::wxCursor(const wxString
& cursor_file
,
157 int hotSpotX
, int hotSpotY
)
159 if ( flags
== wxBITMAP_TYPE_CUR
|| flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
161 m_refData
= new wxCursorRefData();
162 M_CURSORDATA
->m_cursor
= new MGLCursor(cursor_file
.mb_str());
163 if ( !M_CURSORDATA
->m_cursor
->valid() )
165 wxLogError(_("Couldn't create cursor."));
171 wxLogError(wxT("Cannot load cursor resource of this type."));
175 wxCursor::wxCursor(const wxCursor
&cursor
)
180 wxCursor::~wxCursor()
182 // wxObject unrefs data
185 wxCursor
& wxCursor::operator = (const wxCursor
& cursor
)
187 if ( *this == cursor
)
193 bool wxCursor::operator == (const wxCursor
& cursor
) const
195 return (m_refData
== cursor
.m_refData
);
198 bool wxCursor::operator != (const wxCursor
& cursor
) const
200 return (m_refData
!= cursor
.m_refData
);
203 bool wxCursor::Ok() const
205 return (m_refData
!= NULL
);
208 MGLCursor
*wxCursor::GetMGLCursor() const
210 return M_CURSORDATA
->m_cursor
;
215 // ----------------------------------------------------------------------------
216 // Global cursor setting
217 // ----------------------------------------------------------------------------
219 static wxCursor gs_globalCursor
= wxNullCursor
;
221 void wxSetCursor(const wxCursor
& cursor
)
226 MGL_wmSetGlobalCursor(g_winMng
, *cursor
.GetMGLCursor());
227 gs_globalCursor
= cursor
;
232 MGL_wmSetGlobalCursor(g_winMng
, NULL
);
233 gs_globalCursor
= wxNullCursor
;
239 //-----------------------------------------------------------------------------
240 // busy cursor routines
241 //-----------------------------------------------------------------------------
243 static wxCursor gs_savedCursor
= wxNullCursor
;
244 static int gs_busyCount
= 0;
246 const wxCursor
&wxBusyCursor::GetStoredCursor()
248 return gs_savedCursor
;
251 const wxCursor
wxBusyCursor::GetBusyCursor()
253 return gs_globalCursor
;
256 void wxEndBusyCursor()
258 if ( --gs_busyCount
> 0 ) return;
260 wxSetCursor(gs_savedCursor
);
261 gs_savedCursor
= wxNullCursor
;
264 void wxBeginBusyCursor(wxCursor
*cursor
)
266 if ( gs_busyCount
++ > 0 ) return;
268 wxASSERT_MSG( !gs_savedCursor
.Ok(),
269 wxT("forgot to call wxEndBusyCursor, will leak memory") );
271 gs_savedCursor
= gs_globalCursor
;
273 wxSetCursor(*cursor
);
275 wxSetCursor(wxCursor(wxCURSOR_WAIT
));
280 return (gs_busyCount
> 0);
285 //-----------------------------------------------------------------------------
286 // module - clean up code
287 //-----------------------------------------------------------------------------
289 class wxCursorModule
: public wxModule
292 virtual bool OnInit() { return TRUE
; }
294 virtual void OnExit()
296 wxDELETE(gs_cursorsHash
);
300 DECLARE_DYNAMIC_CLASS(wxCursorModule
)
303 IMPLEMENT_DYNAMIC_CLASS(wxCursorModule
, wxModule
)