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