1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/scrolbar.h"
21 #include "wx/gtk/private.h"
23 //-----------------------------------------------------------------------------
24 // "value_changed" from scrollbar
25 //-----------------------------------------------------------------------------
29 gtk_value_changed(GtkRange
* range
, wxScrollBar
* win
)
31 wxEventType eventType
= win
->GetScrollEventType(range
);
32 if (eventType
!= wxEVT_NULL
)
34 const int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
35 const int value
= win
->GetThumbPosition();
36 wxScrollEvent
event(eventType
, win
->GetId(), value
, orient
);
37 event
.SetEventObject(win
);
38 win
->GetEventHandler()->ProcessEvent(event
);
39 if (!win
->m_isScrolling
)
41 wxScrollEvent
event(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
42 event
.SetEventObject(win
);
43 win
->GetEventHandler()->ProcessEvent(event
);
49 //-----------------------------------------------------------------------------
50 // "button_press_event" from scrollbar
51 //-----------------------------------------------------------------------------
55 gtk_button_press_event(GtkRange
*, GdkEventButton
*, wxScrollBar
* win
)
57 win
->m_mouseButtonDown
= true;
62 //-----------------------------------------------------------------------------
63 // "event_after" from scrollbar
64 //-----------------------------------------------------------------------------
68 gtk_event_after(GtkRange
* range
, GdkEvent
* event
, wxScrollBar
* win
)
70 if (event
->type
== GDK_BUTTON_RELEASE
)
72 g_signal_handlers_block_by_func(range
, (void*)gtk_event_after
, win
);
74 const int value
= win
->GetThumbPosition();
75 const int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
77 wxScrollEvent
event(wxEVT_SCROLL_THUMBRELEASE
, win
->GetId(), value
, orient
);
78 event
.SetEventObject(win
);
79 win
->GetEventHandler()->ProcessEvent(event
);
81 wxScrollEvent
event2(wxEVT_SCROLL_CHANGED
, win
->GetId(), value
, orient
);
82 event2
.SetEventObject(win
);
83 win
->GetEventHandler()->ProcessEvent(event2
);
88 //-----------------------------------------------------------------------------
89 // "button_release_event" from scrollbar
90 //-----------------------------------------------------------------------------
94 gtk_button_release_event(GtkRange
* range
, GdkEventButton
*, wxScrollBar
* win
)
96 win
->m_mouseButtonDown
= false;
98 if (win
->m_isScrolling
)
100 win
->m_isScrolling
= false;
101 // Hook up handler to send thumb release event after this emission is finished.
102 // To allow setting scroll position from event handler, sending event must
103 // be deferred until after the GtkRange handler for this signal has run
104 g_signal_handlers_unblock_by_func(range
, (void*)gtk_event_after
, win
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
,wxControl
)
117 wxScrollBar::wxScrollBar()
121 wxScrollBar::~wxScrollBar()
125 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
126 const wxPoint
& pos
, const wxSize
& size
,
127 long style
, const wxValidator
& validator
, const wxString
& name
)
129 if (!PreCreation( parent
, pos
, size
) ||
130 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
132 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
136 const bool isVertical
= (style
& wxSB_VERTICAL
) != 0;
138 m_widget
= gtk_vscrollbar_new( (GtkAdjustment
*) NULL
);
140 m_widget
= gtk_hscrollbar_new( (GtkAdjustment
*) NULL
);
142 m_scrollBar
[int(isVertical
)] = (GtkRange
*)m_widget
;
144 g_signal_connect_after(m_widget
, "value_changed",
145 G_CALLBACK(gtk_value_changed
), this);
146 g_signal_connect(m_widget
, "button_press_event",
147 G_CALLBACK(gtk_button_press_event
), this);
148 g_signal_connect(m_widget
, "button_release_event",
149 G_CALLBACK(gtk_button_release_event
), this);
152 handler_id
= g_signal_connect(
153 m_widget
, "event_after", G_CALLBACK(gtk_event_after
), this);
154 g_signal_handler_block(m_widget
, handler_id
);
156 m_parent
->DoAddChild( this );
163 int wxScrollBar::GetThumbPosition() const
165 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
166 return int(adj
->value
+ 0.5);
169 int wxScrollBar::GetThumbSize() const
171 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
172 return int(adj
->page_size
);
175 int wxScrollBar::GetPageSize() const
177 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
178 return int(adj
->page_increment
);
181 int wxScrollBar::GetRange() const
183 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
184 return int(adj
->upper
);
187 void wxScrollBar::SetThumbPosition( int viewStart
)
189 if (GetThumbPosition() != viewStart
)
191 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
192 const int i
= (GtkRange
*)m_widget
== m_scrollBar
[1];
193 const int max
= int(adj
->upper
- adj
->page_size
);
200 adj
->value
= viewStart
;
202 g_signal_handlers_block_by_func(m_widget
,
203 (gpointer
)gtk_value_changed
, this);
205 gtk_adjustment_value_changed(adj
);
207 g_signal_handlers_unblock_by_func(m_widget
,
208 (gpointer
)gtk_value_changed
, this);
212 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
, bool)
216 // GtkRange requires upper > lower
220 if (position
> range
- thumbSize
)
221 position
= range
- thumbSize
;
224 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
225 adj
->step_increment
= 1;
226 adj
->page_increment
= pageSize
;
227 adj
->page_size
= thumbSize
;
229 SetThumbPosition(position
);
230 gtk_adjustment_changed(adj
);
233 void wxScrollBar::SetPageSize( int pageLength
)
235 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength
);
238 void wxScrollBar::SetRange(int range
)
240 SetScrollbar(GetThumbPosition(), GetThumbSize(), range
, GetPageSize());
243 GdkWindow
*wxScrollBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
245 return m_widget
->window
;
250 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
252 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);
255 #endif // wxUSE_SCROLLBAR