1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "scrolbar.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
20 #include "wx/scrolbar.h"
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 extern bool g_blockEventsOnScroll
;
39 static wxEventType g_currentUpDownEvent
= wxEVT_NULL
;
41 static const float sensitivity
= 0.02;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 // FIXME: is GtkScrollType really passed to us as 2nd argument?
50 static void gtk_scrollbar_callback( GtkAdjustment
*adjust
,
54 if (g_isIdle
) wxapp_install_idle_handler();
56 if (!win
->m_hasVMT
) return;
57 if (g_blockEventsOnDrag
) return;
59 float diff
= adjust
->value
- win
->m_oldPos
;
60 if (fabs(diff
) < sensitivity
) return;
62 win
->m_oldPos
= adjust
->value
;
64 wxEventType command
= GtkScrollTypeToWx(GET_SCROLL_TYPE(win
->m_widget
));
66 double dvalue
= adjust
->value
;
67 int value
= (int)(dvalue
< 0 ? dvalue
- 0.5 : dvalue
+ 0.5);
69 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
71 // throw a LINEUP / LINEDOWN event if necessary
72 if (g_currentUpDownEvent
!= wxEVT_NULL
)
74 wxScrollEvent
event( g_currentUpDownEvent
, win
->GetId(), value
, orient
);
75 event
.SetEventObject( win
);
76 win
->GetEventHandler()->ProcessEvent( event
);
79 // throw other event (wxEVT_SCROLL_THUMBTRACK)
80 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
81 event
.SetEventObject( win
);
82 win
->GetEventHandler()->ProcessEvent( event
);
85 wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() );
86 cevent.SetEventObject( win );
87 win->ProcessEvent( cevent );
92 //-----------------------------------------------------------------------------
93 // "button_press_event" from slider
94 //-----------------------------------------------------------------------------
96 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
97 GdkEventButton
*gdk_event
,
100 if (g_isIdle
) wxapp_install_idle_handler();
102 // check if a LINEUP/LINEDOWN event must be thrown
103 // I suppose here the size of scrollbar top/bottom buttons is 16px height
104 if (gdk_event
->type
== GDK_BUTTON_PRESS
&& gdk_event
->button
== 1)
106 int scroll_height
, mouse_pos
;
108 // get the mouse position when the click is done
109 if (win
->HasFlag(wxSB_VERTICAL
))
111 scroll_height
= GTK_WIDGET(widget
)->allocation
.height
- 16;
112 mouse_pos
= (int)gdk_event
->y
;
116 scroll_height
= GTK_WIDGET(widget
)->allocation
.width
- 16;
117 mouse_pos
= (int)gdk_event
->x
;
120 // compare mouse position to scrollbar height
121 if (mouse_pos
> scroll_height
)
122 g_currentUpDownEvent
= wxEVT_SCROLL_LINEDOWN
;
123 else if (mouse_pos
< 16)
124 g_currentUpDownEvent
= wxEVT_SCROLL_LINEUP
;
128 // There is no slider field any more
129 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
136 //-----------------------------------------------------------------------------
137 // "button_release_event" from slider
138 //-----------------------------------------------------------------------------
142 gtk_scrollbar_button_release_callback( GtkRange
*WXUNUSED(widget
),
143 GdkEventButton
*WXUNUSED(gdk_event
),
147 wxapp_install_idle_handler();
149 if (win
->m_isScrolling
)
151 wxEventType command
= wxEVT_SCROLL_THUMBRELEASE
;
152 int value
= (int)ceil(win
->m_adjust
->value
);
153 int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
155 wxScrollEvent
event( command
, win
->GetId(), value
, orient
);
156 event
.SetEventObject( win
);
157 win
->GetEventHandler()->ProcessEvent( event
);
160 win
->m_isScrolling
= FALSE
;
162 // reset the LINEUP/LINEDOWN flag when the mouse button is released
163 g_currentUpDownEvent
= wxEVT_NULL
;
169 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
175 wxScrollBar::~wxScrollBar()
179 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
180 const wxPoint
& pos
, const wxSize
& size
,
181 long style
, const wxValidator
& validator
, const wxString
& name
)
184 m_acceptsFocus
= TRUE
;
186 if (!PreCreation( parent
, pos
, size
) ||
187 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
189 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
195 if ((style
& wxSB_VERTICAL
) == wxSB_VERTICAL
)
196 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
198 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
200 m_adjust
= gtk_range_get_adjustment( GTK_RANGE(m_widget
) );
202 gtk_signal_connect( GTK_OBJECT(m_adjust
),
204 (GtkSignalFunc
) gtk_scrollbar_callback
,
206 gtk_signal_connect( GTK_OBJECT(m_widget
),
207 "button_press_event",
208 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
,
210 gtk_signal_connect( GTK_OBJECT(m_widget
),
211 "button_release_event",
212 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
,
215 m_parent
->DoAddChild( this );
222 int wxScrollBar::GetThumbPosition() const
224 double val
= m_adjust
->value
;
225 return (int)(val
< 0 ? val
- 0.5 : val
+ 0.5);
228 int wxScrollBar::GetThumbSize() const
230 return (int)(m_adjust
->page_size
+0.5);
233 int wxScrollBar::GetPageSize() const
235 return (int)(m_adjust
->page_increment
+0.5);
238 int wxScrollBar::GetRange() const
240 return (int)(m_adjust
->upper
+0.5);
243 void wxScrollBar::SetThumbPosition( int viewStart
)
245 if (m_isScrolling
) return;
247 float fpos
= (float)viewStart
;
249 if (fabs(fpos
-m_adjust
->value
) < 0.2) return;
250 m_adjust
->value
= fpos
;
252 gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust
),
253 (GtkSignalFunc
) gtk_scrollbar_callback
,
256 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
258 gtk_signal_connect( GTK_OBJECT(m_adjust
),
260 (GtkSignalFunc
) gtk_scrollbar_callback
,
264 void wxScrollBar::SetScrollbar( int position
, int thumbSize
, int range
, int pageSize
,
265 bool WXUNUSED(refresh
) )
267 float fpos
= (float)position
;
268 float frange
= (float)range
;
269 float fthumb
= (float)thumbSize
;
270 float fpage
= (float)pageSize
;
272 if ((fabs(frange
-m_adjust
->upper
) < 0.2) &&
273 (fabs(fthumb
-m_adjust
->page_size
) < 0.2) &&
274 (fabs(fpage
-m_adjust
->page_increment
) < 0.2))
276 SetThumbPosition( position
);
282 m_adjust
->lower
= 0.0;
283 m_adjust
->upper
= frange
;
284 m_adjust
->value
= fpos
;
285 m_adjust
->step_increment
= 1.0;
286 m_adjust
->page_increment
= (float)(wxMax(fpage
,0));
287 m_adjust
->page_size
= fthumb
;
289 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
292 /* Backward compatibility */
293 int wxScrollBar::GetValue() const
295 return GetThumbPosition();
298 void wxScrollBar::SetValue( int viewStart
)
300 SetThumbPosition( viewStart
);
303 void wxScrollBar::GetValues( int *viewStart
, int *viewLength
, int *objectLength
, int *pageLength
) const
305 int pos
= (int)(m_adjust
->value
+0.5);
306 int thumb
= (int)(m_adjust
->page_size
+0.5);
307 int page
= (int)(m_adjust
->page_increment
+0.5);
308 int range
= (int)(m_adjust
->upper
+0.5);
312 *objectLength
= thumb
;
316 int wxScrollBar::GetViewLength() const
318 return (int)(m_adjust
->upper
+0.5);
321 int wxScrollBar::GetObjectLength() const
323 return (int)(m_adjust
->page_size
+0.5);
326 void wxScrollBar::SetPageSize( int pageLength
)
328 int pos
= (int)(m_adjust
->value
+0.5);
329 int thumb
= (int)(m_adjust
->page_size
+0.5);
330 int range
= (int)(m_adjust
->upper
+0.5);
331 SetScrollbar( pos
, thumb
, range
, pageLength
);
334 void wxScrollBar::SetObjectLength( int objectLength
)
336 int pos
= (int)(m_adjust
->value
+0.5);
337 int page
= (int)(m_adjust
->page_increment
+0.5);
338 int range
= (int)(m_adjust
->upper
+0.5);
339 SetScrollbar( pos
, objectLength
, range
, page
);
342 void wxScrollBar::SetViewLength( int viewLength
)
344 int pos
= (int)(m_adjust
->value
+0.5);
345 int thumb
= (int)(m_adjust
->page_size
+0.5);
346 int page
= (int)(m_adjust
->page_increment
+0.5);
347 SetScrollbar( pos
, thumb
, viewLength
, page
);
350 bool wxScrollBar::IsOwnGtkWindow( GdkWindow
*window
)
352 GtkRange
*range
= GTK_RANGE(m_widget
);
353 return ( (window
== GTK_WIDGET(range
)->window
)
355 || (window
== range
->trough
)
356 || (window
== range
->slider
)
357 || (window
== range
->step_forw
)
358 || (window
== range
->step_back
)
363 wxSize
wxScrollBar::DoGetBestSize() const
365 return wxControl::DoGetBestSize();
370 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
372 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);