]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/scrolbar.cpp
reSWIGged
[wxWidgets.git] / src / gtk1 / 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 #include "wx/math.h"
24 #include "wx/gtk/private.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 static wxEventType g_currentUpDownEvent = wxEVT_NULL;
40
41 static const float sensitivity = 0.02;
42
43 //-----------------------------------------------------------------------------
44 // "value_changed"
45 //-----------------------------------------------------------------------------
46
47 // FIXME: is GtkScrollType really passed to us as 2nd argument?
48
49 extern "C" {
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 // throw a LINEUP / LINEDOWN event if necessary
72 if (g_currentUpDownEvent != wxEVT_NULL)
73 {
74 wxScrollEvent event( g_currentUpDownEvent, win->GetId(), value, orient );
75 event.SetEventObject( win );
76 win->GetEventHandler()->ProcessEvent( event );
77 }
78
79 // throw other event (wxEVT_SCROLL_THUMBTRACK)
80 wxScrollEvent event( command, win->GetId(), value, orient );
81 event.SetEventObject( win );
82 win->GetEventHandler()->ProcessEvent( event );
83
84 /*
85 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
86 cevent.SetEventObject( win );
87 win->ProcessEvent( cevent );
88 */
89 }
90 }
91
92 //-----------------------------------------------------------------------------
93 // "button_press_event" from slider
94 //-----------------------------------------------------------------------------
95 extern "C" {
96 static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
97 GdkEventButton *gdk_event,
98 wxScrollBar *win )
99 {
100 if (g_isIdle) wxapp_install_idle_handler();
101
102 // check if a LINEUP/LINEDOWN event must be thrown
103 // I suppose here the size of scrollbar top/bottom buttons is 16px height
104 if (gdk_event->type == GDK_BUTTON_PRESS && gdk_event->button == 1)
105 {
106 int scroll_height, mouse_pos;
107
108 // get the mouse position when the click is done
109 if (win->HasFlag(wxSB_VERTICAL))
110 {
111 scroll_height = GTK_WIDGET(widget)->allocation.height - 16;
112 mouse_pos = (int)gdk_event->y;
113 }
114 else
115 {
116 scroll_height = GTK_WIDGET(widget)->allocation.width - 16;
117 mouse_pos = (int)gdk_event->x;
118 }
119
120 // compare mouse position to scrollbar height
121 if (mouse_pos > scroll_height)
122 g_currentUpDownEvent = wxEVT_SCROLL_LINEDOWN;
123 else if (mouse_pos < 16)
124 g_currentUpDownEvent = wxEVT_SCROLL_LINEUP;
125 }
126
127 #ifndef __WXGTK20__
128 // There is no slider field any more
129 win->m_isScrolling = (gdk_event->window == widget->slider);
130 #endif
131
132 return FALSE;
133 }
134 }
135
136 //-----------------------------------------------------------------------------
137 // "button_release_event" from slider
138 //-----------------------------------------------------------------------------
139
140 extern "C" {
141 static gint
142 gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
143 GdkEventButton *WXUNUSED(gdk_event),
144 wxScrollBar *win )
145 {
146 if (g_isIdle)
147 wxapp_install_idle_handler();
148
149 if (win->m_isScrolling)
150 {
151 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
152 int value = (int)ceil(win->m_adjust->value);
153 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
154
155 wxScrollEvent event( command, win->GetId(), value, orient );
156 event.SetEventObject( win );
157 win->GetEventHandler()->ProcessEvent( event );
158 }
159
160 win->m_isScrolling = FALSE;
161
162 // reset the LINEUP/LINEDOWN flag when the mouse button is released
163 g_currentUpDownEvent = wxEVT_NULL;
164
165 return FALSE;
166 }
167 }
168
169 //-----------------------------------------------------------------------------
170 // wxScrollBar
171 //-----------------------------------------------------------------------------
172
173 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
174
175 wxScrollBar::~wxScrollBar()
176 {
177 }
178
179 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
180 const wxPoint& pos, const wxSize& size,
181 long style, const wxValidator& validator, const wxString& name )
182 {
183 m_needParent = TRUE;
184 m_acceptsFocus = TRUE;
185
186 if (!PreCreation( parent, pos, size ) ||
187 !CreateBase( parent, id, pos, size, style, validator, name ))
188 {
189 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
190 return FALSE;
191 }
192
193 m_oldPos = 0.0;
194
195 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
196 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
197 else
198 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
199
200 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
201
202 gtk_signal_connect( GTK_OBJECT(m_adjust),
203 "value_changed",
204 (GtkSignalFunc) gtk_scrollbar_callback,
205 (gpointer) this );
206 gtk_signal_connect( GTK_OBJECT(m_widget),
207 "button_press_event",
208 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
209 (gpointer) this );
210 gtk_signal_connect( GTK_OBJECT(m_widget),
211 "button_release_event",
212 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
213 (gpointer) this );
214
215 m_parent->DoAddChild( this );
216
217 PostCreation(size);
218
219 return TRUE;
220 }
221
222 int wxScrollBar::GetThumbPosition() const
223 {
224 double val = m_adjust->value;
225 return (int)(val < 0 ? val - 0.5 : val + 0.5);
226 }
227
228 int wxScrollBar::GetThumbSize() const
229 {
230 return (int)(m_adjust->page_size+0.5);
231 }
232
233 int wxScrollBar::GetPageSize() const
234 {
235 return (int)(m_adjust->page_increment+0.5);
236 }
237
238 int wxScrollBar::GetRange() const
239 {
240 return (int)(m_adjust->upper+0.5);
241 }
242
243 void wxScrollBar::SetThumbPosition( int viewStart )
244 {
245 if (m_isScrolling) return;
246
247 float fpos = (float)viewStart;
248 m_oldPos = fpos;
249 if (fabs(fpos-m_adjust->value) < 0.2) return;
250 m_adjust->value = fpos;
251
252 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust),
253 (GtkSignalFunc) gtk_scrollbar_callback,
254 (gpointer) this );
255
256 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
257
258 gtk_signal_connect( GTK_OBJECT(m_adjust),
259 "value_changed",
260 (GtkSignalFunc) gtk_scrollbar_callback,
261 (gpointer) this );
262 }
263
264 void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize,
265 bool WXUNUSED(refresh) )
266 {
267 float fpos = (float)position;
268 float frange = (float)range;
269 float fthumb = (float)thumbSize;
270 float fpage = (float)pageSize;
271
272 if ((fabs(frange-m_adjust->upper) < 0.2) &&
273 (fabs(fthumb-m_adjust->page_size) < 0.2) &&
274 (fabs(fpage-m_adjust->page_increment) < 0.2))
275 {
276 SetThumbPosition( position );
277 return;
278 }
279
280 m_oldPos = fpos;
281
282 m_adjust->lower = 0.0;
283 m_adjust->upper = frange;
284 m_adjust->value = fpos;
285 m_adjust->step_increment = 1.0;
286 m_adjust->page_increment = (float)(wxMax(fpage,0));
287 m_adjust->page_size = fthumb;
288
289 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
290 }
291
292 /* Backward compatibility */
293 int wxScrollBar::GetValue() const
294 {
295 return GetThumbPosition();
296 }
297
298 void wxScrollBar::SetValue( int viewStart )
299 {
300 SetThumbPosition( viewStart );
301 }
302
303 void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const
304 {
305 int pos = (int)(m_adjust->value+0.5);
306 int thumb = (int)(m_adjust->page_size+0.5);
307 int page = (int)(m_adjust->page_increment+0.5);
308 int range = (int)(m_adjust->upper+0.5);
309
310 *viewStart = pos;
311 *viewLength = range;
312 *objectLength = thumb;
313 *pageLength = page;
314 }
315
316 int wxScrollBar::GetViewLength() const
317 {
318 return (int)(m_adjust->upper+0.5);
319 }
320
321 int wxScrollBar::GetObjectLength() const
322 {
323 return (int)(m_adjust->page_size+0.5);
324 }
325
326 void wxScrollBar::SetPageSize( int pageLength )
327 {
328 int pos = (int)(m_adjust->value+0.5);
329 int thumb = (int)(m_adjust->page_size+0.5);
330 int range = (int)(m_adjust->upper+0.5);
331 SetScrollbar( pos, thumb, range, pageLength );
332 }
333
334 void wxScrollBar::SetObjectLength( int objectLength )
335 {
336 int pos = (int)(m_adjust->value+0.5);
337 int page = (int)(m_adjust->page_increment+0.5);
338 int range = (int)(m_adjust->upper+0.5);
339 SetScrollbar( pos, objectLength, range, page );
340 }
341
342 void wxScrollBar::SetViewLength( int viewLength )
343 {
344 int pos = (int)(m_adjust->value+0.5);
345 int thumb = (int)(m_adjust->page_size+0.5);
346 int page = (int)(m_adjust->page_increment+0.5);
347 SetScrollbar( pos, thumb, viewLength, page );
348 }
349
350 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
351 {
352 GtkRange *range = GTK_RANGE(m_widget);
353 return ( (window == GTK_WIDGET(range)->window)
354 #ifndef __WXGTK20__
355 || (window == range->trough)
356 || (window == range->slider)
357 || (window == range->step_forw)
358 || (window == range->step_back)
359 #endif // GTK+ 1.x
360 );
361 }
362
363 wxSize wxScrollBar::DoGetBestSize() const
364 {
365 return wxControl::DoGetBestSize();
366 }
367
368 // static
369 wxVisualAttributes
370 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
371 {
372 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
373 }
374
375 #endif