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