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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "vscroll.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #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
)
43 // ============================================================================
45 // ============================================================================
47 IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow
, wxPanel
)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 void wxVScrolledWindow::Init()
55 // we're initially empty
59 // this one should always be strictly positive
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 wxCoord
wxVScrolledWindow::EstimateTotalHeight() const
71 // estimate the total height: it is impossible to call
72 // OnGetLineHeight() for every line because there may be too many of
73 // them, so we just make a guess using some lines in the beginning,
74 // some in the end and some in the middle
75 static const size_t NUM_LINES_TO_SAMPLE
= 10;
78 if ( m_lineMax
< 3*NUM_LINES_TO_SAMPLE
)
80 // in this case calculating exactly is faster and more correct than
82 heightTotal
= GetLinesHeight(0, m_lineMax
);
84 else // too many lines to calculate exactly
86 // look at some lines in the beginning/middle/end
88 GetLinesHeight(0, NUM_LINES_TO_SAMPLE
) +
89 GetLinesHeight(m_lineMax
- NUM_LINES_TO_SAMPLE
, m_lineMax
) +
90 GetLinesHeight(m_lineMax
/2 - NUM_LINES_TO_SAMPLE
/2,
91 m_lineMax
/2 + NUM_LINES_TO_SAMPLE
/2);
93 // use the height of the lines we looked as the average
94 heightTotal
= (wxCoord
)
95 (((float)m_heightTotal
/ (3*NUM_LINES_TO_SAMPLE
)) * m_lineMax
);
101 wxCoord
wxVScrolledWindow::GetLinesHeight(size_t lineMin
, size_t lineMax
) const
103 if ( lineMin
== lineMax
)
105 else if ( lineMin
> lineMax
)
106 return -GetLinesHeight(lineMax
, lineMin
);
107 //else: lineMin < lineMax
109 // let the user code know that we're going to need all these lines
110 OnGetLinesHint(lineMin
, lineMax
);
112 // do sum up their heights
114 for ( size_t line
= lineMin
; line
< lineMax
; line
++ )
116 height
+= OnGetLineHeight(line
);
122 size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast
, bool full
)
124 const wxCoord hWindow
= GetClientSize().y
;
126 // go upwards until we arrive at a line such that lineLast is not visible
127 // any more when it is shown
128 size_t lineFirst
= lineLast
;
132 h
+= OnGetLineHeight(lineFirst
);
136 // for this line to be fully visible we need to go one line
137 // down, but if it is enough for it to be only partly visible then
138 // this line will do as well
156 void wxVScrolledWindow::UpdateScrollbar()
158 // see how many lines can we fit on screen
159 const wxCoord hWindow
= GetClientSize().y
;
163 for ( line
= m_lineFirst
; line
< m_lineMax
; line
++ )
168 h
+= OnGetLineHeight(line
);
171 m_nVisible
= line
- m_lineFirst
;
173 int pageSize
= m_nVisible
;
176 // last line is only partially visible, we still need the scrollbar and
177 // so we have to "fix" pageSize because if it is equal to m_lineMax the
178 // scrollbar is not shown at all under MSW
182 // set the scrollbar parameters to reflect this
183 SetScrollbar(wxVERTICAL
, m_lineFirst
, pageSize
, m_lineMax
);
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void wxVScrolledWindow::SetLineCount(size_t count
)
192 // save the number of lines
195 // and our estimate for their total height
196 m_heightTotal
= EstimateTotalHeight();
198 // recalculate the scrollbars parameters
199 m_lineFirst
= 1; // make sure it is != 0
203 void wxVScrolledWindow::RefreshLine(size_t line
)
205 // is this line visible?
206 if ( !IsVisible(line
) )
208 // no, it is useless to do anything
212 // calculate the rect occupied by this line on screen
214 rect
.width
= GetClientSize().x
;
215 rect
.height
= OnGetLineHeight(line
);
216 for ( size_t n
= GetFirstVisibleLine(); n
< line
; n
++ )
218 rect
.y
+= OnGetLineHeight(n
);
225 void wxVScrolledWindow::RefreshLines(size_t from
, size_t to
)
227 wxASSERT_MSG( from
<= to
, _T("RefreshLines(): empty range") );
229 // clump the range to just the visible lines -- it is useless to refresh
231 if ( from
< GetFirstVisibleLine() )
232 from
= GetFirstVisibleLine();
234 if ( to
> GetLastVisibleLine() )
235 to
= GetLastVisibleLine();
237 // calculate the rect occupied by these lines on screen
239 rect
.width
= GetClientSize().x
;
240 for ( size_t nBefore
= GetFirstVisibleLine(); nBefore
< from
; nBefore
++ )
242 rect
.y
+= OnGetLineHeight(nBefore
);
245 for ( size_t nBetween
= from
; nBetween
<= to
; nBetween
++ )
247 rect
.height
+= OnGetLineHeight(nBetween
);
254 void wxVScrolledWindow::RefreshAll()
261 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
263 const size_t lineMax
= GetLastVisibleLine();
264 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
266 y
-= OnGetLineHeight(line
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 bool wxVScrolledWindow::ScrollToLine(size_t line
)
282 // we're empty, code below doesn't make sense in this case
286 // determine the real first line to scroll to: we shouldn't scroll beyond
288 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
289 if ( line
> lineFirstLast
)
290 line
= lineFirstLast
;
293 if ( line
== m_lineFirst
)
300 // remember the currently shown lines for the refresh code below
301 size_t lineFirstOld
= GetFirstVisibleLine(),
302 lineLastOld
= GetLastVisibleLine();
307 // the size of scrollbar thumb could have changed
311 // finally refresh the display -- but only redraw as few lines as possible
313 if ( GetFirstVisibleLine() > lineLastOld
||
314 GetLastVisibleLine() < lineFirstOld
)
316 // the simplest case: we don't have any old lines left, just redraw
320 else // overlap between the lines we showed before and should show now
322 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld
));
328 bool wxVScrolledWindow::ScrollLines(int lines
)
330 lines
+= m_lineFirst
;
334 return ScrollToLine(lines
);
337 bool wxVScrolledWindow::ScrollPages(int pages
)
339 bool didSomething
= false;
346 line
= GetLastVisibleLine();
351 line
= FindFirstFromBottom(GetFirstVisibleLine());
355 didSomething
= ScrollToLine(line
);
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
372 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
376 const wxEventType evtType
= event
.GetEventType();
377 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
381 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
383 lineFirstNew
= m_lineMax
;
385 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
387 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
389 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
391 lineFirstNew
= m_lineFirst
+ 1;
393 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
395 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
397 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
399 lineFirstNew
= GetLastVisibleLine();
401 else // unknown scroll event?
403 if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
405 lineFirstNew
= event
.GetPosition();
409 wxASSERT_MSG( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
,
410 _T("unknown scroll event type?") );
412 // don't do anything, otherwise dragging the thumb around would
418 ScrollToLine(lineFirstNew
);