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"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern bool g_blockEventsOnDrag
;
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
30 if (!win
->HasVMT()) return;
31 if (g_blockEventsOnDrag
) return;
33 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
34 if (fabs(diff
) < 0.2) return;
36 wxEventType command
= wxEVT_NULL
;
38 float line_step
= win
->m_adjust
->step_increment
;
39 float page_step
= win
->m_adjust
->page_increment
;
41 if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
42 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
43 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
44 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
45 else command
= wxEVT_SCROLL_THUMBTRACK
;
47 int value
= (int)(win
->m_adjust
->value
+0.5);
49 int orient
= wxHORIZONTAL
;
50 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
52 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
53 event
.SetEventObject( win
);
54 win
->GetEventHandler()->ProcessEvent( event
);
57 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
58 cevent.SetEventObject( win );
59 win->ProcessEvent( cevent );
63 //-----------------------------------------------------------------------------
64 // "button_press_event" from slider
65 //-----------------------------------------------------------------------------
67 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
, GdkEventButton
*gdk_event
, wxScrollBar
*win
)
69 if (gdk_event
->window
!= widget
->slider
) return FALSE
;
71 win
->m_isScrolling
= TRUE
;
76 //-----------------------------------------------------------------------------
77 // "button_release_event" from slider
78 //-----------------------------------------------------------------------------
80 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
, GdkEventButton
*gdk_event
, wxScrollBar
*win
)
82 if (gdk_event
->window
!= widget
->slider
) return FALSE
;
84 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
86 win
->m_isScrolling
= FALSE
;
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
97 wxScrollBar::~wxScrollBar(void)
101 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
102 const wxPoint
& pos
, const wxSize
& size
,
103 long style
, const wxValidator
& validator
, const wxString
& name
)
107 PreCreation( parent
, id
, pos
, size
, style
, name
);
109 SetValidator( validator
);
113 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
114 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
116 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
118 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
120 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
121 (GtkSignalFunc
) gtk_scrollbar_callback
, (gpointer
) this );
123 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_press_event",
124 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
126 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_release_event",
127 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
131 SetBackgroundColour( parent
->GetBackgroundColour() );
138 int wxScrollBar::GetPosition(void) const
140 return (int)(m_adjust
->value
+0.5);
143 int wxScrollBar::GetThumbSize() const
145 return (int)(m_adjust
->page_size
+0.5);
148 int wxScrollBar::GetPageSize() const
150 return (int)(m_adjust
->page_increment
+0.5);
153 int wxScrollBar::GetRange() const
155 return (int)(m_adjust
->upper
+0.5);
158 void wxScrollBar::SetPosition( int viewStart
)
160 float fpos
= (float)viewStart
;
162 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
163 m_adjust
->value
= fpos
;
166 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
169 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
170 bool WXUNUSED(refresh
) )
172 float fpos
= (float)position
;
173 float frange
= (float)range
;
174 float fthumb
= (float)thumbSize
;
175 float fpage
= (float)pageSize
;
177 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
178 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
179 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
181 SetPosition( position
);
187 m_adjust
->lower
= 0.0;
188 m_adjust
->upper
= frange
;
189 m_adjust
->value
= fpos
;
190 m_adjust
->step_increment
= 1.0;
191 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
192 m_adjust
->page_size
= fthumb
;
194 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
197 // Backward compatibility
198 int wxScrollBar::GetValue(void) const
200 return GetPosition();
203 void wxScrollBar::SetValue( int viewStart
)
205 SetPosition( viewStart
);
208 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
210 int pos
= (int)(m_adjust
->value
+0.5);
211 int thumb
= (int)(m_adjust
->page_size
+0.5);
212 int page
= (int)(m_adjust
->page_increment
+0.5);
213 int range
= (int)(m_adjust
->upper
+0.5);
217 *objectLength
= thumb
;
221 int wxScrollBar::GetViewLength() const
223 return (int)(m_adjust
->upper
+0.5);
226 int wxScrollBar::GetObjectLength() const
228 return (int)(m_adjust
->page_size
+0.5);
231 void wxScrollBar::SetPageSize( int pageLength
)
233 int pos
= (int)(m_adjust
->value
+0.5);
234 int thumb
= (int)(m_adjust
->page_size
+0.5);
235 int range
= (int)(m_adjust
->upper
+0.5);
236 SetScrollbar( pos
, thumb
, range
, pageLength
);
239 void wxScrollBar::SetObjectLength( int objectLength
)
241 int pos
= (int)(m_adjust
->value
+0.5);
242 int page
= (int)(m_adjust
->page_increment
+0.5);
243 int range
= (int)(m_adjust
->upper
+0.5);
244 SetScrollbar( pos
, objectLength
, range
, page
);
247 void wxScrollBar::SetViewLength( int viewLength
)
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 SetScrollbar( pos
, thumb
, viewLength
, page
);
255 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
257 GtkRange
*range
= GTK_RANGE(m_widget
);
258 return ( (window
== GTK_WIDGET(range
)->window
) ||
259 (window
== range
->trough
) ||
260 (window
== range
->slider
) ||
261 (window
== range
->step_forw
) ||
262 (window
== range
->step_back
) );
265 void wxScrollBar::ApplyWidgetStyle()
268 gtk_widget_set_style( m_widget
, m_widgetStyle
);