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