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
->GTKGetScrollEventType(range
);
32 if (eventType
!= wxEVT_NULL
)
34 const int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
35 const int value
= win
->GetThumbPosition();
36 const int id
= win
->GetId();
38 // first send the specific event for the user action
39 wxScrollEvent
evtSpec(eventType
, id
, value
, orient
);
40 evtSpec
.SetEventObject(win
);
41 win
->HandleWindowEvent(evtSpec
);
43 if (!win
->m_isScrolling
)
45 // and if it's over also send a general "changed" event
46 wxScrollEvent
evtChanged(wxEVT_SCROLL_CHANGED
, id
, value
, orient
);
47 evtChanged
.SetEventObject(win
);
48 win
->HandleWindowEvent(evtChanged
);
54 //-----------------------------------------------------------------------------
55 // "button_press_event" from scrollbar
56 //-----------------------------------------------------------------------------
60 gtk_button_press_event(GtkRange
*, GdkEventButton
*, wxScrollBar
* win
)
62 win
->m_mouseButtonDown
= true;
67 //-----------------------------------------------------------------------------
68 // "event_after" from scrollbar
69 //-----------------------------------------------------------------------------
73 gtk_event_after(GtkRange
* range
, GdkEvent
* event
, wxScrollBar
* win
)
75 if (event
->type
== GDK_BUTTON_RELEASE
)
77 g_signal_handlers_block_by_func(range
, (void*)gtk_event_after
, win
);
79 const int value
= win
->GetThumbPosition();
80 const int orient
= win
->HasFlag(wxSB_VERTICAL
) ? wxVERTICAL
: wxHORIZONTAL
;
81 const int id
= win
->GetId();
83 wxScrollEvent
evtRel(wxEVT_SCROLL_THUMBRELEASE
, id
, value
, orient
);
84 evtRel
.SetEventObject(win
);
85 win
->HandleWindowEvent(evtRel
);
87 wxScrollEvent
evtChanged(wxEVT_SCROLL_CHANGED
, id
, value
, orient
);
88 evtChanged
.SetEventObject(win
);
89 win
->HandleWindowEvent(evtChanged
);
94 //-----------------------------------------------------------------------------
95 // "button_release_event" from scrollbar
96 //-----------------------------------------------------------------------------
100 gtk_button_release_event(GtkRange
* range
, GdkEventButton
*, wxScrollBar
* win
)
102 win
->m_mouseButtonDown
= false;
104 if (win
->m_isScrolling
)
106 win
->m_isScrolling
= false;
107 // Hook up handler to send thumb release event after this emission is finished.
108 // To allow setting scroll position from event handler, sending event must
109 // be deferred until after the GtkRange handler for this signal has run
110 g_signal_handlers_unblock_by_func(range
, (void*)gtk_event_after
, win
);
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 wxScrollBar::wxScrollBar()
125 wxScrollBar::~wxScrollBar()
129 bool wxScrollBar::Create(wxWindow
*parent
, wxWindowID id
,
130 const wxPoint
& pos
, const wxSize
& size
,
131 long style
, const wxValidator
& validator
, const wxString
& name
)
133 if (!PreCreation( parent
, pos
, size
) ||
134 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
136 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
140 const bool isVertical
= (style
& wxSB_VERTICAL
) != 0;
142 m_widget
= gtk_vscrollbar_new( NULL
);
144 m_widget
= gtk_hscrollbar_new( NULL
);
145 g_object_ref(m_widget
);
147 m_scrollBar
[0] = (GtkRange
*)m_widget
;
149 g_signal_connect_after(m_widget
, "value_changed",
150 G_CALLBACK(gtk_value_changed
), this);
151 g_signal_connect(m_widget
, "button_press_event",
152 G_CALLBACK(gtk_button_press_event
), this);
153 g_signal_connect(m_widget
, "button_release_event",
154 G_CALLBACK(gtk_button_release_event
), this);
157 handler_id
= g_signal_connect(
158 m_widget
, "event_after", G_CALLBACK(gtk_event_after
), this);
159 g_signal_handler_block(m_widget
, handler_id
);
161 m_parent
->DoAddChild( this );
168 int wxScrollBar::GetThumbPosition() const
170 return wxRound(gtk_range_get_value(GTK_RANGE(m_widget
)));
173 int wxScrollBar::GetThumbSize() const
175 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_widget
));
176 return int(gtk_adjustment_get_page_size(adj
));
179 int wxScrollBar::GetPageSize() const
181 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_widget
));
182 return int(gtk_adjustment_get_page_increment(adj
));
185 int wxScrollBar::GetRange() const
187 GtkAdjustment
* adj
= gtk_range_get_adjustment(GTK_RANGE(m_widget
));
188 return int(gtk_adjustment_get_upper(adj
));
191 void wxScrollBar::SetThumbPosition( int viewStart
)
193 if (GetThumbPosition() != viewStart
)
195 g_signal_handlers_block_by_func(m_widget
,
196 (gpointer
)gtk_value_changed
, this);
198 gtk_range_set_value((GtkRange
*)m_widget
, viewStart
);
199 m_scrollPos
[0] = gtk_range_get_value((GtkRange
*)m_widget
);
201 g_signal_handlers_unblock_by_func(m_widget
,
202 (gpointer
)gtk_value_changed
, this);
206 void wxScrollBar::SetScrollbar(int position
, int thumbSize
, int range
, int pageSize
, bool)
210 // GtkRange requires upper > lower
214 g_signal_handlers_block_by_func(m_widget
, (void*)gtk_value_changed
, this);
215 GtkRange
* widget
= GTK_RANGE(m_widget
);
216 gtk_adjustment_set_page_size(gtk_range_get_adjustment(widget
), thumbSize
);
217 gtk_range_set_increments(widget
, 1, pageSize
);
218 gtk_range_set_range(widget
, 0, range
);
219 gtk_range_set_value(widget
, position
);
220 m_scrollPos
[0] = gtk_range_get_value(widget
);
221 g_signal_handlers_unblock_by_func(m_widget
, (void*)gtk_value_changed
, this);
224 void wxScrollBar::SetPageSize( int pageLength
)
226 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength
);
229 void wxScrollBar::SetRange(int range
)
231 SetScrollbar(GetThumbPosition(), GetThumbSize(), range
, GetPageSize());
236 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
238 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new
);
241 #endif // wxUSE_SCROLLBAR