]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: 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 | #ifdef __GNUG__ | |
12 | #pragma implementation "scrolbar.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/scrolbar.h" | |
16 | #include "wx/utils.h" | |
17 | #include <math.h> | |
18 | ||
19 | #include "gdk/gdk.h" | |
20 | #include "gtk/gtk.h" | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // idle system | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern void wxapp_install_idle_handler(); | |
27 | extern bool g_isIdle; | |
28 | ||
29 | //----------------------------------------------------------------------------- | |
30 | // data | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern bool g_blockEventsOnDrag; | |
34 | extern bool g_blockEventsOnScroll; | |
35 | ||
36 | //----------------------------------------------------------------------------- | |
37 | // "value_changed" | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | static void gtk_scrollbar_callback( GtkWidget *WXUNUSED(widget), wxScrollBar *win ) | |
41 | { | |
42 | if (g_isIdle) wxapp_install_idle_handler(); | |
43 | ||
44 | if (!win->HasVMT()) return; | |
45 | if (g_blockEventsOnDrag) return; | |
46 | ||
47 | float diff = win->m_adjust->value - win->m_oldPos; | |
48 | if (fabs(diff) < 0.2) return; | |
49 | win->m_oldPos = win->m_adjust->value; | |
50 | ||
51 | wxEventType command = wxEVT_NULL; | |
52 | ||
53 | float line_step = win->m_adjust->step_increment; | |
54 | float page_step = win->m_adjust->page_increment; | |
55 | ||
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); | |
72 | ||
73 | int orient = wxHORIZONTAL; | |
74 | if (win->GetWindowStyleFlag() & wxSB_VERTICAL == wxSB_VERTICAL) orient = wxVERTICAL; | |
75 | ||
76 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
77 | event.SetEventObject( win ); | |
78 | win->GetEventHandler()->ProcessEvent( event ); | |
79 | ||
80 | /* | |
81 | wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() ); | |
82 | cevent.SetEventObject( win ); | |
83 | win->ProcessEvent( cevent ); | |
84 | */ | |
85 | } | |
86 | ||
87 | //----------------------------------------------------------------------------- | |
88 | // "button_press_event" from slider | |
89 | //----------------------------------------------------------------------------- | |
90 | ||
91 | static gint gtk_scrollbar_button_press_callback( GtkRange *WXUNUSED(widget), | |
92 | GdkEventButton *WXUNUSED(gdk_event), | |
93 | wxScrollBar *win ) | |
94 | { | |
95 | if (g_isIdle) wxapp_install_idle_handler(); | |
96 | ||
97 | win->m_isScrolling = TRUE; | |
98 | // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd | |
99 | ||
100 | return FALSE; | |
101 | } | |
102 | ||
103 | //----------------------------------------------------------------------------- | |
104 | // "button_release_event" from slider | |
105 | //----------------------------------------------------------------------------- | |
106 | ||
107 | static gint gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget), | |
108 | GdkEventButton *WXUNUSED(gdk_event), | |
109 | wxScrollBar *win ) | |
110 | { | |
111 | if (g_isIdle) wxapp_install_idle_handler(); | |
112 | ||
113 | win->m_isScrolling = FALSE; | |
114 | // g_blockEventsOnScroll = FALSE; | |
115 | ||
116 | gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" ); | |
117 | ||
118 | return FALSE; | |
119 | } | |
120 | ||
121 | //----------------------------------------------------------------------------- | |
122 | // wxScrollBar | |
123 | //----------------------------------------------------------------------------- | |
124 | ||
125 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) | |
126 | ||
127 | wxScrollBar::~wxScrollBar(void) | |
128 | { | |
129 | } | |
130 | ||
131 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
132 | const wxPoint& pos, const wxSize& size, | |
133 | long style, const wxValidator& validator, const wxString& name ) | |
134 | { | |
135 | m_needParent = TRUE; | |
136 | m_acceptsFocus = TRUE; | |
137 | ||
138 | PreCreation( parent, id, pos, size, style, name ); | |
139 | ||
140 | SetValidator( validator ); | |
141 | ||
142 | m_oldPos = 0.0; | |
143 | ||
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 ); | |
148 | ||
149 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); | |
150 | ||
151 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
152 | "value_changed", | |
153 | (GtkSignalFunc) gtk_scrollbar_callback, | |
154 | (gpointer) this ); | |
155 | ||
156 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
157 | "button_press_event", | |
158 | (GtkSignalFunc)gtk_scrollbar_button_press_callback, | |
159 | (gpointer) this ); | |
160 | ||
161 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
162 | "button_release_event", | |
163 | (GtkSignalFunc)gtk_scrollbar_button_release_callback, | |
164 | (gpointer) this ); | |
165 | ||
166 | m_parent->AddChild( this ); | |
167 | ||
168 | (m_parent->m_insertCallback)( m_parent, this ); | |
169 | ||
170 | PostCreation(); | |
171 | ||
172 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
173 | ||
174 | Show( TRUE ); | |
175 | ||
176 | return TRUE; | |
177 | } | |
178 | ||
179 | int wxScrollBar::GetThumbPosition(void) const | |
180 | { | |
181 | return (int)(m_adjust->value+0.5); | |
182 | } | |
183 | ||
184 | int wxScrollBar::GetThumbSize() const | |
185 | { | |
186 | return (int)(m_adjust->page_size+0.5); | |
187 | } | |
188 | ||
189 | int wxScrollBar::GetPageSize() const | |
190 | { | |
191 | return (int)(m_adjust->page_increment+0.5); | |
192 | } | |
193 | ||
194 | int wxScrollBar::GetRange() const | |
195 | { | |
196 | return (int)(m_adjust->upper+0.5); | |
197 | } | |
198 | ||
199 | void wxScrollBar::SetThumbPosition( int viewStart ) | |
200 | { | |
201 | if (m_isScrolling) return; | |
202 | ||
203 | float fpos = (float)viewStart; | |
204 | m_oldPos = fpos; | |
205 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
206 | m_adjust->value = fpos; | |
207 | ||
208 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
209 | } | |
210 | ||
211 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, | |
212 | bool WXUNUSED(refresh) ) | |
213 | { | |
214 | float fpos = (float)position; | |
215 | float frange = (float)range; | |
216 | float fthumb = (float)thumbSize; | |
217 | float fpage = (float)pageSize; | |
218 | ||
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 | } | |
226 | ||
227 | m_oldPos = fpos; | |
228 | ||
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" ); | |
237 | } | |
238 | ||
239 | /* Backward compatibility */ | |
240 | int wxScrollBar::GetValue(void) const | |
241 | { | |
242 | return GetThumbPosition(); | |
243 | } | |
244 | ||
245 | void wxScrollBar::SetValue( int viewStart ) | |
246 | { | |
247 | SetThumbPosition( viewStart ); | |
248 | } | |
249 | ||
250 | void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const | |
251 | { | |
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); | |
256 | ||
257 | *viewStart = pos; | |
258 | *viewLength = range; | |
259 | *objectLength = thumb; | |
260 | *pageLength = page; | |
261 | } | |
262 | ||
263 | int wxScrollBar::GetViewLength() const | |
264 | { | |
265 | return (int)(m_adjust->upper+0.5); | |
266 | } | |
267 | ||
268 | int wxScrollBar::GetObjectLength() const | |
269 | { | |
270 | return (int)(m_adjust->page_size+0.5); | |
271 | } | |
272 | ||
273 | void wxScrollBar::SetPageSize( int pageLength ) | |
274 | { | |
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 ); | |
279 | } | |
280 | ||
281 | void wxScrollBar::SetObjectLength( int objectLength ) | |
282 | { | |
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 ); | |
287 | } | |
288 | ||
289 | void wxScrollBar::SetViewLength( int viewLength ) | |
290 | { | |
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 ); | |
295 | } | |
296 | ||
297 | bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window ) | |
298 | { | |
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) ); | |
305 | } | |
306 | ||
307 | void wxScrollBar::ApplyWidgetStyle() | |
308 | { | |
309 | SetWidgetStyle(); | |
310 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
311 | } | |
312 |