]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolbar.cpp
Compilation
[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 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 }
45 }
46 }
47 }
48
49 //-----------------------------------------------------------------------------
50 // "button_press_event" from scrollbar
51 //-----------------------------------------------------------------------------
52
53 extern "C" {
54 static gboolean
55 gtk_button_press_event(GtkRange*, GdkEventButton*, wxScrollBar* win)
56 {
57 win->m_mouseButtonDown = true;
58 return false;
59 }
60 }
61
62 //-----------------------------------------------------------------------------
63 // "event_after" from scrollbar
64 //-----------------------------------------------------------------------------
65
66 extern "C" {
67 static void
68 gtk_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
88 //-----------------------------------------------------------------------------
89 // "button_release_event" from scrollbar
90 //-----------------------------------------------------------------------------
91
92 extern "C" {
93 static gboolean
94 gtk_button_release_event(GtkRange* range, GdkEventButton*, wxScrollBar* win)
95 {
96 win->m_mouseButtonDown = false;
97 // If thumb tracking
98 if (win->m_isScrolling)
99 {
100 win->m_isScrolling = false;
101 // Hook up handler to send thumb release event after this emission is finished.
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
104 g_signal_handlers_unblock_by_func(range, (void*)gtk_event_after, win);
105 }
106
107 return false;
108 }
109 }
110
111 //-----------------------------------------------------------------------------
112 // wxScrollBar
113 //-----------------------------------------------------------------------------
114
115 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl)
116
117 wxScrollBar::wxScrollBar()
118 {
119 }
120
121 wxScrollBar::~wxScrollBar()
122 {
123 }
124
125 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
126 const wxPoint& pos, const wxSize& size,
127 long style, const wxValidator& validator, const wxString& name )
128 {
129 if (!PreCreation( parent, pos, size ) ||
130 !CreateBase( parent, id, pos, size, style, validator, name ))
131 {
132 wxFAIL_MSG( wxT("wxScrollBar creation failed") );
133 return false;
134 }
135
136 const bool isVertical = (style & wxSB_VERTICAL) != 0;
137 if (isVertical)
138 m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL );
139 else
140 m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL );
141
142 m_scrollBar[int(isVertical)] = (GtkRange*)m_widget;
143
144 g_signal_connect_after(m_widget, "value_changed",
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);
150
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
156 m_parent->DoAddChild( this );
157
158 PostCreation(size);
159
160 return true;
161 }
162
163 int wxScrollBar::GetThumbPosition() const
164 {
165 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
166 return int(adj->value + 0.5);
167 }
168
169 int wxScrollBar::GetThumbSize() const
170 {
171 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
172 return int(adj->page_size);
173 }
174
175 int wxScrollBar::GetPageSize() const
176 {
177 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
178 return int(adj->page_increment);
179 }
180
181 int wxScrollBar::GetRange() const
182 {
183 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
184 return int(adj->upper);
185 }
186
187 void wxScrollBar::SetThumbPosition( int viewStart )
188 {
189 if (GetThumbPosition() != viewStart)
190 {
191 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
192 const int i = (GtkRange*)m_widget == m_scrollBar[1];
193 const int max = int(adj->upper - adj->page_size);
194 if (viewStart > max)
195 viewStart = max;
196 if (viewStart < 0)
197 viewStart = 0;
198
199 m_scrollPos[i] =
200 adj->value = viewStart;
201
202 g_signal_handlers_block_by_func(m_widget,
203 (gpointer)gtk_value_changed, this);
204
205 gtk_adjustment_value_changed(adj);
206
207 g_signal_handlers_unblock_by_func(m_widget,
208 (gpointer)gtk_value_changed, this);
209 }
210 }
211
212 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, bool)
213 {
214 if (range == 0)
215 {
216 // GtkRange requires upper > lower
217 range =
218 thumbSize = 1;
219 }
220 if (position > range - thumbSize)
221 position = range - thumbSize;
222 if (position < 0)
223 position = 0;
224 GtkAdjustment* adj = ((GtkRange*)m_widget)->adjustment;
225 adj->step_increment = 1;
226 adj->page_increment = pageSize;
227 adj->page_size = thumbSize;
228 adj->upper = range;
229 SetThumbPosition(position);
230 gtk_adjustment_changed(adj);
231 }
232
233 void wxScrollBar::SetPageSize( int pageLength )
234 {
235 SetScrollbar(GetThumbPosition(), GetThumbSize(), GetRange(), pageLength);
236 }
237
238 void wxScrollBar::SetRange(int range)
239 {
240 SetScrollbar(GetThumbPosition(), GetThumbSize(), range, GetPageSize());
241 }
242
243 GdkWindow *wxScrollBar::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
244 {
245 return m_widget->window;
246 }
247
248 // static
249 wxVisualAttributes
250 wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
251 {
252 return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new);
253 }
254
255 #endif // wxUSE_SCROLLBAR