]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
fixed typo that caused incorrect if there was only one book
[wxWidgets.git] / src / gtk / scrolbar.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: scrolbar.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
f03fc89f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "scrolbar.h"
13#endif
14
15#include "wx/scrolbar.h"
dcf924a3
RR
16
17#if wxUSE_SCROLLBAR
18
c801d85f 19#include "wx/utils.h"
aed8ac3f 20
f5368809 21#include <math.h>
c801d85f 22
aed8ac3f
RR
23#include <gdk/gdk.h>
24#include <gtk/gtk.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
e1811a01 46static void gtk_scrollbar_callback( GtkAdjustment *adjust, wxScrollBar *win )
c801d85f 47{
acfd422a
RR
48 if (g_isIdle) wxapp_install_idle_handler();
49
a2053b27 50 if (!win->m_hasVMT) return;
2d17d68f 51 if (g_blockEventsOnDrag) return;
c801d85f 52
e1811a01 53 float diff = adjust->value - win->m_oldPos;
aed8ac3f 54 if (fabs(diff) < sensitivity) return;
e1811a01
RR
55
56 win->m_oldPos = adjust->value;
57
58 GtkRange *range = GTK_RANGE( win->m_widget );
59
60 wxEventType command = wxEVT_SCROLL_THUMBTRACK;
61 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLL_LINEUP;
62 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLL_LINEDOWN;
63 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLL_PAGEUP;
64 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLL_PAGEDOWN;
65
0a07a7d8 66 double dvalue = adjust->value;
a9f21e71 67 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
c801d85f 68
f03fc89f 69 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
c801d85f 70
2d17d68f
RR
71 wxScrollEvent event( command, win->GetId(), value, orient );
72 event.SetEventObject( win );
73 win->GetEventHandler()->ProcessEvent( event );
c801d85f
KB
74
75/*
2d17d68f
RR
76 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
77 cevent.SetEventObject( win );
78 win->ProcessEvent( cevent );
c801d85f 79*/
6de97a3b 80}
c801d85f 81
cb43b372
RR
82//-----------------------------------------------------------------------------
83// "button_press_event" from slider
84//-----------------------------------------------------------------------------
85
2a23d363
RR
86static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
87 GdkEventButton *gdk_event,
f03fc89f 88 wxScrollBar *win )
cb43b372 89{
acfd422a
RR
90 if (g_isIdle) wxapp_install_idle_handler();
91
1ecc4d80 92// g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
2a23d363
RR
93
94 win->m_isScrolling = (gdk_event->window == widget->slider);
cb43b372 95
acfd422a 96 return FALSE;
cb43b372
RR
97}
98
99//-----------------------------------------------------------------------------
100// "button_release_event" from slider
101//-----------------------------------------------------------------------------
102
76ed8f8d
RR
103static gint gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
104 GdkEventButton *WXUNUSED(gdk_event),
f03fc89f 105 wxScrollBar *win )
cb43b372 106{
acfd422a 107 if (g_isIdle) wxapp_install_idle_handler();
88413fec 108
1ecc4d80 109// g_blockEventsOnScroll = FALSE;
cb43b372 110
2a23d363
RR
111 if (win->m_isScrolling)
112 {
7d56fb8f 113 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
2a23d363
RR
114 int value = (int)ceil(win->m_adjust->value);
115 int dir = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
116
117 wxScrollEvent event( command, value, dir );
2a23d363
RR
118 event.SetEventObject( win );
119 win->GetEventHandler()->ProcessEvent( event );
120 }
76ed8f8d 121
2a23d363
RR
122 win->m_isScrolling = FALSE;
123
2d17d68f 124 return FALSE;
cb43b372
RR
125}
126
b4071e91
RR
127//-----------------------------------------------------------------------------
128// wxScrollBar
129//-----------------------------------------------------------------------------
130
c801d85f
KB
131IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
132
0a07a7d8 133wxScrollBar::~wxScrollBar()
c801d85f 134{
6de97a3b 135}
c801d85f 136
debe6624 137bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 138 const wxPoint& pos, const wxSize& size,
6de97a3b 139 long style, const wxValidator& validator, const wxString& name )
c801d85f 140{
2d17d68f
RR
141 m_needParent = TRUE;
142 m_acceptsFocus = TRUE;
c801d85f 143
4dcaf11a
RR
144 if (!PreCreation( parent, pos, size ) ||
145 !CreateBase( parent, id, pos, size, style, validator, name ))
146 {
223d09f6 147 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
0a07a7d8 148 return FALSE;
4dcaf11a 149 }
6de97a3b 150
2d17d68f 151 m_oldPos = 0.0;
c801d85f 152
d9bd1494 153 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
2d17d68f 154 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
d9bd1494
SB
155 else
156 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
157
2d17d68f 158 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
c801d85f 159
2d17d68f
RR
160 gtk_signal_connect( GTK_OBJECT(m_adjust),
161 "value_changed",
f03fc89f
VZ
162 (GtkSignalFunc) gtk_scrollbar_callback,
163 (gpointer) this );
c801d85f 164
2d17d68f
RR
165 gtk_signal_connect( GTK_OBJECT(m_widget),
166 "button_press_event",
167 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
f03fc89f 168 (gpointer) this );
cb43b372 169
2d17d68f
RR
170 gtk_signal_connect( GTK_OBJECT(m_widget),
171 "button_release_event",
172 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
f03fc89f 173 (gpointer) this );
cb43b372 174
f03fc89f 175 m_parent->DoAddChild( this );
6ca41e57 176
2d17d68f 177 PostCreation();
c801d85f 178
2d17d68f 179 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 180
2d17d68f 181 Show( TRUE );
c801d85f 182
2d17d68f 183 return TRUE;
6de97a3b 184}
c801d85f 185
0a07a7d8 186int wxScrollBar::GetThumbPosition() const
c801d85f 187{
0a07a7d8 188 double val = m_adjust->value;
a9f21e71 189 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 190}
c801d85f
KB
191
192int wxScrollBar::GetThumbSize() const
193{
2d17d68f 194 return (int)(m_adjust->page_size+0.5);
6de97a3b 195}
c801d85f
KB
196
197int wxScrollBar::GetPageSize() const
198{
2d17d68f 199 return (int)(m_adjust->page_increment+0.5);
6de97a3b 200}
c801d85f
KB
201
202int wxScrollBar::GetRange() const
203{
2d17d68f 204 return (int)(m_adjust->upper+0.5);
6de97a3b 205}
c801d85f 206
4fabb575 207void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 208{
2d17d68f 209 if (m_isScrolling) return;
76ed8f8d 210
2d17d68f
RR
211 float fpos = (float)viewStart;
212 m_oldPos = fpos;
213 if (fabs(fpos-m_adjust->value) < 0.2) return;
214 m_adjust->value = fpos;
76ed8f8d 215
2a23d363
RR
216 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
217 (GtkSignalFunc) gtk_scrollbar_callback,
218 (gpointer) this );
219
2d17d68f 220 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
2a23d363
RR
221
222 gtk_signal_connect( GTK_OBJECT(m_adjust),
223 "value_changed",
224 (GtkSignalFunc) gtk_scrollbar_callback,
225 (gpointer) this );
6de97a3b 226}
c801d85f 227
debe6624
JS
228void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
229 bool WXUNUSED(refresh) )
c801d85f 230{
2d17d68f
RR
231 float fpos = (float)position;
232 float frange = (float)range;
233 float fthumb = (float)thumbSize;
234 float fpage = (float)pageSize;
c801d85f 235
2d17d68f
RR
236 if ((fabs(frange-m_adjust->upper) < 0.2) &&
237 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
238 (fabs(fpage-m_adjust->page_increment) < 0.2))
239 {
240 SetThumbPosition( position );
241 return;
242 }
84efdbf1 243
2d17d68f 244 m_oldPos = fpos;
cb43b372 245
2d17d68f
RR
246 m_adjust->lower = 0.0;
247 m_adjust->upper = frange;
248 m_adjust->value = fpos;
249 m_adjust->step_increment = 1.0;
250 m_adjust->page_increment = (float)(wxMax(fpage,0));
251 m_adjust->page_size = fthumb;
252
253 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 254}
c801d85f 255
2d17d68f 256/* Backward compatibility */
0a07a7d8 257int wxScrollBar::GetValue() const
c801d85f 258{
2d17d68f 259 return GetThumbPosition();
6de97a3b 260}
c801d85f 261
debe6624 262void wxScrollBar::SetValue( int viewStart )
c801d85f 263{
2d17d68f 264 SetThumbPosition( viewStart );
6de97a3b 265}
c801d85f
KB
266
267void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
268{
2d17d68f
RR
269 int pos = (int)(m_adjust->value+0.5);
270 int thumb = (int)(m_adjust->page_size+0.5);
271 int page = (int)(m_adjust->page_increment+0.5);
272 int range = (int)(m_adjust->upper+0.5);
c801d85f 273
2d17d68f
RR
274 *viewStart = pos;
275 *viewLength = range;
276 *objectLength = thumb;
277 *pageLength = page;
6de97a3b 278}
c801d85f
KB
279
280int wxScrollBar::GetViewLength() const
281{
2d17d68f 282 return (int)(m_adjust->upper+0.5);
6de97a3b 283}
c801d85f
KB
284
285int wxScrollBar::GetObjectLength() const
286{
2d17d68f 287 return (int)(m_adjust->page_size+0.5);
6de97a3b 288}
c801d85f 289
debe6624 290void wxScrollBar::SetPageSize( int pageLength )
c801d85f 291{
2d17d68f
RR
292 int pos = (int)(m_adjust->value+0.5);
293 int thumb = (int)(m_adjust->page_size+0.5);
294 int range = (int)(m_adjust->upper+0.5);
295 SetScrollbar( pos, thumb, range, pageLength );
6de97a3b 296}
c801d85f 297
debe6624 298void wxScrollBar::SetObjectLength( int objectLength )
c801d85f 299{
2d17d68f
RR
300 int pos = (int)(m_adjust->value+0.5);
301 int page = (int)(m_adjust->page_increment+0.5);
302 int range = (int)(m_adjust->upper+0.5);
303 SetScrollbar( pos, objectLength, range, page );
6de97a3b 304}
c801d85f 305
debe6624 306void wxScrollBar::SetViewLength( int viewLength )
c801d85f 307{
2d17d68f
RR
308 int pos = (int)(m_adjust->value+0.5);
309 int thumb = (int)(m_adjust->page_size+0.5);
310 int page = (int)(m_adjust->page_increment+0.5);
311 SetScrollbar( pos, thumb, viewLength, page );
6de97a3b 312}
c801d85f 313
b4071e91
RR
314bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
315{
2d17d68f
RR
316 GtkRange *range = GTK_RANGE(m_widget);
317 return ( (window == GTK_WIDGET(range)->window) ||
318 (window == range->trough) ||
f03fc89f
VZ
319 (window == range->slider) ||
320 (window == range->step_forw) ||
321 (window == range->step_back) );
b4071e91 322}
58614078
RR
323
324void wxScrollBar::ApplyWidgetStyle()
325{
2d17d68f
RR
326 SetWidgetStyle();
327 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078
RR
328}
329
dcf924a3 330#endif