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 #include "wx/vscroll.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 BEGIN_EVENT_TABLE(wxVScrolledWindow
, wxPanel
)
27 EVT_SIZE(wxVScrolledWindow::OnSize
)
28 EVT_SCROLLWIN(wxVScrolledWindow::OnScroll
)
32 // ============================================================================
34 // ============================================================================
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 void wxVScrolledWindow::Init()
42 // we're initially empty
46 // this one should always be strictly positive
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 wxCoord
wxVScrolledWindow::EstimateTotalHeight() const
58 // estimate the total height: it is impossible to call
59 // OnGetLineHeight() for every line because there may be too many of
60 // them, so we just make a guess using some lines in the beginning,
61 // some in the end and some in the middle
62 static const size_t NUM_LINES_TO_SAMPLE
= 10;
65 if ( m_lineMax
< 3*NUM_LINES_TO_SAMPLE
)
67 // in this case calculating exactly is faster and more correct than
69 heightTotal
= GetLinesHeight(0, m_lineMax
);
71 else // too many lines to calculate exactly
73 // look at some lines in the beginning/middle/end
75 GetLinesHeight(0, NUM_LINES_TO_SAMPLE
) +
76 GetLinesHeight(m_lineMax
- NUM_LINES_TO_SAMPLE
, m_lineMax
) +
77 GetLinesHeight(m_lineMax
/2 - NUM_LINES_TO_SAMPLE
/2,
78 m_lineMax
/2 + NUM_LINES_TO_SAMPLE
/2);
80 // use the height of the lines we looked as the average
81 heightTotal
= (wxCoord
)
82 (((float)m_heightTotal
/ (3*NUM_LINES_TO_SAMPLE
)) * m_lineMax
);
88 wxCoord
wxVScrolledWindow::GetLinesHeight(size_t lineMin
, size_t lineMax
) const
90 if ( lineMin
== lineMax
)
92 else if ( lineMin
> lineMax
)
93 return -GetLinesHeight(lineMax
, lineMin
);
94 //else: lineMin < lineMax
96 // let the user code know that we're going to need all these lines
97 OnGetLinesHint(lineMin
, lineMax
);
99 // do sum up their heights
101 for ( size_t line
= lineMin
; line
< lineMax
; line
++ )
103 height
+= OnGetLineHeight(line
);
109 size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast
, bool full
)
111 const wxCoord hWindow
= GetClientSize().y
;
113 // go upwards until we arrive at a line such that lineLast is not visible
114 // any more when it is shown
115 size_t lineFirst
= lineLast
;
119 h
+= OnGetLineHeight(lineFirst
);
123 // for this line to be fully visible we need to go one line
124 // down, but if it is enough for it to be only partly visible then
125 // this line will do as well
143 void wxVScrolledWindow::UpdateScrollbar()
145 // see how many lines can we fit on screen
146 const wxCoord hWindow
= GetClientSize().y
;
150 for ( line
= m_lineFirst
; line
< m_lineMax
; line
++ )
155 h
+= OnGetLineHeight(line
);
158 m_nVisible
= line
- m_lineFirst
;
160 int pageSize
= m_nVisible
;
163 // last line is only partially visible, we still need the scrollbar and
164 // so we have to "fix" pageSize because if it is equal to m_lineMax the
165 // scrollbar is not shown at all under MSW
169 // set the scrollbar parameters to reflect this
170 SetScrollbar(wxVERTICAL
, m_lineFirst
, pageSize
, m_lineMax
);
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 void wxVScrolledWindow::SetLineCount(size_t count
)
179 // save the number of lines
182 // and our estimate for their total height
183 m_heightTotal
= EstimateTotalHeight();
185 // recalculate the scrollbars parameters
186 m_lineFirst
= 1; // make sure it is != 0
190 void wxVScrolledWindow::RefreshLine(size_t line
)
192 // is this line visible?
193 if ( !IsVisible(line
) )
195 // no, it is useless to do anything
199 // calculate the rect occupied by this line on screen
201 rect
.width
= GetClientSize().x
;
202 rect
.height
= OnGetLineHeight(line
);
203 for ( size_t n
= GetFirstVisibleLine(); n
< line
; n
++ )
205 rect
.y
+= OnGetLineHeight(n
);
212 void wxVScrolledWindow::RefreshLines(size_t from
, size_t to
)
214 wxASSERT_MSG( from
<= to
, _T("RefreshLines(): empty range") );
216 // clump the range to just the visible lines -- it is useless to refresh
218 if ( from
< GetFirstVisibleLine() )
219 from
= GetFirstVisibleLine();
221 if ( to
> GetLastVisibleLine() )
222 to
= GetLastVisibleLine();
224 // calculate the rect occupied by these lines on screen
226 rect
.width
= GetClientSize().x
;
227 for ( size_t nBefore
= GetFirstVisibleLine(); nBefore
< from
; nBefore
++ )
229 rect
.y
+= OnGetLineHeight(nBefore
);
232 for ( size_t nBetween
= from
; nBetween
<= to
; nBetween
++ )
234 rect
.height
+= OnGetLineHeight(nBetween
);
241 void wxVScrolledWindow::RefreshAll()
248 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
250 const size_t lineMax
= GetLastVisibleLine();
251 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
253 y
-= OnGetLineHeight(line
);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 bool wxVScrolledWindow::ScrollToLine(size_t line
)
269 // we're empty, code below doesn't make sense in this case
273 // determine the real first line to scroll to: we shouldn't scroll beyond
275 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
276 if ( line
> lineFirstLast
)
277 line
= lineFirstLast
;
280 if ( line
== m_lineFirst
)
287 // remember the currently shown lines for the refresh code below
288 size_t lineFirstOld
= GetFirstVisibleLine(),
289 lineLastOld
= GetLastVisibleLine();
294 // the size of scrollbar thumb could have changed
298 // finally refresh the display -- but only redraw as few lines as possible
300 if ( GetFirstVisibleLine() > lineLastOld
||
301 GetLastVisibleLine() < lineFirstOld
)
303 // the simplest case: we don't have any old lines left, just redraw
307 else // overlap between the lines we showed before and should show now
309 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld
));
315 bool wxVScrolledWindow::ScrollLines(int lines
)
317 lines
+= m_lineFirst
;
321 return ScrollToLine(lines
);
324 bool wxVScrolledWindow::ScrollPages(int pages
)
326 bool didSomething
= false;
333 line
= GetLastVisibleLine();
338 line
= FindFirstFromBottom(GetFirstVisibleLine());
342 didSomething
= ScrollToLine(line
);
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
352 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
359 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
363 const wxEventType evtType
= event
.GetEventType();
364 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
368 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
370 lineFirstNew
= m_lineMax
;
372 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
374 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
376 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
378 lineFirstNew
= m_lineFirst
+ 1;
380 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
382 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
384 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
386 lineFirstNew
= GetLastVisibleLine();
388 else // unknown scroll event?
390 if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
392 lineFirstNew
= event
.GetPosition();
396 wxASSERT_MSG( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
,
397 _T("unknown scroll event type?") );
399 // don't do anything, otherwise dragging the thumb around would
405 ScrollToLine(lineFirstNew
);