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