]>
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->m_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->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 = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; | |
74 | ||
75 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
76 | event.SetEventObject( win ); | |
77 | win->GetEventHandler()->ProcessEvent( event ); | |
78 | ||
79 | /* | |
80 | wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() ); | |
81 | cevent.SetEventObject( win ); | |
82 | win->ProcessEvent( cevent ); | |
83 | */ | |
84 | } | |
85 | ||
86 | //----------------------------------------------------------------------------- | |
87 | // "button_press_event" from slider | |
88 | //----------------------------------------------------------------------------- | |
89 | ||
90 | static gint gtk_scrollbar_button_press_callback( GtkRange *WXUNUSED(widget), | |
91 | GdkEventButton *WXUNUSED(gdk_event), | |
92 | wxScrollBar *win ) | |
93 | { | |
94 | if (g_isIdle) wxapp_install_idle_handler(); | |
95 | ||
96 | win->SetScrolling(TRUE); | |
97 | // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd | |
98 | ||
99 | return FALSE; | |
100 | } | |
101 | ||
102 | //----------------------------------------------------------------------------- | |
103 | // "button_release_event" from slider | |
104 | //----------------------------------------------------------------------------- | |
105 | ||
106 | static gint gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget), | |
107 | GdkEventButton *WXUNUSED(gdk_event), | |
108 | wxScrollBar *win ) | |
109 | { | |
110 | if (g_isIdle) wxapp_install_idle_handler(); | |
111 | ||
112 | win->SetScrolling(FALSE); | |
113 | // g_blockEventsOnScroll = FALSE; | |
114 | ||
115 | gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" ); | |
116 | ||
117 | return FALSE; | |
118 | } | |
119 | ||
120 | //----------------------------------------------------------------------------- | |
121 | // wxScrollBar | |
122 | //----------------------------------------------------------------------------- | |
123 | ||
124 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) | |
125 | ||
126 | wxScrollBar::~wxScrollBar(void) | |
127 | { | |
128 | } | |
129 | ||
130 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
131 | const wxPoint& pos, const wxSize& size, | |
132 | long style, const wxValidator& validator, const wxString& name ) | |
133 | { | |
134 | m_needParent = TRUE; | |
135 | m_acceptsFocus = TRUE; | |
136 | ||
137 | PreCreation( parent, id, pos, size, style, name ); | |
138 | ||
139 | SetValidator( validator ); | |
140 | ||
141 | m_oldPos = 0.0; | |
142 | ||
143 | if (style & wxSB_VERTICAL == wxSB_VERTICAL) | |
144 | m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL ); | |
145 | else | |
146 | m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL ); | |
147 | ||
148 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); | |
149 | ||
150 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
151 | "value_changed", | |
152 | (GtkSignalFunc) gtk_scrollbar_callback, | |
153 | (gpointer) this ); | |
154 | ||
155 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
156 | "button_press_event", | |
157 | (GtkSignalFunc)gtk_scrollbar_button_press_callback, | |
158 | (gpointer) this ); | |
159 | ||
160 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
161 | "button_release_event", | |
162 | (GtkSignalFunc)gtk_scrollbar_button_release_callback, | |
163 | (gpointer) this ); | |
164 | ||
165 | m_parent->DoAddChild( this ); | |
166 | ||
167 | PostCreation(); | |
168 | ||
169 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
170 | ||
171 | Show( TRUE ); | |
172 | ||
173 | return TRUE; | |
174 | } | |
175 | ||
176 | int wxScrollBar::GetThumbPosition(void) const | |
177 | { | |
178 | return (int)(m_adjust->value+0.5); | |
179 | } | |
180 | ||
181 | int wxScrollBar::GetThumbSize() const | |
182 | { | |
183 | return (int)(m_adjust->page_size+0.5); | |
184 | } | |
185 | ||
186 | int wxScrollBar::GetPageSize() const | |
187 | { | |
188 | return (int)(m_adjust->page_increment+0.5); | |
189 | } | |
190 | ||
191 | int wxScrollBar::GetRange() const | |
192 | { | |
193 | return (int)(m_adjust->upper+0.5); | |
194 | } | |
195 | ||
196 | void wxScrollBar::SetThumbPosition( int viewStart ) | |
197 | { | |
198 | if (m_isScrolling) return; | |
199 | ||
200 | float fpos = (float)viewStart; | |
201 | m_oldPos = fpos; | |
202 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
203 | m_adjust->value = fpos; | |
204 | ||
205 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
206 | } | |
207 | ||
208 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, | |
209 | bool WXUNUSED(refresh) ) | |
210 | { | |
211 | float fpos = (float)position; | |
212 | float frange = (float)range; | |
213 | float fthumb = (float)thumbSize; | |
214 | float fpage = (float)pageSize; | |
215 | ||
216 | if ((fabs(frange-m_adjust->upper) < 0.2) && | |
217 | (fabs(fthumb-m_adjust->page_size) < 0.2) && | |
218 | (fabs(fpage-m_adjust->page_increment) < 0.2)) | |
219 | { | |
220 | SetThumbPosition( position ); | |
221 | return; | |
222 | } | |
223 | ||
224 | m_oldPos = fpos; | |
225 | ||
226 | m_adjust->lower = 0.0; | |
227 | m_adjust->upper = frange; | |
228 | m_adjust->value = fpos; | |
229 | m_adjust->step_increment = 1.0; | |
230 | m_adjust->page_increment = (float)(wxMax(fpage,0)); | |
231 | m_adjust->page_size = fthumb; | |
232 | ||
233 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
234 | } | |
235 | ||
236 | /* Backward compatibility */ | |
237 | int wxScrollBar::GetValue(void) const | |
238 | { | |
239 | return GetThumbPosition(); | |
240 | } | |
241 | ||
242 | void wxScrollBar::SetValue( int viewStart ) | |
243 | { | |
244 | SetThumbPosition( viewStart ); | |
245 | } | |
246 | ||
247 | void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const | |
248 | { | |
249 | int pos = (int)(m_adjust->value+0.5); | |
250 | int thumb = (int)(m_adjust->page_size+0.5); | |
251 | int page = (int)(m_adjust->page_increment+0.5); | |
252 | int range = (int)(m_adjust->upper+0.5); | |
253 | ||
254 | *viewStart = pos; | |
255 | *viewLength = range; | |
256 | *objectLength = thumb; | |
257 | *pageLength = page; | |
258 | } | |
259 | ||
260 | int wxScrollBar::GetViewLength() const | |
261 | { | |
262 | return (int)(m_adjust->upper+0.5); | |
263 | } | |
264 | ||
265 | int wxScrollBar::GetObjectLength() const | |
266 | { | |
267 | return (int)(m_adjust->page_size+0.5); | |
268 | } | |
269 | ||
270 | void wxScrollBar::SetPageSize( int pageLength ) | |
271 | { | |
272 | int pos = (int)(m_adjust->value+0.5); | |
273 | int thumb = (int)(m_adjust->page_size+0.5); | |
274 | int range = (int)(m_adjust->upper+0.5); | |
275 | SetScrollbar( pos, thumb, range, pageLength ); | |
276 | } | |
277 | ||
278 | void wxScrollBar::SetObjectLength( int objectLength ) | |
279 | { | |
280 | int pos = (int)(m_adjust->value+0.5); | |
281 | int page = (int)(m_adjust->page_increment+0.5); | |
282 | int range = (int)(m_adjust->upper+0.5); | |
283 | SetScrollbar( pos, objectLength, range, page ); | |
284 | } | |
285 | ||
286 | void wxScrollBar::SetViewLength( int viewLength ) | |
287 | { | |
288 | int pos = (int)(m_adjust->value+0.5); | |
289 | int thumb = (int)(m_adjust->page_size+0.5); | |
290 | int page = (int)(m_adjust->page_increment+0.5); | |
291 | SetScrollbar( pos, thumb, viewLength, page ); | |
292 | } | |
293 | ||
294 | bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window ) | |
295 | { | |
296 | GtkRange *range = GTK_RANGE(m_widget); | |
297 | return ( (window == GTK_WIDGET(range)->window) || | |
298 | (window == range->trough) || | |
299 | (window == range->slider) || | |
300 | (window == range->step_forw) || | |
301 | (window == range->step_back) ); | |
302 | } | |
303 | ||
304 | void wxScrollBar::ApplyWidgetStyle() | |
305 | { | |
306 | SetWidgetStyle(); | |
307 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
308 | } | |
309 |