]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
get rid of several dozen of warnings when using g++ >= 4.2 about string literals...
[wxWidgets.git] / src / gtk / cursor.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/gtk/cursor.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/cursor.h"
670f9935
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
de6185e2 17 #include "wx/utils.h"
0416c418 18 #include "wx/image.h"
5ff14574 19 #include "wx/colour.h"
88051436 20 #include "wx/bitmap.h"
670f9935
WS
21#endif // WX_PRECOMP
22
a1abca32 23#include <gtk/gtk.h>
84833214 24
c801d85f
KB
25//-----------------------------------------------------------------------------
26// wxCursor
27//-----------------------------------------------------------------------------
28
8f884a0d 29class wxCursorRefData: public wxGDIRefData
c801d85f 30{
8f884a0d 31public:
8bbe427f 32 wxCursorRefData();
d3c7fc99 33 virtual ~wxCursorRefData();
8bbe427f 34
8f884a0d
VZ
35 virtual bool IsOk() const { return m_cursor != NULL; }
36
c801d85f
KB
37 GdkCursor *m_cursor;
38};
39
8bbe427f 40wxCursorRefData::wxCursorRefData()
c801d85f 41{
2d17d68f 42 m_cursor = (GdkCursor *) NULL;
ff7b1510 43}
c801d85f 44
8bbe427f 45wxCursorRefData::~wxCursorRefData()
c801d85f 46{
bc8b8220 47 if (m_cursor) gdk_cursor_unref( m_cursor );
ff7b1510 48}
c801d85f
KB
49
50//-----------------------------------------------------------------------------
51
0c0f973f 52#define M_CURSORDATA wx_static_cast(wxCursorRefData*, m_refData)
c801d85f 53
0c0f973f 54IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
c801d85f 55
8bbe427f 56wxCursor::wxCursor()
c801d85f 57{
ff7b1510 58}
c801d85f 59
debe6624 60wxCursor::wxCursor( int cursorId )
c801d85f 61{
2d17d68f
RR
62 m_refData = new wxCursorRefData();
63
64 GdkCursorType gdk_cur = GDK_LEFT_PTR;
65 switch (cursorId)
66 {
76471ff7
VZ
67 case wxCURSOR_BLANK:
68 {
0c0f973f
PC
69 const char bits[] = { 0 };
70 const GdkColor color = { 0, 0, 0, 0 };
76471ff7
VZ
71
72 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
73 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
74 pixmap,
75 &color,
76 &color,
77 0, 0);
0c0f973f 78 g_object_unref(pixmap);
76471ff7
VZ
79 }
80 return;
81
15dadf31 82 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 83 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 84 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
6e63b5c9 85 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
2d17d68f
RR
86 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
87 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
88 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
83f96286 89 case wxCURSOR_ARROWWAIT:
7c39369e 90 case wxCURSOR_WAIT:
2d17d68f 91 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
57b10cb4 92 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
2d17d68f
RR
93 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
94 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
95 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
96 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
7c39369e 97 case wxCURSOR_SIZENWSE:
2d17d68f
RR
98 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
99 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
100 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
101 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
102 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
2d17d68f
RR
103 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
104 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
105 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
13971833
RD
106 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
107
957d856c
VZ
108 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
109 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 110/*
7c39369e
VZ
111 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
112 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
113 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
114 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
c801d85f 115*/
76471ff7 116
7c39369e 117 default:
223d09f6 118 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 119 // will use the standard one
7c39369e 120 break;
2d17d68f 121 }
8bbe427f 122
2d17d68f 123 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 124}
c801d85f 125
c2fa61e8 126extern GtkWidget *wxGetRootWindow();
a1db952f
VS
127
128wxCursor::wxCursor(const char bits[], int width, int height,
129 int hotSpotX, int hotSpotY,
f516d986 130 const char maskBits[], const wxColour *fg, const wxColour *bg)
a1db952f
VS
131{
132 if (!maskBits)
133 maskBits = bits;
134 if (!fg)
135 fg = wxBLACK;
136 if (!bg)
137 bg = wxWHITE;
138 if (hotSpotX < 0 || hotSpotX >= width)
139 hotSpotX = 0;
140 if (hotSpotY < 0 || hotSpotY >= height)
141 hotSpotY = 0;
142
c2fa61e8
RD
143 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
144 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
a1db952f
VS
145
146 m_refData = new wxCursorRefData;
147 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
4f558e23 148 data, mask, fg->GetColor(), bg->GetColor(),
a1db952f
VS
149 hotSpotX, hotSpotY );
150
3fe39b0c
MR
151 g_object_unref (data);
152 g_object_unref (mask);
a1db952f
VS
153}
154
7eff657c 155#if wxUSE_IMAGE
0fc5dbf5 156
9f556d1d
VZ
157static void GetHotSpot(const wxImage& image, int& x, int& y)
158{
159 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
160 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
161 else
162 x = 0;
163
164 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
165 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
166 else
167 y = 0;
168
169 if (x < 0 || x >= image.GetWidth())
170 x = 0;
171 if (y < 0 || y >= image.GetHeight())
172 y = 0;
173}
174
7eff657c
JS
175wxCursor::wxCursor( const wxImage & image )
176{
0fc5dbf5
VZ
177 int w = image.GetWidth() ;
178 int h = image.GetHeight();
179 bool bHasMask = image.HasMask();
88051436
PC
180 int hotSpotX, hotSpotY;
181 GetHotSpot(image, hotSpotX, hotSpotY);
182 m_refData = new wxCursorRefData;
183 wxImage image_copy(image);
7eff657c 184
88051436
PC
185 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
186 if (gdk_display_supports_cursor_color(display))
9f556d1d 187 {
88051436 188 if (!image.HasAlpha())
ff654490 189 {
88051436
PC
190 // add alpha, so wxBitmap will convert to pixbuf format
191 image_copy.InitAlpha();
cb738b8b 192 }
88051436
PC
193 wxBitmap bitmap(image_copy);
194 wxASSERT(bitmap.HasPixbuf());
ff654490
VZ
195 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
196 (
88051436
PC
197 display,
198 bitmap.GetPixbuf(),
ff654490
VZ
199 hotSpotX, hotSpotY
200 );
ff654490 201 return;
9f556d1d 202 }
9f556d1d 203
e60f8377
PC
204 unsigned long keyMaskColor = 0;
205 GdkPixmap* mask;
206 if (bHasMask)
207 {
208 keyMaskColor = wxImageHistogram::MakeKey(
209 image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
210 // get mask before image is modified
211 wxBitmap bitmap(image, 1);
212 mask = bitmap.GetMask()->GetBitmap();
213 g_object_ref(mask);
214 }
215 else
216 {
217 const int size = ((w + 7) / 8) * h;
218 char* bits = new char[size];
219 memset(bits, 0xff, size);
220 mask = gdk_bitmap_create_from_data(
221 wxGetRootWindow()->window, bits, w, h);
222 delete[] bits;
223 }
224
88051436 225 // modify image so wxBitmap can be used to convert to pixmap
e60f8377 226 image_copy.SetMask(false);
88051436
PC
227 int i, j;
228 wxByte* data = image_copy.GetData();
229 for (j = 0; j < h; j++)
0fc5dbf5 230 {
88051436 231 for (i = 0; i < w; i++, data += 3)
0fc5dbf5 232 {
0fc5dbf5 233 //if average value is > mid grey
88051436
PC
234 if (int(data[0]) + data[1] + data[2] >= 3 * 128)
235 {
236 // wxBitmap only converts (255,255,255) to white
237 data[0] = 255;
238 data[1] = 255;
239 data[2] = 255;
240 }
7eff657c 241 }
0fc5dbf5 242 }
88051436
PC
243 wxBitmap bitmap(image_copy, 1);
244
fa1fcc66 245 // find the most frequent color(s)
952ae1e8 246 wxImageHistogram histogram;
fa1fcc66 247 image.ComputeHistogram(histogram);
7eff657c 248
fa1fcc66 249 long colMostFreq = 0;
952ae1e8 250 unsigned long nMost = 0;
fa1fcc66 251 long colNextMostFreq = 0;
952ae1e8 252 unsigned long nNext = 0;
fa1fcc66
VZ
253 for ( wxImageHistogram::iterator entry = histogram.begin();
254 entry != histogram.end();
255 ++entry )
7eff657c 256 {
88051436 257 unsigned long key = entry->first;
952ae1e8 258 if ( !bHasMask || (key != keyMaskColor) )
7eff657c 259 {
88051436 260 unsigned long value = entry->second.value;
952ae1e8 261 if (value > nMost)
7eff657c 262 {
88051436
PC
263 nNext = nMost;
264 colNextMostFreq = colMostFreq;
952ae1e8 265 nMost = value;
fa1fcc66 266 colMostFreq = key;
7eff657c 267 }
952ae1e8 268 else if (value > nNext)
7eff657c 269 {
952ae1e8 270 nNext = value;
fa1fcc66 271 colNextMostFreq = key;
7eff657c
JS
272 }
273 }
274 }
275
fa1fcc66
VZ
276 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
277 (unsigned char)(colMostFreq >> 8),
278 (unsigned char)(colMostFreq) );
7eff657c 279
fa1fcc66
VZ
280 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
281 (unsigned char)(colNextMostFreq >> 8),
282 (unsigned char)(colNextMostFreq) );
7eff657c 283
624d506b
CE
284 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
285 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
86e69fa5
MR
286
287 if (bg_intensity > fg_intensity)
288 {
624d506b
CE
289 //swap fg and bg
290 wxColour tmp = fg;
291 fg = bg;
292 bg = tmp;
86e69fa5 293 }
624d506b 294
fa1fcc66
VZ
295 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
296 (
88051436 297 bitmap.GetPixmap(),
fa1fcc66
VZ
298 mask,
299 fg.GetColor(), bg.GetColor(),
300 hotSpotX, hotSpotY
301 );
7eff657c 302
3fe39b0c 303 g_object_unref (mask);
7eff657c 304}
0fc5dbf5
VZ
305
306#endif // wxUSE_IMAGE
7eff657c 307
8bbe427f 308wxCursor::~wxCursor()
c801d85f 309{
ff7b1510 310}
c801d85f 311
8f884a0d 312GdkCursor *wxCursor::GetCursor() const
c801d85f 313{
8f884a0d 314 return M_CURSORDATA->m_cursor;
ff7b1510 315}
c801d85f 316
8f884a0d 317wxGDIRefData *wxCursor::CreateGDIRefData() const
c801d85f 318{
8f884a0d
VZ
319 return new wxCursorRefData;
320}
321
322wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
323{
324 return new wxCursorRefData(*wx_static_cast(const wxCursorRefData *, data));
ff7b1510 325}
c801d85f
KB
326
327//-----------------------------------------------------------------------------
328// busy cursor routines
329//-----------------------------------------------------------------------------
330
b541538f
PC
331/* Current cursor, in order to hang on to
332 * cursor handle when setting the cursor globally */
333wxCursor g_globalCursor;
7c39369e 334
238d735d 335static wxCursor gs_savedCursor;
89a43902 336static int gs_busyCount = 0;
c801d85f 337
f6bcfd97
BP
338const wxCursor &wxBusyCursor::GetStoredCursor()
339{
340 return gs_savedCursor;
341}
342
343const wxCursor wxBusyCursor::GetBusyCursor()
344{
345 return wxCursor(wxCURSOR_WATCH);
346}
347
8bbe427f 348void wxEndBusyCursor()
c801d85f 349{
238d735d 350 if (--gs_busyCount > 0)
89a43902
VZ
351 return;
352
238d735d
RR
353 wxSetCursor( gs_savedCursor );
354 gs_savedCursor = wxNullCursor;
13971833 355
d1dea842 356 if (wxTheApp)
8b532f8c 357 wxTheApp->ProcessIdle();
ff7b1510 358}
c801d85f 359
f516d986 360void wxBeginBusyCursor( const wxCursor *WXUNUSED(cursor) )
c801d85f 361{
238d735d 362 if (gs_busyCount++ > 0)
89a43902
VZ
363 return;
364
238d735d 365 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 366 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 367
238d735d 368 gs_savedCursor = g_globalCursor;
13971833 369
238d735d 370 wxSetCursor( wxCursor(wxCURSOR_WATCH) );
13971833 371
d1dea842 372 if (wxTheApp)
3352cfff 373 wxTheApp->ProcessIdle();
15dadf31 374
7e410431 375 gdk_flush();
ff7b1510 376}
c801d85f 377
8bbe427f 378bool wxIsBusy()
c801d85f 379{
89a43902 380 return gs_busyCount > 0;
ff7b1510 381}
c801d85f
KB
382
383void wxSetCursor( const wxCursor& cursor )
384{
238d735d 385 g_globalCursor = cursor;
a1abca32 386 wxTheApp->WakeUpIdle();
ff7b1510 387}