]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/scrolbar.cpp
reSWIGged
[wxWidgets.git] / src / gtk / scrolbar.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/scrolbar.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_SCROLLBAR
14
15#include "wx/scrolbar.h"
16
17#include "wx/utils.h"
18#include "wx/math.h"
19#include "wx/gtk/private.h"
20
21//-----------------------------------------------------------------------------
22// data
23//-----------------------------------------------------------------------------
24
25extern bool g_blockEventsOnDrag;
26static wxEventType g_currentUpDownEvent = wxEVT_NULL;
27
28static const float sensitivity = 0.02;
29
30//-----------------------------------------------------------------------------
31// "value_changed"
32//-----------------------------------------------------------------------------
33
34// FIXME: is GtkScrollType really passed to us as 2nd argument?
35
36extern "C" {
37static void gtk_scrollbar_callback( GtkAdjustment *adjust,
38 wxScrollBar *win )
39{
40 if (g_isIdle) wxapp_install_idle_handler();
41
42 if (!win->m_hasVMT) return;
43 if (g_blockEventsOnDrag) return;
44
45 float diff = adjust->value - win->m_oldPos;
46 if (fabs(diff) < sensitivity) return;
47
48 win->m_oldPos = adjust->value;
49
50 wxEventType command = GtkScrollTypeToWx(GTK_SCROLL_JUMP);
51
52 double dvalue = adjust->value;
53 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
54
55 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
56
57 // throw a LINEUP / LINEDOWN event if necessary
58 if (g_currentUpDownEvent != wxEVT_NULL)
59 {
60 wxScrollEvent event( g_currentUpDownEvent, win->GetId(), value, orient );
61 event.SetEventObject( win );
62 win->GetEventHandler()->ProcessEvent( event );
63 }
64
65 // throw other event (wxEVT_SCROLL_THUMBTRACK)
66 wxScrollEvent event( command, win->GetId(), value, orient );
67 event.SetEventObject( win );
68 win->GetEventHandler()->ProcessEvent( event );
69
70/*
71 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
73 win->ProcessEvent( cevent );
74*/
75}
76}
77
78//-----------------------------------------------------------------------------
79// "button_press_event" from slider
80//-----------------------------------------------------------------------------
81extern "C" {
82static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
83 GdkEventButton *gdk_event,
84 wxScrollBar *win )
85{
86 if (g_isIdle) wxapp_install_idle_handler();
87
88 // check if a LINEUP/LINEDOWN event must be thrown
89 // I suppose here the size of scrollbar top/bottom buttons is 16px height
90 if (gdk_event->type == GDK_BUTTON_PRESS && gdk_event->button == 1)
91 {
92 int scroll_height, mouse_pos;
93
94 // get the mouse position when the click is done
95 if (win->HasFlag(wxSB_VERTICAL))
96 {
97 scroll_height = GTK_WIDGET(widget)->allocation.height - 16;
98 mouse_pos = (int)gdk_event->y;
99 }
100 else
101 {
102 scroll_height = GTK_WIDGET(widget)->allocation.width - 16;
103 mouse_pos = (int)gdk_event->x;
104 }
105
106 // compare mouse position to scrollbar height
107 if (mouse_pos > scroll_height)
108 g_currentUpDownEvent = wxEVT_SCROLL_LINEDOWN;
109 else if (mouse_pos < 16)
110 g_currentUpDownEvent = wxEVT_SCROLL_LINEUP;
111 }
112
113 return FALSE;
114}
115}
116
117//-----------------------------------------------------------------------------
118// "button_release_event" from slider
119//-----------------------------------------------------------------------------
120
121extern "C" {
122static gint
123gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
124 GdkEventButton *WXUNUSED(gdk_event),
125 wxScrollBar *win )
126{
127 if (g_isIdle)
128 wxapp_install_idle_handler();
129
130 if (win->m_isScrolling)
131 {
132 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
133 int value = (int)ceil(win->m_adjust->value);
134 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
135
136 wxScrollEvent event( command, win->GetId(), value, orient );
137 event.SetEventObject( win );
138 win->GetEventHandler()->ProcessEvent( event );
139 }
140
141 win->m_isScrolling = false;
142
143 // reset the LINEUP/LINEDOWN flag when the mouse button is released
144 g_currentUpDownEvent = wxEVT_NULL;
145
146 return FALSE;
147}
148}
149
150//-----------------------------------------------------------------------------
151// wxScrollBar
152//-----------------------------------------------------------------------------
153
154IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
155
156wxScrollBar::~wxScrollBar()
157{
158}
159
160bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
161 const wxPoint& pos, const wxSize& size,
162 long style, const wxValidator& validator, const wxString& name )
163{
164 m_needParent = true;
165 m_acceptsFocus = true;
166
167 if (!PreCreation( parent, pos, size ) ||
168 !CreateBase( parent, id, pos, size, style, validator, name ))
169 {
170 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
171 return false;
172 }
173
174 m_oldPos = 0.0;
175
176 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
177 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
178 else
179 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
180
181 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
182 if ( style & wxSB_VERTICAL )
183 {
184 SetVScrollAdjustment(m_adjust);
185 }
186
187 g_signal_connect (m_adjust, "value_changed",
188 G_CALLBACK (gtk_scrollbar_callback), this);
189 g_signal_connect (m_widget, "button_press_event",
190 G_CALLBACK (gtk_scrollbar_button_press_callback),
191 this);
192 g_signal_connect (m_widget, "button_release_event",
193 G_CALLBACK (gtk_scrollbar_button_release_callback),
194 this);
195
196 m_parent->DoAddChild( this );
197
198 PostCreation(size);
199
200 return true;
201}
202
203int wxScrollBar::GetThumbPosition() const
204{
205 double val = m_adjust->value;
206 return (int)(val < 0 ? val - 0.5 : val + 0.5);
207}
208
209int wxScrollBar::GetThumbSize() const
210{
211 return (int)(m_adjust->page_size+0.5);
212}
213
214int wxScrollBar::GetPageSize() const
215{
216 return (int)(m_adjust->page_increment+0.5);
217}
218
219int wxScrollBar::GetRange() const
220{
221 return (int)(m_adjust->upper+0.5);
222}
223
224void wxScrollBar::SetThumbPosition( int viewStart )
225{
226 if (m_isScrolling) return;
227
228 float fpos = (float)viewStart;
229 m_oldPos = fpos;
230 if (fabs(fpos-m_adjust->value) < 0.2) return;
231 m_adjust->value = fpos;
232
233 g_signal_handlers_disconnect_by_func (m_adjust,
234 (gpointer) gtk_scrollbar_callback,
235 this);
236
237 g_signal_emit_by_name (m_adjust, "value_changed");
238
239 g_signal_connect (m_adjust, "value_changed",
240 G_CALLBACK (gtk_scrollbar_callback), this);
241}
242
243void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
244 bool WXUNUSED(refresh) )
245{
246 float fpos = (float)position;
247 float frange = (float)range;
248 float fthumb = (float)thumbSize;
249 float fpage = (float)pageSize;
250
251 if ((fabs(frange-m_adjust->upper) < 0.2) &&
252 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
253 (fabs(fpage-m_adjust->page_increment) < 0.2))
254 {
255 SetThumbPosition( position );
256 return;
257 }
258
259 m_oldPos = fpos;
260
261 m_adjust->lower = 0.0;
262 m_adjust->upper = frange;
263 m_adjust->value = fpos;
264 m_adjust->step_increment = 1.0;
265 m_adjust->page_increment = (float)(wxMax(fpage,0));
266 m_adjust->page_size = fthumb;
267
268 g_signal_emit_by_name (m_adjust, "changed");
269}
270
271/* Backward compatibility */
272int wxScrollBar::GetValue() const
273{
274 return GetThumbPosition();
275}
276
277void wxScrollBar::SetValue( int viewStart )
278{
279 SetThumbPosition( viewStart );
280}
281
282void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
283{
284 int pos = (int)(m_adjust->value+0.5);
285 int thumb = (int)(m_adjust->page_size+0.5);
286 int page = (int)(m_adjust->page_increment+0.5);
287 int range = (int)(m_adjust->upper+0.5);
288
289 *viewStart = pos;
290 *viewLength = range;
291 *objectLength = thumb;
292 *pageLength = page;
293}
294
295int wxScrollBar::GetViewLength() const
296{
297 return (int)(m_adjust->upper+0.5);
298}
299
300int wxScrollBar::GetObjectLength() const
301{
302 return (int)(m_adjust->page_size+0.5);
303}
304
305void wxScrollBar::SetPageSize( int pageLength )
306{
307 int pos = (int)(m_adjust->value+0.5);
308 int thumb = (int)(m_adjust->page_size+0.5);
309 int range = (int)(m_adjust->upper+0.5);
310 SetScrollbar( pos, thumb, range, pageLength );
311}
312
313void wxScrollBar::SetObjectLength( int objectLength )
314{
315 int pos = (int)(m_adjust->value+0.5);
316 int page = (int)(m_adjust->page_increment+0.5);
317 int range = (int)(m_adjust->upper+0.5);
318 SetScrollbar( pos, objectLength, range, page );
319}
320
321void wxScrollBar::SetViewLength( int viewLength )
322{
323 int pos = (int)(m_adjust->value+0.5);
324 int thumb = (int)(m_adjust->page_size+0.5);
325 int page = (int)(m_adjust->page_increment+0.5);
326 SetScrollbar( pos, thumb, viewLength, page );
327}
328
329bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
330{
331 GtkRange *range = GTK_RANGE(m_widget);
332 return ( (window == GTK_WIDGET(range)->window) );
333}
334
335wxSize wxScrollBar::DoGetBestSize() const
336{
337 return wxControl::DoGetBestSize();
338}
339
340// static
341wxVisualAttributes
342wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
343{
344 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
345}
346
347#endif