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