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