]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/scrolbar.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
12 | #pragma implementation "scrolbar.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #if wxUSE_SCROLLBAR | |
19 | ||
20 | #include "wx/scrolbar.h" | |
21 | ||
22 | #include "wx/utils.h" | |
23 | #include "wx/math.h" | |
24 | #include "wx/gtk/private.h" | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // idle system | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern void wxapp_install_idle_handler(); | |
31 | extern bool g_isIdle; | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // data | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | extern bool g_blockEventsOnDrag; | |
38 | extern bool g_blockEventsOnScroll; | |
39 | ||
40 | static const float sensitivity = 0.02; | |
41 | ||
42 | //----------------------------------------------------------------------------- | |
43 | // "value_changed" | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | // FIXME: is GtkScrollType really passed to us as 2nd argument? | |
47 | ||
48 | static void gtk_scrollbar_callback( GtkAdjustment *adjust, | |
49 | SCROLLBAR_CBACK_ARG | |
50 | wxScrollBar *win ) | |
51 | { | |
52 | if (g_isIdle) wxapp_install_idle_handler(); | |
53 | ||
54 | if (!win->m_hasVMT) return; | |
55 | if (g_blockEventsOnDrag) return; | |
56 | ||
57 | float diff = adjust->value - win->m_oldPos; | |
58 | if (fabs(diff) < sensitivity) return; | |
59 | ||
60 | win->m_oldPos = adjust->value; | |
61 | ||
62 | wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget)); | |
63 | ||
64 | double dvalue = adjust->value; | |
65 | int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5); | |
66 | ||
67 | int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; | |
68 | ||
69 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
70 | event.SetEventObject( win ); | |
71 | win->GetEventHandler()->ProcessEvent( event ); | |
72 | ||
73 | /* | |
74 | wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() ); | |
75 | cevent.SetEventObject( win ); | |
76 | win->ProcessEvent( cevent ); | |
77 | */ | |
78 | } | |
79 | ||
80 | //----------------------------------------------------------------------------- | |
81 | // "button_press_event" from slider | |
82 | //----------------------------------------------------------------------------- | |
83 | ||
84 | static gint gtk_scrollbar_button_press_callback( GtkRange *widget, | |
85 | GdkEventButton *gdk_event, | |
86 | wxScrollBar *win ) | |
87 | { | |
88 | if (g_isIdle) wxapp_install_idle_handler(); | |
89 | ||
90 | // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd | |
91 | ||
92 | // FIXME: there is no slider field any more, what was meant here? | |
93 | #ifndef __WXGTK20__ | |
94 | win->m_isScrolling = (gdk_event->window == widget->slider); | |
95 | #endif | |
96 | ||
97 | return FALSE; | |
98 | } | |
99 | ||
100 | //----------------------------------------------------------------------------- | |
101 | // "button_release_event" from slider | |
102 | //----------------------------------------------------------------------------- | |
103 | ||
104 | static gint | |
105 | gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget), | |
106 | GdkEventButton *WXUNUSED(gdk_event), | |
107 | wxScrollBar *win ) | |
108 | { | |
109 | if (g_isIdle) | |
110 | wxapp_install_idle_handler(); | |
111 | ||
112 | // g_blockEventsOnScroll = FALSE; | |
113 | ||
114 | if (win->m_isScrolling) | |
115 | { | |
116 | wxEventType command = wxEVT_SCROLL_THUMBRELEASE; | |
117 | int value = (int)ceil(win->m_adjust->value); | |
118 | int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; | |
119 | ||
120 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
121 | event.SetEventObject( win ); | |
122 | win->GetEventHandler()->ProcessEvent( event ); | |
123 | } | |
124 | ||
125 | win->m_isScrolling = FALSE; | |
126 | ||
127 | return FALSE; | |
128 | } | |
129 | ||
130 | //----------------------------------------------------------------------------- | |
131 | // wxScrollBar | |
132 | //----------------------------------------------------------------------------- | |
133 | ||
134 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) | |
135 | ||
136 | wxScrollBar::~wxScrollBar() | |
137 | { | |
138 | } | |
139 | ||
140 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
141 | const wxPoint& pos, const wxSize& size, | |
142 | long style, const wxValidator& validator, const wxString& name ) | |
143 | { | |
144 | m_needParent = TRUE; | |
145 | m_acceptsFocus = TRUE; | |
146 | ||
147 | if (!PreCreation( parent, pos, size ) || | |
148 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
149 | { | |
150 | wxFAIL_MSG( wxT("wxScrollBar creation failed") ); | |
151 | return FALSE; | |
152 | } | |
153 | ||
154 | m_oldPos = 0.0; | |
155 | ||
156 | if ((style & wxSB_VERTICAL) == wxSB_VERTICAL) | |
157 | m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL ); | |
158 | else | |
159 | m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL ); | |
160 | ||
161 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); | |
162 | ||
163 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
164 | "value_changed", | |
165 | (GtkSignalFunc) gtk_scrollbar_callback, | |
166 | (gpointer) this ); | |
167 | ||
168 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
169 | "button_press_event", | |
170 | (GtkSignalFunc)gtk_scrollbar_button_press_callback, | |
171 | (gpointer) this ); | |
172 | ||
173 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
174 | "button_release_event", | |
175 | (GtkSignalFunc)gtk_scrollbar_button_release_callback, | |
176 | (gpointer) this ); | |
177 | ||
178 | m_parent->DoAddChild( this ); | |
179 | ||
180 | PostCreation(size); | |
181 | ||
182 | return TRUE; | |
183 | } | |
184 | ||
185 | int wxScrollBar::GetThumbPosition() const | |
186 | { | |
187 | double val = m_adjust->value; | |
188 | return (int)(val < 0 ? val - 0.5 : val + 0.5); | |
189 | } | |
190 | ||
191 | int wxScrollBar::GetThumbSize() const | |
192 | { | |
193 | return (int)(m_adjust->page_size+0.5); | |
194 | } | |
195 | ||
196 | int wxScrollBar::GetPageSize() const | |
197 | { | |
198 | return (int)(m_adjust->page_increment+0.5); | |
199 | } | |
200 | ||
201 | int wxScrollBar::GetRange() const | |
202 | { | |
203 | return (int)(m_adjust->upper+0.5); | |
204 | } | |
205 | ||
206 | void wxScrollBar::SetThumbPosition( int viewStart ) | |
207 | { | |
208 | if (m_isScrolling) return; | |
209 | ||
210 | float fpos = (float)viewStart; | |
211 | m_oldPos = fpos; | |
212 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
213 | m_adjust->value = fpos; | |
214 | ||
215 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust), | |
216 | (GtkSignalFunc) gtk_scrollbar_callback, | |
217 | (gpointer) this ); | |
218 | ||
219 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
220 | ||
221 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
222 | "value_changed", | |
223 | (GtkSignalFunc) gtk_scrollbar_callback, | |
224 | (gpointer) this ); | |
225 | } | |
226 | ||
227 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, | |
228 | bool WXUNUSED(refresh) ) | |
229 | { | |
230 | float fpos = (float)position; | |
231 | float frange = (float)range; | |
232 | float fthumb = (float)thumbSize; | |
233 | float fpage = (float)pageSize; | |
234 | ||
235 | if ((fabs(frange-m_adjust->upper) < 0.2) && | |
236 | (fabs(fthumb-m_adjust->page_size) < 0.2) && | |
237 | (fabs(fpage-m_adjust->page_increment) < 0.2)) | |
238 | { | |
239 | SetThumbPosition( position ); | |
240 | return; | |
241 | } | |
242 | ||
243 | m_oldPos = fpos; | |
244 | ||
245 | m_adjust->lower = 0.0; | |
246 | m_adjust->upper = frange; | |
247 | m_adjust->value = fpos; | |
248 | m_adjust->step_increment = 1.0; | |
249 | m_adjust->page_increment = (float)(wxMax(fpage,0)); | |
250 | m_adjust->page_size = fthumb; | |
251 | ||
252 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
253 | } | |
254 | ||
255 | /* Backward compatibility */ | |
256 | int wxScrollBar::GetValue() const | |
257 | { | |
258 | return GetThumbPosition(); | |
259 | } | |
260 | ||
261 | void wxScrollBar::SetValue( int viewStart ) | |
262 | { | |
263 | SetThumbPosition( viewStart ); | |
264 | } | |
265 | ||
266 | void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const | |
267 | { | |
268 | int pos = (int)(m_adjust->value+0.5); | |
269 | int thumb = (int)(m_adjust->page_size+0.5); | |
270 | int page = (int)(m_adjust->page_increment+0.5); | |
271 | int range = (int)(m_adjust->upper+0.5); | |
272 | ||
273 | *viewStart = pos; | |
274 | *viewLength = range; | |
275 | *objectLength = thumb; | |
276 | *pageLength = page; | |
277 | } | |
278 | ||
279 | int wxScrollBar::GetViewLength() const | |
280 | { | |
281 | return (int)(m_adjust->upper+0.5); | |
282 | } | |
283 | ||
284 | int wxScrollBar::GetObjectLength() const | |
285 | { | |
286 | return (int)(m_adjust->page_size+0.5); | |
287 | } | |
288 | ||
289 | void wxScrollBar::SetPageSize( int pageLength ) | |
290 | { | |
291 | int pos = (int)(m_adjust->value+0.5); | |
292 | int thumb = (int)(m_adjust->page_size+0.5); | |
293 | int range = (int)(m_adjust->upper+0.5); | |
294 | SetScrollbar( pos, thumb, range, pageLength ); | |
295 | } | |
296 | ||
297 | void wxScrollBar::SetObjectLength( int objectLength ) | |
298 | { | |
299 | int pos = (int)(m_adjust->value+0.5); | |
300 | int page = (int)(m_adjust->page_increment+0.5); | |
301 | int range = (int)(m_adjust->upper+0.5); | |
302 | SetScrollbar( pos, objectLength, range, page ); | |
303 | } | |
304 | ||
305 | void wxScrollBar::SetViewLength( int viewLength ) | |
306 | { | |
307 | int pos = (int)(m_adjust->value+0.5); | |
308 | int thumb = (int)(m_adjust->page_size+0.5); | |
309 | int page = (int)(m_adjust->page_increment+0.5); | |
310 | SetScrollbar( pos, thumb, viewLength, page ); | |
311 | } | |
312 | ||
313 | bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window ) | |
314 | { | |
315 | GtkRange *range = GTK_RANGE(m_widget); | |
316 | return ( (window == GTK_WIDGET(range)->window) | |
317 | #ifndef __WXGTK20__ | |
318 | || (window == range->trough) | |
319 | || (window == range->slider) | |
320 | || (window == range->step_forw) | |
321 | || (window == range->step_back) | |
322 | #endif // GTK+ 1.x | |
323 | ); | |
324 | } | |
325 | ||
326 | wxSize wxScrollBar::DoGetBestSize() const | |
327 | { | |
328 | return wxControl::DoGetBestSize(); | |
329 | } | |
330 | ||
331 | // static | |
332 | wxVisualAttributes | |
333 | wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
334 | { | |
335 | return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new); | |
336 | } | |
337 | ||
338 | #endif |