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
)
41 EVT_MOUSEWHEEL(wxVScrolledWindow::OnMouseWheel
)
46 // ============================================================================
48 // ============================================================================
50 IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow
, wxPanel
)
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 void wxVScrolledWindow::Init()
58 // we're initially empty
62 // this one should always be strictly positive
68 m_sumWheelRotation
= 0;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 wxCoord
wxVScrolledWindow::EstimateTotalHeight() const
78 // estimate the total height: it is impossible to call
79 // OnGetLineHeight() for every line because there may be too many of
80 // them, so we just make a guess using some lines in the beginning,
81 // some in the end and some in the middle
82 static const size_t NUM_LINES_TO_SAMPLE
= 10;
85 if ( m_lineMax
< 3*NUM_LINES_TO_SAMPLE
)
87 // in this case calculating exactly is faster and more correct than
89 heightTotal
= GetLinesHeight(0, m_lineMax
);
91 else // too many lines to calculate exactly
93 // look at some lines in the beginning/middle/end
95 GetLinesHeight(0, NUM_LINES_TO_SAMPLE
) +
96 GetLinesHeight(m_lineMax
- NUM_LINES_TO_SAMPLE
, m_lineMax
) +
97 GetLinesHeight(m_lineMax
/2 - NUM_LINES_TO_SAMPLE
/2,
98 m_lineMax
/2 + NUM_LINES_TO_SAMPLE
/2);
100 // use the height of the lines we looked as the average
101 heightTotal
= (wxCoord
)
102 (((float)heightTotal
/ (3*NUM_LINES_TO_SAMPLE
)) * m_lineMax
);
108 wxCoord
wxVScrolledWindow::GetLinesHeight(size_t lineMin
, size_t lineMax
) const
110 if ( lineMin
== lineMax
)
112 else if ( lineMin
> lineMax
)
113 return -GetLinesHeight(lineMax
, lineMin
);
114 //else: lineMin < lineMax
116 // let the user code know that we're going to need all these lines
117 OnGetLinesHint(lineMin
, lineMax
);
119 // do sum up their heights
121 for ( size_t line
= lineMin
; line
< lineMax
; line
++ )
123 height
+= OnGetLineHeight(line
);
129 size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast
, bool full
)
131 const wxCoord hWindow
= GetClientSize().y
;
133 // go upwards until we arrive at a line such that lineLast is not visible
134 // any more when it is shown
135 size_t lineFirst
= lineLast
;
139 h
+= OnGetLineHeight(lineFirst
);
143 // for this line to be fully visible we need to go one line
144 // down, but if it is enough for it to be only partly visible then
145 // this line will do as well
163 void wxVScrolledWindow::UpdateScrollbar()
165 // see how many lines can we fit on screen
166 const wxCoord hWindow
= GetClientSize().y
;
170 for ( line
= m_lineFirst
; line
< m_lineMax
; line
++ )
175 h
+= OnGetLineHeight(line
);
178 m_nVisible
= line
- m_lineFirst
;
180 int pageSize
= m_nVisible
;
183 // last line is only partially visible, we still need the scrollbar and
184 // so we have to "fix" pageSize because if it is equal to m_lineMax the
185 // scrollbar is not shown at all under MSW
189 // set the scrollbar parameters to reflect this
190 SetScrollbar(wxVERTICAL
, m_lineFirst
, pageSize
, m_lineMax
);
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 void wxVScrolledWindow::SetLineCount(size_t count
)
199 // save the number of lines
202 // and our estimate for their total height
203 m_heightTotal
= EstimateTotalHeight();
205 // recalculate the scrollbars parameters
206 m_lineFirst
= 1; // make sure it is != 0
210 void wxVScrolledWindow::RefreshLine(size_t line
)
212 // is this line visible?
213 if ( !IsVisible(line
) )
215 // no, it is useless to do anything
219 // calculate the rect occupied by this line on screen
221 rect
.width
= GetClientSize().x
;
222 rect
.height
= OnGetLineHeight(line
);
223 for ( size_t n
= GetFirstVisibleLine(); n
< line
; n
++ )
225 rect
.y
+= OnGetLineHeight(n
);
232 void wxVScrolledWindow::RefreshLines(size_t from
, size_t to
)
234 wxASSERT_MSG( from
<= to
, _T("RefreshLines(): empty range") );
236 // clump the range to just the visible lines -- it is useless to refresh
238 if ( from
< GetFirstVisibleLine() )
239 from
= GetFirstVisibleLine();
241 if ( to
> GetLastVisibleLine() )
242 to
= GetLastVisibleLine();
244 // calculate the rect occupied by these lines on screen
246 rect
.width
= GetClientSize().x
;
247 for ( size_t nBefore
= GetFirstVisibleLine(); nBefore
< from
; nBefore
++ )
249 rect
.y
+= OnGetLineHeight(nBefore
);
252 for ( size_t nBetween
= from
; nBetween
<= to
; nBetween
++ )
254 rect
.height
+= OnGetLineHeight(nBetween
);
261 void wxVScrolledWindow::RefreshAll()
268 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
270 const size_t lineMax
= GetLastVisibleLine();
271 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
273 y
-= OnGetLineHeight(line
);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 bool wxVScrolledWindow::ScrollToLine(size_t line
)
289 // we're empty, code below doesn't make sense in this case
293 // determine the real first line to scroll to: we shouldn't scroll beyond
295 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
296 if ( line
> lineFirstLast
)
297 line
= lineFirstLast
;
300 if ( line
== m_lineFirst
)
307 // remember the currently shown lines for the refresh code below
308 size_t lineFirstOld
= GetFirstVisibleLine(),
309 lineLastOld
= GetLastVisibleLine();
314 // the size of scrollbar thumb could have changed
318 // finally refresh the display -- but only redraw as few lines as possible
320 if ( GetFirstVisibleLine() > lineLastOld
||
321 GetLastVisibleLine() < lineFirstOld
)
323 // the simplest case: we don't have any old lines left, just redraw
327 else // overlap between the lines we showed before and should show now
329 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld
));
335 bool wxVScrolledWindow::ScrollLines(int lines
)
337 lines
+= m_lineFirst
;
341 return ScrollToLine(lines
);
344 bool wxVScrolledWindow::ScrollPages(int pages
)
346 bool didSomething
= false;
353 line
= GetLastVisibleLine();
358 line
= FindFirstFromBottom(GetFirstVisibleLine());
362 didSomething
= ScrollToLine(line
);
368 // ----------------------------------------------------------------------------
370 // ----------------------------------------------------------------------------
372 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
379 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
383 const wxEventType evtType
= event
.GetEventType();
385 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
389 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
391 lineFirstNew
= m_lineMax
;
393 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
395 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
397 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
399 lineFirstNew
= m_lineFirst
+ 1;
401 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
403 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
405 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
407 lineFirstNew
= GetLastVisibleLine();
409 else if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
411 lineFirstNew
= event
.GetPosition();
413 else if ( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
)
415 lineFirstNew
= event
.GetPosition();
418 else // unknown scroll event?
420 wxFAIL_MSG( _T("unknown scroll event type?") );
424 ScrollToLine(lineFirstNew
);
433 void wxVScrolledWindow::OnMouseWheel(wxMouseEvent
& event
)
435 m_sumWheelRotation
+= event
.GetWheelRotation();
436 int delta
= event
.GetWheelDelta();
438 // how much to scroll this time
439 int units_to_scroll
= -(m_sumWheelRotation
/delta
);
440 if ( !units_to_scroll
)
443 m_sumWheelRotation
+= units_to_scroll
*delta
;
445 if ( !event
.IsPageScroll() )
446 ScrollLines( units_to_scroll
*event
.GetLinesPerAction() );
448 // scroll pages instead of lines
449 ScrollPages( units_to_scroll
);