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