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"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 extern bool g_blockEventsOnScroll
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!win
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
51 if (fabs(diff
) < 0.2) return;
52 win
->m_oldPos
= win
->m_adjust
->value
;
54 wxEventType command
= wxEVT_NULL
;
56 float line_step
= win
->m_adjust
->step_increment
;
57 float page_step
= win
->m_adjust
->page_increment
;
59 if (win
->IsScrolling())
61 command
= wxEVT_SCROLL_THUMBTRACK
;
65 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
66 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
67 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
68 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
69 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
70 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
71 else command
= wxEVT_SCROLL_THUMBTRACK
;
74 int value
= (int)(win
->m_adjust
->value
+0.5);
76 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
78 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
79 event
.SetEventObject( win
);
80 win
->GetEventHandler()->ProcessEvent( event
);
83 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
84 cevent.SetEventObject( win );
85 win->ProcessEvent( cevent );
89 //-----------------------------------------------------------------------------
90 // "button_press_event" from slider
91 //-----------------------------------------------------------------------------
93 static gint
gtk_scrollbar_button_press_callback( GtkRange
*WXUNUSED(widget
),
94 GdkEventButton
*WXUNUSED(gdk_event
),
97 if (g_isIdle
) wxapp_install_idle_handler();
99 win
->SetScrolling(TRUE
);
100 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
105 //-----------------------------------------------------------------------------
106 // "button_release_event" from slider
107 //-----------------------------------------------------------------------------
109 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
110 GdkEventButton
*WXUNUSED(gdk_event
),
113 if (g_isIdle
) wxapp_install_idle_handler();
115 win
->SetScrolling(FALSE
);
116 // g_blockEventsOnScroll = FALSE;
118 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
129 wxScrollBar::~wxScrollBar(void)
133 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
134 const wxPoint
& pos
, const wxSize
& size
,
135 long style
, const wxValidator
& validator
, const wxString
& name
)
138 m_acceptsFocus
= TRUE
;
140 PreCreation( parent
, id
, pos
, size
, style
, name
);
143 SetValidator( validator
);
148 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
149 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
151 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
153 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
155 gtk_signal_connect( GTK_OBJECT(m_adjust
),
157 (GtkSignalFunc
) gtk_scrollbar_callback
,
160 gtk_signal_connect( GTK_OBJECT(m_widget
),
161 "button_press_event",
162 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
165 gtk_signal_connect( GTK_OBJECT(m_widget
),
166 "button_release_event",
167 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
170 m_parent
->DoAddChild( this );
174 SetBackgroundColour( parent
->GetBackgroundColour() );
181 int wxScrollBar::GetThumbPosition(void) const
183 return (int)(m_adjust
->value
+0.5);
186 int wxScrollBar::GetThumbSize() const
188 return (int)(m_adjust
->page_size
+0.5);
191 int wxScrollBar::GetPageSize() const
193 return (int)(m_adjust
->page_increment
+0.5);
196 int wxScrollBar::GetRange() const
198 return (int)(m_adjust
->upper
+0.5);
201 void wxScrollBar::SetThumbPosition( int viewStart
)
203 if (m_isScrolling
) return;
205 float fpos
= (float)viewStart
;
207 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
208 m_adjust
->value
= fpos
;
210 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
213 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
214 bool WXUNUSED(refresh
) )
216 float fpos
= (float)position
;
217 float frange
= (float)range
;
218 float fthumb
= (float)thumbSize
;
219 float fpage
= (float)pageSize
;
221 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
222 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
223 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
225 SetThumbPosition( position
);
231 m_adjust
->lower
= 0.0;
232 m_adjust
->upper
= frange
;
233 m_adjust
->value
= fpos
;
234 m_adjust
->step_increment
= 1.0;
235 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
236 m_adjust
->page_size
= fthumb
;
238 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
241 /* Backward compatibility */
242 int wxScrollBar::GetValue(void) const
244 return GetThumbPosition();
247 void wxScrollBar::SetValue( int viewStart
)
249 SetThumbPosition( viewStart
);
252 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
254 int pos
= (int)(m_adjust
->value
+0.5);
255 int thumb
= (int)(m_adjust
->page_size
+0.5);
256 int page
= (int)(m_adjust
->page_increment
+0.5);
257 int range
= (int)(m_adjust
->upper
+0.5);
261 *objectLength
= thumb
;
265 int wxScrollBar::GetViewLength() const
267 return (int)(m_adjust
->upper
+0.5);
270 int wxScrollBar::GetObjectLength() const
272 return (int)(m_adjust
->page_size
+0.5);
275 void wxScrollBar::SetPageSize( int pageLength
)
277 int pos
= (int)(m_adjust
->value
+0.5);
278 int thumb
= (int)(m_adjust
->page_size
+0.5);
279 int range
= (int)(m_adjust
->upper
+0.5);
280 SetScrollbar( pos
, thumb
, range
, pageLength
);
283 void wxScrollBar::SetObjectLength( int objectLength
)
285 int pos
= (int)(m_adjust
->value
+0.5);
286 int page
= (int)(m_adjust
->page_increment
+0.5);
287 int range
= (int)(m_adjust
->upper
+0.5);
288 SetScrollbar( pos
, objectLength
, range
, page
);
291 void wxScrollBar::SetViewLength( int viewLength
)
293 int pos
= (int)(m_adjust
->value
+0.5);
294 int thumb
= (int)(m_adjust
->page_size
+0.5);
295 int page
= (int)(m_adjust
->page_increment
+0.5);
296 SetScrollbar( pos
, thumb
, viewLength
, page
);
299 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
301 GtkRange
*range
= GTK_RANGE(m_widget
);
302 return ( (window
== GTK_WIDGET(range
)->window
) ||
303 (window
== range
->trough
) ||
304 (window
== range
->slider
) ||
305 (window
== range
->step_forw
) ||
306 (window
== range
->step_back
) );
309 void wxScrollBar::ApplyWidgetStyle()
312 gtk_widget_set_style( m_widget
, m_widgetStyle
);