]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/scrolbar.cpp
Fix bug with out of range date in wxMSW wxDatePickerCtrl under XP too.
[wxWidgets.git] / src / gtk1 / scrolbar.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/scrolbar.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
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"
dcf924a3
RR
12
13#if wxUSE_SCROLLBAR
14
1e6feb95
VZ
15#include "wx/scrolbar.h"
16
de6185e2
WS
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
18680f86 19 #include "wx/math.h"
de6185e2
WS
20#endif
21
3cbab641 22#include "wx/gtk1/private.h"
83624f79 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
66bd6b93
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
35extern bool g_blockEventsOnDrag;
7c2f14ee 36static wxEventType g_currentUpDownEvent = wxEVT_NULL;
66bd6b93 37
aed8ac3f
RR
38static const float sensitivity = 0.02;
39
c801d85f 40//-----------------------------------------------------------------------------
b4071e91 41// "value_changed"
c801d85f
KB
42//-----------------------------------------------------------------------------
43
9e691f46
VZ
44// FIXME: is GtkScrollType really passed to us as 2nd argument?
45
865bb325 46extern "C" {
9e691f46
VZ
47static void gtk_scrollbar_callback( GtkAdjustment *adjust,
48 SCROLLBAR_CBACK_ARG
49 wxScrollBar *win )
a2615ebc 50{
acfd422a
RR
51 if (g_isIdle) wxapp_install_idle_handler();
52
a2053b27 53 if (!win->m_hasVMT) return;
2d17d68f 54 if (g_blockEventsOnDrag) return;
a2615ebc 55
e1811a01 56 float diff = adjust->value - win->m_oldPos;
aed8ac3f 57 if (fabs(diff) < sensitivity) return;
a2615ebc 58
e1811a01
RR
59 win->m_oldPos = adjust->value;
60
9e691f46 61 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
e1811a01 62
0a07a7d8 63 double dvalue = adjust->value;
a9f21e71 64 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
a2615ebc 65
f03fc89f 66 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
a2615ebc 67
7c2f14ee
RR
68 // throw a LINEUP / LINEDOWN event if necessary
69 if (g_currentUpDownEvent != wxEVT_NULL)
70 {
91af0895 71 wxScrollEvent event( g_currentUpDownEvent, win->GetId(), value, orient );
7c2f14ee 72 event.SetEventObject( win );
937013e0 73 win->HandleWindowEvent( event );
91af0895
WS
74 }
75
76 // throw other event (wxEVT_SCROLL_THUMBTRACK)
77 wxScrollEvent event( command, win->GetId(), value, orient );
1b24b2b4 78 event.SetEventObject( win );
937013e0 79 win->HandleWindowEvent( event );
7c2f14ee 80
c801d85f 81/*
2d17d68f
RR
82 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
83 cevent.SetEventObject( win );
84 win->ProcessEvent( cevent );
c801d85f 85*/
6de97a3b 86}
865bb325 87}
c801d85f 88
cb43b372
RR
89//-----------------------------------------------------------------------------
90// "button_press_event" from slider
91//-----------------------------------------------------------------------------
865bb325 92extern "C" {
a2615ebc
VZ
93static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
94 GdkEventButton *gdk_event,
f03fc89f 95 wxScrollBar *win )
cb43b372 96{
acfd422a
RR
97 if (g_isIdle) wxapp_install_idle_handler();
98
7c2f14ee
RR
99 // check if a LINEUP/LINEDOWN event must be thrown
100 // I suppose here the size of scrollbar top/bottom buttons is 16px height
101 if (gdk_event->type == GDK_BUTTON_PRESS && gdk_event->button == 1)
102 {
103 int scroll_height, mouse_pos;
104
91af0895
WS
105 // get the mouse position when the click is done
106 if (win->HasFlag(wxSB_VERTICAL))
107 {
7c2f14ee
RR
108 scroll_height = GTK_WIDGET(widget)->allocation.height - 16;
109 mouse_pos = (int)gdk_event->y;
110 }
111 else
112 {
113 scroll_height = GTK_WIDGET(widget)->allocation.width - 16;
114 mouse_pos = (int)gdk_event->x;
115 }
91af0895 116
7c2f14ee
RR
117 // compare mouse position to scrollbar height
118 if (mouse_pos > scroll_height)
119 g_currentUpDownEvent = wxEVT_SCROLL_LINEDOWN;
120 else if (mouse_pos < 16)
121 g_currentUpDownEvent = wxEVT_SCROLL_LINEUP;
122 }
2a23d363 123
7c2f14ee 124 // There is no slider field any more
2a23d363 125 win->m_isScrolling = (gdk_event->window == widget->slider);
a2615ebc 126
acfd422a 127 return FALSE;
cb43b372 128}
865bb325 129}
cb43b372
RR
130
131//-----------------------------------------------------------------------------
132// "button_release_event" from slider
133//-----------------------------------------------------------------------------
134
865bb325 135extern "C" {
a2615ebc
VZ
136static gint
137gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
138 GdkEventButton *WXUNUSED(gdk_event),
139 wxScrollBar *win )
cb43b372 140{
a2615ebc
VZ
141 if (g_isIdle)
142 wxapp_install_idle_handler();
143
2a23d363
RR
144 if (win->m_isScrolling)
145 {
7d56fb8f 146 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
2a23d363 147 int value = (int)ceil(win->m_adjust->value);
193e19cf 148 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
2a23d363 149
193e19cf 150 wxScrollEvent event( command, win->GetId(), value, orient );
2a23d363 151 event.SetEventObject( win );
937013e0 152 win->HandleWindowEvent( event );
2a23d363 153 }
a2615ebc 154
91af0895 155 win->m_isScrolling = false;
a2615ebc 156
7c2f14ee
RR
157 // reset the LINEUP/LINEDOWN flag when the mouse button is released
158 g_currentUpDownEvent = wxEVT_NULL;
159
2d17d68f 160 return FALSE;
cb43b372 161}
865bb325 162}
cb43b372 163
b4071e91
RR
164//-----------------------------------------------------------------------------
165// wxScrollBar
166//-----------------------------------------------------------------------------
167
0a07a7d8 168wxScrollBar::~wxScrollBar()
c801d85f 169{
6de97a3b 170}
c801d85f 171
debe6624 172bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 173 const wxPoint& pos, const wxSize& size,
6de97a3b 174 long style, const wxValidator& validator, const wxString& name )
c801d85f 175{
91af0895
WS
176 m_needParent = true;
177 m_acceptsFocus = true;
a2615ebc 178
4dcaf11a
RR
179 if (!PreCreation( parent, pos, size ) ||
180 !CreateBase( parent, id, pos, size, style, validator, name ))
181 {
223d09f6 182 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
91af0895 183 return false;
4dcaf11a 184 }
6de97a3b 185
2d17d68f 186 m_oldPos = 0.0;
c801d85f 187
d9bd1494 188 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
d3b9f782 189 m_widget = gtk_vscrollbar_new( NULL );
d9bd1494 190 else
d3b9f782 191 m_widget = gtk_hscrollbar_new( NULL );
d9bd1494 192
2d17d68f 193 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
a2615ebc
VZ
194
195 gtk_signal_connect( GTK_OBJECT(m_adjust),
2d17d68f 196 "value_changed",
a2615ebc 197 (GtkSignalFunc) gtk_scrollbar_callback,
f03fc89f 198 (gpointer) this );
a2615ebc 199 gtk_signal_connect( GTK_OBJECT(m_widget),
2d17d68f 200 "button_press_event",
a2615ebc 201 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
f03fc89f 202 (gpointer) this );
a2615ebc 203 gtk_signal_connect( GTK_OBJECT(m_widget),
2d17d68f 204 "button_release_event",
a2615ebc 205 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
f03fc89f 206 (gpointer) this );
cb43b372 207
f03fc89f 208 m_parent->DoAddChild( this );
a2615ebc 209
abdeb9e7 210 PostCreation(size);
a2615ebc 211
91af0895 212 return true;
6de97a3b 213}
c801d85f 214
0a07a7d8 215int wxScrollBar::GetThumbPosition() const
c801d85f 216{
0a07a7d8 217 double val = m_adjust->value;
a9f21e71 218 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 219}
c801d85f
KB
220
221int wxScrollBar::GetThumbSize() const
222{
2d17d68f 223 return (int)(m_adjust->page_size+0.5);
6de97a3b 224}
c801d85f
KB
225
226int wxScrollBar::GetPageSize() const
227{
2d17d68f 228 return (int)(m_adjust->page_increment+0.5);
6de97a3b 229}
c801d85f
KB
230
231int wxScrollBar::GetRange() const
232{
2d17d68f 233 return (int)(m_adjust->upper+0.5);
6de97a3b 234}
c801d85f 235
4fabb575 236void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 237{
2d17d68f 238 if (m_isScrolling) return;
a2615ebc 239
2d17d68f
RR
240 float fpos = (float)viewStart;
241 m_oldPos = fpos;
242 if (fabs(fpos-m_adjust->value) < 0.2) return;
243 m_adjust->value = fpos;
a2615ebc
VZ
244
245 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
246 (GtkSignalFunc) gtk_scrollbar_callback,
2a23d363 247 (gpointer) this );
a2615ebc 248
2d17d68f 249 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
a2615ebc
VZ
250
251 gtk_signal_connect( GTK_OBJECT(m_adjust),
2a23d363 252 "value_changed",
a2615ebc 253 (GtkSignalFunc) gtk_scrollbar_callback,
2a23d363 254 (gpointer) this );
6de97a3b 255}
c801d85f 256
debe6624
JS
257void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
258 bool WXUNUSED(refresh) )
c801d85f 259{
2d17d68f
RR
260 float fpos = (float)position;
261 float frange = (float)range;
262 float fthumb = (float)thumbSize;
263 float fpage = (float)pageSize;
a2615ebc 264
2d17d68f
RR
265 if ((fabs(frange-m_adjust->upper) < 0.2) &&
266 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
267 (fabs(fpage-m_adjust->page_increment) < 0.2))
268 {
269 SetThumbPosition( position );
270 return;
271 }
a2615ebc 272
2d17d68f 273 m_oldPos = fpos;
a2615ebc 274
2d17d68f
RR
275 m_adjust->lower = 0.0;
276 m_adjust->upper = frange;
277 m_adjust->value = fpos;
278 m_adjust->step_increment = 1.0;
279 m_adjust->page_increment = (float)(wxMax(fpage,0));
280 m_adjust->page_size = fthumb;
281
282 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 283}
c801d85f 284
2d17d68f 285/* Backward compatibility */
0a07a7d8 286int wxScrollBar::GetValue() const
c801d85f 287{
2d17d68f 288 return GetThumbPosition();
6de97a3b 289}
c801d85f 290
debe6624 291void wxScrollBar::SetValue( int viewStart )
c801d85f 292{
2d17d68f 293 SetThumbPosition( viewStart );
6de97a3b 294}
c801d85f
KB
295
296void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
297{
2d17d68f
RR
298 int pos = (int)(m_adjust->value+0.5);
299 int thumb = (int)(m_adjust->page_size+0.5);
300 int page = (int)(m_adjust->page_increment+0.5);
301 int range = (int)(m_adjust->upper+0.5);
a2615ebc 302
2d17d68f
RR
303 *viewStart = pos;
304 *viewLength = range;
305 *objectLength = thumb;
306 *pageLength = page;
6de97a3b 307}
c801d85f
KB
308
309int wxScrollBar::GetViewLength() const
310{
2d17d68f 311 return (int)(m_adjust->upper+0.5);
6de97a3b 312}
c801d85f
KB
313
314int wxScrollBar::GetObjectLength() const
315{
2d17d68f 316 return (int)(m_adjust->page_size+0.5);
6de97a3b 317}
c801d85f 318
debe6624 319void wxScrollBar::SetPageSize( int pageLength )
c801d85f 320{
2d17d68f
RR
321 int pos = (int)(m_adjust->value+0.5);
322 int thumb = (int)(m_adjust->page_size+0.5);
323 int range = (int)(m_adjust->upper+0.5);
324 SetScrollbar( pos, thumb, range, pageLength );
6de97a3b 325}
c801d85f 326
debe6624 327void wxScrollBar::SetObjectLength( int objectLength )
c801d85f 328{
2d17d68f
RR
329 int pos = (int)(m_adjust->value+0.5);
330 int page = (int)(m_adjust->page_increment+0.5);
331 int range = (int)(m_adjust->upper+0.5);
332 SetScrollbar( pos, objectLength, range, page );
6de97a3b 333}
c801d85f 334
debe6624 335void wxScrollBar::SetViewLength( int viewLength )
c801d85f 336{
2d17d68f
RR
337 int pos = (int)(m_adjust->value+0.5);
338 int thumb = (int)(m_adjust->page_size+0.5);
339 int page = (int)(m_adjust->page_increment+0.5);
340 SetScrollbar( pos, thumb, viewLength, page );
6de97a3b 341}
c801d85f 342
b4071e91
RR
343bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
344{
2d17d68f 345 GtkRange *range = GTK_RANGE(m_widget);
9e691f46 346 return ( (window == GTK_WIDGET(range)->window)
9e691f46
VZ
347 || (window == range->trough)
348 || (window == range->slider)
349 || (window == range->step_forw)
350 || (window == range->step_back)
9e691f46 351 );
b4071e91 352}
58614078 353
7f980cff
VS
354wxSize wxScrollBar::DoGetBestSize() const
355{
356 return wxControl::DoGetBestSize();
357}
358
9d522606
RD
359// static
360wxVisualAttributes
361wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
362{
363 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
364}
365
de6185e2 366#endif // wxUSE_SCROLLBAR