1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/scrolbar.h"
19 #include "wx/gtk/private.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
33 static wxEventType g_currentUpDownEvent
= wxEVT_NULL
;
35 static const float sensitivity
= 0.02;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 // FIXME: is GtkScrollType really passed to us as 2nd argument?
44 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
,
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!win
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 float diff
= adjust
->value
- win
->m_oldPos
;
54 if (fabs(diff
) < sensitivity
) return;
56 win
->m_oldPos
= adjust
->value
;
58 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
60 double dvalue
= adjust
->value
;
61 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
63 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
65 // throw a LINEUP / LINEDOWN event if necessary
66 if (g_currentUpDownEvent
!= wxEVT_NULL
)
68 wxScrollEvent
event( g_currentUpDownEvent
, win
->GetId(), value
, orient
);
69 event
.SetEventObject( win
);
70 win
->GetEventHandler()->ProcessEvent( event
);
73 // throw other event (wxEVT_SCROLL_THUMBTRACK)
74 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
75 event
.SetEventObject( win
);
76 win
->GetEventHandler()->ProcessEvent( event
);
79 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
80 cevent.SetEventObject( win );
81 win->ProcessEvent( cevent );
86 //-----------------------------------------------------------------------------
87 // "button_press_event" from slider
88 //-----------------------------------------------------------------------------
90 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
91 GdkEventButton
*gdk_event
,
94 if (g_isIdle
) wxapp_install_idle_handler();
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)
100 int scroll_height
, mouse_pos
;
102 // get the mouse position when the click is done
103 if (win
->HasFlag(wxSB_VERTICAL
))
105 scroll_height
= GTK_WIDGET(widget
)->allocation
.height
- 16;
106 mouse_pos
= (int)gdk_event
->y
;
110 scroll_height
= GTK_WIDGET(widget
)->allocation
.width
- 16;
111 mouse_pos
= (int)gdk_event
->x
;
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
;
125 //-----------------------------------------------------------------------------
126 // "button_release_event" from slider
127 //-----------------------------------------------------------------------------
131 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
132 GdkEventButton
*WXUNUSED(gdk_event
),
136 wxapp_install_idle_handler();
138 if (win
->m_isScrolling
)
140 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
141 int value
= (int)ceil(win
->m_adjust
->value
);
142 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
144 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
145 event
.SetEventObject( win
);
146 win
->GetEventHandler()->ProcessEvent( event
);
149 win
->m_isScrolling
= false;
151 // reset the LINEUP/LINEDOWN flag when the mouse button is released
152 g_currentUpDownEvent
= wxEVT_NULL
;
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
162 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
164 wxScrollBar::~wxScrollBar()
168 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
169 const wxPoint
& pos
, const wxSize
& size
,
170 long style
, const wxValidator
& validator
, const wxString
& name
)
173 m_acceptsFocus
= true;
175 if (!PreCreation( parent
, pos
, size
) ||
176 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
178 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
184 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
185 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
187 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
189 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
191 g_signal_connect (m_adjust
, "value_changed",
192 G_CALLBACK (gtk_scrollbar_callback
), this);
193 g_signal_connect (m_widget
, "button_press_event",
194 G_CALLBACK (gtk_scrollbar_button_press_callback
),
196 g_signal_connect (m_widget
, "button_release_event",
197 G_CALLBACK (gtk_scrollbar_button_release_callback
),
200 m_parent
->DoAddChild( this );
207 int wxScrollBar::GetThumbPosition() const
209 double val
= m_adjust
->value
;
210 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
213 int wxScrollBar::GetThumbSize() const
215 return (int)(m_adjust
->page_size
+0.5);
218 int wxScrollBar::GetPageSize() const
220 return (int)(m_adjust
->page_increment
+0.5);
223 int wxScrollBar::GetRange() const
225 return (int)(m_adjust
->upper
+0.5);
228 void wxScrollBar::SetThumbPosition( int viewStart
)
230 if (m_isScrolling
) return;
232 float fpos
= (float)viewStart
;
234 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
235 m_adjust
->value
= fpos
;
237 g_signal_handlers_disconnect_by_func (m_adjust
,
238 (gpointer
) gtk_scrollbar_callback
,
241 g_signal_emit_by_name (m_adjust
, "value_changed");
243 g_signal_connect (m_adjust
, "value_changed",
244 G_CALLBACK (gtk_scrollbar_callback
), this);
247 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
248 bool WXUNUSED(refresh
) )
250 float fpos
= (float)position
;
251 float frange
= (float)range
;
252 float fthumb
= (float)thumbSize
;
253 float fpage
= (float)pageSize
;
255 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
256 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
257 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
259 SetThumbPosition( position
);
265 m_adjust
->lower
= 0.0;
266 m_adjust
->upper
= frange
;
267 m_adjust
->value
= fpos
;
268 m_adjust
->step_increment
= 1.0;
269 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
270 m_adjust
->page_size
= fthumb
;
272 g_signal_emit_by_name (m_adjust
, "changed");
275 /* Backward compatibility */
276 int wxScrollBar::GetValue() const
278 return GetThumbPosition();
281 void wxScrollBar::SetValue( int viewStart
)
283 SetThumbPosition( viewStart
);
286 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
288 int pos
= (int)(m_adjust
->value
+0.5);
289 int thumb
= (int)(m_adjust
->page_size
+0.5);
290 int page
= (int)(m_adjust
->page_increment
+0.5);
291 int range
= (int)(m_adjust
->upper
+0.5);
295 *objectLength
= thumb
;
299 int wxScrollBar::GetViewLength() const
301 return (int)(m_adjust
->upper
+0.5);
304 int wxScrollBar::GetObjectLength() const
306 return (int)(m_adjust
->page_size
+0.5);
309 void wxScrollBar::SetPageSize( int pageLength
)
311 int pos
= (int)(m_adjust
->value
+0.5);
312 int thumb
= (int)(m_adjust
->page_size
+0.5);
313 int range
= (int)(m_adjust
->upper
+0.5);
314 SetScrollbar( pos
, thumb
, range
, pageLength
);
317 void wxScrollBar::SetObjectLength( int objectLength
)
319 int pos
= (int)(m_adjust
->value
+0.5);
320 int page
= (int)(m_adjust
->page_increment
+0.5);
321 int range
= (int)(m_adjust
->upper
+0.5);
322 SetScrollbar( pos
, objectLength
, range
, page
);
325 void wxScrollBar::SetViewLength( int viewLength
)
327 int pos
= (int)(m_adjust
->value
+0.5);
328 int thumb
= (int)(m_adjust
->page_size
+0.5);
329 int page
= (int)(m_adjust
->page_increment
+0.5);
330 SetScrollbar( pos
, thumb
, viewLength
, page
);
333 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
335 GtkRange
*range
= GTK_RANGE(m_widget
);
336 return ( (window
== GTK_WIDGET(range
)->window
) );
339 wxSize
wxScrollBar::DoGetBestSize() const
341 return wxControl::DoGetBestSize();
346 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
348 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);