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
), "value_changed",
138 (GtkSignalFunc
) gtk_scrollbar_callback
, (gpointer
) this );
140 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_press_event",
141 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
143 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_release_event",
144 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
146 m_parent
->AddChild( this );
148 (m_parent
->m_insertCallback
)( m_parent
, this );
152 SetBackgroundColour( parent
->GetBackgroundColour() );
159 int wxScrollBar::GetThumbPosition(void) const
161 return (int)(m_adjust
->value
+0.5);
164 int wxScrollBar::GetThumbSize() const
166 return (int)(m_adjust
->page_size
+0.5);
169 int wxScrollBar::GetPageSize() const
171 return (int)(m_adjust
->page_increment
+0.5);
174 int wxScrollBar::GetRange() const
176 return (int)(m_adjust
->upper
+0.5);
179 void wxScrollBar::SetThumbPosition( int viewStart
)
181 if (m_isScrolling
) return;
183 float fpos
= (float)viewStart
;
185 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
186 m_adjust
->value
= fpos
;
188 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
191 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
192 bool WXUNUSED(refresh
) )
194 float fpos
= (float)position
;
195 float frange
= (float)range
;
196 float fthumb
= (float)thumbSize
;
197 float fpage
= (float)pageSize
;
199 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
200 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
201 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
203 SetThumbPosition( position
);
209 m_adjust
->lower
= 0.0;
210 m_adjust
->upper
= frange
;
211 m_adjust
->value
= fpos
;
212 m_adjust
->step_increment
= 1.0;
213 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
214 m_adjust
->page_size
= fthumb
;
216 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
219 // Backward compatibility
220 int wxScrollBar::GetValue(void) const
222 return GetThumbPosition();
225 void wxScrollBar::SetValue( int viewStart
)
227 SetThumbPosition( viewStart
);
230 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
232 int pos
= (int)(m_adjust
->value
+0.5);
233 int thumb
= (int)(m_adjust
->page_size
+0.5);
234 int page
= (int)(m_adjust
->page_increment
+0.5);
235 int range
= (int)(m_adjust
->upper
+0.5);
239 *objectLength
= thumb
;
243 int wxScrollBar::GetViewLength() const
245 return (int)(m_adjust
->upper
+0.5);
248 int wxScrollBar::GetObjectLength() const
250 return (int)(m_adjust
->page_size
+0.5);
253 void wxScrollBar::SetPageSize( int pageLength
)
255 int pos
= (int)(m_adjust
->value
+0.5);
256 int thumb
= (int)(m_adjust
->page_size
+0.5);
257 int range
= (int)(m_adjust
->upper
+0.5);
258 SetScrollbar( pos
, thumb
, range
, pageLength
);
261 void wxScrollBar::SetObjectLength( int objectLength
)
263 int pos
= (int)(m_adjust
->value
+0.5);
264 int page
= (int)(m_adjust
->page_increment
+0.5);
265 int range
= (int)(m_adjust
->upper
+0.5);
266 SetScrollbar( pos
, objectLength
, range
, page
);
269 void wxScrollBar::SetViewLength( int viewLength
)
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 SetScrollbar( pos
, thumb
, viewLength
, page
);
277 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
279 GtkRange
*range
= GTK_RANGE(m_widget
);
280 return ( (window
== GTK_WIDGET(range
)->window
) ||
281 (window
== range
->trough
) ||
282 (window
== range
->slider
) ||
283 (window
== range
->step_forw
) ||
284 (window
== range
->step_back
) );
287 void wxScrollBar::ApplyWidgetStyle()
290 gtk_widget_set_style( m_widget
, m_widgetStyle
);