]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
readded wxEditableListBox as part of adv library
[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 34 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
8ea30e36 35 const int i = orient == wxVERTICAL;
add7cadd
PC
36 const int value = win->GetThumbPosition();
37 wxScrollEvent event(eventType, win->GetId(), value, orient);
38 event.SetEventObject(win);
8ea30e36 39 win->m_blockValueChanged[i] = true;
add7cadd
PC
40 win->GetEventHandler()->ProcessEvent(event);
41 if (!win->m_isScrolling)
42 {
43 wxScrollEvent event(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
44 event.SetEventObject(win);
45 win->GetEventHandler()->ProcessEvent(event);
46 }
8ea30e36 47 win->m_blockValueChanged[i] = false;
91af0895 48 }
6de97a3b 49}
865bb325 50}
c801d85f 51
cb43b372 52//-----------------------------------------------------------------------------
add7cadd 53// "button_press_event" from scrollbar
cb43b372 54//-----------------------------------------------------------------------------
add7cadd 55
865bb325 56extern "C" {
add7cadd
PC
57static gboolean
58gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
cb43b372 59{
add7cadd
PC
60 win->m_mouseButtonDown = true;
61 return false;
cb43b372 62}
865bb325 63}
cb43b372 64
c918b2cd
PC
65//-----------------------------------------------------------------------------
66// "event_after" from scrollbar
67//-----------------------------------------------------------------------------
68
69extern "C" {
70static void
71gtk_event_after(GtkRange* range, GdkEvent* event, wxScrollBar* win)
72{
73 if (event->type == GDK_BUTTON_RELEASE)
74 {
75 g_signal_handlers_block_by_func(range, (void*)gtk_event_after, win);
76
77 const int value = win->GetThumbPosition();
78 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
79
80 wxScrollEvent event(wxEVT_SCROLL_THUMBRELEASE, win->GetId(), value, orient);
81 event.SetEventObject(win);
82 win->GetEventHandler()->ProcessEvent(event);
83
84 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
85 event2.SetEventObject(win);
86 win->GetEventHandler()->ProcessEvent(event2);
87 }
88}
89}
90
cb43b372 91//-----------------------------------------------------------------------------
add7cadd 92// "button_release_event" from scrollbar
cb43b372
RR
93//-----------------------------------------------------------------------------
94
865bb325 95extern "C" {
add7cadd 96static gboolean
c918b2cd 97gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win)
cb43b372 98{
add7cadd
PC
99 win->m_mouseButtonDown = false;
100 // If thumb tracking
2a23d363
RR
101 if (win->m_isScrolling)
102 {
add7cadd 103 win->m_isScrolling = false;
c918b2cd 104 // Hook up handler to send thumb release event after this emission is finished.
8ea30e36
PC
105 // To allow setting scroll position from event handler, sending event must
106 // be deferred until after the GtkRange handler for this signal has run
c918b2cd 107 g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win);
add7cadd 108 }
7c2f14ee 109
add7cadd 110 return false;
cb43b372 111}
865bb325 112}
cb43b372 113
b4071e91
RR
114//-----------------------------------------------------------------------------
115// wxScrollBar
116//-----------------------------------------------------------------------------
117
c801d85f
KB
118IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
119
add7cadd
PC
120wxScrollBar::wxScrollBar()
121{
122}
123
0a07a7d8 124wxScrollBar::~wxScrollBar()
c801d85f 125{
6de97a3b 126}
c801d85f 127
debe6624 128bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
c801d85f 129 const wxPoint& pos, const wxSize& size,
6de97a3b 130 long style, const wxValidator& validator, const wxString& name )
c801d85f 131{
91af0895 132 m_needParent = true;
a2615ebc 133
4dcaf11a
RR
134 if (!PreCreation( parent, pos, size ) ||
135 !CreateBase( parent, id, pos, size, style, validator, name ))
136 {
223d09f6 137 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
91af0895 138 return false;
4dcaf11a 139 }
6de97a3b 140
add7cadd
PC
141 const bool isVertical = (style & wxSB_VERTICAL) != 0;
142 if (isVertical)
2d17d68f 143 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
d9bd1494
SB
144 else
145 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
146
add7cadd 147 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
a2615ebc 148
add7cadd
PC
149 g_signal_connect(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);
cb43b372 155
c918b2cd
PC
156 gulong handler_id;
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);
160
f03fc89f 161 m_parent->DoAddChild( this );
a2615ebc 162
abdeb9e7 163 PostCreation(size);
a2615ebc 164
91af0895 165 return true;
6de97a3b 166}
c801d85f 167
0a07a7d8 168int wxScrollBar::GetThumbPosition() const
c801d85f 169{
add7cadd
PC
170 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
171 return int(adj->value + 0.5);
6de97a3b 172}
c801d85f
KB
173
174int wxScrollBar::GetThumbSize() const
175{
add7cadd
PC
176 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
177 return int(adj->page_size);
6de97a3b 178}
c801d85f
KB
179
180int wxScrollBar::GetPageSize() const
181{
add7cadd
PC
182 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
183 return int(adj->page_increment);
6de97a3b 184}
c801d85f
KB
185
186int wxScrollBar::GetRange() const
187{
add7cadd
PC
188 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
189 return int(adj->upper);
6de97a3b 190}
c801d85f 191
4fabb575 192void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 193{
add7cadd 194 if (GetThumbPosition() != viewStart)
2d17d68f 195 {
add7cadd 196 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
8ea30e36
PC
197 const int i = (GtkRange*)m_widget == m_scrollBar[1];
198 const int max = int(adj->upper - adj->page_size);
199 if (viewStart > max)
200 viewStart = max;
201 if (viewStart < 0)
202 viewStart = 0;
203
204 m_scrollPos[i] =
205 adj->value = viewStart;
206 // If a "value_changed" signal emission is not already in progress
207 if (!m_blockValueChanged[i])
208 {
209 gtk_adjustment_value_changed(adj);
210 }
2d17d68f 211 }
6de97a3b 212}
c801d85f 213
add7cadd 214void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
c801d85f 215{
de7bb802
PC
216 if (range == 0)
217 {
218 // GtkRange requires upper > lower
219 range =
220 thumbSize = 1;
221 }
8ea30e36
PC
222 if (position > range - thumbSize)
223 position = range - thumbSize;
224 if (position < 0)
225 position = 0;
add7cadd 226 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
add7cadd
PC
227 adj->step_increment = 1;
228 adj->page_increment = pageSize;
229 adj->page_size = thumbSize;
8ea30e36
PC
230 adj->upper = range;
231 SetThumbPosition(position);
232 gtk_adjustment_changed(adj);
6de97a3b 233}
c801d85f 234
debe6624 235void wxScrollBar::SetPageSize( int pageLength )
c801d85f 236{
add7cadd 237 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
6de97a3b 238}
c801d85f 239
add7cadd 240void wxScrollBar::SetRange(int range)
c801d85f 241{
add7cadd 242 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
6de97a3b 243}
c801d85f 244
ef5c70f9 245GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
b4071e91 246{
10bd1f7d 247 return m_widget->window;
b4071e91 248}
58614078 249
9d522606
RD
250// static
251wxVisualAttributes
252wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
253{
254 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
255}
256
de6185e2 257#endif // wxUSE_SCROLLBAR