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