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