X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cf7d6329530d0a9f181ac24dcc722d276885f05e..4224f059750329cc68964eeb7b2633206e245e0d:/src/generic/vscroll.cpp diff --git a/src/generic/vscroll.cpp b/src/generic/vscroll.cpp index aa52fab013..ad0951f3a7 100644 --- a/src/generic/vscroll.cpp +++ b/src/generic/vscroll.cpp @@ -17,6 +17,17 @@ // headers // ---------------------------------------------------------------------------- +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) +#pragma implementation "vscroll.h" +#endif + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ +#pragma hdrstop +#endif + #include "wx/vscroll.h" // ---------------------------------------------------------------------------- @@ -33,6 +44,8 @@ END_EVENT_TABLE() // implementation // ============================================================================ +IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow, wxPanel) + // ---------------------------------------------------------------------------- // initialization // ---------------------------------------------------------------------------- @@ -53,6 +66,38 @@ void wxVScrolledWindow::Init() // various helpers // ---------------------------------------------------------------------------- +wxCoord wxVScrolledWindow::EstimateTotalHeight() const +{ + // estimate the total height: it is impossible to call + // OnGetLineHeight() for every line because there may be too many of + // them, so we just make a guess using some lines in the beginning, + // some in the end and some in the middle + static const size_t NUM_LINES_TO_SAMPLE = 10; + + wxCoord heightTotal; + if ( m_lineMax < 3*NUM_LINES_TO_SAMPLE ) + { + // in this case calculating exactly is faster and more correct than + // guessing + heightTotal = GetLinesHeight(0, m_lineMax); + } + else // too many lines to calculate exactly + { + // look at some lines in the beginning/middle/end + heightTotal = + GetLinesHeight(0, NUM_LINES_TO_SAMPLE) + + GetLinesHeight(m_lineMax - NUM_LINES_TO_SAMPLE, m_lineMax) + + GetLinesHeight(m_lineMax/2 - NUM_LINES_TO_SAMPLE/2, + m_lineMax/2 + NUM_LINES_TO_SAMPLE/2); + + // use the height of the lines we looked as the average + heightTotal = (wxCoord) + (((float)heightTotal / (3*NUM_LINES_TO_SAMPLE)) * m_lineMax); + } + + return heightTotal; +} + wxCoord wxVScrolledWindow::GetLinesHeight(size_t lineMin, size_t lineMax) const { if ( lineMin == lineMax ) @@ -74,7 +119,7 @@ wxCoord wxVScrolledWindow::GetLinesHeight(size_t lineMin, size_t lineMax) const return height; } -size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast) +size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast, bool full) { const wxCoord hWindow = GetClientSize().y; @@ -88,7 +133,13 @@ size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast) if ( h > hWindow ) { - lineFirst++; + // for this line to be fully visible we need to go one line + // down, but if it is enough for it to be only partly visible then + // this line will do as well + if ( full ) + { + lineFirst++; + } break; } @@ -141,36 +192,83 @@ void wxVScrolledWindow::SetLineCount(size_t count) // save the number of lines m_lineMax = count; + // and our estimate for their total height + m_heightTotal = EstimateTotalHeight(); - // estimate the total height: it is impossible to call - // OnGetLineHeight() for every line because there may be too many of - // them, so we just make a guess using some lines in the beginning, - // some in the end and some in the middle - static const size_t NUM_LINES_TO_SAMPLE = 10; + // recalculate the scrollbars parameters + m_lineFirst = 1; // make sure it is != 0 + ScrollToLine(0); +} - if ( count < 3*NUM_LINES_TO_SAMPLE ) +void wxVScrolledWindow::RefreshLine(size_t line) +{ + // is this line visible? + if ( !IsVisible(line) ) { - // in this case calculating exactly is faster and more correct than - // guessing - m_heightTotal = GetLinesHeight(0, m_lineMax); + // no, it is useless to do anything + return; } - else // too many lines to calculate exactly + + // calculate the rect occupied by this line on screen + wxRect rect; + rect.width = GetClientSize().x; + rect.height = OnGetLineHeight(line); + for ( size_t n = GetFirstVisibleLine(); n < line; n++ ) { - // look at some lines in the beginning/middle/end - m_heightTotal = - GetLinesHeight(0, NUM_LINES_TO_SAMPLE) + - GetLinesHeight(count - NUM_LINES_TO_SAMPLE, count) + - GetLinesHeight(count/2 - NUM_LINES_TO_SAMPLE/2, - count/2 + NUM_LINES_TO_SAMPLE/2); + rect.y += OnGetLineHeight(n); + } - // use the height of the lines we looked as the average - m_heightTotal = ((float)m_heightTotal / (3*NUM_LINES_TO_SAMPLE)) * - m_lineMax; + // do refresh it + RefreshRect(rect); +} + +void wxVScrolledWindow::RefreshLines(size_t from, size_t to) +{ + wxASSERT_MSG( from <= to, _T("RefreshLines(): empty range") ); + + // clump the range to just the visible lines -- it is useless to refresh + // the other ones + if ( from < GetFirstVisibleLine() ) + from = GetFirstVisibleLine(); + + if ( to > GetLastVisibleLine() ) + to = GetLastVisibleLine(); + + // calculate the rect occupied by these lines on screen + wxRect rect; + rect.width = GetClientSize().x; + for ( size_t nBefore = GetFirstVisibleLine(); nBefore < from; nBefore++ ) + { + rect.y += OnGetLineHeight(nBefore); } + for ( size_t nBetween = from; nBetween <= to; nBetween++ ) + { + rect.height += OnGetLineHeight(nBetween); + } - // recalculate the scrollbars parameters - ScrollToLine(0); + // do refresh it + RefreshRect(rect); +} + +void wxVScrolledWindow::RefreshAll() +{ + UpdateScrollbar(); + + Refresh(); +} + +int wxVScrolledWindow::HitTest(wxCoord WXUNUSED(x), wxCoord y) const +{ + const size_t lineMax = GetLastVisibleLine(); + for ( size_t line = GetFirstVisibleLine(); line <= lineMax; line++ ) + { + y -= OnGetLineHeight(line); + if ( y < 0 ) + return line; + } + + return wxNOT_FOUND; } // ---------------------------------------------------------------------------- @@ -187,7 +285,7 @@ bool wxVScrolledWindow::ScrollToLine(size_t line) // determine the real first line to scroll to: we shouldn't scroll beyond // the end - size_t lineFirstLast = FindFirstFromBottom(m_lineMax - 1); + size_t lineFirstLast = FindFirstFromBottom(m_lineMax - 1, true); if ( line > lineFirstLast ) line = lineFirstLast; @@ -276,6 +374,7 @@ void wxVScrolledWindow::OnScroll(wxScrollWinEvent& event) size_t lineFirstNew; const wxEventType evtType = event.GetEventType(); + if ( evtType == wxEVT_SCROLLWIN_TOP ) { lineFirstNew = 0; @@ -300,23 +399,25 @@ void wxVScrolledWindow::OnScroll(wxScrollWinEvent& event) { lineFirstNew = GetLastVisibleLine(); } + else if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE ) + { + lineFirstNew = event.GetPosition(); + } + else if ( evtType == wxEVT_SCROLLWIN_THUMBTRACK ) + { + lineFirstNew = event.GetPosition(); + } + else // unknown scroll event? { - if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE ) - { - lineFirstNew = event.GetPosition(); - } - else - { - wxASSERT_MSG( evtType == wxEVT_SCROLLWIN_THUMBTRACK, - _T("unknown scroll event type?") ); - - // don't do anything, otherwise dragging the thumb around would - // be too slow - return; - } + wxFAIL_MSG( _T("unknown scroll event type?") ); + return; } ScrollToLine(lineFirstNew); + +#ifdef __WXMAC__ + Update(); +#endif // __WXMAC__ }