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 );
129 m_parent
->AddChild( this );
131 (m_parent
->m_insertCallback
)( m_parent
, this );
135 SetBackgroundColour( parent
->GetBackgroundColour() );
142 int wxScrollBar::GetPosition(void) const
144 return (int)(m_adjust
->value
+0.5);
147 int wxScrollBar::GetThumbSize() const
149 return (int)(m_adjust
->page_size
+0.5);
152 int wxScrollBar::GetPageSize() const
154 return (int)(m_adjust
->page_increment
+0.5);
157 int wxScrollBar::GetRange() const
159 return (int)(m_adjust
->upper
+0.5);
162 void wxScrollBar::SetPosition( int viewStart
)
164 float fpos
= (float)viewStart
;
166 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
167 m_adjust
->value
= fpos
;
170 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
173 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
174 bool WXUNUSED(refresh
) )
176 float fpos
= (float)position
;
177 float frange
= (float)range
;
178 float fthumb
= (float)thumbSize
;
179 float fpage
= (float)pageSize
;
181 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
182 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
183 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
185 SetPosition( position
);
191 m_adjust
->lower
= 0.0;
192 m_adjust
->upper
= frange
;
193 m_adjust
->value
= fpos
;
194 m_adjust
->step_increment
= 1.0;
195 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
196 m_adjust
->page_size
= fthumb
;
198 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
201 // Backward compatibility
202 int wxScrollBar::GetValue(void) const
204 return GetPosition();
207 void wxScrollBar::SetValue( int viewStart
)
209 SetPosition( viewStart
);
212 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
214 int pos
= (int)(m_adjust
->value
+0.5);
215 int thumb
= (int)(m_adjust
->page_size
+0.5);
216 int page
= (int)(m_adjust
->page_increment
+0.5);
217 int range
= (int)(m_adjust
->upper
+0.5);
221 *objectLength
= thumb
;
225 int wxScrollBar::GetViewLength() const
227 return (int)(m_adjust
->upper
+0.5);
230 int wxScrollBar::GetObjectLength() const
232 return (int)(m_adjust
->page_size
+0.5);
235 void wxScrollBar::SetPageSize( int pageLength
)
237 int pos
= (int)(m_adjust
->value
+0.5);
238 int thumb
= (int)(m_adjust
->page_size
+0.5);
239 int range
= (int)(m_adjust
->upper
+0.5);
240 SetScrollbar( pos
, thumb
, range
, pageLength
);
243 void wxScrollBar::SetObjectLength( int objectLength
)
245 int pos
= (int)(m_adjust
->value
+0.5);
246 int page
= (int)(m_adjust
->page_increment
+0.5);
247 int range
= (int)(m_adjust
->upper
+0.5);
248 SetScrollbar( pos
, objectLength
, range
, page
);
251 void wxScrollBar::SetViewLength( int viewLength
)
253 int pos
= (int)(m_adjust
->value
+0.5);
254 int thumb
= (int)(m_adjust
->page_size
+0.5);
255 int page
= (int)(m_adjust
->page_increment
+0.5);
256 SetScrollbar( pos
, thumb
, viewLength
, page
);
259 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
261 GtkRange
*range
= GTK_RANGE(m_widget
);
262 return ( (window
== GTK_WIDGET(range
)->window
) ||
263 (window
== range
->trough
) ||
264 (window
== range
->slider
) ||
265 (window
== range
->step_forw
) ||
266 (window
== range
->step_back
) );
269 void wxScrollBar::ApplyWidgetStyle()
272 gtk_widget_set_style( m_widget
, m_widgetStyle
);