]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/scrolbar.cpp
Wait() doesn't cancel the thread any longer
[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 #ifdef __GNUG__
12 #pragma implementation "scrolbar.h"
13 #endif
14
15 #include "wx/defs.h"
16
17 #if wxUSE_SCROLLBAR
18
19 #include "wx/scrolbar.h"
20
21 #include "wx/utils.h"
22
23 #include <math.h>
24
25 #include "wx/gtk/private.h"
26
27 //-----------------------------------------------------------------------------
28 // idle system
29 //-----------------------------------------------------------------------------
30
31 extern void wxapp_install_idle_handler();
32 extern bool g_isIdle;
33
34 //-----------------------------------------------------------------------------
35 // data
36 //-----------------------------------------------------------------------------
37
38 extern bool g_blockEventsOnDrag;
39 extern bool g_blockEventsOnScroll;
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 static void gtk_scrollbar_callback( GtkAdjustment *adjust,
50 SCROLLBAR_CBACK_ARG
51 wxScrollBar *win )
52 {
53 if (g_isIdle) wxapp_install_idle_handler();
54
55 if (!win->m_hasVMT) return;
56 if (g_blockEventsOnDrag) return;
57
58 float diff = adjust->value - win->m_oldPos;
59 if (fabs(diff) < sensitivity) return;
60
61 win->m_oldPos = adjust->value;
62
63 wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget));
64
65 double dvalue = adjust->value;
66 int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5);
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 *widget,
86 GdkEventButton *gdk_event,
87 wxScrollBar *win )
88 {
89 if (g_isIdle) wxapp_install_idle_handler();
90
91 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
92
93 // FIXME: there is no slider field any more, what was meant here?
94 #ifndef __WXGTK20__
95 win->m_isScrolling = (gdk_event->window == widget->slider);
96 #endif
97
98 return FALSE;
99 }
100
101 //-----------------------------------------------------------------------------
102 // "button_release_event" from slider
103 //-----------------------------------------------------------------------------
104
105 static gint
106 gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget),
107 GdkEventButton *WXUNUSED(gdk_event),
108 wxScrollBar *win )
109 {
110 if (g_isIdle)
111 wxapp_install_idle_handler();
112
113 // g_blockEventsOnScroll = FALSE;
114
115 if (win->m_isScrolling)
116 {
117 wxEventType command = wxEVT_SCROLL_THUMBRELEASE;
118 int value = (int)ceil(win->m_adjust->value);
119 int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
120
121 wxScrollEvent event( command, win->GetId(), value, orient );
122 event.SetEventObject( win );
123 win->GetEventHandler()->ProcessEvent( event );
124 }
125
126 win->m_isScrolling = FALSE;
127
128 return FALSE;
129 }
130
131 //-----------------------------------------------------------------------------
132 // wxScrollBar
133 //-----------------------------------------------------------------------------
134
135 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
136
137 wxScrollBar::~wxScrollBar()
138 {
139 }
140
141 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
142 const wxPoint& pos, const wxSize& size,
143 long style, const wxValidator& validator, const wxString& name )
144 {
145 m_needParent = TRUE;
146 m_acceptsFocus = TRUE;
147
148 if (!PreCreation( parent, pos, size ) ||
149 !CreateBase( parent, id, pos, size, style, validator, name ))
150 {
151 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
152 return FALSE;
153 }
154
155 m_oldPos = 0.0;
156
157 if ((style & wxSB_VERTICAL) == wxSB_VERTICAL)
158 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
159 else
160 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
161
162 m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) );
163
164 gtk_signal_connect( GTK_OBJECT(m_adjust),
165 "value_changed",
166 (GtkSignalFunc) gtk_scrollbar_callback,
167 (gpointer) this );
168
169 gtk_signal_connect( GTK_OBJECT(m_widget),
170 "button_press_event",
171 (GtkSignalFunc)gtk_scrollbar_button_press_callback,
172 (gpointer) this );
173
174 gtk_signal_connect( GTK_OBJECT(m_widget),
175 "button_release_event",
176 (GtkSignalFunc)gtk_scrollbar_button_release_callback,
177 (gpointer) this );
178
179 m_parent->DoAddChild( this );
180
181 PostCreation();
182
183 SetBestSize(size);
184
185 SetBackgroundColour( parent->GetBackgroundColour() );
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