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