]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/cursor.cpp
Don't reset bullet number and outline number when applying style sheet.
[wxWidgets.git] / src / gtk / cursor.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/gtk/cursor.cpp
d6764050 3// Purpose: wxCursor implementation
c801d85f 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
df8d50a0 16 #include "wx/window.h"
670f9935 17 #include "wx/app.h"
0416c418 18 #include "wx/image.h"
88051436 19 #include "wx/bitmap.h"
cfaf8bd1 20 #include "wx/log.h"
670f9935
WS
21#endif // WX_PRECOMP
22
a1abca32 23#include <gtk/gtk.h>
833fb475 24#include "wx/gtk/private/object.h"
84833214 25
c801d85f 26//-----------------------------------------------------------------------------
d6764050 27// wxCursorRefData
c801d85f
KB
28//-----------------------------------------------------------------------------
29
8f884a0d 30class wxCursorRefData: public wxGDIRefData
c801d85f 31{
8f884a0d 32public:
8bbe427f 33 wxCursorRefData();
d3c7fc99 34 virtual ~wxCursorRefData();
8bbe427f 35
8f884a0d
VZ
36 virtual bool IsOk() const { return m_cursor != NULL; }
37
c801d85f
KB
38 GdkCursor *m_cursor;
39};
40
8bbe427f 41wxCursorRefData::wxCursorRefData()
c801d85f 42{
d3b9f782 43 m_cursor = NULL;
ff7b1510 44}
c801d85f 45
8bbe427f 46wxCursorRefData::~wxCursorRefData()
c801d85f 47{
bc8b8220 48 if (m_cursor) gdk_cursor_unref( m_cursor );
ff7b1510 49}
c801d85f 50
d6764050
FM
51
52//-----------------------------------------------------------------------------
53// wxCursor
c801d85f
KB
54//-----------------------------------------------------------------------------
55
5c33522f 56#define M_CURSORDATA static_cast<wxCursorRefData*>(m_refData)
c801d85f 57
0c0f973f 58IMPLEMENT_DYNAMIC_CLASS(wxCursor, wxGDIObject)
c801d85f 59
d6764050
FM
60// used in the following two ctors
61extern GtkWidget *wxGetRootWindow();
62
63
8bbe427f 64wxCursor::wxCursor()
c801d85f 65{
ff7b1510 66}
c801d85f 67
d6764050
FM
68#if wxUSE_IMAGE
69wxCursor::wxCursor(const wxString& cursor_file,
70 wxBitmapType type,
71 int hotSpotX, int hotSpotY)
72{
73 wxImage img;
74 if (!img.LoadFile(cursor_file, type))
75 return;
03647350 76
d6764050 77 // eventually set the hotspot:
a4afdcf3 78 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
d6764050 79 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotSpotX);
a4afdcf3 80 if (!img.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
d6764050 81 img.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hotSpotY);
03647350 82
d6764050
FM
83 InitFromImage(img);
84}
85
86wxCursor::wxCursor(const wxImage& img)
87{
88 InitFromImage(img);
89}
90#endif
91
92wxCursor::wxCursor(const char bits[], int width, int height,
93 int hotSpotX, int hotSpotY,
94 const char maskBits[], const wxColour *fg, const wxColour *bg)
95{
96 if (!maskBits)
97 maskBits = bits;
98 if (!fg)
99 fg = wxBLACK;
100 if (!bg)
101 bg = wxWHITE;
102 if (hotSpotX < 0 || hotSpotX >= width)
103 hotSpotX = 0;
104 if (hotSpotY < 0 || hotSpotY >= height)
105 hotSpotY = 0;
106
107 GdkBitmap *data = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
108 GdkBitmap *mask = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) maskBits, width, height);
109
110 m_refData = new wxCursorRefData;
111 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(
112 data, mask, fg->GetColor(), bg->GetColor(),
113 hotSpotX, hotSpotY );
114
115 g_object_unref (data);
116 g_object_unref (mask);
117}
118
119wxCursor::~wxCursor()
120{
121}
122
0ef5b1da 123void wxCursor::InitFromStock( wxStockCursor cursorId )
c801d85f 124{
2d17d68f
RR
125 m_refData = new wxCursorRefData();
126
127 GdkCursorType gdk_cur = GDK_LEFT_PTR;
128 switch (cursorId)
129 {
76471ff7
VZ
130 case wxCURSOR_BLANK:
131 {
0c0f973f
PC
132 const char bits[] = { 0 };
133 const GdkColor color = { 0, 0, 0, 0 };
76471ff7
VZ
134
135 GdkPixmap *pixmap = gdk_bitmap_create_from_data(NULL, bits, 1, 1);
136 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap(pixmap,
137 pixmap,
138 &color,
139 &color,
140 0, 0);
0c0f973f 141 g_object_unref(pixmap);
76471ff7
VZ
142 }
143 return;
144
15dadf31 145 case wxCURSOR_ARROW: // fall through to default
f7bdcdd7 146 case wxCURSOR_DEFAULT: gdk_cur = GDK_LEFT_PTR; break;
15dadf31 147 case wxCURSOR_RIGHT_ARROW: gdk_cur = GDK_RIGHT_PTR; break;
6e63b5c9 148 case wxCURSOR_HAND: gdk_cur = GDK_HAND2; break;
2d17d68f
RR
149 case wxCURSOR_CROSS: gdk_cur = GDK_CROSSHAIR; break;
150 case wxCURSOR_SIZEWE: gdk_cur = GDK_SB_H_DOUBLE_ARROW; break;
151 case wxCURSOR_SIZENS: gdk_cur = GDK_SB_V_DOUBLE_ARROW; break;
83f96286 152 case wxCURSOR_ARROWWAIT:
7c39369e 153 case wxCURSOR_WAIT:
2d17d68f 154 case wxCURSOR_WATCH: gdk_cur = GDK_WATCH; break;
57b10cb4 155 case wxCURSOR_SIZING: gdk_cur = GDK_SIZING; break;
2d17d68f
RR
156 case wxCURSOR_SPRAYCAN: gdk_cur = GDK_SPRAYCAN; break;
157 case wxCURSOR_IBEAM: gdk_cur = GDK_XTERM; break;
158 case wxCURSOR_PENCIL: gdk_cur = GDK_PENCIL; break;
159 case wxCURSOR_NO_ENTRY: gdk_cur = GDK_PIRATE; break;
7c39369e 160 case wxCURSOR_SIZENWSE:
2d17d68f
RR
161 case wxCURSOR_SIZENESW: gdk_cur = GDK_FLEUR; break;
162 case wxCURSOR_QUESTION_ARROW: gdk_cur = GDK_QUESTION_ARROW; break;
7c39369e
VZ
163 case wxCURSOR_PAINT_BRUSH: gdk_cur = GDK_SPRAYCAN; break;
164 case wxCURSOR_MAGNIFIER: gdk_cur = GDK_PLUS; break;
165 case wxCURSOR_CHAR: gdk_cur = GDK_XTERM; break;
2d17d68f
RR
166 case wxCURSOR_LEFT_BUTTON: gdk_cur = GDK_LEFTBUTTON; break;
167 case wxCURSOR_MIDDLE_BUTTON: gdk_cur = GDK_MIDDLEBUTTON; break;
168 case wxCURSOR_RIGHT_BUTTON: gdk_cur = GDK_RIGHTBUTTON; break;
13971833
RD
169 case wxCURSOR_BULLSEYE: gdk_cur = GDK_TARGET; break;
170
957d856c
VZ
171 case wxCURSOR_POINT_LEFT: gdk_cur = GDK_SB_LEFT_ARROW; break;
172 case wxCURSOR_POINT_RIGHT: gdk_cur = GDK_SB_RIGHT_ARROW; break;
c801d85f 173/*
7c39369e
VZ
174 case wxCURSOR_DOUBLE_ARROW: gdk_cur = GDK_DOUBLE_ARROW; break;
175 case wxCURSOR_CROSS_REVERSE: gdk_cur = GDK_CROSS_REVERSE; break;
2d17d68f
RR
176 case wxCURSOR_BASED_ARROW_UP: gdk_cur = GDK_BASED_ARROW_UP; break;
177 case wxCURSOR_BASED_ARROW_DOWN: gdk_cur = GDK_BASED_ARROW_DOWN; break;
c801d85f 178*/
76471ff7 179
7c39369e 180 default:
223d09f6 181 wxFAIL_MSG(wxT("unsupported cursor type"));
7c39369e 182 // will use the standard one
7c39369e 183 break;
2d17d68f 184 }
8bbe427f 185
2d17d68f 186 M_CURSORDATA->m_cursor = gdk_cursor_new( gdk_cur );
ff7b1510 187}
c801d85f 188
7eff657c 189#if wxUSE_IMAGE
0fc5dbf5 190
9f556d1d
VZ
191static void GetHotSpot(const wxImage& image, int& x, int& y)
192{
193 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X))
194 x = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X);
195 else
196 x = 0;
197
198 if (image.HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y))
199 y = image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y);
200 else
201 y = 0;
202
203 if (x < 0 || x >= image.GetWidth())
204 x = 0;
205 if (y < 0 || y >= image.GetHeight())
206 y = 0;
207}
208
d6764050 209void wxCursor::InitFromImage( const wxImage & image )
7eff657c 210{
0fc5dbf5
VZ
211 int w = image.GetWidth() ;
212 int h = image.GetHeight();
213 bool bHasMask = image.HasMask();
88051436
PC
214 int hotSpotX, hotSpotY;
215 GetHotSpot(image, hotSpotX, hotSpotY);
216 m_refData = new wxCursorRefData;
217 wxImage image_copy(image);
7eff657c 218
88051436
PC
219 GdkDisplay* display = gdk_drawable_get_display(wxGetRootWindow()->window);
220 if (gdk_display_supports_cursor_color(display))
9f556d1d 221 {
88051436 222 if (!image.HasAlpha())
ff654490 223 {
88051436
PC
224 // add alpha, so wxBitmap will convert to pixbuf format
225 image_copy.InitAlpha();
cb738b8b 226 }
88051436
PC
227 wxBitmap bitmap(image_copy);
228 wxASSERT(bitmap.HasPixbuf());
ff654490
VZ
229 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixbuf
230 (
88051436
PC
231 display,
232 bitmap.GetPixbuf(),
ff654490
VZ
233 hotSpotX, hotSpotY
234 );
9f556d1d 235 }
833fb475 236 else // no colour cursor support
e60f8377 237 {
833fb475
VZ
238 unsigned long keyMaskColor = 0;
239 GdkPixmap *maskRaw;
240 if (bHasMask)
0fc5dbf5 241 {
833fb475
VZ
242 keyMaskColor = wxImageHistogram::MakeKey(
243 image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
244 // get mask before image is modified
245 wxBitmap bitmap(image, 1);
246 maskRaw = bitmap.GetMask()->GetBitmap();
247 g_object_ref(maskRaw);
7eff657c 248 }
833fb475
VZ
249 else
250 {
251 const int size = ((w + 7) / 8) * h;
252 char* bits = new char[size];
253 memset(bits, 0xff, size);
254 maskRaw = gdk_bitmap_create_from_data(
255 wxGetRootWindow()->window, bits, w, h);
256 delete[] bits;
257 }
258
259 // assign the raw pointer to wxGtkObject to ensure it is unref'd later
260 wxGtkObject<GdkPixmap> mask(maskRaw);
261
262 // modify image so wxBitmap can be used to convert to pixmap
263 image_copy.SetMask(false);
264 wxByte* data = image_copy.GetData();
265 for (int j = 0; j < h; j++)
7eff657c 266 {
833fb475 267 for (int i = 0; i < w; i++, data += 3)
7eff657c 268 {
833fb475
VZ
269 // if average value of the pixel is > mid grey, convert it to
270 // background (0), otherwise to foreground (255, using wxBitmap
271 // convention)
272 data[0] =
273 data[1] =
274 data[2] = int(data[0]) + data[1] + data[2] >= 3 * 128 ? 0 : 255;
7eff657c 275 }
833fb475
VZ
276 }
277 wxBitmap bitmap(image_copy, 1);
278
279 // find the most frequent color(s)
280 wxImageHistogram histogram;
281 image.ComputeHistogram(histogram);
282
283 long colMostFreq = 0;
284 unsigned long nMost = 0;
285 long colNextMostFreq = 0;
286 unsigned long nNext = 0;
287 for ( wxImageHistogram::iterator entry = histogram.begin();
288 entry != histogram.end();
289 ++entry )
290 {
291 unsigned long key = entry->first;
292 if ( !bHasMask || (key != keyMaskColor) )
7eff657c 293 {
833fb475
VZ
294 unsigned long value = entry->second.value;
295 if (value > nMost)
296 {
297 nNext = nMost;
298 colNextMostFreq = colMostFreq;
299 nMost = value;
300 colMostFreq = key;
301 }
302 else if (value > nNext)
303 {
304 nNext = value;
305 colNextMostFreq = key;
306 }
7eff657c
JS
307 }
308 }
7eff657c 309
833fb475
VZ
310 wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
311 (unsigned char)(colMostFreq >> 8),
312 (unsigned char)(colMostFreq) );
7eff657c 313
833fb475
VZ
314 wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
315 (unsigned char)(colNextMostFreq >> 8),
316 (unsigned char)(colNextMostFreq) );
7eff657c 317
833fb475
VZ
318 int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
319 int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
624d506b 320
833fb475
VZ
321 if (bg_intensity > fg_intensity)
322 {
323 //swap fg and bg
324 wxColour tmp = fg;
325 fg = bg;
326 bg = tmp;
327 }
7eff657c 328
833fb475
VZ
329 M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
330 (
331 bitmap.GetPixmap(),
332 mask,
333 fg.GetColor(), bg.GetColor(),
334 hotSpotX, hotSpotY
335 );
336 }
7eff657c 337}
0fc5dbf5
VZ
338
339#endif // wxUSE_IMAGE
7eff657c 340
8f884a0d 341GdkCursor *wxCursor::GetCursor() const
c801d85f 342{
8f884a0d 343 return M_CURSORDATA->m_cursor;
ff7b1510 344}
c801d85f 345
8f884a0d 346wxGDIRefData *wxCursor::CreateGDIRefData() const
c801d85f 347{
8f884a0d
VZ
348 return new wxCursorRefData;
349}
350
351wxGDIRefData *wxCursor::CloneGDIRefData(const wxGDIRefData *data) const
352{
5c33522f 353 return new wxCursorRefData(*static_cast<const wxCursorRefData *>(data));
ff7b1510 354}
c801d85f
KB
355
356//-----------------------------------------------------------------------------
357// busy cursor routines
358//-----------------------------------------------------------------------------
359
b541538f
PC
360/* Current cursor, in order to hang on to
361 * cursor handle when setting the cursor globally */
362wxCursor g_globalCursor;
7c39369e 363
238d735d 364static wxCursor gs_savedCursor;
89a43902 365static int gs_busyCount = 0;
c801d85f 366
f6bcfd97
BP
367const wxCursor &wxBusyCursor::GetStoredCursor()
368{
369 return gs_savedCursor;
370}
371
372const wxCursor wxBusyCursor::GetBusyCursor()
373{
374 return wxCursor(wxCURSOR_WATCH);
375}
376
c2246a38 377static void UpdateCursors(const wxWindowList& list, GdkDisplay*& display)
df8d50a0
PC
378{
379 wxWindowList::const_iterator i = list.begin();
380 for (size_t n = list.size(); n--; ++i)
381 {
382 wxWindow* win = *i;
383 if (display == NULL && win->m_widget && win->m_widget->window)
384 display = gdk_drawable_get_display(win->m_widget->window);
c2246a38
RR
385 win->GTKUpdateCursor(true, false);
386 UpdateCursors(win->GetChildren(), display);
df8d50a0
PC
387 }
388}
389
8bbe427f 390void wxEndBusyCursor()
c801d85f 391{
238d735d 392 if (--gs_busyCount > 0)
89a43902
VZ
393 return;
394
df8d50a0 395 g_globalCursor = gs_savedCursor;
238d735d 396 gs_savedCursor = wxNullCursor;
df8d50a0 397 GdkDisplay* unused = NULL;
c2246a38 398 UpdateCursors(wxTopLevelWindows, unused);
ff7b1510 399}
c801d85f 400
df8d50a0 401void wxBeginBusyCursor(const wxCursor* cursor)
c801d85f 402{
238d735d 403 if (gs_busyCount++ > 0)
89a43902
VZ
404 return;
405
238d735d 406 wxASSERT_MSG( !gs_savedCursor.Ok(),
223d09f6 407 wxT("forgot to call wxEndBusyCursor, will leak memory") );
7c39369e 408
238d735d 409 gs_savedCursor = g_globalCursor;
df8d50a0
PC
410 g_globalCursor = *cursor;
411 GdkDisplay* display = NULL;
c2246a38 412 UpdateCursors(wxTopLevelWindows, display);
df8d50a0
PC
413 if (display)
414 gdk_display_flush(display);
ff7b1510 415}
c801d85f 416
8bbe427f 417bool wxIsBusy()
c801d85f 418{
89a43902 419 return gs_busyCount > 0;
ff7b1510 420}
c801d85f
KB
421
422void wxSetCursor( const wxCursor& cursor )
423{
238d735d 424 g_globalCursor = cursor;
c2246a38
RR
425 GdkDisplay* unused = NULL;
426 UpdateCursors(wxTopLevelWindows, unused);
ff7b1510 427}