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
[0] = (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 g_signal_handlers_block_by_func(m_widget
,
192 (gpointer
)gtk_value_changed
, this);
194 gtk_range_set_value((GtkRange
*)m_widget
, viewStart
);
195 m_scrollPos
[0] = gtk_range_get_value((GtkRange
*)m_widget
);
197 g_signal_handlers_unblock_by_func(m_widget
,
198 (gpointer
)gtk_value_changed
, this);
202 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
, bool)
206 // GtkRange requires upper > lower
210 GtkAdjustment
* adj
= ((GtkRange
*)m_widget
)->adjustment
;
211 adj
->step_increment
= 1;
212 adj
->page_increment
= pageSize
;
213 adj
->page_size
= thumbSize
;
214 adj
->value
= position
;
215 g_signal_handlers_block_by_func(m_widget
, (void*)gtk_value_changed
, this);
216 gtk_range_set_range((GtkRange
*)m_widget
, 0, range
);
217 m_scrollPos
[0] = adj
->value
;
218 g_signal_handlers_unblock_by_func(m_widget
, (void*)gtk_value_changed
, this);
221 void wxScrollBar::SetPageSize( int pageLength
)
223 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength
);
226 void wxScrollBar::SetRange(int range
)
228 SetScrollbar(GetThumbPosition(), GetThumbSize(), range
, GetPageSize());
231 GdkWindow
*wxScrollBar::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
233 return m_widget
->window
;
238 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
240 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);
243 #endif // wxUSE_SCROLLBAR