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