Applied wxImage -> wxCursor patch, tool window event table fix
[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
64 wxCursor::wxCursor( int cursorId )
65 {
66 m_refData = new wxCursorRefData();
67
68 GdkCursorType gdk_cur = GDK_LEFT_PTR;
69 switch (cursorId)
70 {
71 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
72 case wxCURSOR_HAND: gdk_cur = GDK_HAND1; break;
73 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
74 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
75 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
76 case wxCURSOR_ARROWWAIT:
77 case wxCURSOR_WAIT:
78 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
79 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
80 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
81 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
82 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
83 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
84 case wxCURSOR_SIZENWSE:
85 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
86 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
87 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
88 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
89 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
90 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
91 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
92 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
93 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
94
95 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
96 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
97 /*
98 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
99 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
100 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
101 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
102 */
103 default:
104 wxFAIL_MSG(wxT("unsupported cursor type"));
105 // will use the standard one
106
107 case wxCURSOR_ARROW:
108 break;
109 }
110
111 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
112 }
113
114 extern GtkWidget *wxGetRootWindow();
115
116 wxCursor::wxCursor(const char bits[], int width, int height,
117 int hotSpotX, int hotSpotY,
118 const char maskBits[], wxColour *fg, wxColour *bg)
119 {
120 if (!maskBits)
121 maskBits = bits;
122 if (!fg)
123 fg = wxBLACK;
124 if (!bg)
125 bg = wxWHITE;
126 if (hotSpotX < 0 || hotSpotX >= width)
127 hotSpotX = 0;
128 if (hotSpotY < 0 || hotSpotY >= height)
129 hotSpotY = 0;
130
131 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
132 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
133
134 m_refData = new wxCursorRefData;
135 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
136 data, mask, fg->GetColor(), bg->GetColor(),
137 hotSpotX, hotSpotY );
138
139 gdk_bitmap_unref( data );
140 gdk_bitmap_unref( mask );
141 }
142
143
144 wxCursor::wxCursor( const wxCursor &cursor )
145 {
146 Ref( cursor );
147 }
148
149 #if wxUSE_IMAGE
150 wxCursor::wxCursor( const wxImage & image )
151 {
152 unsigned char * rgbBits = image.GetData();
153 int w = image.GetWidth() ;
154 int h = image.GetHeight() ;
155 bool bHasMask = image.HasMask() ;
156 int imagebitcount = (w*h)/8;
157
158 unsigned char r, g, b ;
159 unsigned char * bits = new unsigned char [imagebitcount];
160 unsigned char * maskBits = new unsigned char [imagebitcount];
161
162 int i,j, i8; unsigned char c, cMask;
163 for (i=0; i<imagebitcount; i++)
164 {
165 bits[i] = 0;
166 i8 = i * 8;
167
168 cMask = 1;
169 for (j=0; j<8; j++)
170 {
171 // possible overflow if we do the summation first ?
172 c = rgbBits[(i8+j)*3]/3 + rgbBits[(i8+j)*3+1]/3 + rgbBits[(i8+j)*3+2]/3 ;
173 //if average value is > mid grey
174 if (c>127)
175 bits[i] = bits[i] | cMask ;
176 cMask = cMask * 2 ;
177 }
178 }
179 if (bHasMask)
180 {
181 r = image.GetMaskRed() ;
182 g = image.GetMaskGreen() ;
183 b = image.GetMaskBlue() ;
184
185 for (i=0; i<imagebitcount; i++)
186 {
187 maskBits[i] = 0x0;
188 i8 = i * 8;
189
190 cMask = 1;
191 for (j=0; j<8; j++)
192 {
193 if (rgbBits[(i8+j)*3] != r || rgbBits[(i8+j)*3+1] != g || rgbBits[(i8+j)*3+2] != b)
194 maskBits[i] = maskBits[i] | cMask ;
195 cMask = cMask * 2 ;
196 }
197 }
198 }
199 else
200 {
201 for (i=0; i<imagebitcount; i++)
202 maskBits[i]= 0xFF ;
203 }
204 //find the most frequent color(s)
205 //it seems a waste of effort to copy the image
206 //but otherwise we need to remove the const modifier ??
207 wxImage tmpImage = image.Copy();
208 wxHashTable hTable;
209
210 //colors as rrggbb
211 unsigned long key;
212 unsigned long keyMaskColor = 0;
213 if (bHasMask) keyMaskColor = (r << 16) | (g << 8) | b;
214
215 tmpImage.ComputeHistogram( hTable );
216
217 long MostFreqCol = 0 ; long nMost = 0;
218 long NextFreqCol = 0 ; long nNext = 0;
219 long value ;
220 hTable.BeginFind();
221 wxNode *node = NULL;
222 while ((node = hTable.Next()) != NULL)
223 {
224 wxHNode *hnode = (wxHNode*) node->GetData();
225 value = hnode->value;
226 key = node->GetKeyInteger() ;
227 if (!bHasMask || (key != keyMaskColor) )
228 {
229 if (value > nMost)
230 {
231 nMost = value;
232 MostFreqCol = key;
233 }
234 else
235 if (value > nNext)
236 {
237 nNext = value ;
238 NextFreqCol = key;
239 }
240 }
241 }
242
243
244 wxColour fg = wxColour ( (unsigned char)(MostFreqCol >> 16),
245 (unsigned char)(MostFreqCol >> 8),
246 (unsigned char)(MostFreqCol) ) ;
247
248 wxColour bg = wxColour ( (unsigned char)(NextFreqCol >> 16),
249 (unsigned char)(NextFreqCol >> 8),
250 (unsigned char)(NextFreqCol) ) ;
251
252
253
254 int hotSpotX=0;
255 int hotSpotY=0;
256
257 if (image.HasOption(wxCUR_HOTSPOT_X))
258 hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
259 if (image.HasOption(wxCUR_HOTSPOT_Y))
260 hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
261
262 if (hotSpotX < 0 || hotSpotX >= w)
263 hotSpotX = 0;
264 if (hotSpotY < 0 || hotSpotY >= h)
265 hotSpotY = 0;
266
267 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits,
268 w, h );
269 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits,
270 w, h );
271
272 m_refData = new wxCursorRefData;
273 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
274 data, mask, fg.GetColor(), bg.GetColor(),
275 hotSpotX, hotSpotY );
276
277 gdk_bitmap_unref( data );
278 gdk_bitmap_unref( mask );
279 delete [] bits ;
280 delete [] maskBits;
281
282 }
283 #endif
284
285 wxCursor::~wxCursor()
286 {
287 }
288
289 wxCursor& wxCursor::operator = ( const wxCursor& cursor )
290 {
291 if (*this == cursor)
292 return (*this);
293
294 Ref( cursor );
295
296 return *this;
297 }
298
299 bool wxCursor::operator == ( const wxCursor& cursor ) const
300 {
301 return m_refData == cursor.m_refData;
302 }
303
304 bool wxCursor::operator != ( const wxCursor& cursor ) const
305 {
306 return m_refData != cursor.m_refData;
307 }
308
309 bool wxCursor::Ok() const
310 {
311 return (m_refData != NULL);
312 }
313
314 GdkCursor *wxCursor::GetCursor() const
315 {
316 return M_CURSORDATA->m_cursor;
317 }
318
319 //-----------------------------------------------------------------------------
320 // busy cursor routines
321 //-----------------------------------------------------------------------------
322
323 extern wxCursor g_globalCursor;
324
325 static wxCursor gs_savedCursor;
326 static int gs_busyCount = 0;
327
328 const wxCursor &wxBusyCursor::GetStoredCursor()
329 {
330 return gs_savedCursor;
331 }
332
333 const wxCursor wxBusyCursor::GetBusyCursor()
334 {
335 return wxCursor(wxCURSOR_WATCH);
336 }
337
338 void wxEndBusyCursor()
339 {
340 if (--gs_busyCount > 0)
341 return;
342
343 wxSetCursor( gs_savedCursor );
344 gs_savedCursor = wxNullCursor;
345
346 if (wxTheApp)
347 wxTheApp->SendIdleEvents();
348 }
349
350 void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
351 {
352 if (gs_busyCount++ > 0)
353 return;
354
355 wxASSERT_MSG( !gs_savedCursor.Ok(),
356 wxT("forgot to call wxEndBusyCursor, will leak memory") );
357
358 gs_savedCursor = g_globalCursor;
359
360 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
361
362 if (wxTheApp)
363 wxTheApp->SendIdleEvents();
364
365 gdk_flush();
366 }
367
368 bool wxIsBusy()
369 {
370 return gs_busyCount > 0;
371 }
372
373 void wxSetCursor( const wxCursor& cursor )
374 {
375 if (g_isIdle)
376 wxapp_install_idle_handler();
377
378 g_globalCursor = cursor;
379 }