1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "scrolbar.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
20 #include "wx/scrolbar.h"
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 extern bool g_blockEventsOnScroll
;
39 static wxEventType g_currentUpDownEvent
= wxEVT_NULL
;
41 static const float sensitivity
= 0.02;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 // FIXME: is GtkScrollType really passed to us as 2nd argument?
49 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
,
53 if (g_isIdle
) wxapp_install_idle_handler();
55 if (!win
->m_hasVMT
) return;
56 if (g_blockEventsOnDrag
) return;
58 float diff
= adjust
->value
- win
->m_oldPos
;
59 if (fabs(diff
) < sensitivity
) return;
61 win
->m_oldPos
= adjust
->value
;
63 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
65 double dvalue
= adjust
->value
;
66 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
68 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
70 // throw a LINEUP / LINEDOWN event if necessary
71 if (g_currentUpDownEvent
!= wxEVT_NULL
)
73 wxScrollEvent
event( g_currentUpDownEvent
, win
->GetId(), value
, orient
);
74 event
.SetEventObject( win
);
75 win
->GetEventHandler()->ProcessEvent( event
);
78 // throw other event (wxEVT_SCROLL_THUMBTRACK)
79 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
80 event
.SetEventObject( win
);
81 win
->GetEventHandler()->ProcessEvent( event
);
84 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
85 cevent.SetEventObject( win );
86 win->ProcessEvent( cevent );
90 //-----------------------------------------------------------------------------
91 // "button_press_event" from slider
92 //-----------------------------------------------------------------------------
93 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
94 GdkEventButton
*gdk_event
,
97 if (g_isIdle
) wxapp_install_idle_handler();
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)
103 int scroll_height
, mouse_pos
;
105 // get the mouse position when the click is done
106 if (win
->HasFlag(wxSB_VERTICAL
))
108 scroll_height
= GTK_WIDGET(widget
)->allocation
.height
- 16;
109 mouse_pos
= (int)gdk_event
->y
;
113 scroll_height
= GTK_WIDGET(widget
)->allocation
.width
- 16;
114 mouse_pos
= (int)gdk_event
->x
;
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
;
125 // There is no slider field any more
126 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
132 //-----------------------------------------------------------------------------
133 // "button_release_event" from slider
134 //-----------------------------------------------------------------------------
137 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
138 GdkEventButton
*WXUNUSED(gdk_event
),
142 wxapp_install_idle_handler();
144 if (win
->m_isScrolling
)
146 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
147 int value
= (int)ceil(win
->m_adjust
->value
);
148 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
150 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
151 event
.SetEventObject( win
);
152 win
->GetEventHandler()->ProcessEvent( event
);
155 win
->m_isScrolling
= FALSE
;
157 // reset the LINEUP/LINEDOWN flag when the mouse button is released
158 g_currentUpDownEvent
= wxEVT_NULL
;
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
169 wxScrollBar::~wxScrollBar()
173 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
174 const wxPoint
& pos
, const wxSize
& size
,
175 long style
, const wxValidator
& validator
, const wxString
& name
)
178 m_acceptsFocus
= TRUE
;
180 if (!PreCreation( parent
, pos
, size
) ||
181 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
183 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
189 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
190 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
192 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
194 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
196 gtk_signal_connect( GTK_OBJECT(m_adjust
),
198 (GtkSignalFunc
) gtk_scrollbar_callback
,
200 gtk_signal_connect( GTK_OBJECT(m_widget
),
201 "button_press_event",
202 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
204 gtk_signal_connect( GTK_OBJECT(m_widget
),
205 "button_release_event",
206 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
209 m_parent
->DoAddChild( this );
216 int wxScrollBar::GetThumbPosition() const
218 double val
= m_adjust
->value
;
219 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
222 int wxScrollBar::GetThumbSize() const
224 return (int)(m_adjust
->page_size
+0.5);
227 int wxScrollBar::GetPageSize() const
229 return (int)(m_adjust
->page_increment
+0.5);
232 int wxScrollBar::GetRange() const
234 return (int)(m_adjust
->upper
+0.5);
237 void wxScrollBar::SetThumbPosition( int viewStart
)
239 if (m_isScrolling
) return;
241 float fpos
= (float)viewStart
;
243 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
244 m_adjust
->value
= fpos
;
246 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
247 (GtkSignalFunc
) gtk_scrollbar_callback
,
250 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
252 gtk_signal_connect( GTK_OBJECT(m_adjust
),
254 (GtkSignalFunc
) gtk_scrollbar_callback
,
258 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
259 bool WXUNUSED(refresh
) )
261 float fpos
= (float)position
;
262 float frange
= (float)range
;
263 float fthumb
= (float)thumbSize
;
264 float fpage
= (float)pageSize
;
266 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
267 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
268 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
270 SetThumbPosition( position
);
276 m_adjust
->lower
= 0.0;
277 m_adjust
->upper
= frange
;
278 m_adjust
->value
= fpos
;
279 m_adjust
->step_increment
= 1.0;
280 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
281 m_adjust
->page_size
= fthumb
;
283 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
286 /* Backward compatibility */
287 int wxScrollBar::GetValue() const
289 return GetThumbPosition();
292 void wxScrollBar::SetValue( int viewStart
)
294 SetThumbPosition( viewStart
);
297 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
299 int pos
= (int)(m_adjust
->value
+0.5);
300 int thumb
= (int)(m_adjust
->page_size
+0.5);
301 int page
= (int)(m_adjust
->page_increment
+0.5);
302 int range
= (int)(m_adjust
->upper
+0.5);
306 *objectLength
= thumb
;
310 int wxScrollBar::GetViewLength() const
312 return (int)(m_adjust
->upper
+0.5);
315 int wxScrollBar::GetObjectLength() const
317 return (int)(m_adjust
->page_size
+0.5);
320 void wxScrollBar::SetPageSize( int pageLength
)
322 int pos
= (int)(m_adjust
->value
+0.5);
323 int thumb
= (int)(m_adjust
->page_size
+0.5);
324 int range
= (int)(m_adjust
->upper
+0.5);
325 SetScrollbar( pos
, thumb
, range
, pageLength
);
328 void wxScrollBar::SetObjectLength( int objectLength
)
330 int pos
= (int)(m_adjust
->value
+0.5);
331 int page
= (int)(m_adjust
->page_increment
+0.5);
332 int range
= (int)(m_adjust
->upper
+0.5);
333 SetScrollbar( pos
, objectLength
, range
, page
);
336 void wxScrollBar::SetViewLength( int viewLength
)
338 int pos
= (int)(m_adjust
->value
+0.5);
339 int thumb
= (int)(m_adjust
->page_size
+0.5);
340 int page
= (int)(m_adjust
->page_increment
+0.5);
341 SetScrollbar( pos
, thumb
, viewLength
, page
);
344 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
346 GtkRange
*range
= GTK_RANGE(m_widget
);
347 return ( (window
== GTK_WIDGET(range
)->window
)
349 || (window
== range
->trough
)
350 || (window
== range
->slider
)
351 || (window
== range
->step_forw
)
352 || (window
== range
->step_back
)
357 wxSize
wxScrollBar::DoGetBestSize() const
359 return wxControl::DoGetBestSize();
364 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
366 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);