1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "scrolbar.h"
15 #include "wx/scrolbar.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 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
, wxScrollBar
*win
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!win
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 float diff
= adjust
->value
- win
->m_oldPos
;
54 if (fabs(diff
) < sensitivity
) return;
56 win
->m_oldPos
= adjust
->value
;
58 GtkRange
*range
= GTK_RANGE( win
->m_widget
);
60 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
61 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLL_LINEUP
;
62 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLL_LINEDOWN
;
63 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLL_PAGEUP
;
64 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLL_PAGEDOWN
;
66 int value
= (int)ceil(adjust
->value
);
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
*WXUNUSED(widget
),
86 GdkEventButton
*WXUNUSED(gdk_event
),
89 if (g_isIdle
) wxapp_install_idle_handler();
91 win
->m_isScrolling
= TRUE
;
92 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
97 //-----------------------------------------------------------------------------
98 // "button_release_event" from slider
99 //-----------------------------------------------------------------------------
101 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
102 GdkEventButton
*WXUNUSED(gdk_event
),
105 if (g_isIdle
) wxapp_install_idle_handler();
107 wxASSERT( win
->m_isScrolling
);
109 win
->m_isScrolling
= FALSE
;
110 // g_blockEventsOnScroll = FALSE;
112 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
113 int value
= (int)ceil(win
->m_adjust
->value
);
114 int dir
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
116 wxScrollEvent
event( command
, value
, dir
);
117 event
.SetScrolling( FALSE
);
118 event
.SetEventObject( win
);
119 win
->GetEventHandler()->ProcessEvent( event
);
124 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
130 wxScrollBar::~wxScrollBar(void)
134 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
135 const wxPoint
& pos
, const wxSize
& size
,
136 long style
, const wxValidator
& validator
, const wxString
& name
)
139 m_acceptsFocus
= TRUE
;
141 if (!PreCreation( parent
, pos
, size
) ||
142 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
144 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
150 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
151 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
153 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
155 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
157 gtk_signal_connect( GTK_OBJECT(m_adjust
),
159 (GtkSignalFunc
) gtk_scrollbar_callback
,
162 gtk_signal_connect( GTK_OBJECT(m_widget
),
163 "button_press_event",
164 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
167 gtk_signal_connect( GTK_OBJECT(m_widget
),
168 "button_release_event",
169 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
172 m_parent
->DoAddChild( this );
176 SetBackgroundColour( parent
->GetBackgroundColour() );
183 int wxScrollBar::GetThumbPosition(void) const
185 return (int)(m_adjust
->value
+0.5);
188 int wxScrollBar::GetThumbSize() const
190 return (int)(m_adjust
->page_size
+0.5);
193 int wxScrollBar::GetPageSize() const
195 return (int)(m_adjust
->page_increment
+0.5);
198 int wxScrollBar::GetRange() const
200 return (int)(m_adjust
->upper
+0.5);
203 void wxScrollBar::SetThumbPosition( int viewStart
)
205 if (m_isScrolling
) return;
207 float fpos
= (float)viewStart
;
209 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
210 m_adjust
->value
= fpos
;
212 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
215 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
216 bool WXUNUSED(refresh
) )
218 float fpos
= (float)position
;
219 float frange
= (float)range
;
220 float fthumb
= (float)thumbSize
;
221 float fpage
= (float)pageSize
;
223 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
224 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
225 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
227 SetThumbPosition( position
);
233 m_adjust
->lower
= 0.0;
234 m_adjust
->upper
= frange
;
235 m_adjust
->value
= fpos
;
236 m_adjust
->step_increment
= 1.0;
237 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
238 m_adjust
->page_size
= fthumb
;
240 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
243 /* Backward compatibility */
244 int wxScrollBar::GetValue(void) const
246 return GetThumbPosition();
249 void wxScrollBar::SetValue( int viewStart
)
251 SetThumbPosition( viewStart
);
254 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
256 int pos
= (int)(m_adjust
->value
+0.5);
257 int thumb
= (int)(m_adjust
->page_size
+0.5);
258 int page
= (int)(m_adjust
->page_increment
+0.5);
259 int range
= (int)(m_adjust
->upper
+0.5);
263 *objectLength
= thumb
;
267 int wxScrollBar::GetViewLength() const
269 return (int)(m_adjust
->upper
+0.5);
272 int wxScrollBar::GetObjectLength() const
274 return (int)(m_adjust
->page_size
+0.5);
277 void wxScrollBar::SetPageSize( int pageLength
)
279 int pos
= (int)(m_adjust
->value
+0.5);
280 int thumb
= (int)(m_adjust
->page_size
+0.5);
281 int range
= (int)(m_adjust
->upper
+0.5);
282 SetScrollbar( pos
, thumb
, range
, pageLength
);
285 void wxScrollBar::SetObjectLength( int objectLength
)
287 int pos
= (int)(m_adjust
->value
+0.5);
288 int page
= (int)(m_adjust
->page_increment
+0.5);
289 int range
= (int)(m_adjust
->upper
+0.5);
290 SetScrollbar( pos
, objectLength
, range
, page
);
293 void wxScrollBar::SetViewLength( int viewLength
)
295 int pos
= (int)(m_adjust
->value
+0.5);
296 int thumb
= (int)(m_adjust
->page_size
+0.5);
297 int page
= (int)(m_adjust
->page_increment
+0.5);
298 SetScrollbar( pos
, thumb
, viewLength
, page
);
301 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
303 GtkRange
*range
= GTK_RANGE(m_widget
);
304 return ( (window
== GTK_WIDGET(range
)->window
) ||
305 (window
== range
->trough
) ||
306 (window
== range
->slider
) ||
307 (window
== range
->step_forw
) ||
308 (window
== range
->step_back
) );
311 void wxScrollBar::ApplyWidgetStyle()
314 gtk_widget_set_style( m_widget
, m_widgetStyle
);