]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk1/cursor.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Copyright: (c) 1998 Robert Roebling | |
6 | // Licence: wxWindows licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | // For compilers that support precompilation, includes "wx.h". | |
10 | #include "wx/wxprec.h" | |
11 | ||
12 | #include "wx/cursor.h" | |
13 | ||
14 | #ifndef WX_PRECOMP | |
15 | #include "wx/app.h" | |
16 | #include "wx/utils.h" | |
17 | #endif // WX_PRECOMP | |
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 wxGDIRefData | |
34 | { | |
35 | public: | |
36 | wxCursorRefData(); | |
37 | virtual ~wxCursorRefData(); | |
38 | ||
39 | GdkCursor *m_cursor; | |
40 | ||
41 | private: | |
42 | // There is no way to copy m_cursor so we can't implement a copy ctor | |
43 | // properly. | |
44 | wxDECLARE_NO_COPY_CLASS(wxCursorRefData); | |
45 | }; | |
46 | ||
47 | wxCursorRefData::wxCursorRefData() | |
48 | { | |
49 | m_cursor = NULL; | |
50 | } | |
51 | ||
52 | wxCursorRefData::~wxCursorRefData() | |
53 | { | |
54 | if (m_cursor) gdk_cursor_destroy( m_cursor ); | |
55 | } | |
56 | ||
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | #define M_CURSORDATA ((wxCursorRefData *)m_refData) | |
60 | ||
61 | IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject) | |
62 | ||
63 | wxCursor::wxCursor() | |
64 | { | |
65 | ||
66 | } | |
67 | ||
68 | void wxCursor::InitFromStock( wxStockCursor cursorId ) | |
69 | { | |
70 | m_refData = new wxCursorRefData(); | |
71 | ||
72 | GdkCursorType gdk_cur = GDK_LEFT_PTR; | |
73 | switch (cursorId) | |
74 | { | |
75 | case wxCURSOR_BLANK: | |
76 | { | |
77 | static const gchar bits[] = { 0 }; | |
78 | static /* const -- not in GTK1 */ GdkColor color = { 0, 0, 0, 0 }; | |
79 | ||
80 | GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1); | |
81 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap, | |
82 | pixmap, | |
83 | &color, | |
84 | &color, | |
85 | 0, 0); | |
86 | } | |
87 | return; | |
88 | ||
89 | case wxCURSOR_ARROW: // fall through to default | |
90 | case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break; | |
91 | case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break; | |
92 | case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break; | |
93 | case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break; | |
94 | case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break; | |
95 | case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break; | |
96 | case wxCURSOR_ARROWWAIT: | |
97 | case wxCURSOR_WAIT: | |
98 | case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break; | |
99 | case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break; | |
100 | case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break; | |
101 | case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break; | |
102 | case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break; | |
103 | case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break; | |
104 | case wxCURSOR_SIZENWSE: | |
105 | case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break; | |
106 | case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break; | |
107 | case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break; | |
108 | case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break; | |
109 | case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break; | |
110 | case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break; | |
111 | case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break; | |
112 | case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break; | |
113 | case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break; | |
114 | ||
115 | case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break; | |
116 | case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break; | |
117 | /* | |
118 | case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break; | |
119 | case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break; | |
120 | case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break; | |
121 | case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break; | |
122 | */ | |
123 | ||
124 | default: | |
125 | wxFAIL_MSG(wxT("unsupported cursor type")); | |
126 | // will use the standard one | |
127 | break; | |
128 | } | |
129 | ||
130 | M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur ); | |
131 | } | |
132 | ||
133 | extern GtkWidget *wxGetRootWindow(); | |
134 | ||
135 | wxCursor::wxCursor(const char bits[], int width, int height, | |
136 | int hotSpotX, int hotSpotY, | |
137 | const char maskBits[], const wxColour *fg, const wxColour *bg) | |
138 | { | |
139 | if (!maskBits) | |
140 | maskBits = bits; | |
141 | if (!fg) | |
142 | fg = wxBLACK; | |
143 | if (!bg) | |
144 | bg = wxWHITE; | |
145 | if (hotSpotX < 0 || hotSpotX >= width) | |
146 | hotSpotX = 0; | |
147 | if (hotSpotY < 0 || hotSpotY >= height) | |
148 | hotSpotY = 0; | |
149 | ||
150 | GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height ); | |
151 | GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height); | |
152 | ||
153 | m_refData = new wxCursorRefData; | |
154 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap( | |
155 | data, mask, fg->GetColor(), bg->GetColor(), | |
156 | hotSpotX, hotSpotY ); | |
157 | ||
158 | gdk_bitmap_unref( data ); | |
159 | gdk_bitmap_unref( mask ); | |
160 | } | |
161 | ||
162 | #if wxUSE_IMAGE | |
163 | ||
164 | wxCursor::wxCursor( const wxImage & image ) | |
165 | { | |
166 | unsigned char * rgbBits = image.GetData(); | |
167 | int w = image.GetWidth() ; | |
168 | int h = image.GetHeight(); | |
169 | bool bHasMask = image.HasMask(); | |
170 | int imagebitcount = (w*h)/8; | |
171 | ||
172 | unsigned char * bits = new unsigned char [imagebitcount]; | |
173 | unsigned char * maskBits = new unsigned char [imagebitcount]; | |
174 | ||
175 | int i, j, i8; unsigned char c, cMask; | |
176 | for (i=0; i<imagebitcount; i++) | |
177 | { | |
178 | bits[i] = 0; | |
179 | i8 = i * 8; | |
180 | ||
181 | cMask = 1; | |
182 | for (j=0; j<8; j++) | |
183 | { | |
184 | // possible overflow if we do the summation first ? | |
185 | c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3; | |
186 | //if average value is > mid grey | |
187 | if (c>127) | |
188 | bits[i] = bits[i] | cMask; | |
189 | cMask = cMask * 2; | |
190 | } | |
191 | } | |
192 | ||
193 | unsigned long keyMaskColor; | |
194 | if (bHasMask) | |
195 | { | |
196 | unsigned char | |
197 | r = image.GetMaskRed(), | |
198 | g = image.GetMaskGreen(), | |
199 | b = image.GetMaskBlue(); | |
200 | ||
201 | for (i=0; i<imagebitcount; i++) | |
202 | { | |
203 | maskBits[i] = 0x0; | |
204 | i8 = i * 8; | |
205 | ||
206 | cMask = 1; | |
207 | for (j=0; j<8; j++) | |
208 | { | |
209 | if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b) | |
210 | maskBits[i] = maskBits[i] | cMask; | |
211 | cMask = cMask * 2; | |
212 | } | |
213 | } | |
214 | ||
215 | keyMaskColor = (r << 16) | (g << 8) | b; | |
216 | } | |
217 | else // no mask | |
218 | { | |
219 | for (i=0; i<imagebitcount; i++) | |
220 | maskBits[i] = 0xFF; | |
221 | ||
222 | // init it to avoid compiler warnings | |
223 | keyMaskColor = 0; | |
224 | } | |
225 | ||
226 | // find the most frequent color(s) | |
227 | wxImageHistogram histogram; | |
228 | image.ComputeHistogram(histogram); | |
229 | ||
230 | // colors as rrggbb | |
231 | unsigned long key; | |
232 | unsigned long value; | |
233 | ||
234 | long colMostFreq = 0; | |
235 | unsigned long nMost = 0; | |
236 | long colNextMostFreq = 0; | |
237 | unsigned long nNext = 0; | |
238 | for ( wxImageHistogram::iterator entry = histogram.begin(); | |
239 | entry != histogram.end(); | |
240 | ++entry ) | |
241 | { | |
242 | value = entry->second.value; | |
243 | key = entry->first; | |
244 | if ( !bHasMask || (key != keyMaskColor) ) | |
245 | { | |
246 | if (value > nMost) | |
247 | { | |
248 | nMost = value; | |
249 | colMostFreq = key; | |
250 | } | |
251 | else if (value > nNext) | |
252 | { | |
253 | nNext = value; | |
254 | colNextMostFreq = key; | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16), | |
260 | (unsigned char)(colMostFreq >> 8), | |
261 | (unsigned char)(colMostFreq) ); | |
262 | ||
263 | wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16), | |
264 | (unsigned char)(colNextMostFreq >> 8), | |
265 | (unsigned char)(colNextMostFreq) ); | |
266 | ||
267 | int fg_intensity = fg.Red() + fg.Green() + fg.Blue(); | |
268 | int bg_intensity = bg.Red() + bg.Green() + bg.Blue(); | |
269 | ||
270 | if (bg_intensity > fg_intensity) | |
271 | { | |
272 | //swap fg and bg | |
273 | wxColour tmp = fg; | |
274 | fg = bg; | |
275 | bg = tmp; | |
276 | } | |
277 | ||
278 | int hotSpotX; | |
279 | int hotSpotY; | |
280 | ||
281 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X)) | |
282 | hotSpotX = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X); | |
283 | else | |
284 | hotSpotX = 0; | |
285 | ||
286 | if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y)) | |
287 | hotSpotY = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y); | |
288 | else | |
289 | hotSpotY = 0; | |
290 | ||
291 | if (hotSpotX < 0 || hotSpotX >= w) | |
292 | hotSpotX = 0; | |
293 | if (hotSpotY < 0 || hotSpotY >= h) | |
294 | hotSpotY = 0; | |
295 | ||
296 | GdkBitmap *data = gdk_bitmap_create_from_data(wxGetRootWindow()->window, | |
297 | (gchar *) bits, w, h); | |
298 | GdkBitmap *mask = gdk_bitmap_create_from_data(wxGetRootWindow()->window, | |
299 | (gchar *) maskBits, w, h); | |
300 | ||
301 | m_refData = new wxCursorRefData; | |
302 | M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap | |
303 | ( | |
304 | data, | |
305 | mask, | |
306 | fg.GetColor(), bg.GetColor(), | |
307 | hotSpotX, hotSpotY | |
308 | ); | |
309 | ||
310 | gdk_bitmap_unref( data ); | |
311 | gdk_bitmap_unref( mask ); | |
312 | delete [] bits; | |
313 | delete [] maskBits; | |
314 | } | |
315 | ||
316 | #endif // wxUSE_IMAGE | |
317 | ||
318 | wxCursor::~wxCursor() | |
319 | { | |
320 | } | |
321 | ||
322 | GdkCursor *wxCursor::GetCursor() const | |
323 | { | |
324 | return M_CURSORDATA->m_cursor; | |
325 | } | |
326 | ||
327 | wxGDIRefData *wxCursor::CreateGDIRefData() const | |
328 | { | |
329 | return new wxCursorRefData; | |
330 | } | |
331 | ||
332 | wxGDIRefData * | |
333 | wxCursor::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const | |
334 | { | |
335 | wxFAIL_MSG( wxS("Cloning cursors is not implemented in wxGTK.") ); | |
336 | ||
337 | return new wxCursorRefData; | |
338 | } | |
339 | ||
340 | //----------------------------------------------------------------------------- | |
341 | // busy cursor routines | |
342 | //----------------------------------------------------------------------------- | |
343 | ||
344 | extern wxCursor g_globalCursor; | |
345 | ||
346 | static wxCursor gs_savedCursor; | |
347 | static int gs_busyCount = 0; | |
348 | ||
349 | const wxCursor &wxBusyCursor::GetStoredCursor() | |
350 | { | |
351 | return gs_savedCursor; | |
352 | } | |
353 | ||
354 | const wxCursor wxBusyCursor::GetBusyCursor() | |
355 | { | |
356 | return wxCursor(wxCURSOR_WATCH); | |
357 | } | |
358 | ||
359 | void wxEndBusyCursor() | |
360 | { | |
361 | if (--gs_busyCount > 0) | |
362 | return; | |
363 | ||
364 | wxSetCursor( gs_savedCursor ); | |
365 | gs_savedCursor = wxNullCursor; | |
366 | ||
367 | if (wxTheApp) | |
368 | wxTheApp->ProcessIdle(); | |
369 | } | |
370 | ||
371 | void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) ) | |
372 | { | |
373 | if (gs_busyCount++ > 0) | |
374 | return; | |
375 | ||
376 | wxASSERT_MSG( !gs_savedCursor.IsOk(), | |
377 | wxT("forgot to call wxEndBusyCursor, will leak memory") ); | |
378 | ||
379 | gs_savedCursor = g_globalCursor; | |
380 | ||
381 | wxSetCursor( wxCursor(wxCURSOR_WATCH) ); | |
382 | ||
383 | if (wxTheApp) | |
384 | wxTheApp->ProcessIdle(); | |
385 | ||
386 | gdk_flush(); | |
387 | } | |
388 | ||
389 | bool wxIsBusy() | |
390 | { | |
391 | return gs_busyCount > 0; | |
392 | } | |
393 | ||
394 | void wxSetCursor( const wxCursor& cursor ) | |
395 | { | |
396 | if (g_isIdle) | |
397 | wxapp_install_idle_handler(); | |
398 | ||
399 | g_globalCursor = cursor; | |
400 | } |