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