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