]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
check default library directories in WX_PATH_FIND_LIBRARIES; do *not* add default...
[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
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "scrolbar.h"
13#endif
14
14f355c2
VS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
dcf924a3
RR
17
18#if wxUSE_SCROLLBAR
19
1e6feb95
VZ
20#include "wx/scrolbar.h"
21
c801d85f 22#include "wx/utils.h"
463c4d71 23#include "wx/math.h"
9e691f46 24#include "wx/gtk/private.h"
83624f79 25
acfd422a
RR
26//-----------------------------------------------------------------------------
27// idle system
28//-----------------------------------------------------------------------------
29
30extern void wxapp_install_idle_handler();
31extern bool g_isIdle;
32
66bd6b93
RR
33//-----------------------------------------------------------------------------
34// data
35//-----------------------------------------------------------------------------
36
37extern bool g_blockEventsOnDrag;
7c2f14ee 38static wxEventType g_currentUpDownEvent = wxEVT_NULL;
66bd6b93 39
aed8ac3f
RR
40static const float sensitivity = 0.02;
41
c801d85f 42//-----------------------------------------------------------------------------
b4071e91 43// "value_changed"
c801d85f
KB
44//-----------------------------------------------------------------------------
45
9e691f46
VZ
46// FIXME: is GtkScrollType really passed to us as 2nd argument?
47
865bb325 48extern "C" {
9e691f46
VZ
49static void gtk_scrollbar_callback( GtkAdjustment *adjust,
50 SCROLLBAR_CBACK_ARG
51 wxScrollBar *win )
a2615ebc 52{
acfd422a
RR
53 if (g_isIdle) wxapp_install_idle_handler();
54
a2053b27 55 if (!win->m_hasVMT) return;
2d17d68f 56 if (g_blockEventsOnDrag) return;
a2615ebc 57
e1811a01 58 float diff = adjust->value - win->m_oldPos;
aed8ac3f 59 if (fabs(diff) < sensitivity) return;
a2615ebc 60
e1811a01
RR
61 win->m_oldPos = adjust->value;
62
9e691f46 63 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
e1811a01 64
0a07a7d8 65 double dvalue = adjust->value;
a9f21e71 66 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
a2615ebc 67
f03fc89f 68 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
a2615ebc 69
7c2f14ee
RR
70 // throw a LINEUP / LINEDOWN event if necessary
71 if (g_currentUpDownEvent != wxEVT_NULL)
72 {
91af0895 73 wxScrollEvent event( g_currentUpDownEvent, win->GetId(), value, orient );
7c2f14ee
RR
74 event.SetEventObject( win );
75 win->GetEventHandler()->ProcessEvent( event );
91af0895
WS
76 }
77
78 // throw other event (wxEVT_SCROLL_THUMBTRACK)
79 wxScrollEvent event( command, win->GetId(), value, orient );
1b24b2b4
RR
80 event.SetEventObject( win );
81 win->GetEventHandler()->ProcessEvent( event );
7c2f14ee 82
c801d85f 83/*
2d17d68f
RR
84 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
85 cevent.SetEventObject( win );
86 win->ProcessEvent( cevent );
c801d85f 87*/
6de97a3b 88}
865bb325 89}
c801d85f 90
cb43b372
RR
91//-----------------------------------------------------------------------------
92// "button_press_event" from slider
93//-----------------------------------------------------------------------------
865bb325 94extern "C" {
a2615ebc
VZ
95static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
96 GdkEventButton *gdk_event,
f03fc89f 97 wxScrollBar *win )
cb43b372 98{
acfd422a
RR
99 if (g_isIdle) wxapp_install_idle_handler();
100
7c2f14ee
RR
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
91af0895
WS
107 // get the mouse position when the click is done
108 if (win->HasFlag(wxSB_VERTICAL))
109 {
7c2f14ee
RR
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 }
91af0895 118
7c2f14ee
RR
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 }
2a23d363 125
9e691f46 126#ifndef __WXGTK20__
7c2f14ee 127 // There is no slider field any more
2a23d363 128 win->m_isScrolling = (gdk_event->window == widget->slider);
9e691f46 129#endif
a2615ebc 130
acfd422a 131 return FALSE;
cb43b372 132}
865bb325 133}
cb43b372
RR
134
135//-----------------------------------------------------------------------------
136// "button_release_event" from slider
137//-----------------------------------------------------------------------------
138
865bb325 139extern "C" {
a2615ebc
VZ
140static gint
141gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
142 GdkEventButton *WXUNUSED(gdk_event),
143 wxScrollBar *win )
cb43b372 144{
a2615ebc
VZ
145 if (g_isIdle)
146 wxapp_install_idle_handler();
147
2a23d363
RR
148 if (win->m_isScrolling)
149 {
7d56fb8f 150 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
2a23d363 151 int value = (int)ceil(win->m_adjust->value);
193e19cf 152 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
2a23d363 153
193e19cf 154 wxScrollEvent event( command, win->GetId(), value, orient );
2a23d363
RR
155 event.SetEventObject( win );
156 win->GetEventHandler()->ProcessEvent( event );
157 }
a2615ebc 158
91af0895 159 win->m_isScrolling = false;
a2615ebc 160
7c2f14ee
RR
161 // reset the LINEUP/LINEDOWN flag when the mouse button is released
162 g_currentUpDownEvent = wxEVT_NULL;
163
2d17d68f 164 return FALSE;
cb43b372 165}
865bb325 166}
cb43b372 167
b4071e91
RR
168//-----------------------------------------------------------------------------
169// wxScrollBar
170//-----------------------------------------------------------------------------
171
c801d85f
KB
172IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
173
0a07a7d8 174wxScrollBar::~wxScrollBar()
c801d85f 175{
6de97a3b 176}
c801d85f 177
debe6624 178bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 179 const wxPoint& pos, const wxSize& size,
6de97a3b 180 long style, const wxValidator& validator, const wxString& name )
c801d85f 181{
91af0895
WS
182 m_needParent = true;
183 m_acceptsFocus = true;
a2615ebc 184
4dcaf11a
RR
185 if (!PreCreation( parent, pos, size ) ||
186 !CreateBase( parent, id, pos, size, style, validator, name ))
187 {
223d09f6 188 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
91af0895 189 return false;
4dcaf11a 190 }
6de97a3b 191
2d17d68f 192 m_oldPos = 0.0;
c801d85f 193
d9bd1494 194 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
2d17d68f 195 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
d9bd1494
SB
196 else
197 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
198
2d17d68f 199 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
a2615ebc
VZ
200
201 gtk_signal_connect( GTK_OBJECT(m_adjust),
2d17d68f 202 "value_changed",
a2615ebc 203 (GtkSignalFunc) gtk_scrollbar_callback,
f03fc89f 204 (gpointer) this );
a2615ebc 205 gtk_signal_connect( GTK_OBJECT(m_widget),
2d17d68f 206 "button_press_event",
a2615ebc 207 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
f03fc89f 208 (gpointer) this );
a2615ebc 209 gtk_signal_connect( GTK_OBJECT(m_widget),
2d17d68f 210 "button_release_event",
a2615ebc 211 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
f03fc89f 212 (gpointer) this );
cb43b372 213
f03fc89f 214 m_parent->DoAddChild( this );
a2615ebc 215
abdeb9e7 216 PostCreation(size);
a2615ebc 217
91af0895 218 return true;
6de97a3b 219}
c801d85f 220
0a07a7d8 221int wxScrollBar::GetThumbPosition() const
c801d85f 222{
0a07a7d8 223 double val = m_adjust->value;
a9f21e71 224 return (int)(val < 0 ? val - 0.5 : val + 0.5);
6de97a3b 225}
c801d85f
KB
226
227int wxScrollBar::GetThumbSize() const
228{
2d17d68f 229 return (int)(m_adjust->page_size+0.5);
6de97a3b 230}
c801d85f
KB
231
232int wxScrollBar::GetPageSize() const
233{
2d17d68f 234 return (int)(m_adjust->page_increment+0.5);
6de97a3b 235}
c801d85f
KB
236
237int wxScrollBar::GetRange() const
238{
2d17d68f 239 return (int)(m_adjust->upper+0.5);
6de97a3b 240}
c801d85f 241
4fabb575 242void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 243{
2d17d68f 244 if (m_isScrolling) return;
a2615ebc 245
2d17d68f
RR
246 float fpos = (float)viewStart;
247 m_oldPos = fpos;
248 if (fabs(fpos-m_adjust->value) < 0.2) return;
249 m_adjust->value = fpos;
a2615ebc
VZ
250
251 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
252 (GtkSignalFunc) gtk_scrollbar_callback,
2a23d363 253 (gpointer) this );
a2615ebc 254
2d17d68f 255 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
a2615ebc
VZ
256
257 gtk_signal_connect( GTK_OBJECT(m_adjust),
2a23d363 258 "value_changed",
a2615ebc 259 (GtkSignalFunc) gtk_scrollbar_callback,
2a23d363 260 (gpointer) this );
6de97a3b 261}
c801d85f 262
debe6624
JS
263void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
264 bool WXUNUSED(refresh) )
c801d85f 265{
2d17d68f
RR
266 float fpos = (float)position;
267 float frange = (float)range;
268 float fthumb = (float)thumbSize;
269 float fpage = (float)pageSize;
a2615ebc 270
2d17d68f
RR
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 }
a2615ebc 278
2d17d68f 279 m_oldPos = fpos;
a2615ebc 280
2d17d68f
RR
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" );
6de97a3b 289}
c801d85f 290
2d17d68f 291/* Backward compatibility */
0a07a7d8 292int wxScrollBar::GetValue() const
c801d85f 293{
2d17d68f 294 return GetThumbPosition();
6de97a3b 295}
c801d85f 296
debe6624 297void wxScrollBar::SetValue( int viewStart )
c801d85f 298{
2d17d68f 299 SetThumbPosition( viewStart );
6de97a3b 300}
c801d85f
KB
301
302void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
303{
2d17d68f
RR
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);
a2615ebc 308
2d17d68f
RR
309 *viewStart = pos;
310 *viewLength = range;
311 *objectLength = thumb;
312 *pageLength = page;
6de97a3b 313}
c801d85f
KB
314
315int wxScrollBar::GetViewLength() const
316{
2d17d68f 317 return (int)(m_adjust->upper+0.5);
6de97a3b 318}
c801d85f
KB
319
320int wxScrollBar::GetObjectLength() const
321{
2d17d68f 322 return (int)(m_adjust->page_size+0.5);
6de97a3b 323}
c801d85f 324
debe6624 325void wxScrollBar::SetPageSize( int pageLength )
c801d85f 326{
2d17d68f
RR
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 );
6de97a3b 331}
c801d85f 332
debe6624 333void wxScrollBar::SetObjectLength( int objectLength )
c801d85f 334{
2d17d68f
RR
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 );
6de97a3b 339}
c801d85f 340
debe6624 341void wxScrollBar::SetViewLength( int viewLength )
c801d85f 342{
2d17d68f
RR
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 );
6de97a3b 347}
c801d85f 348
b4071e91
RR
349bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
350{
2d17d68f 351 GtkRange *range = GTK_RANGE(m_widget);
9e691f46
VZ
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 );
b4071e91 360}
58614078 361
7f980cff
VS
362wxSize wxScrollBar::DoGetBestSize() const
363{
364 return wxControl::DoGetBestSize();
365}
366
9d522606
RD
367// static
368wxVisualAttributes
369wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
370{
371 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
372}
373
dcf924a3 374#endif