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