1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "scrolbar.h"
19 #include "wx/scrolbar.h"
25 #include "wx/gtk/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
39 extern bool g_blockEventsOnScroll
;
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 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
71 event
.SetEventObject( win
);
72 win
->GetEventHandler()->ProcessEvent( event
);
75 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
76 cevent.SetEventObject( win );
77 win->ProcessEvent( cevent );
81 //-----------------------------------------------------------------------------
82 // "button_press_event" from slider
83 //-----------------------------------------------------------------------------
85 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
86 GdkEventButton
*gdk_event
,
89 if (g_isIdle
) wxapp_install_idle_handler();
91 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
93 // FIXME: there is no slider field any more, what was meant here?
95 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
101 //-----------------------------------------------------------------------------
102 // "button_release_event" from slider
103 //-----------------------------------------------------------------------------
106 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
107 GdkEventButton
*WXUNUSED(gdk_event
),
111 wxapp_install_idle_handler();
113 // g_blockEventsOnScroll = FALSE;
115 if (win
->m_isScrolling
)
117 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
118 int value
= (int)ceil(win
->m_adjust
->value
);
119 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
121 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
122 event
.SetEventObject( win
);
123 win
->GetEventHandler()->ProcessEvent( event
);
126 win
->m_isScrolling
= FALSE
;
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
137 wxScrollBar::~wxScrollBar()
141 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
142 const wxPoint
& pos
, const wxSize
& size
,
143 long style
, const wxValidator
& validator
, const wxString
& name
)
146 m_acceptsFocus
= TRUE
;
148 if (!PreCreation( parent
, pos
, size
) ||
149 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
151 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
157 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
158 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
160 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
162 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
164 gtk_signal_connect( GTK_OBJECT(m_adjust
),
166 (GtkSignalFunc
) gtk_scrollbar_callback
,
169 gtk_signal_connect( GTK_OBJECT(m_widget
),
170 "button_press_event",
171 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
174 gtk_signal_connect( GTK_OBJECT(m_widget
),
175 "button_release_event",
176 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
179 m_parent
->DoAddChild( this );
183 SetBackgroundColour( parent
->GetBackgroundColour() );
190 int wxScrollBar::GetThumbPosition() const
192 double val
= m_adjust
->value
;
193 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
196 int wxScrollBar::GetThumbSize() const
198 return (int)(m_adjust
->page_size
+0.5);
201 int wxScrollBar::GetPageSize() const
203 return (int)(m_adjust
->page_increment
+0.5);
206 int wxScrollBar::GetRange() const
208 return (int)(m_adjust
->upper
+0.5);
211 void wxScrollBar::SetThumbPosition( int viewStart
)
213 if (m_isScrolling
) return;
215 float fpos
= (float)viewStart
;
217 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
218 m_adjust
->value
= fpos
;
220 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
221 (GtkSignalFunc
) gtk_scrollbar_callback
,
224 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
226 gtk_signal_connect( GTK_OBJECT(m_adjust
),
228 (GtkSignalFunc
) gtk_scrollbar_callback
,
232 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
233 bool WXUNUSED(refresh
) )
235 float fpos
= (float)position
;
236 float frange
= (float)range
;
237 float fthumb
= (float)thumbSize
;
238 float fpage
= (float)pageSize
;
240 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
241 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
242 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
244 SetThumbPosition( position
);
250 m_adjust
->lower
= 0.0;
251 m_adjust
->upper
= frange
;
252 m_adjust
->value
= fpos
;
253 m_adjust
->step_increment
= 1.0;
254 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
255 m_adjust
->page_size
= fthumb
;
257 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
260 /* Backward compatibility */
261 int wxScrollBar::GetValue() const
263 return GetThumbPosition();
266 void wxScrollBar::SetValue( int viewStart
)
268 SetThumbPosition( viewStart
);
271 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
273 int pos
= (int)(m_adjust
->value
+0.5);
274 int thumb
= (int)(m_adjust
->page_size
+0.5);
275 int page
= (int)(m_adjust
->page_increment
+0.5);
276 int range
= (int)(m_adjust
->upper
+0.5);
280 *objectLength
= thumb
;
284 int wxScrollBar::GetViewLength() const
286 return (int)(m_adjust
->upper
+0.5);
289 int wxScrollBar::GetObjectLength() const
291 return (int)(m_adjust
->page_size
+0.5);
294 void wxScrollBar::SetPageSize( int pageLength
)
296 int pos
= (int)(m_adjust
->value
+0.5);
297 int thumb
= (int)(m_adjust
->page_size
+0.5);
298 int range
= (int)(m_adjust
->upper
+0.5);
299 SetScrollbar( pos
, thumb
, range
, pageLength
);
302 void wxScrollBar::SetObjectLength( int objectLength
)
304 int pos
= (int)(m_adjust
->value
+0.5);
305 int page
= (int)(m_adjust
->page_increment
+0.5);
306 int range
= (int)(m_adjust
->upper
+0.5);
307 SetScrollbar( pos
, objectLength
, range
, page
);
310 void wxScrollBar::SetViewLength( int viewLength
)
312 int pos
= (int)(m_adjust
->value
+0.5);
313 int thumb
= (int)(m_adjust
->page_size
+0.5);
314 int page
= (int)(m_adjust
->page_increment
+0.5);
315 SetScrollbar( pos
, thumb
, viewLength
, page
);
318 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
320 GtkRange
*range
= GTK_RANGE(m_widget
);
321 return ( (window
== GTK_WIDGET(range
)->window
)
323 || (window
== range
->trough
)
324 || (window
== range
->slider
)
325 || (window
== range
->step_forw
)
326 || (window
== range
->step_back
)
331 void wxScrollBar::ApplyWidgetStyle()
334 gtk_widget_set_style( m_widget
, m_widgetStyle
);