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"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 extern bool g_blockEventsOnScroll
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
, wxScrollBar
*win
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!win
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 float diff
= adjust
->value
- win
->m_oldPos
;
51 if (fabs(diff
) < 0.2) return;
53 win
->m_oldPos
= adjust
->value
;
55 GtkRange
*range
= GTK_RANGE( win
->m_widget
);
57 wxEventType command
= wxEVT_SCROLL_THUMBTRACK
;
58 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLL_LINEUP
;
59 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLL_LINEDOWN
;
60 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLL_PAGEUP
;
61 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLL_PAGEDOWN
;
63 int value
= (int)ceil(adjust
->value
);
65 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
67 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
68 event
.SetEventObject( win
);
69 win
->GetEventHandler()->ProcessEvent( event
);
72 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
73 cevent.SetEventObject( win );
74 win->ProcessEvent( cevent );
78 //-----------------------------------------------------------------------------
79 // "button_press_event" from slider
80 //-----------------------------------------------------------------------------
82 static gint
gtk_scrollbar_button_press_callback( GtkRange
*WXUNUSED(widget
),
83 GdkEventButton
*WXUNUSED(gdk_event
),
86 if (g_isIdle
) wxapp_install_idle_handler();
88 win
->SetScrolling(TRUE
);
89 // g_blockEventsOnScroll = TRUE; doesn't work in DialogEd
94 //-----------------------------------------------------------------------------
95 // "button_release_event" from slider
96 //-----------------------------------------------------------------------------
98 static gint
gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
99 GdkEventButton
*WXUNUSED(gdk_event
),
102 if (g_isIdle
) wxapp_install_idle_handler();
104 win
->SetScrolling(FALSE
);
105 // g_blockEventsOnScroll = FALSE;
107 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
118 wxScrollBar::~wxScrollBar(void)
122 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
123 const wxPoint
& pos
, const wxSize
& size
,
124 long style
, const wxValidator
& validator
, const wxString
& name
)
127 m_acceptsFocus
= TRUE
;
129 if (!PreCreation( parent
, pos
, size
) ||
130 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
132 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
138 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
139 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
141 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
143 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
145 gtk_signal_connect( GTK_OBJECT(m_adjust
),
147 (GtkSignalFunc
) gtk_scrollbar_callback
,
150 gtk_signal_connect( GTK_OBJECT(m_widget
),
151 "button_press_event",
152 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
155 gtk_signal_connect( GTK_OBJECT(m_widget
),
156 "button_release_event",
157 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
160 m_parent
->DoAddChild( this );
164 SetBackgroundColour( parent
->GetBackgroundColour() );
171 int wxScrollBar::GetThumbPosition(void) const
173 return (int)(m_adjust
->value
+0.5);
176 int wxScrollBar::GetThumbSize() const
178 return (int)(m_adjust
->page_size
+0.5);
181 int wxScrollBar::GetPageSize() const
183 return (int)(m_adjust
->page_increment
+0.5);
186 int wxScrollBar::GetRange() const
188 return (int)(m_adjust
->upper
+0.5);
191 void wxScrollBar::SetThumbPosition( int viewStart
)
193 if (m_isScrolling
) return;
195 float fpos
= (float)viewStart
;
197 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
198 m_adjust
->value
= fpos
;
200 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
203 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
204 bool WXUNUSED(refresh
) )
206 float fpos
= (float)position
;
207 float frange
= (float)range
;
208 float fthumb
= (float)thumbSize
;
209 float fpage
= (float)pageSize
;
211 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
212 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
213 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
215 SetThumbPosition( position
);
221 m_adjust
->lower
= 0.0;
222 m_adjust
->upper
= frange
;
223 m_adjust
->value
= fpos
;
224 m_adjust
->step_increment
= 1.0;
225 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
226 m_adjust
->page_size
= fthumb
;
228 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
231 /* Backward compatibility */
232 int wxScrollBar::GetValue(void) const
234 return GetThumbPosition();
237 void wxScrollBar::SetValue( int viewStart
)
239 SetThumbPosition( viewStart
);
242 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
244 int pos
= (int)(m_adjust
->value
+0.5);
245 int thumb
= (int)(m_adjust
->page_size
+0.5);
246 int page
= (int)(m_adjust
->page_increment
+0.5);
247 int range
= (int)(m_adjust
->upper
+0.5);
251 *objectLength
= thumb
;
255 int wxScrollBar::GetViewLength() const
257 return (int)(m_adjust
->upper
+0.5);
260 int wxScrollBar::GetObjectLength() const
262 return (int)(m_adjust
->page_size
+0.5);
265 void wxScrollBar::SetPageSize( int pageLength
)
267 int pos
= (int)(m_adjust
->value
+0.5);
268 int thumb
= (int)(m_adjust
->page_size
+0.5);
269 int range
= (int)(m_adjust
->upper
+0.5);
270 SetScrollbar( pos
, thumb
, range
, pageLength
);
273 void wxScrollBar::SetObjectLength( int objectLength
)
275 int pos
= (int)(m_adjust
->value
+0.5);
276 int page
= (int)(m_adjust
->page_increment
+0.5);
277 int range
= (int)(m_adjust
->upper
+0.5);
278 SetScrollbar( pos
, objectLength
, range
, page
);
281 void wxScrollBar::SetViewLength( int viewLength
)
283 int pos
= (int)(m_adjust
->value
+0.5);
284 int thumb
= (int)(m_adjust
->page_size
+0.5);
285 int page
= (int)(m_adjust
->page_increment
+0.5);
286 SetScrollbar( pos
, thumb
, viewLength
, page
);
289 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
291 GtkRange
*range
= GTK_RANGE(m_widget
);
292 return ( (window
== GTK_WIDGET(range
)->window
) ||
293 (window
== range
->trough
) ||
294 (window
== range
->slider
) ||
295 (window
== range
->step_forw
) ||
296 (window
== range
->step_back
) );
299 void wxScrollBar::ApplyWidgetStyle()
302 gtk_widget_set_style( m_widget
, m_widgetStyle
);