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