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"
23 #include "wx/hashmap.h"
26 #include "wx/module.h"
28 #include "wx/mgl/private.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 class wxCursorRefData
: public wxObjectRefData
45 wxCursorRefData::wxCursorRefData()
47 m_cursor
= (MGLCursor
*) NULL
;
50 wxCursorRefData::~wxCursorRefData()
55 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
57 //-----------------------------------------------------------------------------
59 WX_DECLARE_HASH_MAP(int, wxCursor
, wxIntegerHash
, wxIntegerEqual
, wxCursorsHash
);
61 static wxCursorsHash
*gs_cursorsHash
= NULL
;
63 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
69 wxCursor::wxCursor(int cursorId
)
71 if ( !gs_cursorsHash
)
72 gs_cursorsHash
= new wxCursorsHash
;
74 if ( gs_cursorsHash
->find(cursorId
) != gs_cursorsHash
->end() )
76 wxLogTrace(_T("mglcursor"), _T("cursor id %i fetched from cache"), cursorId
);
77 *this = (*gs_cursorsHash
)[cursorId
];
81 const char *cursorname
= NULL
;
82 m_refData
= new wxCursorRefData();
86 case wxCURSOR_ARROW
: cursorname
= "arrow.cur"; break;
87 case wxCURSOR_RIGHT_ARROW
: cursorname
= "rightarr.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
;
118 wxFAIL_MSG(wxT("unsupported cursor type"));
122 M_CURSORDATA
->m_cursor
= new MGLCursor(cursorname
);
124 // if we cannot load arrow cursor, use MGL's default arrow cursor:
125 if ( !M_CURSORDATA
->m_cursor
->valid() && cursorId
== wxCURSOR_ARROW
)
127 delete M_CURSORDATA
->m_cursor
;
128 M_CURSORDATA
->m_cursor
= new MGLCursor(MGL_DEF_CURSOR
);
131 if ( !M_CURSORDATA
->m_cursor
->valid() )
133 wxLogError(_("Couldn't create cursor."));
138 (*gs_cursorsHash
)[cursorId
] = *this;
139 wxLogTrace(_T("mglcursor"), _T("cursor id %i added to cache (%s)"),
140 cursorId
, cursorname
);
144 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
146 int WXUNUSED(height
),
147 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
148 const char WXUNUSED(maskBits
)[],
149 wxColour
* WXUNUSED(fg
), wxColour
* WXUNUSED(bg
) )
154 wxCursor::wxCursor(const wxString
& cursor_file
,
156 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
))
158 if ( flags
== wxBITMAP_TYPE_CUR
|| flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
160 m_refData
= new wxCursorRefData();
161 M_CURSORDATA
->m_cursor
= new MGLCursor(cursor_file
.mb_str());
162 if ( !M_CURSORDATA
->m_cursor
->valid() )
164 wxLogError(_("Couldn't create cursor."));
170 wxLogError(wxT("Cannot load cursor resource of this type."));
174 wxCursor::~wxCursor()
176 // wxObject unrefs data
179 bool wxCursor::operator == (const wxCursor
& cursor
) const
181 return (m_refData
== cursor
.m_refData
);
184 bool wxCursor::operator != (const wxCursor
& cursor
) const
186 return (m_refData
!= cursor
.m_refData
);
189 bool wxCursor::Ok() const
191 return (m_refData
!= NULL
);
194 MGLCursor
*wxCursor::GetMGLCursor() const
196 return M_CURSORDATA
->m_cursor
;
201 // ----------------------------------------------------------------------------
202 // Global cursor setting
203 // ----------------------------------------------------------------------------
205 static wxCursor gs_globalCursor
= wxNullCursor
;
207 void wxSetCursor(const wxCursor
& cursor
)
212 MGL_wmSetGlobalCursor(g_winMng
, *cursor
.GetMGLCursor());
213 gs_globalCursor
= cursor
;
218 MGL_wmSetGlobalCursor(g_winMng
, NULL
);
219 gs_globalCursor
= wxNullCursor
;
225 //-----------------------------------------------------------------------------
226 // busy cursor routines
227 //-----------------------------------------------------------------------------
229 static wxCursor gs_savedCursor
= wxNullCursor
;
230 static int gs_busyCount
= 0;
232 const wxCursor
&wxBusyCursor::GetStoredCursor()
234 return gs_savedCursor
;
237 const wxCursor
wxBusyCursor::GetBusyCursor()
239 return gs_globalCursor
;
242 void wxEndBusyCursor()
244 if ( --gs_busyCount
> 0 ) return;
246 wxSetCursor(gs_savedCursor
);
247 gs_savedCursor
= wxNullCursor
;
250 void wxBeginBusyCursor(const wxCursor
*cursor
)
252 if ( gs_busyCount
++ > 0 ) return;
254 wxASSERT_MSG( !gs_savedCursor
.Ok(),
255 wxT("forgot to call wxEndBusyCursor, will leak memory") );
257 gs_savedCursor
= gs_globalCursor
;
259 wxSetCursor(*cursor
);
261 wxSetCursor(wxCursor(wxCURSOR_WAIT
));
266 return (gs_busyCount
> 0);
271 //-----------------------------------------------------------------------------
272 // module - clean up code
273 //-----------------------------------------------------------------------------
275 class wxCursorModule
: public wxModule
278 virtual bool OnInit() { return true; }
280 virtual void OnExit()
282 wxDELETE(gs_cursorsHash
);
286 DECLARE_DYNAMIC_CLASS(wxCursorModule
)
289 IMPLEMENT_DYNAMIC_CLASS(wxCursorModule
, wxModule
)