]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
Added sashtest GTK makefiles (crashing bug to be solved); added typetest sample
[wxWidgets.git] / src / gtk / scrolbar.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: scrolbar.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "scrolbar.h"
14#endif
15
16#include "wx/scrolbar.h"
17#include "wx/utils.h"
18
66bd6b93
RR
19//-----------------------------------------------------------------------------
20// data
21//-----------------------------------------------------------------------------
22
23extern bool g_blockEventsOnDrag;
24
c801d85f 25//-----------------------------------------------------------------------------
b4071e91 26// "value_changed"
c801d85f
KB
27//-----------------------------------------------------------------------------
28
66bd6b93 29static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win )
c801d85f 30{
c801d85f 31 if (!win->HasVMT()) return;
66bd6b93 32 if (g_blockEventsOnDrag) return;
c801d85f
KB
33
34 float diff = win->m_adjust->value - win->m_oldPos;
35 if (fabs(diff) < 0.2) return;
36
7798a18e 37 wxEventType command = wxEVT_NULL;
c801d85f
KB
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 = wxHORIZONTAL;
52
53 wxScrollEvent event( command, win->GetId(), value, orient );
54 event.SetEventObject( win );
47908e25 55 win->GetEventHandler()->ProcessEvent( event );
c801d85f
KB
56
57/*
58 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
59 cevent.SetEventObject( win );
60 win->ProcessEvent( cevent );
61*/
6de97a3b 62}
c801d85f 63
cb43b372
RR
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
b4071e91
RR
92//-----------------------------------------------------------------------------
93// wxScrollBar
94//-----------------------------------------------------------------------------
95
c801d85f
KB
96IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
97
c801d85f
KB
98wxScrollBar::~wxScrollBar(void)
99{
6de97a3b 100}
c801d85f 101
debe6624 102bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 103 const wxPoint& pos, const wxSize& size,
6de97a3b 104 long style, const wxValidator& validator, const wxString& name )
c801d85f
KB
105{
106 m_needParent = TRUE;
107
108 PreCreation( parent, id, pos, size, style, name );
109
6de97a3b
RR
110 SetValidator( validator );
111
c801d85f
KB
112 m_oldPos = 0.0;
113
114 if (style & wxSB_VERTICAL == wxSB_VERTICAL)
c67daf87 115 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
c801d85f 116 else
c67daf87 117 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
c801d85f
KB
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
cb43b372
RR
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
c801d85f
KB
130 PostCreation();
131
132 Show( TRUE );
133
134 return TRUE;
6de97a3b 135}
c801d85f
KB
136
137int wxScrollBar::GetPosition(void) const
138{
139 return (int)(m_adjust->value+0.5);
6de97a3b 140}
c801d85f
KB
141
142int wxScrollBar::GetThumbSize() const
143{
144 return (int)(m_adjust->page_size+0.5);
6de97a3b 145}
c801d85f
KB
146
147int wxScrollBar::GetPageSize() const
148{
149 return (int)(m_adjust->page_increment+0.5);
6de97a3b 150}
c801d85f
KB
151
152int wxScrollBar::GetRange() const
153{
154 return (int)(m_adjust->upper+0.5);
6de97a3b 155}
c801d85f 156
debe6624 157void wxScrollBar::SetPosition( int viewStart )
c801d85f
KB
158{
159 float fpos = (float)viewStart;
160 m_oldPos = fpos;
161 if (fabs(fpos-m_adjust->value) < 0.2) return;
162 m_adjust->value = fpos;
cb43b372
RR
163
164 if (!m_isScrolling)
165 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 166}
c801d85f 167
debe6624
JS
168void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
169 bool WXUNUSED(refresh) )
c801d85f
KB
170{
171 float fpos = (float)position;
c801d85f
KB
172 float frange = (float)range;
173 float fthumb = (float)thumbSize;
174 float fpage = (float)pageSize;
175
cb43b372 176 if ((fabs(frange-m_adjust->upper) < 0.2) &&
c801d85f
KB
177 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
178 (fabs(fpage-m_adjust->page_increment) < 0.2))
cb43b372
RR
179 {
180 SetPosition( position );
181 return;
182 }
c801d85f 183
cb43b372
RR
184 m_oldPos = fpos;
185
c801d85f
KB
186 m_adjust->lower = 0.0;
187 m_adjust->upper = frange;
188 m_adjust->value = fpos;
189 m_adjust->step_increment = 1.0;
190 m_adjust->page_increment = (float)(wxMax(fpage-2,0));
191 m_adjust->page_size = fthumb;
192
193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 194}
c801d85f
KB
195
196// Backward compatibility
197int wxScrollBar::GetValue(void) const
198{
199 return GetPosition();
6de97a3b 200}
c801d85f 201
debe6624 202void wxScrollBar::SetValue( int viewStart )
c801d85f
KB
203{
204 SetPosition( viewStart );
6de97a3b 205}
c801d85f
KB
206
207void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
208{
209 int pos = (int)(m_adjust->value+0.5);
210 int thumb = (int)(m_adjust->page_size+0.5);
211 int page = (int)(m_adjust->page_increment+0.5);
212 int range = (int)(m_adjust->upper+0.5);
213
214 *viewStart = pos;
215 *viewLength = range;
216 *objectLength = thumb;
217 *pageLength = page;
6de97a3b 218}
c801d85f
KB
219
220int wxScrollBar::GetViewLength() const
221{
222 return (int)(m_adjust->upper+0.5);
6de97a3b 223}
c801d85f
KB
224
225int wxScrollBar::GetObjectLength() const
226{
227 return (int)(m_adjust->page_size+0.5);
6de97a3b 228}
c801d85f 229
debe6624 230void wxScrollBar::SetPageSize( int pageLength )
c801d85f
KB
231{
232 int pos = (int)(m_adjust->value+0.5);
233 int thumb = (int)(m_adjust->page_size+0.5);
234 int range = (int)(m_adjust->upper+0.5);
235 SetScrollbar( pos, thumb, range, pageLength );
6de97a3b 236}
c801d85f 237
debe6624 238void wxScrollBar::SetObjectLength( int objectLength )
c801d85f
KB
239{
240 int pos = (int)(m_adjust->value+0.5);
241 int page = (int)(m_adjust->page_increment+0.5);
242 int range = (int)(m_adjust->upper+0.5);
243 SetScrollbar( pos, objectLength, range, page );
6de97a3b 244}
c801d85f 245
debe6624 246void wxScrollBar::SetViewLength( int viewLength )
c801d85f
KB
247{
248 int pos = (int)(m_adjust->value+0.5);
249 int thumb = (int)(m_adjust->page_size+0.5);
250 int page = (int)(m_adjust->page_increment+0.5);
251 SetScrollbar( pos, thumb, viewLength, page );
6de97a3b 252}
c801d85f 253
b4071e91
RR
254bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
255{
256 GtkRange *range = GTK_RANGE(m_widget);
257 return ( (window == GTK_WIDGET(range)->window) ||
258 (window == range->trough) ||
259 (window == range->slider) ||
260 (window == range->step_forw) ||
261 (window == range->step_back) );
262}