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