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"
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 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
, wxScrollBar
*win
)
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (!win
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 float diff
= adjust
->value
- win
->m_oldPos
;
56 if (fabs(diff
) < sensitivity
) return;
58 win
->m_oldPos
= adjust
->value
;
60 GtkRange
*range
= GTK_RANGE( win
->m_widget
);
62 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
63 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLL_LINEUP
;
64 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLL_LINEDOWN
;
65 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLL_PAGEUP
;
66 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLL_PAGEDOWN
;
68 double dvalue
= adjust
->value
;
69 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
71 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
73 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
74 event
.SetEventObject( win
);
75 win
->GetEventHandler()->ProcessEvent( event
);
78 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
79 cevent.SetEventObject( win );
80 win->ProcessEvent( cevent );
84 //-----------------------------------------------------------------------------
85 // "button_press_event" from slider
86 //-----------------------------------------------------------------------------
88 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
89 GdkEventButton
*gdk_event
,
92 if (g_isIdle
) wxapp_install_idle_handler();
94 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
96 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
101 //-----------------------------------------------------------------------------
102 // "button_release_event" from slider
103 //-----------------------------------------------------------------------------
105 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
106 GdkEventButton
*WXUNUSED(gdk_event
),
109 if (g_isIdle
) wxapp_install_idle_handler();
111 // g_blockEventsOnScroll = FALSE;
113 if (win
->m_isScrolling
)
115 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
116 int value
= (int)ceil(win
->m_adjust
->value
);
117 int dir
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
119 wxScrollEvent
event( command
, value
, dir
);
120 event
.SetEventObject( win
);
121 win
->GetEventHandler()->ProcessEvent( event
);
124 win
->m_isScrolling
= FALSE
;
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
133 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
135 wxScrollBar::~wxScrollBar()
139 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
140 const wxPoint
& pos
, const wxSize
& size
,
141 long style
, const wxValidator
& validator
, const wxString
& name
)
144 m_acceptsFocus
= TRUE
;
146 if (!PreCreation( parent
, pos
, size
) ||
147 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
149 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
155 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
156 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
158 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
160 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
162 gtk_signal_connect( GTK_OBJECT(m_adjust
),
164 (GtkSignalFunc
) gtk_scrollbar_callback
,
167 gtk_signal_connect( GTK_OBJECT(m_widget
),
168 "button_press_event",
169 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
172 gtk_signal_connect( GTK_OBJECT(m_widget
),
173 "button_release_event",
174 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
177 m_parent
->DoAddChild( this );
181 SetBackgroundColour( parent
->GetBackgroundColour() );
188 int wxScrollBar::GetThumbPosition() const
190 double val
= m_adjust
->value
;
191 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
194 int wxScrollBar::GetThumbSize() const
196 return (int)(m_adjust
->page_size
+0.5);
199 int wxScrollBar::GetPageSize() const
201 return (int)(m_adjust
->page_increment
+0.5);
204 int wxScrollBar::GetRange() const
206 return (int)(m_adjust
->upper
+0.5);
209 void wxScrollBar::SetThumbPosition( int viewStart
)
211 if (m_isScrolling
) return;
213 float fpos
= (float)viewStart
;
215 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
216 m_adjust
->value
= fpos
;
218 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
219 (GtkSignalFunc
) gtk_scrollbar_callback
,
222 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
224 gtk_signal_connect( GTK_OBJECT(m_adjust
),
226 (GtkSignalFunc
) gtk_scrollbar_callback
,
230 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
231 bool WXUNUSED(refresh
) )
233 float fpos
= (float)position
;
234 float frange
= (float)range
;
235 float fthumb
= (float)thumbSize
;
236 float fpage
= (float)pageSize
;
238 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
239 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
240 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
242 SetThumbPosition( position
);
248 m_adjust
->lower
= 0.0;
249 m_adjust
->upper
= frange
;
250 m_adjust
->value
= fpos
;
251 m_adjust
->step_increment
= 1.0;
252 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
253 m_adjust
->page_size
= fthumb
;
255 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
258 /* Backward compatibility */
259 int wxScrollBar::GetValue() const
261 return GetThumbPosition();
264 void wxScrollBar::SetValue( int viewStart
)
266 SetThumbPosition( viewStart
);
269 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
271 int pos
= (int)(m_adjust
->value
+0.5);
272 int thumb
= (int)(m_adjust
->page_size
+0.5);
273 int page
= (int)(m_adjust
->page_increment
+0.5);
274 int range
= (int)(m_adjust
->upper
+0.5);
278 *objectLength
= thumb
;
282 int wxScrollBar::GetViewLength() const
284 return (int)(m_adjust
->upper
+0.5);
287 int wxScrollBar::GetObjectLength() const
289 return (int)(m_adjust
->page_size
+0.5);
292 void wxScrollBar::SetPageSize( int pageLength
)
294 int pos
= (int)(m_adjust
->value
+0.5);
295 int thumb
= (int)(m_adjust
->page_size
+0.5);
296 int range
= (int)(m_adjust
->upper
+0.5);
297 SetScrollbar( pos
, thumb
, range
, pageLength
);
300 void wxScrollBar::SetObjectLength( int objectLength
)
302 int pos
= (int)(m_adjust
->value
+0.5);
303 int page
= (int)(m_adjust
->page_increment
+0.5);
304 int range
= (int)(m_adjust
->upper
+0.5);
305 SetScrollbar( pos
, objectLength
, range
, page
);
308 void wxScrollBar::SetViewLength( int viewLength
)
310 int pos
= (int)(m_adjust
->value
+0.5);
311 int thumb
= (int)(m_adjust
->page_size
+0.5);
312 int page
= (int)(m_adjust
->page_increment
+0.5);
313 SetScrollbar( pos
, thumb
, viewLength
, page
);
316 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
318 GtkRange
*range
= GTK_RANGE(m_widget
);
319 return ( (window
== GTK_WIDGET(range
)->window
) ||
320 (window
== range
->trough
) ||
321 (window
== range
->slider
) ||
322 (window
== range
->step_forw
) ||
323 (window
== range
->step_back
) );
326 void wxScrollBar::ApplyWidgetStyle()
329 gtk_widget_set_style( m_widget
, m_widgetStyle
);