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(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
43 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
44 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
45 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
46 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
47 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
48 else command
= wxEVT_SCROLL_THUMBTRACK
;
50 int value
= (int)(win
->m_adjust
->value
+0.5);
52 int orient
= wxHORIZONTAL
;
53 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
55 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
56 event
.SetEventObject( win
);
57 win
->GetEventHandler()->ProcessEvent( event
);
60 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
61 cevent.SetEventObject( win );
62 win->ProcessEvent( cevent );
66 //-----------------------------------------------------------------------------
67 // "button_press_event" from slider
68 //-----------------------------------------------------------------------------
70 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
, GdkEventButton
*gdk_event
, wxScrollBar
*win
)
72 if (gdk_event
->window
!= widget
->slider
) return FALSE
;
74 win
->m_isScrolling
= TRUE
;
79 //-----------------------------------------------------------------------------
80 // "button_release_event" from slider
81 //-----------------------------------------------------------------------------
83 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
, GdkEventButton
*gdk_event
, wxScrollBar
*win
)
85 if (gdk_event
->window
!= widget
->slider
) return FALSE
;
87 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
89 win
->m_isScrolling
= FALSE
;
94 //-----------------------------------------------------------------------------
96 //-----------------------------------------------------------------------------
98 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
100 wxScrollBar::~wxScrollBar(void)
104 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
105 const wxPoint
& pos
, const wxSize
& size
,
106 long style
, const wxValidator
& validator
, const wxString
& name
)
110 PreCreation( parent
, id
, pos
, size
, style
, name
);
112 SetValidator( validator
);
116 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
117 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
119 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
121 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
123 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
124 (GtkSignalFunc
) gtk_scrollbar_callback
, (gpointer
) this );
126 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_press_event",
127 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
129 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_release_event",
130 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
132 m_parent
->AddChild( this );
134 (m_parent
->m_insertCallback
)( m_parent
, this );
138 SetBackgroundColour( parent
->GetBackgroundColour() );
145 int wxScrollBar::GetPosition(void) const
147 return (int)(m_adjust
->value
+0.5);
150 int wxScrollBar::GetThumbSize() const
152 return (int)(m_adjust
->page_size
+0.5);
155 int wxScrollBar::GetPageSize() const
157 return (int)(m_adjust
->page_increment
+0.5);
160 int wxScrollBar::GetRange() const
162 return (int)(m_adjust
->upper
+0.5);
165 void wxScrollBar::SetPosition( int viewStart
)
167 float fpos
= (float)viewStart
;
169 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
170 m_adjust
->value
= fpos
;
173 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
176 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
177 bool WXUNUSED(refresh
) )
179 float fpos
= (float)position
;
180 float frange
= (float)range
;
181 float fthumb
= (float)thumbSize
;
182 float fpage
= (float)pageSize
;
184 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
185 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
186 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
188 SetPosition( position
);
194 m_adjust
->lower
= 0.0;
195 m_adjust
->upper
= frange
;
196 m_adjust
->value
= fpos
;
197 m_adjust
->step_increment
= 1.0;
198 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
199 m_adjust
->page_size
= fthumb
;
201 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
204 // Backward compatibility
205 int wxScrollBar::GetValue(void) const
207 return GetPosition();
210 void wxScrollBar::SetValue( int viewStart
)
212 SetPosition( viewStart
);
215 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
217 int pos
= (int)(m_adjust
->value
+0.5);
218 int thumb
= (int)(m_adjust
->page_size
+0.5);
219 int page
= (int)(m_adjust
->page_increment
+0.5);
220 int range
= (int)(m_adjust
->upper
+0.5);
224 *objectLength
= thumb
;
228 int wxScrollBar::GetViewLength() const
230 return (int)(m_adjust
->upper
+0.5);
233 int wxScrollBar::GetObjectLength() const
235 return (int)(m_adjust
->page_size
+0.5);
238 void wxScrollBar::SetPageSize( int pageLength
)
240 int pos
= (int)(m_adjust
->value
+0.5);
241 int thumb
= (int)(m_adjust
->page_size
+0.5);
242 int range
= (int)(m_adjust
->upper
+0.5);
243 SetScrollbar( pos
, thumb
, range
, pageLength
);
246 void wxScrollBar::SetObjectLength( int objectLength
)
248 int pos
= (int)(m_adjust
->value
+0.5);
249 int page
= (int)(m_adjust
->page_increment
+0.5);
250 int range
= (int)(m_adjust
->upper
+0.5);
251 SetScrollbar( pos
, objectLength
, range
, page
);
254 void wxScrollBar::SetViewLength( int viewLength
)
256 int pos
= (int)(m_adjust
->value
+0.5);
257 int thumb
= (int)(m_adjust
->page_size
+0.5);
258 int page
= (int)(m_adjust
->page_increment
+0.5);
259 SetScrollbar( pos
, thumb
, viewLength
, page
);
262 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
264 GtkRange
*range
= GTK_RANGE(m_widget
);
265 return ( (window
== GTK_WIDGET(range
)->window
) ||
266 (window
== range
->trough
) ||
267 (window
== range
->slider
) ||
268 (window
== range
->step_forw
) ||
269 (window
== range
->step_back
) );
272 void wxScrollBar::ApplyWidgetStyle()
275 gtk_widget_set_style( m_widget
, m_widgetStyle
);