]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
don't generate a scroll event when setting scroll position from scroll event handler
[wxWidgets.git] / src / gtk / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/scrolbar.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SCROLLBAR
14
15 #include "wx/scrolbar.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #endif
20
21 #include "wx/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // "value_changed" from scrollbar
25 //-----------------------------------------------------------------------------
26
27 extern "C" {
28 static void
29 gtk_value_changed(GtkRange* range, wxScrollBar* win)
30 {
31 wxEventType eventType = win->GetScrollEventType(range);
32 if (eventType != wxEVT_NULL)
33 {
34 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
35 const int i = orient == wxVERTICAL;
36 const int value = win->GetThumbPosition();
37 wxScrollEvent event(eventType, win->GetId(), value, orient);
38 event.SetEventObject(win);
39 win->m_blockValueChanged[i] = true;
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 }
47 win->m_blockValueChanged[i] = false;
48 }
49 }
50 }
51
52 //-----------------------------------------------------------------------------
53 // "button_press_event" from scrollbar
54 //-----------------------------------------------------------------------------
55
56 extern "C" {
57 static gboolean
58 gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
59 {
60 if (g_isIdle) wxapp_install_idle_handler();
61
62 win->m_mouseButtonDown = true;
63 return false;
64 }
65 }
66
67 //-----------------------------------------------------------------------------
68 // "button_release_event" from scrollbar
69 //-----------------------------------------------------------------------------
70
71 extern "C" {
72 static gboolean
73 gtk_button_release_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
74 {
75 if (g_isIdle)
76 wxapp_install_idle_handler();
77
78 win->m_mouseButtonDown = false;
79 // If thumb tracking
80 if (win->m_isScrolling)
81 {
82 win->m_isScrolling = false;
83 const int value = win->GetThumbPosition();
84 const int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL;
85
86 wxScrollEvent event(wxEVT_SCROLL_THUMBRELEASE, win->GetId(), value, orient);
87 event.SetEventObject(win);
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);
91
92 wxScrollEvent event2(wxEVT_SCROLL_CHANGED, win->GetId(), value, orient);
93 event2.SetEventObject(win);
94 win->GetEventHandler()->AddPendingEvent(event2);
95 }
96
97 return false;
98 }
99 }
100
101 //-----------------------------------------------------------------------------
102 // wxScrollBar
103 //-----------------------------------------------------------------------------
104
105 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
106
107 wxScrollBar::wxScrollBar()
108 {
109 }
110
111 wxScrollBar::~wxScrollBar()
112 {
113 }
114
115 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
116 const wxPoint& pos, const wxSize& size,
117 long style, const wxValidator& validator, const wxString& name )
118 {
119 m_needParent = true;
120 m_acceptsFocus = true;
121
122 if (!PreCreation( parent, pos, size ) ||
123 !CreateBase( parent, id, pos, size, style, validator, name ))
124 {
125 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
126 return false;
127 }
128
129 const bool isVertical = (style & wxSB_VERTICAL) != 0;
130 if (isVertical)
131 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
132 else
133 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
134
135 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
136
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);
143
144 m_parent->DoAddChild( this );
145
146 PostCreation(size);
147
148 return true;
149 }
150
151 int wxScrollBar::GetThumbPosition() const
152 {
153 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
154 return int(adj->value + 0.5);
155 }
156
157 int wxScrollBar::GetThumbSize() const
158 {
159 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
160 return int(adj->page_size);
161 }
162
163 int wxScrollBar::GetPageSize() const
164 {
165 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
166 return int(adj->page_increment);
167 }
168
169 int wxScrollBar::GetRange() const
170 {
171 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
172 return int(adj->upper);
173 }
174
175 void wxScrollBar::SetThumbPosition( int viewStart )
176 {
177 if (GetThumbPosition() != viewStart)
178 {
179 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
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 }
194 }
195 }
196
197 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
198 {
199 if (range == 0)
200 {
201 // GtkRange requires upper > lower
202 range =
203 thumbSize = 1;
204 }
205 if (position > range - thumbSize)
206 position = range - thumbSize;
207 if (position < 0)
208 position = 0;
209 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
210 adj->step_increment = 1;
211 adj->page_increment = pageSize;
212 adj->page_size = thumbSize;
213 adj->upper = range;
214 SetThumbPosition(position);
215 gtk_adjustment_changed(adj);
216 }
217
218 void wxScrollBar::SetPageSize( int pageLength )
219 {
220 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
221 }
222
223 void wxScrollBar::SetRange(int range)
224 {
225 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
226 }
227
228 bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window )
229 {
230 GtkRange *range = GTK_RANGE(m_widget);
231 return ( (window == GTK_WIDGET(range)->window) );
232 }
233
234 // static
235 wxVisualAttributes
236 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
237 {
238 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
239 }
240
241 #endif // wxUSE_SCROLLBAR