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
;
24 extern bool g_blockEventsOnScroll
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static void gtk_scrollbar_callback( GtkWidget
*WXUNUSED(widget
), wxScrollBar
*win
)
32 if (!win
->HasVMT()) return;
33 if (g_blockEventsOnDrag
) return;
35 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
36 if (fabs(diff
) < 0.2) return;
38 wxEventType command
= wxEVT_NULL
;
40 float line_step
= win
->m_adjust
->step_increment
;
41 float page_step
= win
->m_adjust
->page_increment
;
43 if (win
->m_isScrolling
)
45 command
= wxEVT_SCROLL_THUMBTRACK
;
49 if (fabs(win
->m_adjust
->value
-win
->m_adjust
->lower
) < 0.2) command
= wxEVT_SCROLL_BOTTOM
;
50 else if (fabs(win
->m_adjust
->value
-win
->m_adjust
->upper
) < 0.2) command
= wxEVT_SCROLL_TOP
;
51 else if (fabs(diff
-line_step
) < 0.2) command
= wxEVT_SCROLL_LINEDOWN
;
52 else if (fabs(diff
+line_step
) < 0.2) command
= wxEVT_SCROLL_LINEUP
;
53 else if (fabs(diff
-page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEDOWN
;
54 else if (fabs(diff
+page_step
) < 0.2) command
= wxEVT_SCROLL_PAGEUP
;
55 else command
= wxEVT_SCROLL_THUMBTRACK
;
58 int value
= (int)(win
->m_adjust
->value
+0.5);
60 int orient
= wxHORIZONTAL
;
61 if (win
->GetWindowStyleFlag() & wxSB_VERTICAL
== wxSB_VERTICAL
) orient
= wxVERTICAL
;
63 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
64 event
.SetEventObject( win
);
65 win
->GetEventHandler()->ProcessEvent( event
);
68 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
69 cevent.SetEventObject( win );
70 win->ProcessEvent( cevent );
74 //-----------------------------------------------------------------------------
75 // "button_press_event" from slider
76 //-----------------------------------------------------------------------------
78 static gint
gtk_scrollbar_button_press_callback( GtkRange
*WXUNUSED(widget
),
79 GdkEventButton
*WXUNUSED(gdk_event
),
82 win
->m_isScrolling
= TRUE
;
83 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
88 //-----------------------------------------------------------------------------
89 // "button_release_event" from slider
90 //-----------------------------------------------------------------------------
92 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
93 GdkEventButton
*WXUNUSED(gdk_event
),
96 win
->m_isScrolling
= FALSE
;
97 // g_blockEventsOnScroll = FALSE;
99 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
110 wxScrollBar::~wxScrollBar(void)
114 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
115 const wxPoint
& pos
, const wxSize
& size
,
116 long style
, const wxValidator
& validator
, const wxString
& name
)
119 m_acceptsFocus
= TRUE
;
121 PreCreation( parent
, id
, pos
, size
, style
, name
);
123 SetValidator( validator
);
127 if (style
& wxSB_VERTICAL
== wxSB_VERTICAL
)
128 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
130 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
132 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
134 gtk_signal_connect (GTK_OBJECT (m_adjust
), "value_changed",
135 (GtkSignalFunc
) gtk_scrollbar_callback
, (gpointer
) this );
137 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_press_event",
138 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
140 gtk_signal_connect( GTK_OBJECT(m_widget
), "button_release_event",
141 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
143 m_parent
->AddChild( this );
145 (m_parent
->m_insertCallback
)( m_parent
, this );
149 SetBackgroundColour( parent
->GetBackgroundColour() );
156 int wxScrollBar::GetThumbPosition(void) const
158 return (int)(m_adjust
->value
+0.5);
161 int wxScrollBar::GetThumbSize() const
163 return (int)(m_adjust
->page_size
+0.5);
166 int wxScrollBar::GetPageSize() const
168 return (int)(m_adjust
->page_increment
+0.5);
171 int wxScrollBar::GetRange() const
173 return (int)(m_adjust
->upper
+0.5);
176 void wxScrollBar::SetThumbPosition( int viewStart
)
178 if (m_isScrolling
) return;
180 float fpos
= (float)viewStart
;
182 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
183 m_adjust
->value
= fpos
;
185 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
188 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
189 bool WXUNUSED(refresh
) )
191 float fpos
= (float)position
;
192 float frange
= (float)range
;
193 float fthumb
= (float)thumbSize
;
194 float fpage
= (float)pageSize
;
196 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
197 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
198 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
200 SetThumbPosition( position
);
206 m_adjust
->lower
= 0.0;
207 m_adjust
->upper
= frange
;
208 m_adjust
->value
= fpos
;
209 m_adjust
->step_increment
= 1.0;
210 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
211 m_adjust
->page_size
= fthumb
;
213 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
216 // Backward compatibility
217 int wxScrollBar::GetValue(void) const
219 return GetThumbPosition();
222 void wxScrollBar::SetValue( int viewStart
)
224 SetThumbPosition( viewStart
);
227 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
229 int pos
= (int)(m_adjust
->value
+0.5);
230 int thumb
= (int)(m_adjust
->page_size
+0.5);
231 int page
= (int)(m_adjust
->page_increment
+0.5);
232 int range
= (int)(m_adjust
->upper
+0.5);
236 *objectLength
= thumb
;
240 int wxScrollBar::GetViewLength() const
242 return (int)(m_adjust
->upper
+0.5);
245 int wxScrollBar::GetObjectLength() const
247 return (int)(m_adjust
->page_size
+0.5);
250 void wxScrollBar::SetPageSize( int pageLength
)
252 int pos
= (int)(m_adjust
->value
+0.5);
253 int thumb
= (int)(m_adjust
->page_size
+0.5);
254 int range
= (int)(m_adjust
->upper
+0.5);
255 SetScrollbar( pos
, thumb
, range
, pageLength
);
258 void wxScrollBar::SetObjectLength( int objectLength
)
260 int pos
= (int)(m_adjust
->value
+0.5);
261 int page
= (int)(m_adjust
->page_increment
+0.5);
262 int range
= (int)(m_adjust
->upper
+0.5);
263 SetScrollbar( pos
, objectLength
, range
, page
);
266 void wxScrollBar::SetViewLength( int viewLength
)
268 int pos
= (int)(m_adjust
->value
+0.5);
269 int thumb
= (int)(m_adjust
->page_size
+0.5);
270 int page
= (int)(m_adjust
->page_increment
+0.5);
271 SetScrollbar( pos
, thumb
, viewLength
, page
);
274 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
276 GtkRange
*range
= GTK_RANGE(m_widget
);
277 return ( (window
== GTK_WIDGET(range
)->window
) ||
278 (window
== range
->trough
) ||
279 (window
== range
->slider
) ||
280 (window
== range
->step_forw
) ||
281 (window
== range
->step_back
) );
284 void wxScrollBar::ApplyWidgetStyle()
287 gtk_widget_set_style( m_widget
, m_widgetStyle
);