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