]> git.saurik.com Git - wxWidgets.git/blame - src/generic/vscroll.cpp
don't paint an infinite number of items if the control is empty
[wxWidgets.git] / src / generic / vscroll.cpp
CommitLineData
cf7d6329
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/vscroll.cpp
3// Purpose: wxVScrolledWindow implementation
4// Author: Vadim Zeitlin
234b1c01 5// Modified by:
cf7d6329
VZ
6// Created: 30.05.03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
65571936 9// Licence: wxWindows licence
cf7d6329
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
687dcff3
VS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
2c1e2499
VZ
27#ifndef WX_PRECOMP
28 #include "wx/sizer.h"
29#endif
30
cf7d6329
VZ
31#include "wx/vscroll.h"
32
33// ----------------------------------------------------------------------------
34// event tables
35// ----------------------------------------------------------------------------
36
37BEGIN_EVENT_TABLE(wxVScrolledWindow, wxPanel)
38 EVT_SIZE(wxVScrolledWindow::OnSize)
39 EVT_SCROLLWIN(wxVScrolledWindow::OnScroll)
4719e58d
RD
40#if wxUSE_MOUSEWHEEL
41 EVT_MOUSEWHEEL(wxVScrolledWindow::OnMouseWheel)
42#endif
cf7d6329
VZ
43END_EVENT_TABLE()
44
45
46// ============================================================================
47// implementation
48// ============================================================================
49
0c8392ca
RD
50IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow, wxPanel)
51
cf7d6329
VZ
52// ----------------------------------------------------------------------------
53// initialization
54// ----------------------------------------------------------------------------
55
56void wxVScrolledWindow::Init()
57{
58 // we're initially empty
59 m_lineMax =
60 m_lineFirst = 0;
61
62 // this one should always be strictly positive
63 m_nVisible = 1;
64
65 m_heightTotal = 0;
4719e58d
RD
66
67#if wxUSE_MOUSEWHEEL
68 m_sumWheelRotation = 0;
69#endif
cf7d6329
VZ
70}
71
72// ----------------------------------------------------------------------------
73// various helpers
74// ----------------------------------------------------------------------------
75
1e0af0bc
VZ
76wxCoord wxVScrolledWindow::EstimateTotalHeight() const
77{
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;
83
84 wxCoord heightTotal;
85 if ( m_lineMax < 3*NUM_LINES_TO_SAMPLE )
86 {
87 // in this case calculating exactly is faster and more correct than
88 // guessing
89 heightTotal = GetLinesHeight(0, m_lineMax);
90 }
91 else // too many lines to calculate exactly
92 {
93 // look at some lines in the beginning/middle/end
94 heightTotal =
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);
99
100 // use the height of the lines we looked as the average
101 heightTotal = (wxCoord)
999836aa 102 (((float)heightTotal / (3*NUM_LINES_TO_SAMPLE)) * m_lineMax);
1e0af0bc
VZ
103 }
104
105 return heightTotal;
106}
107
cf7d6329
VZ
108wxCoord wxVScrolledWindow::GetLinesHeight(size_t lineMin, size_t lineMax) const
109{
110 if ( lineMin == lineMax )
111 return 0;
112 else if ( lineMin > lineMax )
113 return -GetLinesHeight(lineMax, lineMin);
114 //else: lineMin < lineMax
115
116 // let the user code know that we're going to need all these lines
117 OnGetLinesHint(lineMin, lineMax);
118
119 // do sum up their heights
120 wxCoord height = 0;
121 for ( size_t line = lineMin; line < lineMax; line++ )
122 {
123 height += OnGetLineHeight(line);
124 }
125
126 return height;
127}
128
0b49ccf8 129size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast, bool full)
cf7d6329
VZ
130{
131 const wxCoord hWindow = GetClientSize().y;
132
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;
136 wxCoord h = 0;
137 for ( ;; )
138 {
139 h += OnGetLineHeight(lineFirst);
140
141 if ( h > hWindow )
142 {
0b49ccf8
VZ
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
146 if ( full )
147 {
148 lineFirst++;
149 }
cf7d6329
VZ
150
151 break;
152 }
153
154 if ( !lineFirst )
155 break;
156
157 lineFirst--;
158 }
159
160 return lineFirst;
161}
162
163void wxVScrolledWindow::UpdateScrollbar()
164{
165 // see how many lines can we fit on screen
166 const wxCoord hWindow = GetClientSize().y;
167
168 wxCoord h = 0;
169 size_t line;
170 for ( line = m_lineFirst; line < m_lineMax; line++ )
171 {
172 if ( h > hWindow )
173 break;
174
175 h += OnGetLineHeight(line);
176 }
177
63916e44
VZ
178 // if we still have remaining space below, maybe we can fit everything?
179 if ( h < hWindow )
180 {
181 wxCoord hAll = h;
182 for ( size_t lineFirst = m_lineFirst; lineFirst > 0; lineFirst-- )
183 {
184 hAll += OnGetLineHeight(m_lineFirst - 1);
185 if ( hAll > hWindow )
186 break;
187 }
188
189 if ( hAll < hWindow )
190 {
191 // we don't need scrollbar at all
192 m_lineFirst = 0;
193 SetScrollbar(wxVERTICAL, 0, 0, 0);
194 }
195 }
196
cf7d6329
VZ
197 m_nVisible = line - m_lineFirst;
198
199 int pageSize = m_nVisible;
200 if ( h > hWindow )
201 {
202 // last line is only partially visible, we still need the scrollbar and
203 // so we have to "fix" pageSize because if it is equal to m_lineMax the
204 // scrollbar is not shown at all under MSW
205 pageSize--;
206 }
207
208 // set the scrollbar parameters to reflect this
209 SetScrollbar(wxVERTICAL, m_lineFirst, pageSize, m_lineMax);
210}
211
212// ----------------------------------------------------------------------------
213// operations
214// ----------------------------------------------------------------------------
215
216void wxVScrolledWindow::SetLineCount(size_t count)
217{
218 // save the number of lines
219 m_lineMax = count;
220
1e0af0bc
VZ
221 // and our estimate for their total height
222 m_heightTotal = EstimateTotalHeight();
cf7d6329
VZ
223
224 // recalculate the scrollbars parameters
e0c6027b 225 m_lineFirst = 1; // make sure it is != 0
cf7d6329
VZ
226 ScrollToLine(0);
227}
228
e0c6027b
VZ
229void wxVScrolledWindow::RefreshLine(size_t line)
230{
231 // is this line visible?
232 if ( !IsVisible(line) )
233 {
234 // no, it is useless to do anything
235 return;
236 }
237
238 // calculate the rect occupied by this line on screen
239 wxRect rect;
240 rect.width = GetClientSize().x;
241 rect.height = OnGetLineHeight(line);
dd932cbe 242 for ( size_t n = GetVisibleBegin(); n < line; n++ )
e0c6027b
VZ
243 {
244 rect.y += OnGetLineHeight(n);
245 }
ae0f0223
VZ
246
247 // do refresh it
248 RefreshRect(rect);
249}
250
251void wxVScrolledWindow::RefreshLines(size_t from, size_t to)
252{
253 wxASSERT_MSG( from <= to, _T("RefreshLines(): empty range") );
254
255 // clump the range to just the visible lines -- it is useless to refresh
256 // the other ones
dd932cbe
VZ
257 if ( from < GetVisibleBegin() )
258 from = GetVisibleBegin();
ae0f0223 259
dd932cbe
VZ
260 if ( to >= GetVisibleEnd() )
261 to = GetVisibleEnd();
262 else
263 to++;
ae0f0223
VZ
264
265 // calculate the rect occupied by these lines on screen
266 wxRect rect;
267 rect.width = GetClientSize().x;
dd932cbe 268 for ( size_t nBefore = GetVisibleBegin(); nBefore < from; nBefore++ )
ae0f0223
VZ
269 {
270 rect.y += OnGetLineHeight(nBefore);
271 }
272
dd932cbe 273 for ( size_t nBetween = from; nBetween < to; nBetween++ )
ae0f0223
VZ
274 {
275 rect.height += OnGetLineHeight(nBetween);
276 }
e0c6027b
VZ
277
278 // do refresh it
279 RefreshRect(rect);
280}
281
8b053348
VZ
282void wxVScrolledWindow::RefreshAll()
283{
284 UpdateScrollbar();
285
286 Refresh();
287}
288
61bb5a9c
VZ
289bool wxVScrolledWindow::Layout()
290{
291 if ( GetSizer() )
292 {
293 // adjust the sizer dimensions/position taking into account the
294 // virtual size and scrolled position of the window.
295
296 int w, h;
297 GetVirtualSize(&w, &h);
298
299 // x is always 0 so no variable needed
300 int y = -GetLinesHeight(0, GetFirstVisibleLine());
301
302 GetSizer()->SetDimension(0, y, w, h);
303 return true;
304 }
305
306 // fall back to default for LayoutConstraints
307 return wxPanel::Layout();
308}
309
e0c6027b
VZ
310int wxVScrolledWindow::HitTest(wxCoord WXUNUSED(x), wxCoord y) const
311{
dd932cbe
VZ
312 const size_t lineMax = GetVisibleEnd();
313 for ( size_t line = GetVisibleBegin(); line < lineMax; line++ )
e0c6027b
VZ
314 {
315 y -= OnGetLineHeight(line);
316 if ( y < 0 )
317 return line;
318 }
319
320 return wxNOT_FOUND;
321}
322
cf7d6329
VZ
323// ----------------------------------------------------------------------------
324// scrolling
325// ----------------------------------------------------------------------------
326
327bool wxVScrolledWindow::ScrollToLine(size_t line)
328{
329 if ( !m_lineMax )
330 {
331 // we're empty, code below doesn't make sense in this case
332 return false;
333 }
334
335 // determine the real first line to scroll to: we shouldn't scroll beyond
336 // the end
0b49ccf8 337 size_t lineFirstLast = FindFirstFromBottom(m_lineMax - 1, true);
cf7d6329
VZ
338 if ( line > lineFirstLast )
339 line = lineFirstLast;
340
341 // anything to do?
342 if ( line == m_lineFirst )
343 {
344 // no
345 return false;
346 }
347
348
349 // remember the currently shown lines for the refresh code below
234b1c01
VZ
350 size_t lineFirstOld = GetVisibleBegin(),
351 lineLastOld = GetVisibleEnd();
cf7d6329
VZ
352
353 m_lineFirst = line;
354
355
356 // the size of scrollbar thumb could have changed
357 UpdateScrollbar();
358
234b1c01
VZ
359
360 // finally refresh the display -- but only redraw as few lines as possible
361 // to avoid flicker
362 if ( GetVisibleBegin() >= lineLastOld ||
363 GetVisibleEnd() <= lineFirstOld )
364 {
365 // the simplest case: we don't have any old lines left, just redraw
366 // everything
367 Refresh();
368 }
369 else // overlap between the lines we showed before and should show now
370 {
371 ScrollWindow(0, GetLinesHeight(GetVisibleBegin(), lineFirstOld));
372 }
cf7d6329
VZ
373
374 return true;
375}
376
377bool wxVScrolledWindow::ScrollLines(int lines)
378{
379 lines += m_lineFirst;
380 if ( lines < 0 )
381 lines = 0;
382
383 return ScrollToLine(lines);
384}
385
386bool wxVScrolledWindow::ScrollPages(int pages)
387{
388 bool didSomething = false;
389
390 while ( pages )
391 {
392 int line;
393 if ( pages > 0 )
394 {
dd932cbe
VZ
395 line = GetVisibleEnd();
396 if ( line )
397 line--;
cf7d6329
VZ
398 pages--;
399 }
400 else // pages < 0
401 {
dd932cbe 402 line = FindFirstFromBottom(GetVisibleBegin());
cf7d6329
VZ
403 pages++;
404 }
405
406 didSomething = ScrollToLine(line);
407 }
408
409 return didSomething;
410}
411
412// ----------------------------------------------------------------------------
413// event handling
414// ----------------------------------------------------------------------------
415
416void wxVScrolledWindow::OnSize(wxSizeEvent& event)
417{
418 UpdateScrollbar();
419
420 event.Skip();
421}
422
423void wxVScrolledWindow::OnScroll(wxScrollWinEvent& event)
424{
425 size_t lineFirstNew;
426
427 const wxEventType evtType = event.GetEventType();
5d2ad055 428
cf7d6329
VZ
429 if ( evtType == wxEVT_SCROLLWIN_TOP )
430 {
431 lineFirstNew = 0;
432 }
433 else if ( evtType == wxEVT_SCROLLWIN_BOTTOM )
434 {
435 lineFirstNew = m_lineMax;
436 }
437 else if ( evtType == wxEVT_SCROLLWIN_LINEUP )
438 {
439 lineFirstNew = m_lineFirst ? m_lineFirst - 1 : 0;
440 }
441 else if ( evtType == wxEVT_SCROLLWIN_LINEDOWN )
442 {
443 lineFirstNew = m_lineFirst + 1;
444 }
445 else if ( evtType == wxEVT_SCROLLWIN_PAGEUP )
446 {
447 lineFirstNew = FindFirstFromBottom(m_lineFirst);
448 }
449 else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN )
450 {
dd932cbe
VZ
451 lineFirstNew = GetVisibleEnd();
452 if ( lineFirstNew )
453 lineFirstNew--;
cf7d6329 454 }
5d2ad055
RD
455 else if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE )
456 {
457 lineFirstNew = event.GetPosition();
458 }
459 else if ( evtType == wxEVT_SCROLLWIN_THUMBTRACK )
460 {
461 lineFirstNew = event.GetPosition();
462 }
ca65c044 463
cf7d6329
VZ
464 else // unknown scroll event?
465 {
5d2ad055
RD
466 wxFAIL_MSG( _T("unknown scroll event type?") );
467 return;
cf7d6329
VZ
468 }
469
470 ScrollToLine(lineFirstNew);
b544a278
VZ
471
472#ifdef __WXMAC__
473 Update();
474#endif // __WXMAC__
cf7d6329
VZ
475}
476
4719e58d
RD
477#if wxUSE_MOUSEWHEEL
478
479void wxVScrolledWindow::OnMouseWheel(wxMouseEvent& event)
480{
481 m_sumWheelRotation += event.GetWheelRotation();
482 int delta = event.GetWheelDelta();
483
484 // how much to scroll this time
485 int units_to_scroll = -(m_sumWheelRotation/delta);
486 if ( !units_to_scroll )
487 return;
488
489 m_sumWheelRotation += units_to_scroll*delta;
490
491 if ( !event.IsPageScroll() )
492 ScrollLines( units_to_scroll*event.GetLinesPerAction() );
493 else
494 // scroll pages instead of lines
495 ScrollPages( units_to_scroll );
496}
497
234b1c01 498#endif // wxUSE_MOUSEWHEEL
d77836e4 499