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