1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/vscroll.cpp
3 // Purpose: wxVScrolledWindow implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/vscroll.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 BEGIN_EVENT_TABLE(wxVScrolledWindow
, wxPanel
)
38 EVT_SIZE(wxVScrolledWindow::OnSize
)
39 EVT_SCROLLWIN(wxVScrolledWindow::OnScroll
)
41 EVT_MOUSEWHEEL(wxVScrolledWindow::OnMouseWheel
)
46 // ============================================================================
48 // ============================================================================
50 IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow
, wxPanel
)
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 void wxVScrolledWindow::Init()
58 // we're initially empty
62 // this one should always be strictly positive
68 m_sumWheelRotation
= 0;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 wxCoord
wxVScrolledWindow::EstimateTotalHeight() const
78 // estimate the total height: it is impossible to call
79 // OnGetLineHeight() for every line because there may be too many of
80 // them, so we just make a guess using some lines in the beginning,
81 // some in the end and some in the middle
82 static const size_t NUM_LINES_TO_SAMPLE
= 10;
85 if ( m_lineMax
< 3*NUM_LINES_TO_SAMPLE
)
87 // in this case calculating exactly is faster and more correct than
89 heightTotal
= GetLinesHeight(0, m_lineMax
);
91 else // too many lines to calculate exactly
93 // look at some lines in the beginning/middle/end
95 GetLinesHeight(0, NUM_LINES_TO_SAMPLE
) +
96 GetLinesHeight(m_lineMax
- NUM_LINES_TO_SAMPLE
, m_lineMax
) +
97 GetLinesHeight(m_lineMax
/2 - NUM_LINES_TO_SAMPLE
/2,
98 m_lineMax
/2 + NUM_LINES_TO_SAMPLE
/2);
100 // use the height of the lines we looked as the average
101 heightTotal
= (wxCoord
)
102 (((float)heightTotal
/ (3*NUM_LINES_TO_SAMPLE
)) * m_lineMax
);
108 wxCoord
wxVScrolledWindow::GetLinesHeight(size_t lineMin
, size_t lineMax
) const
110 if ( lineMin
== lineMax
)
112 else if ( lineMin
> lineMax
)
113 return -GetLinesHeight(lineMax
, lineMin
);
114 //else: lineMin < lineMax
116 // let the user code know that we're going to need all these lines
117 OnGetLinesHint(lineMin
, lineMax
);
119 // do sum up their heights
121 for ( size_t line
= lineMin
; line
< lineMax
; line
++ )
123 height
+= OnGetLineHeight(line
);
129 size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast
, bool full
)
131 const wxCoord hWindow
= GetClientSize().y
;
133 // go upwards until we arrive at a line such that lineLast is not visible
134 // any more when it is shown
135 size_t lineFirst
= lineLast
;
139 h
+= OnGetLineHeight(lineFirst
);
143 // for this line to be fully visible we need to go one line
144 // down, but if it is enough for it to be only partly visible then
145 // this line will do as well
163 void wxVScrolledWindow::UpdateScrollbar()
165 // see how many lines can we fit on screen
166 const wxCoord hWindow
= GetClientSize().y
;
170 for ( line
= m_lineFirst
; line
< m_lineMax
; line
++ )
175 h
+= OnGetLineHeight(line
);
178 // if we still have remaining space below, maybe we can fit everything?
182 for ( size_t lineFirst
= m_lineFirst
; lineFirst
> 0; lineFirst
-- )
184 hAll
+= OnGetLineHeight(m_lineFirst
- 1);
185 if ( hAll
> hWindow
)
189 if ( hAll
< hWindow
)
191 // we don't need scrollbar at all
193 SetScrollbar(wxVERTICAL
, 0, 0, 0);
197 m_nVisible
= line
- m_lineFirst
;
199 int pageSize
= m_nVisible
;
202 // last line is only partially visible, we still need the scrollbar and
203 // so we have to "fix" pageSize because if it is equal to m_lineMax the
204 // scrollbar is not shown at all under MSW
208 // set the scrollbar parameters to reflect this
209 SetScrollbar(wxVERTICAL
, m_lineFirst
, pageSize
, m_lineMax
);
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 void wxVScrolledWindow::SetLineCount(size_t count
)
218 // save the number of lines
221 // and our estimate for their total height
222 m_heightTotal
= EstimateTotalHeight();
224 // recalculate the scrollbars parameters
225 m_lineFirst
= 1; // make sure it is != 0
229 void wxVScrolledWindow::RefreshLine(size_t line
)
231 // is this line visible?
232 if ( !IsVisible(line
) )
234 // no, it is useless to do anything
238 // calculate the rect occupied by this line on screen
240 rect
.width
= GetClientSize().x
;
241 rect
.height
= OnGetLineHeight(line
);
242 for ( size_t n
= GetVisibleBegin(); n
< line
; n
++ )
244 rect
.y
+= OnGetLineHeight(n
);
251 void wxVScrolledWindow::RefreshLines(size_t from
, size_t to
)
253 wxASSERT_MSG( from
<= to
, _T("RefreshLines(): empty range") );
255 // clump the range to just the visible lines -- it is useless to refresh
257 if ( from
< GetVisibleBegin() )
258 from
= GetVisibleBegin();
260 if ( to
>= GetVisibleEnd() )
261 to
= GetVisibleEnd();
265 // calculate the rect occupied by these lines on screen
267 rect
.width
= GetClientSize().x
;
268 for ( size_t nBefore
= GetVisibleBegin(); nBefore
< from
; nBefore
++ )
270 rect
.y
+= OnGetLineHeight(nBefore
);
273 for ( size_t nBetween
= from
; nBetween
< to
; nBetween
++ )
275 rect
.height
+= OnGetLineHeight(nBetween
);
282 void wxVScrolledWindow::RefreshAll()
289 bool wxVScrolledWindow::Layout()
293 // adjust the sizer dimensions/position taking into account the
294 // virtual size and scrolled position of the window.
297 GetVirtualSize(&w
, &h
);
299 // x is always 0 so no variable needed
300 int y
= -GetLinesHeight(0, GetFirstVisibleLine());
302 GetSizer()->SetDimension(0, y
, w
, h
);
306 // fall back to default for LayoutConstraints
307 return wxPanel::Layout();
310 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
312 const size_t lineMax
= GetVisibleEnd();
313 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
315 y
-= OnGetLineHeight(line
);
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
327 bool wxVScrolledWindow::ScrollToLine(size_t line
)
331 // we're empty, code below doesn't make sense in this case
335 // determine the real first line to scroll to: we shouldn't scroll beyond
337 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
338 if ( line
> lineFirstLast
)
339 line
= lineFirstLast
;
342 if ( line
== m_lineFirst
)
349 // remember the currently shown lines for the refresh code below
350 size_t lineFirstOld
= GetVisibleBegin(),
351 lineLastOld
= GetVisibleEnd();
356 // the size of scrollbar thumb could have changed
360 // finally refresh the display -- but only redraw as few lines as possible
362 if ( GetVisibleBegin() >= lineLastOld
||
363 GetVisibleEnd() <= lineFirstOld
)
365 // the simplest case: we don't have any old lines left, just redraw
369 else // overlap between the lines we showed before and should show now
371 ScrollWindow(0, GetLinesHeight(GetVisibleBegin(), lineFirstOld
));
377 bool wxVScrolledWindow::ScrollLines(int lines
)
379 lines
+= m_lineFirst
;
383 return ScrollToLine(lines
);
386 bool wxVScrolledWindow::ScrollPages(int pages
)
388 bool didSomething
= false;
395 line
= GetVisibleEnd();
402 line
= FindFirstFromBottom(GetVisibleBegin());
406 didSomething
= ScrollToLine(line
);
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
423 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
427 const wxEventType evtType
= event
.GetEventType();
429 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
433 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
435 lineFirstNew
= m_lineMax
;
437 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
439 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
441 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
443 lineFirstNew
= m_lineFirst
+ 1;
445 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
447 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
449 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
451 lineFirstNew
= GetVisibleEnd();
455 else if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
457 lineFirstNew
= event
.GetPosition();
459 else if ( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
)
461 lineFirstNew
= event
.GetPosition();
464 else // unknown scroll event?
466 wxFAIL_MSG( _T("unknown scroll event type?") );
470 ScrollToLine(lineFirstNew
);
479 void wxVScrolledWindow::OnMouseWheel(wxMouseEvent
& event
)
481 m_sumWheelRotation
+= event
.GetWheelRotation();
482 int delta
= event
.GetWheelDelta();
484 // how much to scroll this time
485 int units_to_scroll
= -(m_sumWheelRotation
/delta
);
486 if ( !units_to_scroll
)
489 m_sumWheelRotation
+= units_to_scroll
*delta
;
491 if ( !event
.IsPageScroll() )
492 ScrollLines( units_to_scroll
*event
.GetLinesPerAction() );
494 // scroll pages instead of lines
495 ScrollPages( units_to_scroll
);
498 #endif // wxUSE_MOUSEWHEEL