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;
40 win
->m_oldPos
= win
->m_adjust
->value
;
42 wxEventType command
= wxEVT_NULL
;
44 float line_step
= win
->m_adjust
->step_increment
;
45 float page_step
= win
->m_adjust
->page_increment
;
47 if (win
->m_isScrolling
)
49 command
= wxEVT_SCROLL_THUMBTRACK
;
53 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
54 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
55 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
56 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
57 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
58 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
59 else command
= wxEVT_SCROLL_THUMBTRACK
;
62 int value
= (int)(win
->m_adjust
->value
+0.5);
64 int orient
= wxHORIZONTAL
;
65 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
67 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
68 event
.SetEventObject( win
);
69 win
->GetEventHandler()->ProcessEvent( event
);
72 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
73 cevent.SetEventObject( win );
74 win->ProcessEvent( cevent );
78 //-----------------------------------------------------------------------------
79 // "button_press_event" from slider
80 //-----------------------------------------------------------------------------
82 static gint
gtk_scrollbar_button_press_callback( GtkRange
*WXUNUSED(widget
),
83 GdkEventButton
*WXUNUSED(gdk_event
),
86 win
->m_isScrolling
= TRUE
;
87 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
92 //-----------------------------------------------------------------------------
93 // "button_release_event" from slider
94 //-----------------------------------------------------------------------------
96 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
97 GdkEventButton
*WXUNUSED(gdk_event
),
100 win
->m_isScrolling
= FALSE
;
101 // g_blockEventsOnScroll = FALSE;
103 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
112 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
114 wxScrollBar::~wxScrollBar(void)
118 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
119 const wxPoint
& pos
, const wxSize
& size
,
120 long style
, const wxValidator
& validator
, const wxString
& name
)
123 m_acceptsFocus
= TRUE
;
125 PreCreation( parent
, id
, pos
, size
, style
, name
);
127 SetValidator( validator
);
131 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
132 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
134 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
136 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
138 gtk_signal_connect( GTK_OBJECT(m_adjust
),
140 (GtkSignalFunc
) gtk_scrollbar_callback
,
143 gtk_signal_connect( GTK_OBJECT(m_widget
),
144 "button_press_event",
145 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
148 gtk_signal_connect( GTK_OBJECT(m_widget
),
149 "button_release_event",
150 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
153 m_parent
->AddChild( this );
155 (m_parent
->m_insertCallback
)( m_parent
, this );
159 SetBackgroundColour( parent
->GetBackgroundColour() );
166 int wxScrollBar::GetThumbPosition(void) const
168 return (int)(m_adjust
->value
+0.5);
171 int wxScrollBar::GetThumbSize() const
173 return (int)(m_adjust
->page_size
+0.5);
176 int wxScrollBar::GetPageSize() const
178 return (int)(m_adjust
->page_increment
+0.5);
181 int wxScrollBar::GetRange() const
183 return (int)(m_adjust
->upper
+0.5);
186 void wxScrollBar::SetThumbPosition( int viewStart
)
188 if (m_isScrolling
) return;
190 float fpos
= (float)viewStart
;
192 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
193 m_adjust
->value
= fpos
;
195 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
198 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
199 bool WXUNUSED(refresh
) )
201 float fpos
= (float)position
;
202 float frange
= (float)range
;
203 float fthumb
= (float)thumbSize
;
204 float fpage
= (float)pageSize
;
206 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
207 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
208 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
210 SetThumbPosition( position
);
216 m_adjust
->lower
= 0.0;
217 m_adjust
->upper
= frange
;
218 m_adjust
->value
= fpos
;
219 m_adjust
->step_increment
= 1.0;
220 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
221 m_adjust
->page_size
= fthumb
;
223 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
226 /* Backward compatibility */
227 int wxScrollBar::GetValue(void) const
229 return GetThumbPosition();
232 void wxScrollBar::SetValue( int viewStart
)
234 SetThumbPosition( viewStart
);
237 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
239 int pos
= (int)(m_adjust
->value
+0.5);
240 int thumb
= (int)(m_adjust
->page_size
+0.5);
241 int page
= (int)(m_adjust
->page_increment
+0.5);
242 int range
= (int)(m_adjust
->upper
+0.5);
246 *objectLength
= thumb
;
250 int wxScrollBar::GetViewLength() const
252 return (int)(m_adjust
->upper
+0.5);
255 int wxScrollBar::GetObjectLength() const
257 return (int)(m_adjust
->page_size
+0.5);
260 void wxScrollBar::SetPageSize( int pageLength
)
262 int pos
= (int)(m_adjust
->value
+0.5);
263 int thumb
= (int)(m_adjust
->page_size
+0.5);
264 int range
= (int)(m_adjust
->upper
+0.5);
265 SetScrollbar( pos
, thumb
, range
, pageLength
);
268 void wxScrollBar::SetObjectLength( int objectLength
)
270 int pos
= (int)(m_adjust
->value
+0.5);
271 int page
= (int)(m_adjust
->page_increment
+0.5);
272 int range
= (int)(m_adjust
->upper
+0.5);
273 SetScrollbar( pos
, objectLength
, range
, page
);
276 void wxScrollBar::SetViewLength( int viewLength
)
278 int pos
= (int)(m_adjust
->value
+0.5);
279 int thumb
= (int)(m_adjust
->page_size
+0.5);
280 int page
= (int)(m_adjust
->page_increment
+0.5);
281 SetScrollbar( pos
, thumb
, viewLength
, page
);
284 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
286 GtkRange
*range
= GTK_RANGE(m_widget
);
287 return ( (window
== GTK_WIDGET(range
)->window
) ||
288 (window
== range
->trough
) ||
289 (window
== range
->slider
) ||
290 (window
== range
->step_forw
) ||
291 (window
== range
->step_back
) );
294 void wxScrollBar::ApplyWidgetStyle()
297 gtk_widget_set_style( m_widget
, m_widgetStyle
);