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