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