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
;
40 static const float sensitivity
= 0.02;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 // FIXME: is GtkScrollType really passed to us as 2nd argument?
48 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
,
52 if (g_isIdle
) wxapp_install_idle_handler();
54 if (!win
->m_hasVMT
) return;
55 if (g_blockEventsOnDrag
) return;
57 float diff
= adjust
->value
- win
->m_oldPos
;
58 if (fabs(diff
) < sensitivity
) return;
60 win
->m_oldPos
= adjust
->value
;
62 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
64 double dvalue
= adjust
->value
;
65 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
67 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
69 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
70 event
.SetEventObject( win
);
71 win
->GetEventHandler()->ProcessEvent( event
);
74 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
75 cevent.SetEventObject( win );
76 win->ProcessEvent( cevent );
80 //-----------------------------------------------------------------------------
81 // "button_press_event" from slider
82 //-----------------------------------------------------------------------------
84 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
85 GdkEventButton
*gdk_event
,
88 if (g_isIdle
) wxapp_install_idle_handler();
90 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
92 // FIXME: there is no slider field any more, what was meant here?
94 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
100 //-----------------------------------------------------------------------------
101 // "button_release_event" from slider
102 //-----------------------------------------------------------------------------
105 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
106 GdkEventButton
*WXUNUSED(gdk_event
),
110 wxapp_install_idle_handler();
112 // g_blockEventsOnScroll = FALSE;
114 if (win
->m_isScrolling
)
116 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
117 int value
= (int)ceil(win
->m_adjust
->value
);
118 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
120 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
121 event
.SetEventObject( win
);
122 win
->GetEventHandler()->ProcessEvent( event
);
125 win
->m_isScrolling
= FALSE
;
130 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
134 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
136 wxScrollBar::~wxScrollBar()
140 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
141 const wxPoint
& pos
, const wxSize
& size
,
142 long style
, const wxValidator
& validator
, const wxString
& name
)
145 m_acceptsFocus
= TRUE
;
147 if (!PreCreation( parent
, pos
, size
) ||
148 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
150 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
156 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
157 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
159 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
161 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
163 gtk_signal_connect( GTK_OBJECT(m_adjust
),
165 (GtkSignalFunc
) gtk_scrollbar_callback
,
168 gtk_signal_connect( GTK_OBJECT(m_widget
),
169 "button_press_event",
170 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
173 gtk_signal_connect( GTK_OBJECT(m_widget
),
174 "button_release_event",
175 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
178 m_parent
->DoAddChild( this );
185 int wxScrollBar::GetThumbPosition() const
187 double val
= m_adjust
->value
;
188 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
191 int wxScrollBar::GetThumbSize() const
193 return (int)(m_adjust
->page_size
+0.5);
196 int wxScrollBar::GetPageSize() const
198 return (int)(m_adjust
->page_increment
+0.5);
201 int wxScrollBar::GetRange() const
203 return (int)(m_adjust
->upper
+0.5);
206 void wxScrollBar::SetThumbPosition( int viewStart
)
208 if (m_isScrolling
) return;
210 float fpos
= (float)viewStart
;
212 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
213 m_adjust
->value
= fpos
;
215 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
216 (GtkSignalFunc
) gtk_scrollbar_callback
,
219 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
221 gtk_signal_connect( GTK_OBJECT(m_adjust
),
223 (GtkSignalFunc
) gtk_scrollbar_callback
,
227 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
228 bool WXUNUSED(refresh
) )
230 float fpos
= (float)position
;
231 float frange
= (float)range
;
232 float fthumb
= (float)thumbSize
;
233 float fpage
= (float)pageSize
;
235 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
236 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
237 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
239 SetThumbPosition( position
);
245 m_adjust
->lower
= 0.0;
246 m_adjust
->upper
= frange
;
247 m_adjust
->value
= fpos
;
248 m_adjust
->step_increment
= 1.0;
249 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
250 m_adjust
->page_size
= fthumb
;
252 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
255 /* Backward compatibility */
256 int wxScrollBar::GetValue() const
258 return GetThumbPosition();
261 void wxScrollBar::SetValue( int viewStart
)
263 SetThumbPosition( viewStart
);
266 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
268 int pos
= (int)(m_adjust
->value
+0.5);
269 int thumb
= (int)(m_adjust
->page_size
+0.5);
270 int page
= (int)(m_adjust
->page_increment
+0.5);
271 int range
= (int)(m_adjust
->upper
+0.5);
275 *objectLength
= thumb
;
279 int wxScrollBar::GetViewLength() const
281 return (int)(m_adjust
->upper
+0.5);
284 int wxScrollBar::GetObjectLength() const
286 return (int)(m_adjust
->page_size
+0.5);
289 void wxScrollBar::SetPageSize( int pageLength
)
291 int pos
= (int)(m_adjust
->value
+0.5);
292 int thumb
= (int)(m_adjust
->page_size
+0.5);
293 int range
= (int)(m_adjust
->upper
+0.5);
294 SetScrollbar( pos
, thumb
, range
, pageLength
);
297 void wxScrollBar::SetObjectLength( int objectLength
)
299 int pos
= (int)(m_adjust
->value
+0.5);
300 int page
= (int)(m_adjust
->page_increment
+0.5);
301 int range
= (int)(m_adjust
->upper
+0.5);
302 SetScrollbar( pos
, objectLength
, range
, page
);
305 void wxScrollBar::SetViewLength( int viewLength
)
307 int pos
= (int)(m_adjust
->value
+0.5);
308 int thumb
= (int)(m_adjust
->page_size
+0.5);
309 int page
= (int)(m_adjust
->page_increment
+0.5);
310 SetScrollbar( pos
, thumb
, viewLength
, page
);
313 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
315 GtkRange
*range
= GTK_RANGE(m_widget
);
316 return ( (window
== GTK_WIDGET(range
)->window
)
318 || (window
== range
->trough
)
319 || (window
== range
->slider
)
320 || (window
== range
->step_forw
)
321 || (window
== range
->step_back
)
326 wxSize
wxScrollBar::DoGetBestSize() const
328 return wxControl::DoGetBestSize();
333 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
335 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);