]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolbar.cpp
update frm Ivan Masar
[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{
4dcaf11a
RR
132 if (!PreCreation( parent, pos, size ) ||
133 !CreateBase( parent, id, pos, size, style, validator, name ))
134 {
223d09f6 135 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
91af0895 136 return false;
4dcaf11a 137 }
6de97a3b 138
add7cadd
PC
139 const bool isVertical = (style & wxSB_VERTICAL) != 0;
140 if (isVertical)
2d17d68f 141 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
d9bd1494
SB
142 else
143 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
144
add7cadd 145 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
a2615ebc 146
add7cadd
PC
147 g_signal_connect(m_widget, "value_changed",
148 G_CALLBACK(gtk_value_changed), this);
149 g_signal_connect(m_widget, "button_press_event",
150 G_CALLBACK(gtk_button_press_event), this);
151 g_signal_connect(m_widget, "button_release_event",
152 G_CALLBACK(gtk_button_release_event), this);
cb43b372 153
c918b2cd
PC
154 gulong handler_id;
155 handler_id = g_signal_connect(
156 m_widget, "event_after", G_CALLBACK(gtk_event_after), this);
157 g_signal_handler_block(m_widget, handler_id);
158
f03fc89f 159 m_parent->DoAddChild( this );
a2615ebc 160
abdeb9e7 161 PostCreation(size);
a2615ebc 162
91af0895 163 return true;
6de97a3b 164}
c801d85f 165
0a07a7d8 166int wxScrollBar::GetThumbPosition() const
c801d85f 167{
add7cadd
PC
168 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
169 return int(adj->value + 0.5);
6de97a3b 170}
c801d85f
KB
171
172int wxScrollBar::GetThumbSize() const
173{
add7cadd
PC
174 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
175 return int(adj->page_size);
6de97a3b 176}
c801d85f
KB
177
178int wxScrollBar::GetPageSize() const
179{
add7cadd
PC
180 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
181 return int(adj->page_increment);
6de97a3b 182}
c801d85f
KB
183
184int wxScrollBar::GetRange() const
185{
add7cadd
PC
186 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
187 return int(adj->upper);
6de97a3b 188}
c801d85f 189
4fabb575 190void wxScrollBar::SetThumbPosition( int viewStart )
c801d85f 191{
add7cadd 192 if (GetThumbPosition() != viewStart)
2d17d68f 193 {
add7cadd 194 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
8ea30e36
PC
195 const int i = (GtkRange*)m_widget == m_scrollBar[1];
196 const int max = int(adj->upper - adj->page_size);
197 if (viewStart > max)
198 viewStart = max;
199 if (viewStart < 0)
200 viewStart = 0;
201
202 m_scrollPos[i] =
203 adj->value = viewStart;
204 // If a "value_changed" signal emission is not already in progress
205 if (!m_blockValueChanged[i])
206 {
207 gtk_adjustment_value_changed(adj);
208 }
2d17d68f 209 }
6de97a3b 210}
c801d85f 211
add7cadd 212void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
c801d85f 213{
de7bb802
PC
214 if (range == 0)
215 {
216 // GtkRange requires upper > lower
217 range =
218 thumbSize = 1;
219 }
8ea30e36
PC
220 if (position > range - thumbSize)
221 position = range - thumbSize;
222 if (position < 0)
223 position = 0;
add7cadd 224 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
add7cadd
PC
225 adj->step_increment = 1;
226 adj->page_increment = pageSize;
227 adj->page_size = thumbSize;
8ea30e36
PC
228 adj->upper = range;
229 SetThumbPosition(position);
230 gtk_adjustment_changed(adj);
6de97a3b 231}
c801d85f 232
debe6624 233void wxScrollBar::SetPageSize( int pageLength )
c801d85f 234{
add7cadd 235 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
6de97a3b 236}
c801d85f 237
add7cadd 238void wxScrollBar::SetRange(int range)
c801d85f 239{
add7cadd 240 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
6de97a3b 241}
c801d85f 242
ef5c70f9 243GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
b4071e91 244{
10bd1f7d 245 return m_widget->window;
b4071e91 246}
58614078 247
9d522606
RD
248// static
249wxVisualAttributes
250wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
251{
252 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
253}
254
de6185e2 255#endif // wxUSE_SCROLLBAR