]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/gtk/scrolbar.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
dcf924a3 RR |
12 | |
13 | #if wxUSE_SCROLLBAR | |
14 | ||
1e6feb95 VZ |
15 | #include "wx/scrolbar.h" |
16 | ||
c801d85f | 17 | #include "wx/utils.h" |
463c4d71 | 18 | #include "wx/math.h" |
9e691f46 | 19 | #include "wx/gtk/private.h" |
83624f79 | 20 | |
acfd422a RR |
21 | //----------------------------------------------------------------------------- |
22 | // idle system | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern void wxapp_install_idle_handler(); | |
26 | extern bool g_isIdle; | |
27 | ||
66bd6b93 RR |
28 | //----------------------------------------------------------------------------- |
29 | // data | |
30 | //----------------------------------------------------------------------------- | |
31 | ||
32 | extern bool g_blockEventsOnDrag; | |
7c2f14ee | 33 | static wxEventType g_currentUpDownEvent = wxEVT_NULL; |
66bd6b93 | 34 | |
aed8ac3f RR |
35 | static const float sensitivity = 0.02; |
36 | ||
c801d85f | 37 | //----------------------------------------------------------------------------- |
b4071e91 | 38 | // "value_changed" |
c801d85f KB |
39 | //----------------------------------------------------------------------------- |
40 | ||
9e691f46 VZ |
41 | // FIXME: is GtkScrollType really passed to us as 2nd argument? |
42 | ||
865bb325 | 43 | extern "C" { |
9e691f46 VZ |
44 | static void gtk_scrollbar_callback( GtkAdjustment *adjust, |
45 | SCROLLBAR_CBACK_ARG | |
46 | wxScrollBar *win ) | |
a2615ebc | 47 | { |
acfd422a RR |
48 | if (g_isIdle) wxapp_install_idle_handler(); |
49 | ||
a2053b27 | 50 | if (!win->m_hasVMT) return; |
2d17d68f | 51 | if (g_blockEventsOnDrag) return; |
a2615ebc | 52 | |
e1811a01 | 53 | float diff = adjust->value - win->m_oldPos; |
aed8ac3f | 54 | if (fabs(diff) < sensitivity) return; |
a2615ebc | 55 | |
e1811a01 RR |
56 | win->m_oldPos = adjust->value; |
57 | ||
9e691f46 | 58 | wxEventType command = GtkScrollTypeToWx(GET_SCROLL_TYPE(win->m_widget)); |
e1811a01 | 59 | |
0a07a7d8 | 60 | double dvalue = adjust->value; |
a9f21e71 | 61 | int value = (int)(dvalue < 0 ? dvalue - 0.5 : dvalue + 0.5); |
a2615ebc | 62 | |
f03fc89f | 63 | int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; |
a2615ebc | 64 | |
7c2f14ee RR |
65 | // throw a LINEUP / LINEDOWN event if necessary |
66 | if (g_currentUpDownEvent != wxEVT_NULL) | |
67 | { | |
91af0895 | 68 | wxScrollEvent event( g_currentUpDownEvent, win->GetId(), value, orient ); |
7c2f14ee RR |
69 | event.SetEventObject( win ); |
70 | win->GetEventHandler()->ProcessEvent( event ); | |
91af0895 WS |
71 | } |
72 | ||
73 | // throw other event (wxEVT_SCROLL_THUMBTRACK) | |
74 | wxScrollEvent event( command, win->GetId(), value, orient ); | |
1b24b2b4 RR |
75 | event.SetEventObject( win ); |
76 | win->GetEventHandler()->ProcessEvent( event ); | |
7c2f14ee | 77 | |
c801d85f | 78 | /* |
2d17d68f RR |
79 | wxCommandEvent cevent( wxEVT_COMMAND_SCROLLBAR_UPDATED, win->GetId() ); |
80 | cevent.SetEventObject( win ); | |
81 | win->ProcessEvent( cevent ); | |
c801d85f | 82 | */ |
6de97a3b | 83 | } |
865bb325 | 84 | } |
c801d85f | 85 | |
cb43b372 RR |
86 | //----------------------------------------------------------------------------- |
87 | // "button_press_event" from slider | |
88 | //----------------------------------------------------------------------------- | |
865bb325 | 89 | extern "C" { |
a2615ebc VZ |
90 | static gint gtk_scrollbar_button_press_callback( GtkRange *widget, |
91 | GdkEventButton *gdk_event, | |
f03fc89f | 92 | wxScrollBar *win ) |
cb43b372 | 93 | { |
acfd422a RR |
94 | if (g_isIdle) wxapp_install_idle_handler(); |
95 | ||
7c2f14ee RR |
96 | // check if a LINEUP/LINEDOWN event must be thrown |
97 | // I suppose here the size of scrollbar top/bottom buttons is 16px height | |
98 | if (gdk_event->type == GDK_BUTTON_PRESS && gdk_event->button == 1) | |
99 | { | |
100 | int scroll_height, mouse_pos; | |
101 | ||
91af0895 WS |
102 | // get the mouse position when the click is done |
103 | if (win->HasFlag(wxSB_VERTICAL)) | |
104 | { | |
7c2f14ee RR |
105 | scroll_height = GTK_WIDGET(widget)->allocation.height - 16; |
106 | mouse_pos = (int)gdk_event->y; | |
107 | } | |
108 | else | |
109 | { | |
110 | scroll_height = GTK_WIDGET(widget)->allocation.width - 16; | |
111 | mouse_pos = (int)gdk_event->x; | |
112 | } | |
91af0895 | 113 | |
7c2f14ee RR |
114 | // compare mouse position to scrollbar height |
115 | if (mouse_pos > scroll_height) | |
116 | g_currentUpDownEvent = wxEVT_SCROLL_LINEDOWN; | |
117 | else if (mouse_pos < 16) | |
118 | g_currentUpDownEvent = wxEVT_SCROLL_LINEUP; | |
119 | } | |
2a23d363 | 120 | |
acfd422a | 121 | return FALSE; |
cb43b372 | 122 | } |
865bb325 | 123 | } |
cb43b372 RR |
124 | |
125 | //----------------------------------------------------------------------------- | |
126 | // "button_release_event" from slider | |
127 | //----------------------------------------------------------------------------- | |
128 | ||
865bb325 | 129 | extern "C" { |
a2615ebc VZ |
130 | static gint |
131 | gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget), | |
132 | GdkEventButton *WXUNUSED(gdk_event), | |
133 | wxScrollBar *win ) | |
cb43b372 | 134 | { |
a2615ebc VZ |
135 | if (g_isIdle) |
136 | wxapp_install_idle_handler(); | |
137 | ||
2a23d363 RR |
138 | if (win->m_isScrolling) |
139 | { | |
7d56fb8f | 140 | wxEventType command = wxEVT_SCROLL_THUMBRELEASE; |
2a23d363 | 141 | int value = (int)ceil(win->m_adjust->value); |
193e19cf | 142 | int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; |
2a23d363 | 143 | |
193e19cf | 144 | wxScrollEvent event( command, win->GetId(), value, orient ); |
2a23d363 RR |
145 | event.SetEventObject( win ); |
146 | win->GetEventHandler()->ProcessEvent( event ); | |
147 | } | |
a2615ebc | 148 | |
91af0895 | 149 | win->m_isScrolling = false; |
a2615ebc | 150 | |
7c2f14ee RR |
151 | // reset the LINEUP/LINEDOWN flag when the mouse button is released |
152 | g_currentUpDownEvent = wxEVT_NULL; | |
153 | ||
2d17d68f | 154 | return FALSE; |
cb43b372 | 155 | } |
865bb325 | 156 | } |
cb43b372 | 157 | |
b4071e91 RR |
158 | //----------------------------------------------------------------------------- |
159 | // wxScrollBar | |
160 | //----------------------------------------------------------------------------- | |
161 | ||
c801d85f KB |
162 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) |
163 | ||
0a07a7d8 | 164 | wxScrollBar::~wxScrollBar() |
c801d85f | 165 | { |
6de97a3b | 166 | } |
c801d85f | 167 | |
debe6624 | 168 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, |
c801d85f | 169 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 170 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 171 | { |
91af0895 WS |
172 | m_needParent = true; |
173 | m_acceptsFocus = true; | |
a2615ebc | 174 | |
4dcaf11a RR |
175 | if (!PreCreation( parent, pos, size ) || |
176 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
177 | { | |
223d09f6 | 178 | wxFAIL_MSG( wxT("wxScrollBar creation failed") ); |
91af0895 | 179 | return false; |
4dcaf11a | 180 | } |
6de97a3b | 181 | |
2d17d68f | 182 | m_oldPos = 0.0; |
c801d85f | 183 | |
d9bd1494 | 184 | if ((style & wxSB_VERTICAL) == wxSB_VERTICAL) |
2d17d68f | 185 | m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL ); |
d9bd1494 SB |
186 | else |
187 | m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL ); | |
188 | ||
2d17d68f | 189 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
a2615ebc | 190 | |
9fa72bd2 MR |
191 | g_signal_connect (m_adjust, "value_changed", |
192 | G_CALLBACK (gtk_scrollbar_callback), this); | |
193 | g_signal_connect (m_widget, "button_press_event", | |
194 | G_CALLBACK (gtk_scrollbar_button_press_callback), | |
195 | this); | |
196 | g_signal_connect (m_widget, "button_release_event", | |
197 | G_CALLBACK (gtk_scrollbar_button_release_callback), | |
198 | this); | |
cb43b372 | 199 | |
f03fc89f | 200 | m_parent->DoAddChild( this ); |
a2615ebc | 201 | |
abdeb9e7 | 202 | PostCreation(size); |
a2615ebc | 203 | |
91af0895 | 204 | return true; |
6de97a3b | 205 | } |
c801d85f | 206 | |
0a07a7d8 | 207 | int wxScrollBar::GetThumbPosition() const |
c801d85f | 208 | { |
0a07a7d8 | 209 | double val = m_adjust->value; |
a9f21e71 | 210 | return (int)(val < 0 ? val - 0.5 : val + 0.5); |
6de97a3b | 211 | } |
c801d85f KB |
212 | |
213 | int wxScrollBar::GetThumbSize() const | |
214 | { | |
2d17d68f | 215 | return (int)(m_adjust->page_size+0.5); |
6de97a3b | 216 | } |
c801d85f KB |
217 | |
218 | int wxScrollBar::GetPageSize() const | |
219 | { | |
2d17d68f | 220 | return (int)(m_adjust->page_increment+0.5); |
6de97a3b | 221 | } |
c801d85f KB |
222 | |
223 | int wxScrollBar::GetRange() const | |
224 | { | |
2d17d68f | 225 | return (int)(m_adjust->upper+0.5); |
6de97a3b | 226 | } |
c801d85f | 227 | |
4fabb575 | 228 | void wxScrollBar::SetThumbPosition( int viewStart ) |
c801d85f | 229 | { |
2d17d68f | 230 | if (m_isScrolling) return; |
a2615ebc | 231 | |
2d17d68f RR |
232 | float fpos = (float)viewStart; |
233 | m_oldPos = fpos; | |
234 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
235 | m_adjust->value = fpos; | |
a2615ebc | 236 | |
9fa72bd2 MR |
237 | g_signal_handlers_disconnect_by_func (m_adjust, |
238 | (gpointer) gtk_scrollbar_callback, | |
239 | this); | |
a2615ebc | 240 | |
9fa72bd2 | 241 | g_signal_emit_by_name (m_adjust, "value_changed"); |
a2615ebc | 242 | |
9fa72bd2 MR |
243 | g_signal_connect (m_adjust, "value_changed", |
244 | G_CALLBACK (gtk_scrollbar_callback), this); | |
6de97a3b | 245 | } |
c801d85f | 246 | |
debe6624 JS |
247 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, |
248 | bool WXUNUSED(refresh) ) | |
c801d85f | 249 | { |
2d17d68f RR |
250 | float fpos = (float)position; |
251 | float frange = (float)range; | |
252 | float fthumb = (float)thumbSize; | |
253 | float fpage = (float)pageSize; | |
a2615ebc | 254 | |
2d17d68f RR |
255 | if ((fabs(frange-m_adjust->upper) < 0.2) && |
256 | (fabs(fthumb-m_adjust->page_size) < 0.2) && | |
257 | (fabs(fpage-m_adjust->page_increment) < 0.2)) | |
258 | { | |
259 | SetThumbPosition( position ); | |
260 | return; | |
261 | } | |
a2615ebc | 262 | |
2d17d68f | 263 | m_oldPos = fpos; |
a2615ebc | 264 | |
2d17d68f RR |
265 | m_adjust->lower = 0.0; |
266 | m_adjust->upper = frange; | |
267 | m_adjust->value = fpos; | |
268 | m_adjust->step_increment = 1.0; | |
269 | m_adjust->page_increment = (float)(wxMax(fpage,0)); | |
270 | m_adjust->page_size = fthumb; | |
271 | ||
9fa72bd2 | 272 | g_signal_emit_by_name (m_adjust, "changed"); |
6de97a3b | 273 | } |
c801d85f | 274 | |
2d17d68f | 275 | /* Backward compatibility */ |
0a07a7d8 | 276 | int wxScrollBar::GetValue() const |
c801d85f | 277 | { |
2d17d68f | 278 | return GetThumbPosition(); |
6de97a3b | 279 | } |
c801d85f | 280 | |
debe6624 | 281 | void wxScrollBar::SetValue( int viewStart ) |
c801d85f | 282 | { |
2d17d68f | 283 | SetThumbPosition( viewStart ); |
6de97a3b | 284 | } |
c801d85f KB |
285 | |
286 | void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const | |
287 | { | |
2d17d68f RR |
288 | int pos = (int)(m_adjust->value+0.5); |
289 | int thumb = (int)(m_adjust->page_size+0.5); | |
290 | int page = (int)(m_adjust->page_increment+0.5); | |
291 | int range = (int)(m_adjust->upper+0.5); | |
a2615ebc | 292 | |
2d17d68f RR |
293 | *viewStart = pos; |
294 | *viewLength = range; | |
295 | *objectLength = thumb; | |
296 | *pageLength = page; | |
6de97a3b | 297 | } |
c801d85f KB |
298 | |
299 | int wxScrollBar::GetViewLength() const | |
300 | { | |
2d17d68f | 301 | return (int)(m_adjust->upper+0.5); |
6de97a3b | 302 | } |
c801d85f KB |
303 | |
304 | int wxScrollBar::GetObjectLength() const | |
305 | { | |
2d17d68f | 306 | return (int)(m_adjust->page_size+0.5); |
6de97a3b | 307 | } |
c801d85f | 308 | |
debe6624 | 309 | void wxScrollBar::SetPageSize( int pageLength ) |
c801d85f | 310 | { |
2d17d68f RR |
311 | int pos = (int)(m_adjust->value+0.5); |
312 | int thumb = (int)(m_adjust->page_size+0.5); | |
313 | int range = (int)(m_adjust->upper+0.5); | |
314 | SetScrollbar( pos, thumb, range, pageLength ); | |
6de97a3b | 315 | } |
c801d85f | 316 | |
debe6624 | 317 | void wxScrollBar::SetObjectLength( int objectLength ) |
c801d85f | 318 | { |
2d17d68f RR |
319 | int pos = (int)(m_adjust->value+0.5); |
320 | int page = (int)(m_adjust->page_increment+0.5); | |
321 | int range = (int)(m_adjust->upper+0.5); | |
322 | SetScrollbar( pos, objectLength, range, page ); | |
6de97a3b | 323 | } |
c801d85f | 324 | |
debe6624 | 325 | void wxScrollBar::SetViewLength( int viewLength ) |
c801d85f | 326 | { |
2d17d68f RR |
327 | int pos = (int)(m_adjust->value+0.5); |
328 | int thumb = (int)(m_adjust->page_size+0.5); | |
329 | int page = (int)(m_adjust->page_increment+0.5); | |
330 | SetScrollbar( pos, thumb, viewLength, page ); | |
6de97a3b | 331 | } |
c801d85f | 332 | |
b4071e91 RR |
333 | bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window ) |
334 | { | |
2d17d68f | 335 | GtkRange *range = GTK_RANGE(m_widget); |
68567a96 | 336 | return ( (window == GTK_WIDGET(range)->window) ); |
b4071e91 | 337 | } |
58614078 | 338 | |
7f980cff VS |
339 | wxSize wxScrollBar::DoGetBestSize() const |
340 | { | |
341 | return wxControl::DoGetBestSize(); | |
342 | } | |
343 | ||
9d522606 RD |
344 | // static |
345 | wxVisualAttributes | |
346 | wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
347 | { | |
348 | return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new); | |
349 | } | |
350 | ||
dcf924a3 | 351 | #endif |