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