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::GetLinesHeight(size_t lineMin
, size_t lineMax
) const
58 if ( lineMin
== lineMax
)
60 else if ( lineMin
> lineMax
)
61 return -GetLinesHeight(lineMax
, lineMin
);
62 //else: lineMin < lineMax
64 // let the user code know that we're going to need all these lines
65 OnGetLinesHint(lineMin
, lineMax
);
67 // do sum up their heights
69 for ( size_t line
= lineMin
; line
< lineMax
; line
++ )
71 height
+= OnGetLineHeight(line
);
77 size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast
, bool full
)
79 const wxCoord hWindow
= GetClientSize().y
;
81 // go upwards until we arrive at a line such that lineLast is not visible
82 // any more when it is shown
83 size_t lineFirst
= lineLast
;
87 h
+= OnGetLineHeight(lineFirst
);
91 // for this line to be fully visible we need to go one line
92 // down, but if it is enough for it to be only partly visible then
93 // this line will do as well
111 void wxVScrolledWindow::UpdateScrollbar()
113 // see how many lines can we fit on screen
114 const wxCoord hWindow
= GetClientSize().y
;
118 for ( line
= m_lineFirst
; line
< m_lineMax
; line
++ )
123 h
+= OnGetLineHeight(line
);
126 m_nVisible
= line
- m_lineFirst
;
128 int pageSize
= m_nVisible
;
131 // last line is only partially visible, we still need the scrollbar and
132 // so we have to "fix" pageSize because if it is equal to m_lineMax the
133 // scrollbar is not shown at all under MSW
137 // set the scrollbar parameters to reflect this
138 SetScrollbar(wxVERTICAL
, m_lineFirst
, pageSize
, m_lineMax
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 void wxVScrolledWindow::SetLineCount(size_t count
)
147 // save the number of lines
151 // estimate the total height: it is impossible to call
152 // OnGetLineHeight() for every line because there may be too many of
153 // them, so we just make a guess using some lines in the beginning,
154 // some in the end and some in the middle
155 static const size_t NUM_LINES_TO_SAMPLE
= 10;
157 if ( count
< 3*NUM_LINES_TO_SAMPLE
)
159 // in this case calculating exactly is faster and more correct than
161 m_heightTotal
= GetLinesHeight(0, m_lineMax
);
163 else // too many lines to calculate exactly
165 // look at some lines in the beginning/middle/end
167 GetLinesHeight(0, NUM_LINES_TO_SAMPLE
) +
168 GetLinesHeight(count
- NUM_LINES_TO_SAMPLE
, count
) +
169 GetLinesHeight(count
/2 - NUM_LINES_TO_SAMPLE
/2,
170 count
/2 + NUM_LINES_TO_SAMPLE
/2);
172 // use the height of the lines we looked as the average
173 m_heightTotal
= (wxCoord
)
174 (((float)m_heightTotal
/ (3*NUM_LINES_TO_SAMPLE
)) * m_lineMax
);
178 // recalculate the scrollbars parameters
179 m_lineFirst
= 1; // make sure it is != 0
183 void wxVScrolledWindow::RefreshLine(size_t line
)
185 // is this line visible?
186 if ( !IsVisible(line
) )
188 // no, it is useless to do anything
192 // calculate the rect occupied by this line on screen
194 rect
.width
= GetClientSize().x
;
195 rect
.height
= OnGetLineHeight(line
);
196 for ( size_t n
= GetFirstVisibleLine(); n
< line
; n
++ )
198 rect
.y
+= OnGetLineHeight(n
);
205 void wxVScrolledWindow::RefreshLines(size_t from
, size_t to
)
207 wxASSERT_MSG( from
<= to
, _T("RefreshLines(): empty range") );
209 // clump the range to just the visible lines -- it is useless to refresh
211 if ( from
< GetFirstVisibleLine() )
212 from
= GetFirstVisibleLine();
214 if ( to
> GetLastVisibleLine() )
215 to
= GetLastVisibleLine();
217 // calculate the rect occupied by these lines on screen
219 rect
.width
= GetClientSize().x
;
220 for ( size_t nBefore
= GetFirstVisibleLine(); nBefore
< from
; nBefore
++ )
222 rect
.y
+= OnGetLineHeight(nBefore
);
225 for ( size_t nBetween
= from
; nBetween
<= to
; nBetween
++ )
227 rect
.height
+= OnGetLineHeight(nBetween
);
234 void wxVScrolledWindow::RefreshAll()
241 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
243 const size_t lineMax
= GetLastVisibleLine();
244 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
246 y
-= OnGetLineHeight(line
);
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 bool wxVScrolledWindow::ScrollToLine(size_t line
)
262 // we're empty, code below doesn't make sense in this case
266 // determine the real first line to scroll to: we shouldn't scroll beyond
268 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
269 if ( line
> lineFirstLast
)
270 line
= lineFirstLast
;
273 if ( line
== m_lineFirst
)
280 // remember the currently shown lines for the refresh code below
281 size_t lineFirstOld
= GetFirstVisibleLine(),
282 lineLastOld
= GetLastVisibleLine();
287 // the size of scrollbar thumb could have changed
291 // finally refresh the display -- but only redraw as few lines as possible
293 if ( GetFirstVisibleLine() > lineLastOld
||
294 GetLastVisibleLine() < lineFirstOld
)
296 // the simplest case: we don't have any old lines left, just redraw
300 else // overlap between the lines we showed before and should show now
302 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld
));
308 bool wxVScrolledWindow::ScrollLines(int lines
)
310 lines
+= m_lineFirst
;
314 return ScrollToLine(lines
);
317 bool wxVScrolledWindow::ScrollPages(int pages
)
319 bool didSomething
= false;
326 line
= GetLastVisibleLine();
331 line
= FindFirstFromBottom(GetFirstVisibleLine());
335 didSomething
= ScrollToLine(line
);
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
352 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
356 const wxEventType evtType
= event
.GetEventType();
357 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
361 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
363 lineFirstNew
= m_lineMax
;
365 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
367 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
369 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
371 lineFirstNew
= m_lineFirst
+ 1;
373 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
375 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
377 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
379 lineFirstNew
= GetLastVisibleLine();
381 else // unknown scroll event?
383 if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
385 lineFirstNew
= event
.GetPosition();
389 wxASSERT_MSG( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
,
390 _T("unknown scroll event type?") );
392 // don't do anything, otherwise dragging the thumb around would
398 ScrollToLine(lineFirstNew
);