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