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