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