Changed wxRootWindow from a global to a staic variable with an
[wxWidgets.git] / src / gtk1 / cursor.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cursor.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "cursor.h"
13 #endif
14
15 #include "wx/cursor.h"
16 #include "wx/utils.h"
17 #include "wx/app.h"
18
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // wxCursor
31 //-----------------------------------------------------------------------------
32
33 class wxCursorRefData: public wxObjectRefData
34 {
35 public:
36
37 wxCursorRefData();
38 ~wxCursorRefData();
39
40 GdkCursor *m_cursor;
41 };
42
43 wxCursorRefData::wxCursorRefData()
44 {
45 m_cursor = (GdkCursor *) NULL;
46 }
47
48 wxCursorRefData::~wxCursorRefData()
49 {
50 if (m_cursor) gdk_cursor_destroy( m_cursor );
51 }
52
53 //-----------------------------------------------------------------------------
54
55 #define M_CURSORDATA ((wxCursorRefData *)m_refData)
56
57 IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
58
59 wxCursor::wxCursor()
60 {
61 }
62
63 wxCursor::wxCursor( int cursorId )
64 {
65 m_refData = new wxCursorRefData();
66
67 GdkCursorType gdk_cur = GDK_LEFT_PTR;
68 switch (cursorId)
69 {
70 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
71 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
72 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
73 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
74 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
75 case wxCURSOR_ARROWWAIT:
76 case wxCURSOR_WAIT:
77 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
78 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
79 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
80 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
81 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
82 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
83 case wxCURSOR_SIZENWSE:
84 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
85 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
86 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
87 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
88 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
89 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
90 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
91 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
92 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
93
94 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
95 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
96 /*
97 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
98 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
99 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
100 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
101 */
102 default:
103 wxFAIL_MSG(wxT("unsupported cursor type"));
104 // will use the standard one
105
106 case wxCURSOR_ARROW:
107 break;
108 }
109
110 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
111 }
112
113 extern GtkWidget *wxGetRootWindow();
114
115 wxCursor::wxCursor(const char bits[], int width, int height,
116 int hotSpotX, int hotSpotY,
117 const char maskBits[], wxColour *fg, wxColour *bg)
118 {
119 if (!maskBits)
120 maskBits = bits;
121 if (!fg)
122 fg = wxBLACK;
123 if (!bg)
124 bg = wxWHITE;
125 if (hotSpotX < 0 || hotSpotX >= width)
126 hotSpotX = 0;
127 if (hotSpotY < 0 || hotSpotY >= height)
128 hotSpotY = 0;
129
130 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
131 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
132
133 m_refData = new wxCursorRefData;
134 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
135 data, mask, fg->GetColor(), bg->GetColor(),
136 hotSpotX, hotSpotY );
137
138 gdk_bitmap_unref( data );
139 gdk_bitmap_unref( mask );
140 }
141
142
143 wxCursor::wxCursor( const wxCursor &cursor )
144 {
145 Ref( cursor );
146 }
147
148 wxCursor::~wxCursor()
149 {
150 }
151
152 wxCursor& wxCursor::operator = ( const wxCursor& cursor )
153 {
154 if (*this == cursor)
155 return (*this);
156
157 Ref( cursor );
158
159 return *this;
160 }
161
162 bool wxCursor::operator == ( const wxCursor& cursor ) const
163 {
164 return m_refData == cursor.m_refData;
165 }
166
167 bool wxCursor::operator != ( const wxCursor& cursor ) const
168 {
169 return m_refData != cursor.m_refData;
170 }
171
172 bool wxCursor::Ok() const
173 {
174 return (m_refData != NULL);
175 }
176
177 GdkCursor *wxCursor::GetCursor() const
178 {
179 return M_CURSORDATA->m_cursor;
180 }
181
182 //-----------------------------------------------------------------------------
183 // busy cursor routines
184 //-----------------------------------------------------------------------------
185
186 extern wxCursor g_globalCursor;
187
188 static wxCursor gs_savedCursor;
189 static int gs_busyCount = 0;
190
191 const wxCursor &wxBusyCursor::GetStoredCursor()
192 {
193 return gs_savedCursor;
194 }
195
196 const wxCursor wxBusyCursor::GetBusyCursor()
197 {
198 return wxCursor(wxCURSOR_WATCH);
199 }
200
201 void wxEndBusyCursor()
202 {
203 if (--gs_busyCount > 0)
204 return;
205
206 wxSetCursor( gs_savedCursor );
207 gs_savedCursor = wxNullCursor;
208
209 if (wxTheApp)
210 wxTheApp->SendIdleEvents();
211 }
212
213 void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
214 {
215 if (gs_busyCount++ > 0)
216 return;
217
218 wxASSERT_MSG( !gs_savedCursor.Ok(),
219 wxT("forgot to call wxEndBusyCursor, will leak memory") );
220
221 gs_savedCursor = g_globalCursor;
222
223 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
224
225 if (wxTheApp)
226 wxTheApp->SendIdleEvents();
227 }
228
229 bool wxIsBusy()
230 {
231 return gs_busyCount > 0;
232 }
233
234 void wxSetCursor( const wxCursor& cursor )
235 {
236 if (g_isIdle)
237 wxapp_install_idle_handler();
238
239 g_globalCursor = cursor;
240 }