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