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 bool g_blockEventsOnDrag
;
26 static wxEventType g_currentUpDownEvent
= wxEVT_NULL
;
28 static const float sensitivity
= 0.02;
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 // FIXME: is GtkScrollType really passed to us as 2nd argument?
37 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
,
40 if (g_isIdle
) wxapp_install_idle_handler();
42 if (!win
->m_hasVMT
) return;
43 if (g_blockEventsOnDrag
) return;
45 float diff
= adjust
->value
- win
->m_oldPos
;
46 if (fabs(diff
) < sensitivity
) return;
48 win
->m_oldPos
= adjust
->value
;
50 wxEventType command
= GtkScrollTypeToWx(GTK_SCROLL_JUMP
);
52 double dvalue
= adjust
->value
;
53 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
55 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
57 // throw a LINEUP / LINEDOWN event if necessary
58 if (g_currentUpDownEvent
!= wxEVT_NULL
)
60 wxScrollEvent
event( g_currentUpDownEvent
, win
->GetId(), value
, orient
);
61 event
.SetEventObject( win
);
62 win
->GetEventHandler()->ProcessEvent( event
);
65 // throw other event (wxEVT_SCROLL_THUMBTRACK)
66 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
67 event
.SetEventObject( win
);
68 win
->GetEventHandler()->ProcessEvent( event
);
71 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
73 win->ProcessEvent( cevent );
78 //-----------------------------------------------------------------------------
79 // "button_press_event" from slider
80 //-----------------------------------------------------------------------------
82 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
83 GdkEventButton
*gdk_event
,
86 if (g_isIdle
) wxapp_install_idle_handler();
88 // check if a LINEUP/LINEDOWN event must be thrown
89 // I suppose here the size of scrollbar top/bottom buttons is 16px height
90 if (gdk_event
->type
== GDK_BUTTON_PRESS
&& gdk_event
->button
== 1)
92 int scroll_height
, mouse_pos
;
94 // get the mouse position when the click is done
95 if (win
->HasFlag(wxSB_VERTICAL
))
97 scroll_height
= GTK_WIDGET(widget
)->allocation
.height
- 16;
98 mouse_pos
= (int)gdk_event
->y
;
102 scroll_height
= GTK_WIDGET(widget
)->allocation
.width
- 16;
103 mouse_pos
= (int)gdk_event
->x
;
106 // compare mouse position to scrollbar height
107 if (mouse_pos
> scroll_height
)
108 g_currentUpDownEvent
= wxEVT_SCROLL_LINEDOWN
;
109 else if (mouse_pos
< 16)
110 g_currentUpDownEvent
= wxEVT_SCROLL_LINEUP
;
117 //-----------------------------------------------------------------------------
118 // "button_release_event" from slider
119 //-----------------------------------------------------------------------------
123 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
124 GdkEventButton
*WXUNUSED(gdk_event
),
128 wxapp_install_idle_handler();
130 if (win
->m_isScrolling
)
132 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
133 int value
= (int)ceil(win
->m_adjust
->value
);
134 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
136 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
137 event
.SetEventObject( win
);
138 win
->GetEventHandler()->ProcessEvent( event
);
141 win
->m_isScrolling
= false;
143 // reset the LINEUP/LINEDOWN flag when the mouse button is released
144 g_currentUpDownEvent
= wxEVT_NULL
;
150 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
154 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
156 wxScrollBar::~wxScrollBar()
160 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
161 const wxPoint
& pos
, const wxSize
& size
,
162 long style
, const wxValidator
& validator
, const wxString
& name
)
165 m_acceptsFocus
= true;
167 if (!PreCreation( parent
, pos
, size
) ||
168 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
170 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
176 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
177 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
179 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
181 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
182 if ( style
& wxSB_VERTICAL
)
184 SetVScrollAdjustment(m_adjust
);
187 g_signal_connect (m_adjust
, "value_changed",
188 G_CALLBACK (gtk_scrollbar_callback
), this);
189 g_signal_connect (m_widget
, "button_press_event",
190 G_CALLBACK (gtk_scrollbar_button_press_callback
),
192 g_signal_connect (m_widget
, "button_release_event",
193 G_CALLBACK (gtk_scrollbar_button_release_callback
),
196 m_parent
->DoAddChild( this );
203 int wxScrollBar::GetThumbPosition() const
205 double val
= m_adjust
->value
;
206 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
209 int wxScrollBar::GetThumbSize() const
211 return (int)(m_adjust
->page_size
+0.5);
214 int wxScrollBar::GetPageSize() const
216 return (int)(m_adjust
->page_increment
+0.5);
219 int wxScrollBar::GetRange() const
221 return (int)(m_adjust
->upper
+0.5);
224 void wxScrollBar::SetThumbPosition( int viewStart
)
226 if (m_isScrolling
) return;
228 float fpos
= (float)viewStart
;
230 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
231 m_adjust
->value
= fpos
;
233 g_signal_handlers_disconnect_by_func (m_adjust
,
234 (gpointer
) gtk_scrollbar_callback
,
237 g_signal_emit_by_name (m_adjust
, "value_changed");
239 g_signal_connect (m_adjust
, "value_changed",
240 G_CALLBACK (gtk_scrollbar_callback
), this);
243 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
244 bool WXUNUSED(refresh
) )
246 float fpos
= (float)position
;
247 float frange
= (float)range
;
248 float fthumb
= (float)thumbSize
;
249 float fpage
= (float)pageSize
;
251 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
252 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
253 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
255 SetThumbPosition( position
);
261 m_adjust
->lower
= 0.0;
262 m_adjust
->upper
= frange
;
263 m_adjust
->value
= fpos
;
264 m_adjust
->step_increment
= 1.0;
265 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
266 m_adjust
->page_size
= fthumb
;
268 g_signal_emit_by_name (m_adjust
, "changed");
271 /* Backward compatibility */
272 int wxScrollBar::GetValue() const
274 return GetThumbPosition();
277 void wxScrollBar::SetValue( int viewStart
)
279 SetThumbPosition( viewStart
);
282 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
284 int pos
= (int)(m_adjust
->value
+0.5);
285 int thumb
= (int)(m_adjust
->page_size
+0.5);
286 int page
= (int)(m_adjust
->page_increment
+0.5);
287 int range
= (int)(m_adjust
->upper
+0.5);
291 *objectLength
= thumb
;
295 int wxScrollBar::GetViewLength() const
297 return (int)(m_adjust
->upper
+0.5);
300 int wxScrollBar::GetObjectLength() const
302 return (int)(m_adjust
->page_size
+0.5);
305 void wxScrollBar::SetPageSize( int pageLength
)
307 int pos
= (int)(m_adjust
->value
+0.5);
308 int thumb
= (int)(m_adjust
->page_size
+0.5);
309 int range
= (int)(m_adjust
->upper
+0.5);
310 SetScrollbar( pos
, thumb
, range
, pageLength
);
313 void wxScrollBar::SetObjectLength( int objectLength
)
315 int pos
= (int)(m_adjust
->value
+0.5);
316 int page
= (int)(m_adjust
->page_increment
+0.5);
317 int range
= (int)(m_adjust
->upper
+0.5);
318 SetScrollbar( pos
, objectLength
, range
, page
);
321 void wxScrollBar::SetViewLength( int viewLength
)
323 int pos
= (int)(m_adjust
->value
+0.5);
324 int thumb
= (int)(m_adjust
->page_size
+0.5);
325 int page
= (int)(m_adjust
->page_increment
+0.5);
326 SetScrollbar( pos
, thumb
, viewLength
, page
);
329 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
331 GtkRange
*range
= GTK_RANGE(m_widget
);
332 return ( (window
== GTK_WIDGET(range
)->window
) );
335 wxSize
wxScrollBar::DoGetBestSize() const
337 return wxControl::DoGetBestSize();
342 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
344 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);