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