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