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