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