]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
suppress (harmless) gcc warning about non-virtual dtor in a class with virtual functions
[wxWidgets.git] / src / gtk / scrolbar.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/gtk/scrolbar.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
dcf924a3
RR
12
13#if wxUSE_SCROLLBAR
14
1e6feb95
VZ
15#include "wx/scrolbar.h"
16
de6185e2
WS
17#ifndef WX_PRECOMP
18 #include "wx/utils.h"
19#endif
20
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
66bd6b93 23//-----------------------------------------------------------------------------
add7cadd 24// "value_changed" from scrollbar
66bd6b93
RR
25//-----------------------------------------------------------------------------
26
865bb325 27extern "C" {
add7cadd
PC
28static void
29gtk_value_changed(GtkRange* range, wxScrollBar* win)
a2615ebc 30{
add7cadd
PC
31 wxEventType eventType = win->GetScrollEventType(range);
32 if (eventType != wxEVT_NULL)
7c2f14ee 33 {
add7cadd
PC
34 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
35 const int value = win->GetThumbPosition();
74ab5f5b
VZ
36 const int id = win->GetId();
37
38 // first send the specific event for the user action
39 wxScrollEvent evtSpec(eventType, id, value, orient);
40 evtSpec.SetEventObject(win);
41 win->GetEventHandler()->ProcessEvent(evtSpec);
42
add7cadd
PC
43 if (!win->m_isScrolling)
44 {
74ab5f5b
VZ
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->GetEventHandler()->ProcessEvent(evtChanged);
add7cadd 49 }
91af0895 50 }
6de97a3b 51}
865bb325 52}
c801d85f 53
cb43b372 54//-----------------------------------------------------------------------------
add7cadd 55// "button_press_event" from scrollbar
cb43b372 56//-----------------------------------------------------------------------------
add7cadd 57
865bb325 58extern "C" {
add7cadd
PC
59static gboolean
60gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
cb43b372 61{
add7cadd
PC
62 win->m_mouseButtonDown = true;
63 return false;
cb43b372 64}
865bb325 65}
cb43b372 66
c918b2cd
PC
67//-----------------------------------------------------------------------------
68// "event_after" from scrollbar
69//-----------------------------------------------------------------------------
70
71extern "C" {
72static void
73gtk_event_after(GtkRange* range, GdkEvent* event, wxScrollBar* win)
74{
75 if (event->type == GDK_BUTTON_RELEASE)
76 {
77 g_signal_handlers_block_by_func(range, (void*)gtk_event_after, win);
78
79 const int value = win->GetThumbPosition();
80 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
74ab5f5b 81 const int id = win->GetId();
c918b2cd 82
74ab5f5b
VZ
83 wxScrollEvent evtRel(wxEVT_SCROLL_THUMBRELEASE, id, value, orient);
84 evtRel.SetEventObject(win);
85 win->GetEventHandler()->ProcessEvent(evtRel);
c918b2cd 86
74ab5f5b
VZ
87 wxScrollEvent evtChanged(wxEVT_SCROLL_CHANGED, id, value, orient);
88 evtChanged.SetEventObject(win);
89 win->GetEventHandler()->ProcessEvent(evtChanged);
c918b2cd
PC
90 }
91}
92}
93
cb43b372 94//-----------------------------------------------------------------------------
add7cadd 95// "button_release_event" from scrollbar
cb43b372
RR
96//-----------------------------------------------------------------------------
97
865bb325 98extern "C" {
add7cadd 99static gboolean
c918b2cd 100gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win)
cb43b372 101{
add7cadd
PC
102 win->m_mouseButtonDown = false;
103 // If thumb tracking
2a23d363
RR
104 if (win->m_isScrolling)
105 {
add7cadd 106 win->m_isScrolling = false;
c918b2cd 107 // Hook up handler to send thumb release event after this emission is finished.
8ea30e36
PC
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
c918b2cd 110 g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win);
add7cadd 111 }
7c2f14ee 112
add7cadd 113 return false;
cb43b372 114}
865bb325 115}
cb43b372 116
b4071e91
RR
117//-----------------------------------------------------------------------------
118// wxScrollBar
119//-----------------------------------------------------------------------------
120
c801d85f
KB
121IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
122
add7cadd
PC
123wxScrollBar::wxScrollBar()
124{
125}
126
0a07a7d8 127wxScrollBar::~wxScrollBar()
c801d85f 128{
6de97a3b 129}
c801d85f 130
debe6624 131bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 132 const wxPoint& pos, const wxSize& size,
6de97a3b 133 long style, const wxValidator& validator, const wxString& name )
c801d85f 134{
4dcaf11a
RR
135 if (!PreCreation( parent, pos, size ) ||
136 !CreateBase( parent, id, pos, size, style, validator, name ))
137 {
223d09f6 138 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
91af0895 139 return false;
4dcaf11a 140 }
6de97a3b 141
add7cadd
PC
142 const bool isVertical = (style & wxSB_VERTICAL) != 0;
143 if (isVertical)
2d17d68f 144 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
d9bd1494
SB
145 else
146 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
147
63c95f27 148 m_scrollBar[0] = (GtkRange*)m_widget;
a2615ebc 149
40e5ebbf 150 g_signal_connect_after(m_widget, "value_changed",
add7cadd
PC
151 G_CALLBACK(gtk_value_changed), this);
152 g_signal_connect(m_widget, "button_press_event",
153 G_CALLBACK(gtk_button_press_event), this);
154 g_signal_connect(m_widget, "button_release_event",
155 G_CALLBACK(gtk_button_release_event), this);
cb43b372 156
c918b2cd
PC
157 gulong handler_id;
158 handler_id = g_signal_connect(
159 m_widget, "event_after", G_CALLBACK(gtk_event_after), this);
160 g_signal_handler_block(m_widget, handler_id);
161
f03fc89f 162 m_parent->DoAddChild( this );
a2615ebc 163
abdeb9e7 164 PostCreation(size);
a2615ebc 165
91af0895 166 return true;
6de97a3b 167}
c801d85f 168
0a07a7d8 169int wxScrollBar::GetThumbPosition() const
c801d85f 170{
add7cadd
PC
171 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
172 return int(adj->value + 0.5);
6de97a3b 173}
c801d85f
KB
174
175int wxScrollBar::GetThumbSize() const
176{
add7cadd
PC
177 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
178 return int(adj->page_size);
6de97a3b 179}
c801d85f
KB
180
181int wxScrollBar::GetPageSize() const
182{
add7cadd
PC
183 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
184 return int(adj->page_increment);
6de97a3b 185}
c801d85f
KB
186
187int wxScrollBar::GetRange() const
188{
add7cadd
PC
189 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
190 return int(adj->upper);
6de97a3b 191}
c801d85f 192
4fabb575 193void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 194{
add7cadd 195 if (GetThumbPosition() != viewStart)
2d17d68f 196 {
98264520
PC
197 g_signal_handlers_block_by_func(m_widget,
198 (gpointer)gtk_value_changed, this);
40e5ebbf 199
63c95f27
PC
200 gtk_range_set_value((GtkRange*)m_widget, viewStart);
201 m_scrollPos[0] = gtk_range_get_value((GtkRange*)m_widget);
98264520
PC
202
203 g_signal_handlers_unblock_by_func(m_widget,
204 (gpointer)gtk_value_changed, this);
2d17d68f 205 }
6de97a3b 206}
c801d85f 207
add7cadd 208void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
c801d85f 209{
de7bb802
PC
210 if (range == 0)
211 {
212 // GtkRange requires upper > lower
213 range =
214 thumbSize = 1;
215 }
add7cadd 216 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
add7cadd
PC
217 adj->step_increment = 1;
218 adj->page_increment = pageSize;
219 adj->page_size = thumbSize;
63c95f27
PC
220 adj->value = position;
221 g_signal_handlers_block_by_func(m_widget, (void*)gtk_value_changed, this);
222 gtk_range_set_range((GtkRange*)m_widget, 0, range);
223 m_scrollPos[0] = adj->value;
224 g_signal_handlers_unblock_by_func(m_widget, (void*)gtk_value_changed, this);
6de97a3b 225}
c801d85f 226
debe6624 227void wxScrollBar::SetPageSize( int pageLength )
c801d85f 228{
add7cadd 229 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
6de97a3b 230}
c801d85f 231
add7cadd 232void wxScrollBar::SetRange(int range)
c801d85f 233{
add7cadd 234 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
6de97a3b 235}
c801d85f 236
ef5c70f9 237GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
b4071e91 238{
10bd1f7d 239 return m_widget->window;
b4071e91 240}
58614078 241
9d522606
RD
242// static
243wxVisualAttributes
244wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
245{
246 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
247}
248
de6185e2 249#endif // wxUSE_SCROLLBAR