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
->find(cursorId
) != gs_cursorsHash
->end() )
74 wxLogTrace(_T("mglcursor"), _T("cursor id %i fetched from cache"), cursorId
);
75 *this = (*gs_cursorsHash
)[cursorId
];
79 const char *cursorname
= NULL
;
80 m_refData
= new wxCursorRefData();
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;
111 *this = wxNullCursor
;
116 wxFAIL_MSG(wxT("unsupported cursor type"));
120 M_CURSORDATA
->m_cursor
= new MGLCursor(cursorname
);
122 // if we cannot load arrow cursor, use MGL's default arrow cursor:
123 if ( !M_CURSORDATA
->m_cursor
->valid() && cursorId
== wxCURSOR_ARROW
)
125 delete M_CURSORDATA
->m_cursor
;
126 M_CURSORDATA
->m_cursor
= new MGLCursor(MGL_DEF_CURSOR
);
129 if ( !M_CURSORDATA
->m_cursor
->valid() )
131 wxLogError(_("Couldn't create cursor."));
136 (*gs_cursorsHash
)[cursorId
] = *this;
137 wxLogTrace(_T("mglcursor"), _T("cursor id %i added to cache (%s)"),
138 cursorId
, cursorname
);
142 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
144 int WXUNUSED(height
),
145 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
146 const char WXUNUSED(maskBits
)[],
147 wxColour
* WXUNUSED(fg
), wxColour
* WXUNUSED(bg
) )
152 wxCursor::wxCursor(const wxString
& cursor_file
,
154 int hotSpotX
, int hotSpotY
)
156 if ( flags
== wxBITMAP_TYPE_CUR
|| flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
158 m_refData
= new wxCursorRefData();
159 M_CURSORDATA
->m_cursor
= new MGLCursor(cursor_file
.mb_str());
160 if ( !M_CURSORDATA
->m_cursor
->valid() )
162 wxLogError(_("Couldn't create cursor."));
168 wxLogError(wxT("Cannot load cursor resource of this type."));
172 wxCursor::wxCursor(const wxCursor
&cursor
)
177 wxCursor::~wxCursor()
179 // wxObject unrefs data
182 wxCursor
& wxCursor::operator = (const wxCursor
& cursor
)
184 if ( *this == cursor
)
190 bool wxCursor::operator == (const wxCursor
& cursor
) const
192 return (m_refData
== cursor
.m_refData
);
195 bool wxCursor::operator != (const wxCursor
& cursor
) const
197 return (m_refData
!= cursor
.m_refData
);
200 bool wxCursor::Ok() const
202 return (m_refData
!= NULL
);
205 MGLCursor
*wxCursor::GetMGLCursor() const
207 return M_CURSORDATA
->m_cursor
;
212 // ----------------------------------------------------------------------------
213 // Global cursor setting
214 // ----------------------------------------------------------------------------
216 static wxCursor gs_globalCursor
= wxNullCursor
;
218 void wxSetCursor(const wxCursor
& cursor
)
223 MGL_wmSetGlobalCursor(g_winMng
, *cursor
.GetMGLCursor());
224 gs_globalCursor
= cursor
;
229 MGL_wmSetGlobalCursor(g_winMng
, NULL
);
230 gs_globalCursor
= wxNullCursor
;
236 //-----------------------------------------------------------------------------
237 // busy cursor routines
238 //-----------------------------------------------------------------------------
240 static wxCursor gs_savedCursor
= wxNullCursor
;
241 static int gs_busyCount
= 0;
243 const wxCursor
&wxBusyCursor::GetStoredCursor()
245 return gs_savedCursor
;
248 const wxCursor
wxBusyCursor::GetBusyCursor()
250 return gs_globalCursor
;
253 void wxEndBusyCursor()
255 if ( --gs_busyCount
> 0 ) return;
257 wxSetCursor(gs_savedCursor
);
258 gs_savedCursor
= wxNullCursor
;
261 void wxBeginBusyCursor(wxCursor
*cursor
)
263 if ( gs_busyCount
++ > 0 ) return;
265 wxASSERT_MSG( !gs_savedCursor
.Ok(),
266 wxT("forgot to call wxEndBusyCursor, will leak memory") );
268 gs_savedCursor
= gs_globalCursor
;
270 wxSetCursor(*cursor
);
272 wxSetCursor(wxCursor(wxCURSOR_WAIT
));
277 return (gs_busyCount
> 0);
282 //-----------------------------------------------------------------------------
283 // module - clean up code
284 //-----------------------------------------------------------------------------
286 class wxCursorModule
: public wxModule
289 virtual bool OnInit()
291 gs_cursorsHash
= new wxCursorsHash
;
295 virtual void OnExit()
297 wxDELETE(gs_cursorsHash
);
301 DECLARE_DYNAMIC_CLASS(wxCursorModule
)
304 IMPLEMENT_DYNAMIC_CLASS(wxCursorModule
, wxModule
)