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"
24 #include "wx/module.h"
27 #include "wx/mgl/private.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 class wxCursorRefData
: public wxObjectRefData
39 virtual ~wxCursorRefData();
44 wxCursorRefData::wxCursorRefData()
46 m_cursor
= (MGLCursor
*) NULL
;
49 wxCursorRefData::~wxCursorRefData()
54 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
56 //-----------------------------------------------------------------------------
58 WX_DECLARE_HASH_MAP(int, wxCursor
, wxIntegerHash
, wxIntegerEqual
, wxCursorsHash
);
60 static wxCursorsHash
*gs_cursorsHash
= NULL
;
62 IMPLEMENT_DYNAMIC_CLASS(wxCursor
,wxObject
)
68 wxCursor::wxCursor(int cursorId
)
70 if ( !gs_cursorsHash
)
71 gs_cursorsHash
= new wxCursorsHash
;
73 if ( gs_cursorsHash
->find(cursorId
) != gs_cursorsHash
->end() )
75 wxLogTrace(_T("mglcursor"), _T("cursor id %i fetched from cache"), cursorId
);
76 *this = (*gs_cursorsHash
)[cursorId
];
80 const char *cursorname
= NULL
;
81 m_refData
= new wxCursorRefData();
85 case wxCURSOR_ARROW
: cursorname
= "arrow.cur"; break;
86 case wxCURSOR_RIGHT_ARROW
: cursorname
= "rightarr.cur"; break;
87 case wxCURSOR_BULLSEYE
: cursorname
= "bullseye.cur"; break;
88 case wxCURSOR_CHAR
: cursorname
= "char.cur"; break;
89 case wxCURSOR_CROSS
: cursorname
= "cross.cur"; break;
90 case wxCURSOR_HAND
: cursorname
= "hand.cur"; break;
91 case wxCURSOR_IBEAM
: cursorname
= "ibeam.cur"; break;
92 case wxCURSOR_LEFT_BUTTON
: cursorname
= "leftbtn.cur"; break;
93 case wxCURSOR_MAGNIFIER
: cursorname
= "magnif1.cur"; break;
94 case wxCURSOR_MIDDLE_BUTTON
: cursorname
= "midbtn.cur"; break;
95 case wxCURSOR_NO_ENTRY
: cursorname
= "noentry.cur"; break;
96 case wxCURSOR_PAINT_BRUSH
: cursorname
= "pbrush.cur"; break;
97 case wxCURSOR_PENCIL
: cursorname
= "pencil.cur"; break;
98 case wxCURSOR_POINT_LEFT
: cursorname
= "pntleft.cur"; break;
99 case wxCURSOR_POINT_RIGHT
: cursorname
= "pntright.cur"; break;
100 case wxCURSOR_QUESTION_ARROW
: cursorname
= "query.cur"; break;
101 case wxCURSOR_RIGHT_BUTTON
: cursorname
= "rightbtn.cur"; break;
102 case wxCURSOR_SIZENESW
: cursorname
= "sizenesw.cur"; break;
103 case wxCURSOR_SIZENS
: cursorname
= "sizens.cur"; break;
104 case wxCURSOR_SIZENWSE
: cursorname
= "sizenwse.cur"; break;
105 case wxCURSOR_SIZEWE
: cursorname
= "sizewe.cur"; break;
106 case wxCURSOR_SIZING
: cursorname
= "size.cur"; break;
107 case wxCURSOR_SPRAYCAN
: cursorname
= "spraycan.cur"; break;
108 case wxCURSOR_WAIT
: cursorname
= "wait.cur"; break;
109 case wxCURSOR_WATCH
: cursorname
= "clock.cur"; break;
110 case wxCURSOR_BLANK
: cursorname
= "blank.cur"; break;
113 *this = wxNullCursor
;
117 wxFAIL_MSG(wxT("unsupported cursor type"));
121 M_CURSORDATA
->m_cursor
= new MGLCursor(cursorname
);
123 // if we cannot load arrow cursor, use MGL's default arrow cursor:
124 if ( !M_CURSORDATA
->m_cursor
->valid() && cursorId
== wxCURSOR_ARROW
)
126 delete M_CURSORDATA
->m_cursor
;
127 M_CURSORDATA
->m_cursor
= new MGLCursor(MGL_DEF_CURSOR
);
130 if ( !M_CURSORDATA
->m_cursor
->valid() )
132 wxLogError(_("Couldn't create cursor."));
137 (*gs_cursorsHash
)[cursorId
] = *this;
138 wxLogTrace(_T("mglcursor"), _T("cursor id %i added to cache (%s)"),
139 cursorId
, cursorname
);
143 wxCursor::wxCursor(const char WXUNUSED(bits
)[],
145 int WXUNUSED(height
),
146 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
),
147 const char WXUNUSED(maskBits
)[],
148 wxColour
* WXUNUSED(fg
), wxColour
* WXUNUSED(bg
) )
153 wxCursor::wxCursor(const wxString
& cursor_file
,
155 int WXUNUSED(hotSpotX
), int WXUNUSED(hotSpotY
))
157 if ( flags
== wxBITMAP_TYPE_CUR
|| flags
== wxBITMAP_TYPE_CUR_RESOURCE
)
159 m_refData
= new wxCursorRefData();
160 M_CURSORDATA
->m_cursor
= new MGLCursor(cursor_file
.mb_str());
161 if ( !M_CURSORDATA
->m_cursor
->valid() )
163 wxLogError(_("Couldn't create cursor."));
169 wxLogError(wxT("Cannot load cursor resource of this type."));
173 wxCursor::~wxCursor()
175 // wxObject unrefs data
178 bool wxCursor::IsOk() const
180 return (m_refData
!= NULL
);
183 MGLCursor
*wxCursor::GetMGLCursor() const
185 return M_CURSORDATA
->m_cursor
;
190 // ----------------------------------------------------------------------------
191 // Global cursor setting
192 // ----------------------------------------------------------------------------
194 static wxCursor gs_globalCursor
= wxNullCursor
;
196 void wxSetCursor(const wxCursor
& cursor
)
201 MGL_wmSetGlobalCursor(g_winMng
, *cursor
.GetMGLCursor());
202 gs_globalCursor
= cursor
;
207 MGL_wmSetGlobalCursor(g_winMng
, NULL
);
208 gs_globalCursor
= wxNullCursor
;
214 //-----------------------------------------------------------------------------
215 // busy cursor routines
216 //-----------------------------------------------------------------------------
218 static wxCursor gs_savedCursor
= wxNullCursor
;
219 static int gs_busyCount
= 0;
221 const wxCursor
&wxBusyCursor::GetStoredCursor()
223 return gs_savedCursor
;
226 const wxCursor
wxBusyCursor::GetBusyCursor()
228 return gs_globalCursor
;
231 void wxEndBusyCursor()
233 if ( --gs_busyCount
> 0 ) return;
235 wxSetCursor(gs_savedCursor
);
236 gs_savedCursor
= wxNullCursor
;
239 void wxBeginBusyCursor(const wxCursor
*cursor
)
241 if ( gs_busyCount
++ > 0 ) return;
243 wxASSERT_MSG( !gs_savedCursor
.Ok(),
244 wxT("forgot to call wxEndBusyCursor, will leak memory") );
246 gs_savedCursor
= gs_globalCursor
;
248 wxSetCursor(*cursor
);
250 wxSetCursor(wxCursor(wxCURSOR_WAIT
));
255 return (gs_busyCount
> 0);
260 //-----------------------------------------------------------------------------
261 // module - clean up code
262 //-----------------------------------------------------------------------------
264 class wxCursorModule
: public wxModule
267 virtual bool OnInit() { return true; }
269 virtual void OnExit()
271 wxDELETE(gs_cursorsHash
);
275 DECLARE_DYNAMIC_CLASS(wxCursorModule
)
278 IMPLEMENT_DYNAMIC_CLASS(wxCursorModule
, wxModule
)