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