]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
free old resources in DoLoad(), apparently fixes memory leaks with gstreamer 0.10...
[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 // don't need to install idle handler, its done from "event" signal
61
62 win->m_mouseButtonDown = true;
63 return false;
64 }
65 }
66
67 //-----------------------------------------------------------------------------
68 // "event_after" from scrollbar
69 //-----------------------------------------------------------------------------
70
71 extern "C" {
72 static void
73 gtk_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
93 //-----------------------------------------------------------------------------
94 // "button_release_event" from scrollbar
95 //-----------------------------------------------------------------------------
96
97 extern "C" {
98 static gboolean
99 gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win)
100 {
101 // don't need to install idle handler, its done from "event" signal
102
103 win->m_mouseButtonDown = false;
104 // If thumb tracking
105 if (win->m_isScrolling)
106 {
107 win->m_isScrolling = false;
108 // Hook up handler to send thumb release event after this emission is finished.
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
111 g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win);
112 }
113
114 return false;
115 }
116 }
117
118 //-----------------------------------------------------------------------------
119 // wxScrollBar
120 //-----------------------------------------------------------------------------
121
122 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
123
124 wxScrollBar::wxScrollBar()
125 {
126 }
127
128 wxScrollBar::~wxScrollBar()
129 {
130 }
131
132 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
133 const wxPoint& pos, const wxSize& size,
134 long style, const wxValidator& validator, const wxString& name )
135 {
136 m_needParent = true;
137 m_acceptsFocus = true;
138
139 if (!PreCreation( parent, pos, size ) ||
140 !CreateBase( parent, id, pos, size, style, validator, name ))
141 {
142 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
143 return false;
144 }
145
146 const bool isVertical = (style & wxSB_VERTICAL) != 0;
147 if (isVertical)
148 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
149 else
150 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
151
152 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
153
154 g_signal_connect(m_widget, "value_changed",
155 G_CALLBACK(gtk_value_changed), this);
156 g_signal_connect(m_widget, "button_press_event",
157 G_CALLBACK(gtk_button_press_event), this);
158 g_signal_connect(m_widget, "button_release_event",
159 G_CALLBACK(gtk_button_release_event), this);
160
161 gulong handler_id;
162 handler_id = g_signal_connect(
163 m_widget, "event_after", G_CALLBACK(gtk_event_after), this);
164 g_signal_handler_block(m_widget, handler_id);
165
166 m_parent->DoAddChild( this );
167
168 PostCreation(size);
169
170 return true;
171 }
172
173 int wxScrollBar::GetThumbPosition() const
174 {
175 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
176 return int(adj->value + 0.5);
177 }
178
179 int wxScrollBar::GetThumbSize() const
180 {
181 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
182 return int(adj->page_size);
183 }
184
185 int wxScrollBar::GetPageSize() const
186 {
187 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
188 return int(adj->page_increment);
189 }
190
191 int wxScrollBar::GetRange() const
192 {
193 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
194 return int(adj->upper);
195 }
196
197 void wxScrollBar::SetThumbPosition( int viewStart )
198 {
199 if (GetThumbPosition() != viewStart)
200 {
201 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
202 const int i = (GtkRange*)m_widget == m_scrollBar[1];
203 const int max = int(adj->upper - adj->page_size);
204 if (viewStart > max)
205 viewStart = max;
206 if (viewStart < 0)
207 viewStart = 0;
208
209 m_scrollPos[i] =
210 adj->value = viewStart;
211 // If a "value_changed" signal emission is not already in progress
212 if (!m_blockValueChanged[i])
213 {
214 gtk_adjustment_value_changed(adj);
215 }
216 }
217 }
218
219 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
220 {
221 if (range == 0)
222 {
223 // GtkRange requires upper > lower
224 range =
225 thumbSize = 1;
226 }
227 if (position > range - thumbSize)
228 position = range - thumbSize;
229 if (position < 0)
230 position = 0;
231 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
232 adj->step_increment = 1;
233 adj->page_increment = pageSize;
234 adj->page_size = thumbSize;
235 adj->upper = range;
236 SetThumbPosition(position);
237 gtk_adjustment_changed(adj);
238 }
239
240 void wxScrollBar::SetPageSize( int pageLength )
241 {
242 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
243 }
244
245 void wxScrollBar::SetRange(int range)
246 {
247 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
248 }
249
250 GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
251 {
252 return GTK_WIDGET(GTK_RANGE(m_widget))->window;
253 }
254
255 // static
256 wxVisualAttributes
257 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
258 {
259 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
260 }
261
262 #endif // wxUSE_SCROLLBAR