]>
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 | |
9e691f46 | 121 | #ifndef __WXGTK20__ |
7c2f14ee | 122 | // There is no slider field any more |
2a23d363 | 123 | win->m_isScrolling = (gdk_event->window == widget->slider); |
9e691f46 | 124 | #endif |
a2615ebc | 125 | |
acfd422a | 126 | return FALSE; |
cb43b372 | 127 | } |
865bb325 | 128 | } |
cb43b372 RR |
129 | |
130 | //----------------------------------------------------------------------------- | |
131 | // "button_release_event" from slider | |
132 | //----------------------------------------------------------------------------- | |
133 | ||
865bb325 | 134 | extern "C" { |
a2615ebc VZ |
135 | static gint |
136 | gtk_scrollbar_button_release_callback( GtkRange *WXUNUSED(widget), | |
137 | GdkEventButton *WXUNUSED(gdk_event), | |
138 | wxScrollBar *win ) | |
cb43b372 | 139 | { |
a2615ebc VZ |
140 | if (g_isIdle) |
141 | wxapp_install_idle_handler(); | |
142 | ||
2a23d363 RR |
143 | if (win->m_isScrolling) |
144 | { | |
7d56fb8f | 145 | wxEventType command = wxEVT_SCROLL_THUMBRELEASE; |
2a23d363 | 146 | int value = (int)ceil(win->m_adjust->value); |
193e19cf | 147 | int orient = win->HasFlag(wxSB_VERTICAL) ? wxVERTICAL : wxHORIZONTAL; |
2a23d363 | 148 | |
193e19cf | 149 | wxScrollEvent event( command, win->GetId(), value, orient ); |
2a23d363 RR |
150 | event.SetEventObject( win ); |
151 | win->GetEventHandler()->ProcessEvent( event ); | |
152 | } | |
a2615ebc | 153 | |
91af0895 | 154 | win->m_isScrolling = false; |
a2615ebc | 155 | |
7c2f14ee RR |
156 | // reset the LINEUP/LINEDOWN flag when the mouse button is released |
157 | g_currentUpDownEvent = wxEVT_NULL; | |
158 | ||
2d17d68f | 159 | return FALSE; |
cb43b372 | 160 | } |
865bb325 | 161 | } |
cb43b372 | 162 | |
b4071e91 RR |
163 | //----------------------------------------------------------------------------- |
164 | // wxScrollBar | |
165 | //----------------------------------------------------------------------------- | |
166 | ||
c801d85f KB |
167 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar,wxControl) |
168 | ||
0a07a7d8 | 169 | wxScrollBar::~wxScrollBar() |
c801d85f | 170 | { |
6de97a3b | 171 | } |
c801d85f | 172 | |
debe6624 | 173 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, |
c801d85f | 174 | const wxPoint& pos, const wxSize& size, |
6de97a3b | 175 | long style, const wxValidator& validator, const wxString& name ) |
c801d85f | 176 | { |
91af0895 WS |
177 | m_needParent = true; |
178 | m_acceptsFocus = true; | |
a2615ebc | 179 | |
4dcaf11a RR |
180 | if (!PreCreation( parent, pos, size ) || |
181 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
182 | { | |
223d09f6 | 183 | wxFAIL_MSG( wxT("wxScrollBar creation failed") ); |
91af0895 | 184 | return false; |
4dcaf11a | 185 | } |
6de97a3b | 186 | |
2d17d68f | 187 | m_oldPos = 0.0; |
c801d85f | 188 | |
d9bd1494 | 189 | if ((style & wxSB_VERTICAL) == wxSB_VERTICAL) |
2d17d68f | 190 | m_widget = gtk_vscrollbar_new( (GtkAdjustment *) NULL ); |
d9bd1494 SB |
191 | else |
192 | m_widget = gtk_hscrollbar_new( (GtkAdjustment *) NULL ); | |
193 | ||
2d17d68f | 194 | m_adjust = gtk_range_get_adjustment( GTK_RANGE(m_widget) ); |
a2615ebc VZ |
195 | |
196 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
2d17d68f | 197 | "value_changed", |
a2615ebc | 198 | (GtkSignalFunc) gtk_scrollbar_callback, |
f03fc89f | 199 | (gpointer) this ); |
a2615ebc | 200 | gtk_signal_connect( GTK_OBJECT(m_widget), |
2d17d68f | 201 | "button_press_event", |
a2615ebc | 202 | (GtkSignalFunc)gtk_scrollbar_button_press_callback, |
f03fc89f | 203 | (gpointer) this ); |
a2615ebc | 204 | gtk_signal_connect( GTK_OBJECT(m_widget), |
2d17d68f | 205 | "button_release_event", |
a2615ebc | 206 | (GtkSignalFunc)gtk_scrollbar_button_release_callback, |
f03fc89f | 207 | (gpointer) this ); |
cb43b372 | 208 | |
f03fc89f | 209 | m_parent->DoAddChild( this ); |
a2615ebc | 210 | |
abdeb9e7 | 211 | PostCreation(size); |
a2615ebc | 212 | |
91af0895 | 213 | return true; |
6de97a3b | 214 | } |
c801d85f | 215 | |
0a07a7d8 | 216 | int wxScrollBar::GetThumbPosition() const |
c801d85f | 217 | { |
0a07a7d8 | 218 | double val = m_adjust->value; |
a9f21e71 | 219 | return (int)(val < 0 ? val - 0.5 : val + 0.5); |
6de97a3b | 220 | } |
c801d85f KB |
221 | |
222 | int wxScrollBar::GetThumbSize() const | |
223 | { | |
2d17d68f | 224 | return (int)(m_adjust->page_size+0.5); |
6de97a3b | 225 | } |
c801d85f KB |
226 | |
227 | int wxScrollBar::GetPageSize() const | |
228 | { | |
2d17d68f | 229 | return (int)(m_adjust->page_increment+0.5); |
6de97a3b | 230 | } |
c801d85f KB |
231 | |
232 | int wxScrollBar::GetRange() const | |
233 | { | |
2d17d68f | 234 | return (int)(m_adjust->upper+0.5); |
6de97a3b | 235 | } |
c801d85f | 236 | |
4fabb575 | 237 | void wxScrollBar::SetThumbPosition( int viewStart ) |
c801d85f | 238 | { |
2d17d68f | 239 | if (m_isScrolling) return; |
a2615ebc | 240 | |
2d17d68f RR |
241 | float fpos = (float)viewStart; |
242 | m_oldPos = fpos; | |
243 | if (fabs(fpos-m_adjust->value) < 0.2) return; | |
244 | m_adjust->value = fpos; | |
a2615ebc VZ |
245 | |
246 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust), | |
247 | (GtkSignalFunc) gtk_scrollbar_callback, | |
2a23d363 | 248 | (gpointer) this ); |
a2615ebc | 249 | |
2d17d68f | 250 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
a2615ebc VZ |
251 | |
252 | gtk_signal_connect( GTK_OBJECT(m_adjust), | |
2a23d363 | 253 | "value_changed", |
a2615ebc | 254 | (GtkSignalFunc) gtk_scrollbar_callback, |
2a23d363 | 255 | (gpointer) this ); |
6de97a3b | 256 | } |
c801d85f | 257 | |
debe6624 JS |
258 | void wxScrollBar::SetScrollbar( int position, int thumbSize, int range, int pageSize, |
259 | bool WXUNUSED(refresh) ) | |
c801d85f | 260 | { |
2d17d68f RR |
261 | float fpos = (float)position; |
262 | float frange = (float)range; | |
263 | float fthumb = (float)thumbSize; | |
264 | float fpage = (float)pageSize; | |
a2615ebc | 265 | |
2d17d68f RR |
266 | if ((fabs(frange-m_adjust->upper) < 0.2) && |
267 | (fabs(fthumb-m_adjust->page_size) < 0.2) && | |
268 | (fabs(fpage-m_adjust->page_increment) < 0.2)) | |
269 | { | |
270 | SetThumbPosition( position ); | |
271 | return; | |
272 | } | |
a2615ebc | 273 | |
2d17d68f | 274 | m_oldPos = fpos; |
a2615ebc | 275 | |
2d17d68f RR |
276 | m_adjust->lower = 0.0; |
277 | m_adjust->upper = frange; | |
278 | m_adjust->value = fpos; | |
279 | m_adjust->step_increment = 1.0; | |
280 | m_adjust->page_increment = (float)(wxMax(fpage,0)); | |
281 | m_adjust->page_size = fthumb; | |
282 | ||
283 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
6de97a3b | 284 | } |
c801d85f | 285 | |
2d17d68f | 286 | /* Backward compatibility */ |
0a07a7d8 | 287 | int wxScrollBar::GetValue() const |
c801d85f | 288 | { |
2d17d68f | 289 | return GetThumbPosition(); |
6de97a3b | 290 | } |
c801d85f | 291 | |
debe6624 | 292 | void wxScrollBar::SetValue( int viewStart ) |
c801d85f | 293 | { |
2d17d68f | 294 | SetThumbPosition( viewStart ); |
6de97a3b | 295 | } |
c801d85f KB |
296 | |
297 | void wxScrollBar::GetValues( int *viewStart, int *viewLength, int *objectLength, int *pageLength ) const | |
298 | { | |
2d17d68f RR |
299 | int pos = (int)(m_adjust->value+0.5); |
300 | int thumb = (int)(m_adjust->page_size+0.5); | |
301 | int page = (int)(m_adjust->page_increment+0.5); | |
302 | int range = (int)(m_adjust->upper+0.5); | |
a2615ebc | 303 | |
2d17d68f RR |
304 | *viewStart = pos; |
305 | *viewLength = range; | |
306 | *objectLength = thumb; | |
307 | *pageLength = page; | |
6de97a3b | 308 | } |
c801d85f KB |
309 | |
310 | int wxScrollBar::GetViewLength() const | |
311 | { | |
2d17d68f | 312 | return (int)(m_adjust->upper+0.5); |
6de97a3b | 313 | } |
c801d85f KB |
314 | |
315 | int wxScrollBar::GetObjectLength() const | |
316 | { | |
2d17d68f | 317 | return (int)(m_adjust->page_size+0.5); |
6de97a3b | 318 | } |
c801d85f | 319 | |
debe6624 | 320 | void wxScrollBar::SetPageSize( int pageLength ) |
c801d85f | 321 | { |
2d17d68f RR |
322 | int pos = (int)(m_adjust->value+0.5); |
323 | int thumb = (int)(m_adjust->page_size+0.5); | |
324 | int range = (int)(m_adjust->upper+0.5); | |
325 | SetScrollbar( pos, thumb, range, pageLength ); | |
6de97a3b | 326 | } |
c801d85f | 327 | |
debe6624 | 328 | void wxScrollBar::SetObjectLength( int objectLength ) |
c801d85f | 329 | { |
2d17d68f RR |
330 | int pos = (int)(m_adjust->value+0.5); |
331 | int page = (int)(m_adjust->page_increment+0.5); | |
332 | int range = (int)(m_adjust->upper+0.5); | |
333 | SetScrollbar( pos, objectLength, range, page ); | |
6de97a3b | 334 | } |
c801d85f | 335 | |
debe6624 | 336 | void wxScrollBar::SetViewLength( int viewLength ) |
c801d85f | 337 | { |
2d17d68f RR |
338 | int pos = (int)(m_adjust->value+0.5); |
339 | int thumb = (int)(m_adjust->page_size+0.5); | |
340 | int page = (int)(m_adjust->page_increment+0.5); | |
341 | SetScrollbar( pos, thumb, viewLength, page ); | |
6de97a3b | 342 | } |
c801d85f | 343 | |
b4071e91 RR |
344 | bool wxScrollBar::IsOwnGtkWindow( GdkWindow *window ) |
345 | { | |
2d17d68f | 346 | GtkRange *range = GTK_RANGE(m_widget); |
9e691f46 VZ |
347 | return ( (window == GTK_WIDGET(range)->window) |
348 | #ifndef __WXGTK20__ | |
349 | || (window == range->trough) | |
350 | || (window == range->slider) | |
351 | || (window == range->step_forw) | |
352 | || (window == range->step_back) | |
353 | #endif // GTK+ 1.x | |
354 | ); | |
b4071e91 | 355 | } |
58614078 | 356 | |
7f980cff VS |
357 | wxSize wxScrollBar::DoGetBestSize() const |
358 | { | |
359 | return wxControl::DoGetBestSize(); | |
360 | } | |
361 | ||
9d522606 RD |
362 | // static |
363 | wxVisualAttributes | |
364 | wxScrollBar::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
365 | { | |
366 | return GetDefaultAttributesFromGTKWidget(gtk_vscrollbar_new); | |
367 | } | |
368 | ||
dcf924a3 | 369 | #endif |