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