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