]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
return version of GTK+ library the app links against, not the one it was compiled...
[wxWidgets.git] / src / gtk / scrolbar.cpp
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
24 #include <math.h>
25
26 #include "wx/gtk/private.h"
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40 extern bool g_blockEventsOnScroll;
41
42 static const float sensitivity = 0.02;
43
44 //-----------------------------------------------------------------------------
45 // "value_changed"
46 //-----------------------------------------------------------------------------
47
48 // FIXME: is GtkScrollType really passed to us as 2nd argument?
49
50 static void gtk_scrollbar_callback( GtkAdjustment *adjust,
51 SCROLLBAR_CBACK_ARG
52 wxScrollBar *win )
53 {
54 if (g_isIdle) wxapp_install_idle_handler();
55
56 if (!win->m_hasVMT) return;
57 if (g_blockEventsOnDrag) return;
58
59 float diff = adjust->value - win->m_oldPos;
60 if (fabs(diff) < sensitivity) return;
61
62 win->m_oldPos = adjust->value;
63
64 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
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 // FIXME: there is no slider field any more, what was meant here?
95 #ifndef __WXGTK20__
96 win->m_isScrolling = (gdk_event->window == widget->slider);
97 #endif
98
99 return FALSE;
100 }
101
102 //-----------------------------------------------------------------------------
103 // "button_release_event" from slider
104 //-----------------------------------------------------------------------------
105
106 static gint
107 gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
108 GdkEventButton *WXUNUSED(gdk_event),
109 wxScrollBar *win )
110 {
111 if (g_isIdle)
112 wxapp_install_idle_handler();
113
114 // g_blockEventsOnScroll = FALSE;
115
116 if (win->m_isScrolling)
117 {
118 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
119 int value = (int)ceil(win->m_adjust->value);
120 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
121
122 wxScrollEvent event( command, win->GetId(), value, orient );
123 event.SetEventObject( win );
124 win->GetEventHandler()->ProcessEvent( event );
125 }
126
127 win->m_isScrolling = FALSE;
128
129 return FALSE;
130 }
131
132 //-----------------------------------------------------------------------------
133 // wxScrollBar
134 //-----------------------------------------------------------------------------
135
136 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
137
138 wxScrollBar::~wxScrollBar()
139 {
140 }
141
142 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
143 const wxPoint& pos, const wxSize& size,
144 long style, const wxValidator& validator, const wxString& name )
145 {
146 m_needParent = TRUE;
147 m_acceptsFocus = TRUE;
148
149 if (!PreCreation( parent, pos, size ) ||
150 !CreateBase( parent, id, pos, size, style, validator, name ))
151 {
152 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
153 return FALSE;
154 }
155
156 m_oldPos = 0.0;
157
158 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
159 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
160 else
161 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
162
163 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
164
165 gtk_signal_connect( GTK_OBJECT(m_adjust),
166 "value_changed",
167 (GtkSignalFunc) gtk_scrollbar_callback,
168 (gpointer) this );
169
170 gtk_signal_connect( GTK_OBJECT(m_widget),
171 "button_press_event",
172 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
173 (gpointer) this );
174
175 gtk_signal_connect( GTK_OBJECT(m_widget),
176 "button_release_event",
177 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
178 (gpointer) this );
179
180 m_parent->DoAddChild( this );
181
182 PostCreation();
183 InheritAttributes();
184
185 SetBestSize(size);
186
187 Show( TRUE );
188
189 return TRUE;
190 }
191
192 int wxScrollBar::GetThumbPosition() const
193 {
194 double val = m_adjust->value;
195 return (int)(val < 0 ? val - 0.5 : val + 0.5);
196 }
197
198 int wxScrollBar::GetThumbSize() const
199 {
200 return (int)(m_adjust->page_size+0.5);
201 }
202
203 int wxScrollBar::GetPageSize() const
204 {
205 return (int)(m_adjust->page_increment+0.5);
206 }
207
208 int wxScrollBar::GetRange() const
209 {
210 return (int)(m_adjust->upper+0.5);
211 }
212
213 void wxScrollBar::SetThumbPosition( int viewStart )
214 {
215 if (m_isScrolling) return;
216
217 float fpos = (float)viewStart;
218 m_oldPos = fpos;
219 if (fabs(fpos-m_adjust->value) < 0.2) return;
220 m_adjust->value = fpos;
221
222 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
223 (GtkSignalFunc) gtk_scrollbar_callback,
224 (gpointer) this );
225
226 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
227
228 gtk_signal_connect( GTK_OBJECT(m_adjust),
229 "value_changed",
230 (GtkSignalFunc) gtk_scrollbar_callback,
231 (gpointer) this );
232 }
233
234 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
235 bool WXUNUSED(refresh) )
236 {
237 float fpos = (float)position;
238 float frange = (float)range;
239 float fthumb = (float)thumbSize;
240 float fpage = (float)pageSize;
241
242 if ((fabs(frange-m_adjust->upper) < 0.2) &&
243 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
244 (fabs(fpage-m_adjust->page_increment) < 0.2))
245 {
246 SetThumbPosition( position );
247 return;
248 }
249
250 m_oldPos = fpos;
251
252 m_adjust->lower = 0.0;
253 m_adjust->upper = frange;
254 m_adjust->value = fpos;
255 m_adjust->step_increment = 1.0;
256 m_adjust->page_increment = (float)(wxMax(fpage,0));
257 m_adjust->page_size = fthumb;
258
259 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
260 }
261
262 /* Backward compatibility */
263 int wxScrollBar::GetValue() const
264 {
265 return GetThumbPosition();
266 }
267
268 void wxScrollBar::SetValue( int viewStart )
269 {
270 SetThumbPosition( viewStart );
271 }
272
273 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
274 {
275 int pos = (int)(m_adjust->value+0.5);
276 int thumb = (int)(m_adjust->page_size+0.5);
277 int page = (int)(m_adjust->page_increment+0.5);
278 int range = (int)(m_adjust->upper+0.5);
279
280 *viewStart = pos;
281 *viewLength = range;
282 *objectLength = thumb;
283 *pageLength = page;
284 }
285
286 int wxScrollBar::GetViewLength() const
287 {
288 return (int)(m_adjust->upper+0.5);
289 }
290
291 int wxScrollBar::GetObjectLength() const
292 {
293 return (int)(m_adjust->page_size+0.5);
294 }
295
296 void wxScrollBar::SetPageSize( int pageLength )
297 {
298 int pos = (int)(m_adjust->value+0.5);
299 int thumb = (int)(m_adjust->page_size+0.5);
300 int range = (int)(m_adjust->upper+0.5);
301 SetScrollbar( pos, thumb, range, pageLength );
302 }
303
304 void wxScrollBar::SetObjectLength( int objectLength )
305 {
306 int pos = (int)(m_adjust->value+0.5);
307 int page = (int)(m_adjust->page_increment+0.5);
308 int range = (int)(m_adjust->upper+0.5);
309 SetScrollbar( pos, objectLength, range, page );
310 }
311
312 void wxScrollBar::SetViewLength( int viewLength )
313 {
314 int pos = (int)(m_adjust->value+0.5);
315 int thumb = (int)(m_adjust->page_size+0.5);
316 int page = (int)(m_adjust->page_increment+0.5);
317 SetScrollbar( pos, thumb, viewLength, page );
318 }
319
320 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
321 {
322 GtkRange *range = GTK_RANGE(m_widget);
323 return ( (window == GTK_WIDGET(range)->window)
324 #ifndef __WXGTK20__
325 || (window == range->trough)
326 || (window == range->slider)
327 || (window == range->step_forw)
328 || (window == range->step_back)
329 #endif // GTK+ 1.x
330 );
331 }
332
333 void wxScrollBar::ApplyWidgetStyle()
334 {
335 SetWidgetStyle();
336 gtk_widget_set_style( m_widget, m_widgetStyle );
337 }
338
339 wxSize wxScrollBar::DoGetBestSize() const
340 {
341 return wxControl::DoGetBestSize();
342 }
343
344 #endif