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"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
27 extern bool g_blockEventsOnScroll
;
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
35 if (!win
->HasVMT()) return;
36 if (g_blockEventsOnDrag
) return;
38 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
39 if (fabs(diff
) < 0.2) return;
41 wxEventType command
= wxEVT_NULL
;
43 float line_step
= win
->m_adjust
->step_increment
;
44 float page_step
= win
->m_adjust
->page_increment
;
46 if (win
->m_isScrolling
)
48 command
= wxEVT_SCROLL_THUMBTRACK
;
52 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
53 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
54 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
55 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
56 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
57 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
58 else command
= wxEVT_SCROLL_THUMBTRACK
;
61 int value
= (int)(win
->m_adjust
->value
+0.5);
63 int orient
= wxHORIZONTAL
;
64 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
66 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
67 event
.SetEventObject( win
);
68 win
->GetEventHandler()->ProcessEvent( event
);
71 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
72 cevent.SetEventObject( win );
73 win->ProcessEvent( cevent );
77 //-----------------------------------------------------------------------------
78 // "button_press_event" from slider
79 //-----------------------------------------------------------------------------
81 static gint
gtk_scrollbar_button_press_callback( GtkRange
*WXUNUSED(widget
),
82 GdkEventButton
*WXUNUSED(gdk_event
),
85 win
->m_isScrolling
= TRUE
;
86 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
91 //-----------------------------------------------------------------------------
92 // "button_release_event" from slider
93 //-----------------------------------------------------------------------------
95 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
96 GdkEventButton
*WXUNUSED(gdk_event
),
99 win
->m_isScrolling
= FALSE
;
100 // g_blockEventsOnScroll = FALSE;
102 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
111 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
113 wxScrollBar::~wxScrollBar(void)
117 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
118 const wxPoint
& pos
, const wxSize
& size
,
119 long style
, const wxValidator
& validator
, const wxString
& name
)
122 m_acceptsFocus
= TRUE
;
124 PreCreation( parent
, id
, pos
, size
, style
, name
);
126 SetValidator( validator
);
130 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
131 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
133 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
135 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
137 gtk_signal_connect( GTK_OBJECT(m_adjust
),
139 (GtkSignalFunc
) gtk_scrollbar_callback
,
142 gtk_signal_connect( GTK_OBJECT(m_widget
),
143 "button_press_event",
144 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
147 gtk_signal_connect( GTK_OBJECT(m_widget
),
148 "button_release_event",
149 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
152 m_parent
->AddChild( this );
154 (m_parent
->m_insertCallback
)( m_parent
, this );
158 SetBackgroundColour( parent
->GetBackgroundColour() );
165 int wxScrollBar::GetThumbPosition(void) const
167 return (int)(m_adjust
->value
+0.5);
170 int wxScrollBar::GetThumbSize() const
172 return (int)(m_adjust
->page_size
+0.5);
175 int wxScrollBar::GetPageSize() const
177 return (int)(m_adjust
->page_increment
+0.5);
180 int wxScrollBar::GetRange() const
182 return (int)(m_adjust
->upper
+0.5);
185 void wxScrollBar::SetThumbPosition( int viewStart
)
187 if (m_isScrolling
) return;
189 float fpos
= (float)viewStart
;
191 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
192 m_adjust
->value
= fpos
;
194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
197 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
198 bool WXUNUSED(refresh
) )
200 float fpos
= (float)position
;
201 float frange
= (float)range
;
202 float fthumb
= (float)thumbSize
;
203 float fpage
= (float)pageSize
;
205 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
206 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
207 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
209 SetThumbPosition( position
);
215 m_adjust
->lower
= 0.0;
216 m_adjust
->upper
= frange
;
217 m_adjust
->value
= fpos
;
218 m_adjust
->step_increment
= 1.0;
219 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
220 m_adjust
->page_size
= fthumb
;
222 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
225 /* Backward compatibility */
226 int wxScrollBar::GetValue(void) const
228 return GetThumbPosition();
231 void wxScrollBar::SetValue( int viewStart
)
233 SetThumbPosition( viewStart
);
236 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
238 int pos
= (int)(m_adjust
->value
+0.5);
239 int thumb
= (int)(m_adjust
->page_size
+0.5);
240 int page
= (int)(m_adjust
->page_increment
+0.5);
241 int range
= (int)(m_adjust
->upper
+0.5);
245 *objectLength
= thumb
;
249 int wxScrollBar::GetViewLength() const
251 return (int)(m_adjust
->upper
+0.5);
254 int wxScrollBar::GetObjectLength() const
256 return (int)(m_adjust
->page_size
+0.5);
259 void wxScrollBar::SetPageSize( int pageLength
)
261 int pos
= (int)(m_adjust
->value
+0.5);
262 int thumb
= (int)(m_adjust
->page_size
+0.5);
263 int range
= (int)(m_adjust
->upper
+0.5);
264 SetScrollbar( pos
, thumb
, range
, pageLength
);
267 void wxScrollBar::SetObjectLength( int objectLength
)
269 int pos
= (int)(m_adjust
->value
+0.5);
270 int page
= (int)(m_adjust
->page_increment
+0.5);
271 int range
= (int)(m_adjust
->upper
+0.5);
272 SetScrollbar( pos
, objectLength
, range
, page
);
275 void wxScrollBar::SetViewLength( int viewLength
)
277 int pos
= (int)(m_adjust
->value
+0.5);
278 int thumb
= (int)(m_adjust
->page_size
+0.5);
279 int page
= (int)(m_adjust
->page_increment
+0.5);
280 SetScrollbar( pos
, thumb
, viewLength
, page
);
283 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
285 GtkRange
*range
= GTK_RANGE(m_widget
);
286 return ( (window
== GTK_WIDGET(range
)->window
) ||
287 (window
== range
->trough
) ||
288 (window
== range
->slider
) ||
289 (window
== range
->step_forw
) ||
290 (window
== range
->step_back
) );
293 void wxScrollBar::ApplyWidgetStyle()
296 gtk_widget_set_style( m_widget
, m_widgetStyle
);