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