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"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
31 if (!win
->HasVMT()) return;
32 if (g_blockEventsOnDrag
) return;
34 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
35 if (fabs(diff
) < 0.2) return;
37 wxEventType command
= wxEVT_NULL
;
39 float line_step
= win
->m_adjust
->step_increment
;
40 float page_step
= win
->m_adjust
->page_increment
;
42 if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
43 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
44 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
45 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
46 else command
= wxEVT_SCROLL_THUMBTRACK
;
48 int value
= (int)(win
->m_adjust
->value
+0.5);
50 int orient
= wxHORIZONTAL
;
51 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
53 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
54 event
.SetEventObject( win
);
55 win
->GetEventHandler()->ProcessEvent( event
);
58 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
59 cevent.SetEventObject( win );
60 win->ProcessEvent( cevent );
64 //-----------------------------------------------------------------------------
65 // "button_press_event" from slider
66 //-----------------------------------------------------------------------------
68 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
, GdkEventButton
*gdk_event
, wxScrollBar
*win
)
70 if (gdk_event
->window
!= widget
->slider
) return FALSE
;
72 win
->m_isScrolling
= TRUE
;
77 //-----------------------------------------------------------------------------
78 // "button_release_event" from slider
79 //-----------------------------------------------------------------------------
81 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
, GdkEventButton
*gdk_event
, wxScrollBar
*win
)
83 if (gdk_event
->window
!= widget
->slider
) return FALSE
;
85 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
87 win
->m_isScrolling
= FALSE
;
92 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
96 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
98 wxScrollBar::~wxScrollBar(void)
102 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
103 const wxPoint
& pos
, const wxSize
& size
,
104 long style
, const wxValidator
& validator
, const wxString
& name
)
108 PreCreation( parent
, id
, pos
, size
, style
, name
);
110 SetValidator( validator
);
114 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
115 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
117 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
119 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
121 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
122 (GtkSignalFunc
) gtk_scrollbar_callback
, (gpointer
) this );
124 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_press_event",
125 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
127 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_release_event",
128 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
130 m_parent
->AddChild( this );
132 (m_parent
->m_insertCallback
)( m_parent
, this );
136 SetBackgroundColour( parent
->GetBackgroundColour() );
143 int wxScrollBar::GetPosition(void) const
145 return (int)(m_adjust
->value
+0.5);
148 int wxScrollBar::GetThumbSize() const
150 return (int)(m_adjust
->page_size
+0.5);
153 int wxScrollBar::GetPageSize() const
155 return (int)(m_adjust
->page_increment
+0.5);
158 int wxScrollBar::GetRange() const
160 return (int)(m_adjust
->upper
+0.5);
163 void wxScrollBar::SetPosition( int viewStart
)
165 float fpos
= (float)viewStart
;
167 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
168 m_adjust
->value
= fpos
;
171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
174 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
175 bool WXUNUSED(refresh
) )
177 float fpos
= (float)position
;
178 float frange
= (float)range
;
179 float fthumb
= (float)thumbSize
;
180 float fpage
= (float)pageSize
;
182 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
183 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
184 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
186 SetPosition( position
);
192 m_adjust
->lower
= 0.0;
193 m_adjust
->upper
= frange
;
194 m_adjust
->value
= fpos
;
195 m_adjust
->step_increment
= 1.0;
196 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
197 m_adjust
->page_size
= fthumb
;
199 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
202 // Backward compatibility
203 int wxScrollBar::GetValue(void) const
205 return GetPosition();
208 void wxScrollBar::SetValue( int viewStart
)
210 SetPosition( viewStart
);
213 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
215 int pos
= (int)(m_adjust
->value
+0.5);
216 int thumb
= (int)(m_adjust
->page_size
+0.5);
217 int page
= (int)(m_adjust
->page_increment
+0.5);
218 int range
= (int)(m_adjust
->upper
+0.5);
222 *objectLength
= thumb
;
226 int wxScrollBar::GetViewLength() const
228 return (int)(m_adjust
->upper
+0.5);
231 int wxScrollBar::GetObjectLength() const
233 return (int)(m_adjust
->page_size
+0.5);
236 void wxScrollBar::SetPageSize( int pageLength
)
238 int pos
= (int)(m_adjust
->value
+0.5);
239 int thumb
= (int)(m_adjust
->page_size
+0.5);
240 int range
= (int)(m_adjust
->upper
+0.5);
241 SetScrollbar( pos
, thumb
, range
, pageLength
);
244 void wxScrollBar::SetObjectLength( int objectLength
)
246 int pos
= (int)(m_adjust
->value
+0.5);
247 int page
= (int)(m_adjust
->page_increment
+0.5);
248 int range
= (int)(m_adjust
->upper
+0.5);
249 SetScrollbar( pos
, objectLength
, range
, page
);
252 void wxScrollBar::SetViewLength( int viewLength
)
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 SetScrollbar( pos
, thumb
, viewLength
, page
);
260 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
262 GtkRange
*range
= GTK_RANGE(m_widget
);
263 return ( (window
== GTK_WIDGET(range
)->window
) ||
264 (window
== range
->trough
) ||
265 (window
== range
->slider
) ||
266 (window
== range
->step_forw
) ||
267 (window
== range
->step_back
) );
270 void wxScrollBar::ApplyWidgetStyle()
273 gtk_widget_set_style( m_widget
, m_widgetStyle
);