]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/cursor.cpp
Various changes to make pop up menus work
[wxWidgets.git] / src / gtk1 / cursor.cpp
... / ...
CommitLineData
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
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
29//-----------------------------------------------------------------------------
30// wxCursor
31//-----------------------------------------------------------------------------
32
33class wxCursorRefData: public wxObjectRefData
34{
35 public:
36
37 wxCursorRefData();
38 ~wxCursorRefData();
39
40 GdkCursor *m_cursor;
41};
42
43wxCursorRefData::wxCursorRefData()
44{
45 m_cursor = (GdkCursor *) NULL;
46}
47
48wxCursorRefData::~wxCursorRefData()
49{
50 if (m_cursor) gdk_cursor_destroy( m_cursor );
51}
52
53//-----------------------------------------------------------------------------
54
55#define M_CURSORDATA ((wxCursorRefData *)m_refData)
56
57IMPLEMENT_DYNAMIC_CLASS(wxCursor,wxObject)
58
59wxCursor::wxCursor()
60{
61
62}
63
64wxCursor::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
114extern GtkWidget *wxGetRootWindow();
115
116wxCursor::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
144wxCursor::wxCursor( const wxCursor &cursor )
145{
146 Ref( cursor );
147}
148
149#if wxUSE_IMAGE
150wxCursor::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 wxImageHistogram histogram;
209
210 //colors as rrggbb
211 unsigned long key;
212 unsigned long value;
213 unsigned long keyMaskColor = 0;
214 if (bHasMask)
215 keyMaskColor = (r << 16) | (g << 8) | b;
216
217 tmpImage.ComputeHistogram(histogram);
218
219 long MostFreqCol = 0;
220 unsigned long nMost = 0;
221 long NextFreqCol = 0;
222 unsigned long nNext = 0;
223 wxImageHistogram::iterator entry = histogram.begin();
224
225 while ( entry != histogram.end() )
226 {
227 value = entry->second.value;
228 key = entry->first;
229 if ( !bHasMask || (key != keyMaskColor) )
230 {
231 if (value > nMost)
232 {
233 nMost = value;
234 MostFreqCol = key;
235 }
236 else if (value > nNext)
237 {
238 nNext = value;
239 NextFreqCol = key;
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 int hotSpotX=0;
253 int hotSpotY=0;
254
255 if (image.HasOption(wxCUR_HOTSPOT_X))
256 hotSpotX = image.GetOptionInt(wxCUR_HOTSPOT_X);
257 if (image.HasOption(wxCUR_HOTSPOT_Y))
258 hotSpotY = image.GetOptionInt(wxCUR_HOTSPOT_Y);
259
260 if (hotSpotX < 0 || hotSpotX >= w)
261 hotSpotX = 0;
262 if (hotSpotY < 0 || hotSpotY >= h)
263 hotSpotY = 0;
264
265 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits,
266 w, h );
267 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits,
268 w, h );
269
270 m_refData = new wxCursorRefData;
271 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
272 data, mask, fg.GetColor(), bg.GetColor(),
273 hotSpotX, hotSpotY );
274
275 gdk_bitmap_unref( data );
276 gdk_bitmap_unref( mask );
277 delete [] bits ;
278 delete [] maskBits;
279}
280#endif
281
282wxCursor::~wxCursor()
283{
284}
285
286wxCursor& wxCursor::operator = ( const wxCursor& cursor )
287{
288 if (*this == cursor)
289 return (*this);
290
291 Ref( cursor );
292
293 return *this;
294}
295
296bool wxCursor::operator == ( const wxCursor& cursor ) const
297{
298 return m_refData == cursor.m_refData;
299}
300
301bool wxCursor::operator != ( const wxCursor& cursor ) const
302{
303 return m_refData != cursor.m_refData;
304}
305
306bool wxCursor::Ok() const
307{
308 return (m_refData != NULL);
309}
310
311GdkCursor *wxCursor::GetCursor() const
312{
313 return M_CURSORDATA->m_cursor;
314}
315
316//-----------------------------------------------------------------------------
317// busy cursor routines
318//-----------------------------------------------------------------------------
319
320extern wxCursor g_globalCursor;
321
322static wxCursor gs_savedCursor;
323static int gs_busyCount = 0;
324
325const wxCursor &wxBusyCursor::GetStoredCursor()
326{
327 return gs_savedCursor;
328}
329
330const wxCursor wxBusyCursor::GetBusyCursor()
331{
332 return wxCursor(wxCURSOR_WATCH);
333}
334
335void wxEndBusyCursor()
336{
337 if (--gs_busyCount > 0)
338 return;
339
340 wxSetCursor( gs_savedCursor );
341 gs_savedCursor = wxNullCursor;
342
343 if (wxTheApp)
344 wxTheApp->SendIdleEvents();
345}
346
347void wxBeginBusyCursor( wxCursor *WXUNUSED(cursor) )
348{
349 if (gs_busyCount++ > 0)
350 return;
351
352 wxASSERT_MSG( !gs_savedCursor.Ok(),
353 wxT("forgot to call wxEndBusyCursor, will leak memory") );
354
355 gs_savedCursor = g_globalCursor;
356
357 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
358
359 if (wxTheApp)
360 wxTheApp->SendIdleEvents();
361
362 gdk_flush();
363}
364
365bool wxIsBusy()
366{
367 return gs_busyCount > 0;
368}
369
370void wxSetCursor( const wxCursor& cursor )
371{
372 if (g_isIdle)
373 wxapp_install_idle_handler();
374
375 g_globalCursor = cursor;
376}