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
= GetVisibleBegin(); 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
< GetVisibleBegin() )
239 from
= GetVisibleBegin();
241 if ( to
>= GetVisibleEnd() )
242 to
= GetVisibleEnd();
246 // calculate the rect occupied by these lines on screen
248 rect
.width
= GetClientSize().x
;
249 for ( size_t nBefore
= GetVisibleBegin(); nBefore
< from
; nBefore
++ )
251 rect
.y
+= OnGetLineHeight(nBefore
);
254 for ( size_t nBetween
= from
; nBetween
< to
; nBetween
++ )
256 rect
.height
+= OnGetLineHeight(nBetween
);
263 void wxVScrolledWindow::RefreshAll()
270 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
272 const size_t lineMax
= GetVisibleEnd();
273 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
275 y
-= OnGetLineHeight(line
);
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 bool wxVScrolledWindow::ScrollToLine(size_t line
)
291 // we're empty, code below doesn't make sense in this case
295 // determine the real first line to scroll to: we shouldn't scroll beyond
297 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
298 if ( line
> lineFirstLast
)
299 line
= lineFirstLast
;
302 if ( line
== m_lineFirst
)
309 // remember the currently shown lines for the refresh code below
310 size_t lineFirstOld
= GetVisibleBegin(),
311 lineLastOld
= GetVisibleEnd();
316 // the size of scrollbar thumb could have changed
320 // finally refresh the display -- but only redraw as few lines as possible
322 if ( GetVisibleBegin() >= lineLastOld
||
323 GetVisibleEnd() <= lineFirstOld
)
325 // the simplest case: we don't have any old lines left, just redraw
329 else // overlap between the lines we showed before and should show now
331 ScrollWindow(0, GetLinesHeight(GetVisibleBegin(), lineFirstOld
));
337 bool wxVScrolledWindow::ScrollLines(int lines
)
339 lines
+= m_lineFirst
;
343 return ScrollToLine(lines
);
346 bool wxVScrolledWindow::ScrollPages(int pages
)
348 bool didSomething
= false;
355 line
= GetVisibleEnd();
362 line
= FindFirstFromBottom(GetVisibleBegin());
366 didSomething
= ScrollToLine(line
);
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
376 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
383 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
387 const wxEventType evtType
= event
.GetEventType();
389 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
393 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
395 lineFirstNew
= m_lineMax
;
397 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
399 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
401 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
403 lineFirstNew
= m_lineFirst
+ 1;
405 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
407 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
409 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
411 lineFirstNew
= GetVisibleEnd();
415 else if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
417 lineFirstNew
= event
.GetPosition();
419 else if ( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
)
421 lineFirstNew
= event
.GetPosition();
424 else // unknown scroll event?
426 wxFAIL_MSG( _T("unknown scroll event type?") );
430 ScrollToLine(lineFirstNew
);
439 void wxVScrolledWindow::OnMouseWheel(wxMouseEvent
& event
)
441 m_sumWheelRotation
+= event
.GetWheelRotation();
442 int delta
= event
.GetWheelDelta();
444 // how much to scroll this time
445 int units_to_scroll
= -(m_sumWheelRotation
/delta
);
446 if ( !units_to_scroll
)
449 m_sumWheelRotation
+= units_to_scroll
*delta
;
451 if ( !event
.IsPageScroll() )
452 ScrollLines( units_to_scroll
*event
.GetLinesPerAction() );
454 // scroll pages instead of lines
455 ScrollPages( units_to_scroll
);