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 // For compilers that support precompilation, includes "wx.h".
21 #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::RemoveScrollbar()
166 m_nVisible
= m_lineMax
;
167 SetScrollbar(wxVERTICAL
, 0, 0, 0);
170 void wxVScrolledWindow::UpdateScrollbar()
172 // see how many lines can we fit on screen
173 const wxCoord hWindow
= GetClientSize().y
;
177 for ( line
= m_lineFirst
; line
< m_lineMax
; line
++ )
182 h
+= OnGetLineHeight(line
);
185 // if we still have remaining space below, maybe we can fit everything?
189 for ( size_t lineFirst
= m_lineFirst
; lineFirst
> 0; lineFirst
-- )
191 hAll
+= OnGetLineHeight(m_lineFirst
- 1);
192 if ( hAll
> hWindow
)
196 if ( hAll
< hWindow
)
198 // we don't need scrollbar at all
204 m_nVisible
= line
- m_lineFirst
;
206 int pageSize
= m_nVisible
;
209 // last line is only partially visible, we still need the scrollbar and
210 // so we have to "fix" pageSize because if it is equal to m_lineMax the
211 // scrollbar is not shown at all under MSW
215 // set the scrollbar parameters to reflect this
216 SetScrollbar(wxVERTICAL
, m_lineFirst
, pageSize
, m_lineMax
);
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 void wxVScrolledWindow::SetLineCount(size_t count
)
225 // save the number of lines
228 // and our estimate for their total height
229 m_heightTotal
= EstimateTotalHeight();
231 // recalculate the scrollbars parameters
234 m_lineFirst
= 1; // make sure it is != 0
243 void wxVScrolledWindow::RefreshLine(size_t line
)
245 // is this line visible?
246 if ( !IsVisible(line
) )
248 // no, it is useless to do anything
252 // calculate the rect occupied by this line on screen
254 rect
.width
= GetClientSize().x
;
255 rect
.height
= OnGetLineHeight(line
);
256 for ( size_t n
= GetVisibleBegin(); n
< line
; n
++ )
258 rect
.y
+= OnGetLineHeight(n
);
265 void wxVScrolledWindow::RefreshLines(size_t from
, size_t to
)
267 wxASSERT_MSG( from
<= to
, _T("RefreshLines(): empty range") );
269 // clump the range to just the visible lines -- it is useless to refresh
271 if ( from
< GetVisibleBegin() )
272 from
= GetVisibleBegin();
274 if ( to
>= GetVisibleEnd() )
275 to
= GetVisibleEnd();
279 // calculate the rect occupied by these lines on screen
281 rect
.width
= GetClientSize().x
;
282 for ( size_t nBefore
= GetVisibleBegin(); nBefore
< from
; nBefore
++ )
284 rect
.y
+= OnGetLineHeight(nBefore
);
287 for ( size_t nBetween
= from
; nBetween
< to
; nBetween
++ )
289 rect
.height
+= OnGetLineHeight(nBetween
);
296 void wxVScrolledWindow::RefreshAll()
303 bool wxVScrolledWindow::Layout()
307 // adjust the sizer dimensions/position taking into account the
308 // virtual size and scrolled position of the window.
311 GetVirtualSize(&w
, &h
);
313 // x is always 0 so no variable needed
314 int y
= -GetLinesHeight(0, GetFirstVisibleLine());
316 GetSizer()->SetDimension(0, y
, w
, h
);
320 // fall back to default for LayoutConstraints
321 return wxPanel::Layout();
324 int wxVScrolledWindow::HitTest(wxCoord
WXUNUSED(x
), wxCoord y
) const
326 const size_t lineMax
= GetVisibleEnd();
327 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
329 y
-= OnGetLineHeight(line
);
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 bool wxVScrolledWindow::ScrollToLine(size_t line
)
345 // we're empty, code below doesn't make sense in this case
349 // determine the real first line to scroll to: we shouldn't scroll beyond
351 size_t lineFirstLast
= FindFirstFromBottom(m_lineMax
- 1, true);
352 if ( line
> lineFirstLast
)
353 line
= lineFirstLast
;
356 if ( line
== m_lineFirst
)
363 // remember the currently shown lines for the refresh code below
364 size_t lineFirstOld
= GetVisibleBegin(),
365 lineLastOld
= GetVisibleEnd();
370 // the size of scrollbar thumb could have changed
374 // finally refresh the display -- but only redraw as few lines as possible
376 if ( GetVisibleBegin() >= lineLastOld
||
377 GetVisibleEnd() <= lineFirstOld
)
379 // the simplest case: we don't have any old lines left, just redraw
383 else // overlap between the lines we showed before and should show now
385 ScrollWindow(0, GetLinesHeight(GetVisibleBegin(), lineFirstOld
));
391 bool wxVScrolledWindow::ScrollLines(int lines
)
393 lines
+= m_lineFirst
;
397 return ScrollToLine(lines
);
400 bool wxVScrolledWindow::ScrollPages(int pages
)
402 bool didSomething
= false;
409 line
= GetVisibleEnd();
416 line
= FindFirstFromBottom(GetVisibleBegin());
420 didSomething
= ScrollToLine(line
);
426 // ----------------------------------------------------------------------------
428 // ----------------------------------------------------------------------------
430 void wxVScrolledWindow::OnSize(wxSizeEvent
& event
)
437 void wxVScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
441 const wxEventType evtType
= event
.GetEventType();
443 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
447 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
449 lineFirstNew
= m_lineMax
;
451 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
453 lineFirstNew
= m_lineFirst
? m_lineFirst
- 1 : 0;
455 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
457 lineFirstNew
= m_lineFirst
+ 1;
459 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
461 lineFirstNew
= FindFirstFromBottom(m_lineFirst
);
463 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
465 lineFirstNew
= GetVisibleEnd();
469 else if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
471 lineFirstNew
= event
.GetPosition();
473 else if ( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
)
475 lineFirstNew
= event
.GetPosition();
478 else // unknown scroll event?
480 wxFAIL_MSG( _T("unknown scroll event type?") );
484 ScrollToLine(lineFirstNew
);
493 void wxVScrolledWindow::OnMouseWheel(wxMouseEvent
& event
)
495 m_sumWheelRotation
+= event
.GetWheelRotation();
496 int delta
= event
.GetWheelDelta();
498 // how much to scroll this time
499 int units_to_scroll
= -(m_sumWheelRotation
/delta
);
500 if ( !units_to_scroll
)
503 m_sumWheelRotation
+= units_to_scroll
*delta
;
505 if ( !event
.IsPageScroll() )
506 ScrollLines( units_to_scroll
*event
.GetLinesPerAction() );
508 // scroll pages instead of lines
509 ScrollPages( units_to_scroll
);
512 #endif // wxUSE_MOUSEWHEEL