]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
tree ctrl sorting shouldn't crash when items don't have data
[wxWidgets.git] / src / gtk / scrolbar.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: scrolbar.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "scrolbar.h"
13#endif
14
15#include "wx/scrolbar.h"
16#include "wx/utils.h"
f5368809 17#include <math.h>
c801d85f 18
83624f79
RR
19#include "gdk/gdk.h"
20#include "gtk/gtk.h"
21
acfd422a
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
66bd6b93
RR
29//-----------------------------------------------------------------------------
30// data
31//-----------------------------------------------------------------------------
32
33extern bool g_blockEventsOnDrag;
76ed8f8d 34extern bool g_blockEventsOnScroll;
66bd6b93 35
c801d85f 36//-----------------------------------------------------------------------------
b4071e91 37// "value_changed"
c801d85f
KB
38//-----------------------------------------------------------------------------
39
66bd6b93 40static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win )
c801d85f 41{
acfd422a
RR
42 if (g_isIdle) wxapp_install_idle_handler();
43
2d17d68f
RR
44 if (!win->HasVMT()) return;
45 if (g_blockEventsOnDrag) return;
c801d85f 46
2d17d68f
RR
47 float diff = win->m_adjust->value - win->m_oldPos;
48 if (fabs(diff) < 0.2) return;
828f655f 49 win->m_oldPos = win->m_adjust->value;
c801d85f 50
2d17d68f 51 wxEventType command = wxEVT_NULL;
c801d85f 52
2d17d68f
RR
53 float line_step = win->m_adjust->step_increment;
54 float page_step = win->m_adjust->page_increment;
c801d85f 55
2d17d68f
RR
56 if (win->m_isScrolling)
57 {
58 command = wxEVT_SCROLL_THUMBTRACK;
59 }
60 else
61 {
62 if (fabs(win->m_adjust->value-win->m_adjust->lower) < 0.2) command = wxEVT_SCROLL_BOTTOM;
63 else if (fabs(win->m_adjust->value-win->m_adjust->upper) < 0.2) command = wxEVT_SCROLL_TOP;
64 else if (fabs(diff-line_step) < 0.2) command = wxEVT_SCROLL_LINEDOWN;
65 else if (fabs(diff+line_step) < 0.2) command = wxEVT_SCROLL_LINEUP;
66 else if (fabs(diff-page_step) < 0.2) command = wxEVT_SCROLL_PAGEDOWN;
67 else if (fabs(diff+page_step) < 0.2) command = wxEVT_SCROLL_PAGEUP;
68 else command = wxEVT_SCROLL_THUMBTRACK;
69 }
70
71 int value = (int)(win->m_adjust->value+0.5);
c801d85f 72
2d17d68f
RR
73 int orient = wxHORIZONTAL;
74 if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL;
c801d85f 75
2d17d68f
RR
76 wxScrollEvent event( command, win->GetId(), value, orient );
77 event.SetEventObject( win );
78 win->GetEventHandler()->ProcessEvent( event );
c801d85f
KB
79
80/*
2d17d68f
RR
81 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
82 cevent.SetEventObject( win );
83 win->ProcessEvent( cevent );
c801d85f 84*/
6de97a3b 85}
c801d85f 86
cb43b372
RR
87//-----------------------------------------------------------------------------
88// "button_press_event" from slider
89//-----------------------------------------------------------------------------
90
76ed8f8d
RR
91static gint gtk_scrollbar_button_press_callback( GtkRange *WXUNUSED(widget),
92 GdkEventButton *WXUNUSED(gdk_event),
93 wxScrollBar *win )
cb43b372 94{
acfd422a
RR
95 if (g_isIdle) wxapp_install_idle_handler();
96
97 win->m_isScrolling = TRUE;
1ecc4d80 98// g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
cb43b372 99
acfd422a 100 return FALSE;
cb43b372
RR
101}
102
103//-----------------------------------------------------------------------------
104// "button_release_event" from slider
105//-----------------------------------------------------------------------------
106
76ed8f8d
RR
107static gint gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
108 GdkEventButton *WXUNUSED(gdk_event),
109 wxScrollBar *win )
cb43b372 110{
acfd422a
RR
111 if (g_isIdle) wxapp_install_idle_handler();
112
2d17d68f 113 win->m_isScrolling = FALSE;
1ecc4d80 114// g_blockEventsOnScroll = FALSE;
cb43b372 115
2d17d68f 116 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
76ed8f8d 117
2d17d68f 118 return FALSE;
cb43b372
RR
119}
120
b4071e91
RR
121//-----------------------------------------------------------------------------
122// wxScrollBar
123//-----------------------------------------------------------------------------
124
c801d85f
KB
125IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
126
c801d85f
KB
127wxScrollBar::~wxScrollBar(void)
128{
6de97a3b 129}
c801d85f 130
debe6624 131bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 132 const wxPoint& pos, const wxSize& size,
6de97a3b 133 long style, const wxValidator& validator, const wxString& name )
c801d85f 134{
2d17d68f
RR
135 m_needParent = TRUE;
136 m_acceptsFocus = TRUE;
c801d85f 137
2d17d68f 138 PreCreation( parent, id, pos, size, style, name );
c801d85f 139
2d17d68f 140 SetValidator( validator );
6de97a3b 141
2d17d68f 142 m_oldPos = 0.0;
c801d85f 143
2d17d68f
RR
144 if (style & wxSB_VERTICAL == wxSB_VERTICAL)
145 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
146 else
147 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
c801d85f 148
2d17d68f 149 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
c801d85f 150
2d17d68f
RR
151 gtk_signal_connect( GTK_OBJECT(m_adjust),
152 "value_changed",
153 (GtkSignalFunc) gtk_scrollbar_callback,
154 (gpointer) this );
c801d85f 155
2d17d68f
RR
156 gtk_signal_connect( GTK_OBJECT(m_widget),
157 "button_press_event",
158 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
159 (gpointer) this );
cb43b372 160
2d17d68f
RR
161 gtk_signal_connect( GTK_OBJECT(m_widget),
162 "button_release_event",
163 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
164 (gpointer) this );
cb43b372 165
2d17d68f 166 m_parent->AddChild( this );
6ca41e57 167
2d17d68f 168 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 169
2d17d68f 170 PostCreation();
c801d85f 171
2d17d68f 172 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 173
2d17d68f 174 Show( TRUE );
c801d85f 175
2d17d68f 176 return TRUE;
6de97a3b 177}
c801d85f 178
4fabb575 179int wxScrollBar::GetThumbPosition(void) const
c801d85f 180{
2d17d68f 181 return (int)(m_adjust->value+0.5);
6de97a3b 182}
c801d85f
KB
183
184int wxScrollBar::GetThumbSize() const
185{
2d17d68f 186 return (int)(m_adjust->page_size+0.5);
6de97a3b 187}
c801d85f
KB
188
189int wxScrollBar::GetPageSize() const
190{
2d17d68f 191 return (int)(m_adjust->page_increment+0.5);
6de97a3b 192}
c801d85f
KB
193
194int wxScrollBar::GetRange() const
195{
2d17d68f 196 return (int)(m_adjust->upper+0.5);
6de97a3b 197}
c801d85f 198
4fabb575 199void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 200{
2d17d68f 201 if (m_isScrolling) return;
76ed8f8d 202
2d17d68f
RR
203 float fpos = (float)viewStart;
204 m_oldPos = fpos;
205 if (fabs(fpos-m_adjust->value) < 0.2) return;
206 m_adjust->value = fpos;
76ed8f8d 207
2d17d68f 208 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
6de97a3b 209}
c801d85f 210
debe6624
JS
211void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
212 bool WXUNUSED(refresh) )
c801d85f 213{
2d17d68f
RR
214 float fpos = (float)position;
215 float frange = (float)range;
216 float fthumb = (float)thumbSize;
217 float fpage = (float)pageSize;
c801d85f 218
2d17d68f
RR
219 if ((fabs(frange-m_adjust->upper) < 0.2) &&
220 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
221 (fabs(fpage-m_adjust->page_increment) < 0.2))
222 {
223 SetThumbPosition( position );
224 return;
225 }
84efdbf1 226
2d17d68f 227 m_oldPos = fpos;
cb43b372 228
2d17d68f
RR
229 m_adjust->lower = 0.0;
230 m_adjust->upper = frange;
231 m_adjust->value = fpos;
232 m_adjust->step_increment = 1.0;
233 m_adjust->page_increment = (float)(wxMax(fpage,0));
234 m_adjust->page_size = fthumb;
235
236 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
6de97a3b 237}
c801d85f 238
2d17d68f 239/* Backward compatibility */
c801d85f
KB
240int wxScrollBar::GetValue(void) const
241{
2d17d68f 242 return GetThumbPosition();
6de97a3b 243}
c801d85f 244
debe6624 245void wxScrollBar::SetValue( int viewStart )
c801d85f 246{
2d17d68f 247 SetThumbPosition( viewStart );
6de97a3b 248}
c801d85f
KB
249
250void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
251{
2d17d68f
RR
252 int pos = (int)(m_adjust->value+0.5);
253 int thumb = (int)(m_adjust->page_size+0.5);
254 int page = (int)(m_adjust->page_increment+0.5);
255 int range = (int)(m_adjust->upper+0.5);
c801d85f 256
2d17d68f
RR
257 *viewStart = pos;
258 *viewLength = range;
259 *objectLength = thumb;
260 *pageLength = page;
6de97a3b 261}
c801d85f
KB
262
263int wxScrollBar::GetViewLength() const
264{
2d17d68f 265 return (int)(m_adjust->upper+0.5);
6de97a3b 266}
c801d85f
KB
267
268int wxScrollBar::GetObjectLength() const
269{
2d17d68f 270 return (int)(m_adjust->page_size+0.5);
6de97a3b 271}
c801d85f 272
debe6624 273void wxScrollBar::SetPageSize( int pageLength )
c801d85f 274{
2d17d68f
RR
275 int pos = (int)(m_adjust->value+0.5);
276 int thumb = (int)(m_adjust->page_size+0.5);
277 int range = (int)(m_adjust->upper+0.5);
278 SetScrollbar( pos, thumb, range, pageLength );
6de97a3b 279}
c801d85f 280
debe6624 281void wxScrollBar::SetObjectLength( int objectLength )
c801d85f 282{
2d17d68f
RR
283 int pos = (int)(m_adjust->value+0.5);
284 int page = (int)(m_adjust->page_increment+0.5);
285 int range = (int)(m_adjust->upper+0.5);
286 SetScrollbar( pos, objectLength, range, page );
6de97a3b 287}
c801d85f 288
debe6624 289void wxScrollBar::SetViewLength( int viewLength )
c801d85f 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 SetScrollbar( pos, thumb, viewLength, page );
6de97a3b 295}
c801d85f 296
b4071e91
RR
297bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
298{
2d17d68f
RR
299 GtkRange *range = GTK_RANGE(m_widget);
300 return ( (window == GTK_WIDGET(range)->window) ||
301 (window == range->trough) ||
302 (window == range->slider) ||
303 (window == range->step_forw) ||
304 (window == range->step_back) );
b4071e91 305}
58614078
RR
306
307void wxScrollBar::ApplyWidgetStyle()
308{
2d17d68f
RR
309 SetWidgetStyle();
310 gtk_widget_set_style( m_widget, m_widgetStyle );
58614078
RR
311}
312