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 void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
34 extern bool g_blockEventsOnScroll
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
42 if (g_isIdle
) wxapp_install_idle_handler();
44 if (!win
->HasVMT()) return;
45 if (g_blockEventsOnDrag
) return;
47 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
48 if (fabs(diff
) < 0.2) return;
49 win
->m_oldPos
= win
->m_adjust
->value
;
51 wxEventType command
= wxEVT_NULL
;
53 float line_step
= win
->m_adjust
->step_increment
;
54 float page_step
= win
->m_adjust
->page_increment
;
56 if (win
->IsScrolling())
58 command
= wxEVT_SCROLL_THUMBTRACK
;
62 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
63 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
64 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
65 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
66 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
67 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
68 else command
= wxEVT_SCROLL_THUMBTRACK
;
71 int value
= (int)(win
->m_adjust
->value
+0.5);
73 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
75 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
76 event
.SetEventObject( win
);
77 win
->GetEventHandler()->ProcessEvent( event
);
80 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
81 cevent.SetEventObject( win );
82 win->ProcessEvent( cevent );
86 //-----------------------------------------------------------------------------
87 // "button_press_event" from slider
88 //-----------------------------------------------------------------------------
90 static gint
gtk_scrollbar_button_press_callback( GtkRange
*WXUNUSED(widget
),
91 GdkEventButton
*WXUNUSED(gdk_event
),
94 if (g_isIdle
) wxapp_install_idle_handler();
96 win
->SetScrolling(TRUE
);
97 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
102 //-----------------------------------------------------------------------------
103 // "button_release_event" from slider
104 //-----------------------------------------------------------------------------
106 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
107 GdkEventButton
*WXUNUSED(gdk_event
),
110 if (g_isIdle
) wxapp_install_idle_handler();
112 win
->SetScrolling(FALSE
);
113 // g_blockEventsOnScroll = FALSE;
115 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
126 wxScrollBar::~wxScrollBar(void)
130 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
131 const wxPoint
& pos
, const wxSize
& size
,
132 long style
, const wxValidator
& validator
, const wxString
& name
)
135 m_acceptsFocus
= TRUE
;
137 PreCreation( parent
, id
, pos
, size
, style
, name
);
139 SetValidator( validator
);
143 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
144 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
146 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
148 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
150 gtk_signal_connect( GTK_OBJECT(m_adjust
),
152 (GtkSignalFunc
) gtk_scrollbar_callback
,
155 gtk_signal_connect( GTK_OBJECT(m_widget
),
156 "button_press_event",
157 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
160 gtk_signal_connect( GTK_OBJECT(m_widget
),
161 "button_release_event",
162 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
165 m_parent
->DoAddChild( this );
169 SetBackgroundColour( parent
->GetBackgroundColour() );
176 int wxScrollBar::GetThumbPosition(void) const
178 return (int)(m_adjust
->value
+0.5);
181 int wxScrollBar::GetThumbSize() const
183 return (int)(m_adjust
->page_size
+0.5);
186 int wxScrollBar::GetPageSize() const
188 return (int)(m_adjust
->page_increment
+0.5);
191 int wxScrollBar::GetRange() const
193 return (int)(m_adjust
->upper
+0.5);
196 void wxScrollBar::SetThumbPosition( int viewStart
)
198 if (m_isScrolling
) return;
200 float fpos
= (float)viewStart
;
202 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
203 m_adjust
->value
= fpos
;
205 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
208 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
209 bool WXUNUSED(refresh
) )
211 float fpos
= (float)position
;
212 float frange
= (float)range
;
213 float fthumb
= (float)thumbSize
;
214 float fpage
= (float)pageSize
;
216 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
217 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
218 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
220 SetThumbPosition( position
);
226 m_adjust
->lower
= 0.0;
227 m_adjust
->upper
= frange
;
228 m_adjust
->value
= fpos
;
229 m_adjust
->step_increment
= 1.0;
230 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
231 m_adjust
->page_size
= fthumb
;
233 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
236 /* Backward compatibility */
237 int wxScrollBar::GetValue(void) const
239 return GetThumbPosition();
242 void wxScrollBar::SetValue( int viewStart
)
244 SetThumbPosition( viewStart
);
247 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
249 int pos
= (int)(m_adjust
->value
+0.5);
250 int thumb
= (int)(m_adjust
->page_size
+0.5);
251 int page
= (int)(m_adjust
->page_increment
+0.5);
252 int range
= (int)(m_adjust
->upper
+0.5);
256 *objectLength
= thumb
;
260 int wxScrollBar::GetViewLength() const
262 return (int)(m_adjust
->upper
+0.5);
265 int wxScrollBar::GetObjectLength() const
267 return (int)(m_adjust
->page_size
+0.5);
270 void wxScrollBar::SetPageSize( int pageLength
)
272 int pos
= (int)(m_adjust
->value
+0.5);
273 int thumb
= (int)(m_adjust
->page_size
+0.5);
274 int range
= (int)(m_adjust
->upper
+0.5);
275 SetScrollbar( pos
, thumb
, range
, pageLength
);
278 void wxScrollBar::SetObjectLength( int objectLength
)
280 int pos
= (int)(m_adjust
->value
+0.5);
281 int page
= (int)(m_adjust
->page_increment
+0.5);
282 int range
= (int)(m_adjust
->upper
+0.5);
283 SetScrollbar( pos
, objectLength
, range
, page
);
286 void wxScrollBar::SetViewLength( int viewLength
)
288 int pos
= (int)(m_adjust
->value
+0.5);
289 int thumb
= (int)(m_adjust
->page_size
+0.5);
290 int page
= (int)(m_adjust
->page_increment
+0.5);
291 SetScrollbar( pos
, thumb
, viewLength
, page
);
294 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
296 GtkRange
*range
= GTK_RANGE(m_widget
);
297 return ( (window
== GTK_WIDGET(range
)->window
) ||
298 (window
== range
->trough
) ||
299 (window
== range
->slider
) ||
300 (window
== range
->step_forw
) ||
301 (window
== range
->step_back
) );
304 void wxScrollBar::ApplyWidgetStyle()
307 gtk_widget_set_style( m_widget
, m_widgetStyle
);