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"
26 #include "wx/gtk/private.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
40 extern bool g_blockEventsOnScroll
;
42 static const float sensitivity
= 0.02;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 // FIXME: is GtkScrollType really passed to us as 2nd argument?
50 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
,
54 if (g_isIdle
) wxapp_install_idle_handler();
56 if (!win
->m_hasVMT
) return;
57 if (g_blockEventsOnDrag
) return;
59 float diff
= adjust
->value
- win
->m_oldPos
;
60 if (fabs(diff
) < sensitivity
) return;
62 win
->m_oldPos
= adjust
->value
;
64 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
66 double dvalue
= adjust
->value
;
67 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
69 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
71 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
72 event
.SetEventObject( win
);
73 win
->GetEventHandler()->ProcessEvent( event
);
76 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
77 cevent.SetEventObject( win );
78 win->ProcessEvent( cevent );
82 //-----------------------------------------------------------------------------
83 // "button_press_event" from slider
84 //-----------------------------------------------------------------------------
86 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
87 GdkEventButton
*gdk_event
,
90 if (g_isIdle
) wxapp_install_idle_handler();
92 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
94 // FIXME: there is no slider field any more, what was meant here?
96 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
102 //-----------------------------------------------------------------------------
103 // "button_release_event" from slider
104 //-----------------------------------------------------------------------------
107 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
108 GdkEventButton
*WXUNUSED(gdk_event
),
112 wxapp_install_idle_handler();
114 // g_blockEventsOnScroll = FALSE;
116 if (win
->m_isScrolling
)
118 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
119 int value
= (int)ceil(win
->m_adjust
->value
);
120 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
122 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
123 event
.SetEventObject( win
);
124 win
->GetEventHandler()->ProcessEvent( event
);
127 win
->m_isScrolling
= FALSE
;
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
138 wxScrollBar::~wxScrollBar()
142 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
143 const wxPoint
& pos
, const wxSize
& size
,
144 long style
, const wxValidator
& validator
, const wxString
& name
)
147 m_acceptsFocus
= TRUE
;
149 if (!PreCreation( parent
, pos
, size
) ||
150 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
152 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
158 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
159 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
161 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
163 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
165 gtk_signal_connect( GTK_OBJECT(m_adjust
),
167 (GtkSignalFunc
) gtk_scrollbar_callback
,
170 gtk_signal_connect( GTK_OBJECT(m_widget
),
171 "button_press_event",
172 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
175 gtk_signal_connect( GTK_OBJECT(m_widget
),
176 "button_release_event",
177 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
180 m_parent
->DoAddChild( this );
186 SetBackgroundColour( parent
->GetBackgroundColour() );
193 int wxScrollBar::GetThumbPosition() const
195 double val
= m_adjust
->value
;
196 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
199 int wxScrollBar::GetThumbSize() const
201 return (int)(m_adjust
->page_size
+0.5);
204 int wxScrollBar::GetPageSize() const
206 return (int)(m_adjust
->page_increment
+0.5);
209 int wxScrollBar::GetRange() const
211 return (int)(m_adjust
->upper
+0.5);
214 void wxScrollBar::SetThumbPosition( int viewStart
)
216 if (m_isScrolling
) return;
218 float fpos
= (float)viewStart
;
220 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
221 m_adjust
->value
= fpos
;
223 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
224 (GtkSignalFunc
) gtk_scrollbar_callback
,
227 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
229 gtk_signal_connect( GTK_OBJECT(m_adjust
),
231 (GtkSignalFunc
) gtk_scrollbar_callback
,
235 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
236 bool WXUNUSED(refresh
) )
238 float fpos
= (float)position
;
239 float frange
= (float)range
;
240 float fthumb
= (float)thumbSize
;
241 float fpage
= (float)pageSize
;
243 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
244 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
245 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
247 SetThumbPosition( position
);
253 m_adjust
->lower
= 0.0;
254 m_adjust
->upper
= frange
;
255 m_adjust
->value
= fpos
;
256 m_adjust
->step_increment
= 1.0;
257 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
258 m_adjust
->page_size
= fthumb
;
260 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
263 /* Backward compatibility */
264 int wxScrollBar::GetValue() const
266 return GetThumbPosition();
269 void wxScrollBar::SetValue( int viewStart
)
271 SetThumbPosition( viewStart
);
274 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
276 int pos
= (int)(m_adjust
->value
+0.5);
277 int thumb
= (int)(m_adjust
->page_size
+0.5);
278 int page
= (int)(m_adjust
->page_increment
+0.5);
279 int range
= (int)(m_adjust
->upper
+0.5);
283 *objectLength
= thumb
;
287 int wxScrollBar::GetViewLength() const
289 return (int)(m_adjust
->upper
+0.5);
292 int wxScrollBar::GetObjectLength() const
294 return (int)(m_adjust
->page_size
+0.5);
297 void wxScrollBar::SetPageSize( int pageLength
)
299 int pos
= (int)(m_adjust
->value
+0.5);
300 int thumb
= (int)(m_adjust
->page_size
+0.5);
301 int range
= (int)(m_adjust
->upper
+0.5);
302 SetScrollbar( pos
, thumb
, range
, pageLength
);
305 void wxScrollBar::SetObjectLength( int objectLength
)
307 int pos
= (int)(m_adjust
->value
+0.5);
308 int page
= (int)(m_adjust
->page_increment
+0.5);
309 int range
= (int)(m_adjust
->upper
+0.5);
310 SetScrollbar( pos
, objectLength
, range
, page
);
313 void wxScrollBar::SetViewLength( int viewLength
)
315 int pos
= (int)(m_adjust
->value
+0.5);
316 int thumb
= (int)(m_adjust
->page_size
+0.5);
317 int page
= (int)(m_adjust
->page_increment
+0.5);
318 SetScrollbar( pos
, thumb
, viewLength
, page
);
321 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
323 GtkRange
*range
= GTK_RANGE(m_widget
);
324 return ( (window
== GTK_WIDGET(range
)->window
)
326 || (window
== range
->trough
)
327 || (window
== range
->slider
)
328 || (window
== range
->step_forw
)
329 || (window
== range
->step_back
)
334 void wxScrollBar::ApplyWidgetStyle()
337 gtk_widget_set_style( m_widget
, m_widgetStyle
);
340 wxSize
wxScrollBar::DoGetBestSize() const
342 return wxControl::DoGetBestSize();