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::RefreshAll()
212 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
214 const size_t lineMax
= GetLastVisibleLine();
215 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
217 y
-= OnGetLineHeight(line
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 bool wxVScrolledWindow::ScrollToLine(size_t line
)
233 // we're empty, code below doesn't make sense in this case
237 // determine the real first line to scroll to: we shouldn't scroll beyond
239 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
240 if ( line
> lineFirstLast
)
241 line
= lineFirstLast
;
244 if ( line
== m_lineFirst
)
251 // remember the currently shown lines for the refresh code below
252 size_t lineFirstOld
= GetFirstVisibleLine(),
253 lineLastOld
= GetLastVisibleLine();
258 // the size of scrollbar thumb could have changed
262 // finally refresh the display -- but only redraw as few lines as possible
264 if ( GetFirstVisibleLine() > lineLastOld
||
265 GetLastVisibleLine() < lineFirstOld
)
267 // the simplest case: we don't have any old lines left, just redraw
271 else // overlap between the lines we showed before and should show now
273 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld
));
279 bool wxVScrolledWindow::ScrollLines(int lines
)
281 lines
+= m_lineFirst
;
285 return ScrollToLine(lines
);
288 bool wxVScrolledWindow::ScrollPages(int pages
)
290 bool didSomething
= false;
297 line
= GetLastVisibleLine();
302 line
= FindFirstFromBottom(GetFirstVisibleLine());
306 didSomething
= ScrollToLine(line
);
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
316 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
323 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
327 const wxEventType evtType
= event
.GetEventType();
328 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
332 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
334 lineFirstNew
= m_lineMax
;
336 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
338 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
340 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
342 lineFirstNew
= m_lineFirst
+ 1;
344 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
346 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
348 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
350 lineFirstNew
= GetLastVisibleLine();
352 else // unknown scroll event?
354 if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
356 lineFirstNew
= event
.GetPosition();
360 wxASSERT_MSG( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
,
361 _T("unknown scroll event type?") );
363 // don't do anything, otherwise dragging the thumb around would
369 ScrollToLine(lineFirstNew
);