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