added wxVListBox using wxVScrolledWindow and wxHtmlListBox using it
[wxWidgets.git] / src / generic / vscroll.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/vscroll.cpp
3 // Purpose: wxVScrolledWindow implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 30.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/vscroll.h"
21
22 // ----------------------------------------------------------------------------
23 // event tables
24 // ----------------------------------------------------------------------------
25
26 BEGIN_EVENT_TABLE(wxVScrolledWindow, wxPanel)
27 EVT_SIZE(wxVScrolledWindow::OnSize)
28 EVT_SCROLLWIN(wxVScrolledWindow::OnScroll)
29 END_EVENT_TABLE()
30
31
32 // ============================================================================
33 // implementation
34 // ============================================================================
35
36 // ----------------------------------------------------------------------------
37 // initialization
38 // ----------------------------------------------------------------------------
39
40 void wxVScrolledWindow::Init()
41 {
42 // we're initially empty
43 m_lineMax =
44 m_lineFirst = 0;
45
46 // this one should always be strictly positive
47 m_nVisible = 1;
48
49 m_heightTotal = 0;
50 }
51
52 // ----------------------------------------------------------------------------
53 // various helpers
54 // ----------------------------------------------------------------------------
55
56 wxCoord wxVScrolledWindow::GetLinesHeight(size_t lineMin, size_t lineMax) const
57 {
58 if ( lineMin == lineMax )
59 return 0;
60 else if ( lineMin > lineMax )
61 return -GetLinesHeight(lineMax, lineMin);
62 //else: lineMin < lineMax
63
64 // let the user code know that we're going to need all these lines
65 OnGetLinesHint(lineMin, lineMax);
66
67 // do sum up their heights
68 wxCoord height = 0;
69 for ( size_t line = lineMin; line < lineMax; line++ )
70 {
71 height += OnGetLineHeight(line);
72 }
73
74 return height;
75 }
76
77 size_t wxVScrolledWindow::FindFirstFromBottom(size_t lineLast)
78 {
79 const wxCoord hWindow = GetClientSize().y;
80
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;
84 wxCoord h = 0;
85 for ( ;; )
86 {
87 h += OnGetLineHeight(lineFirst);
88
89 if ( h > hWindow )
90 {
91 lineFirst++;
92
93 break;
94 }
95
96 if ( !lineFirst )
97 break;
98
99 lineFirst--;
100 }
101
102 return lineFirst;
103 }
104
105 void wxVScrolledWindow::UpdateScrollbar()
106 {
107 // see how many lines can we fit on screen
108 const wxCoord hWindow = GetClientSize().y;
109
110 wxCoord h = 0;
111 size_t line;
112 for ( line = m_lineFirst; line < m_lineMax; line++ )
113 {
114 if ( h > hWindow )
115 break;
116
117 h += OnGetLineHeight(line);
118 }
119
120 m_nVisible = line - m_lineFirst;
121
122 int pageSize = m_nVisible;
123 if ( h > hWindow )
124 {
125 // last line is only partially visible, we still need the scrollbar and
126 // so we have to "fix" pageSize because if it is equal to m_lineMax the
127 // scrollbar is not shown at all under MSW
128 pageSize--;
129 }
130
131 // set the scrollbar parameters to reflect this
132 SetScrollbar(wxVERTICAL, m_lineFirst, pageSize, m_lineMax);
133 }
134
135 // ----------------------------------------------------------------------------
136 // operations
137 // ----------------------------------------------------------------------------
138
139 void wxVScrolledWindow::SetLineCount(size_t count)
140 {
141 // save the number of lines
142 m_lineMax = count;
143
144
145 // estimate the total height: it is impossible to call
146 // OnGetLineHeight() for every line because there may be too many of
147 // them, so we just make a guess using some lines in the beginning,
148 // some in the end and some in the middle
149 static const size_t NUM_LINES_TO_SAMPLE = 10;
150
151 if ( count < 3*NUM_LINES_TO_SAMPLE )
152 {
153 // in this case calculating exactly is faster and more correct than
154 // guessing
155 m_heightTotal = GetLinesHeight(0, m_lineMax);
156 }
157 else // too many lines to calculate exactly
158 {
159 // look at some lines in the beginning/middle/end
160 m_heightTotal =
161 GetLinesHeight(0, NUM_LINES_TO_SAMPLE) +
162 GetLinesHeight(count - NUM_LINES_TO_SAMPLE, count) +
163 GetLinesHeight(count/2 - NUM_LINES_TO_SAMPLE/2,
164 count/2 + NUM_LINES_TO_SAMPLE/2);
165
166 // use the height of the lines we looked as the average
167 m_heightTotal = ((float)m_heightTotal / (3*NUM_LINES_TO_SAMPLE)) *
168 m_lineMax;
169 }
170
171
172 // recalculate the scrollbars parameters
173 m_lineFirst = 1; // make sure it is != 0
174 ScrollToLine(0);
175 }
176
177 void wxVScrolledWindow::RefreshLine(size_t line)
178 {
179 // is this line visible?
180 if ( !IsVisible(line) )
181 {
182 // no, it is useless to do anything
183 return;
184 }
185
186 // calculate the rect occupied by this line on screen
187 wxRect rect;
188 rect.width = GetClientSize().x;
189 rect.height = OnGetLineHeight(line);
190 for ( size_t n = GetFirstVisibleLine(); n < line; n++ )
191 {
192 rect.y += OnGetLineHeight(n);
193 }
194
195 // do refresh it
196 RefreshRect(rect);
197 }
198
199 int wxVScrolledWindow::HitTest(wxCoord WXUNUSED(x), wxCoord y) const
200 {
201 const size_t lineMax = GetLastVisibleLine();
202 for ( size_t line = GetFirstVisibleLine(); line <= lineMax; line++ )
203 {
204 y -= OnGetLineHeight(line);
205 if ( y < 0 )
206 return line;
207 }
208
209 return wxNOT_FOUND;
210 }
211
212 // ----------------------------------------------------------------------------
213 // scrolling
214 // ----------------------------------------------------------------------------
215
216 bool wxVScrolledWindow::ScrollToLine(size_t line)
217 {
218 if ( !m_lineMax )
219 {
220 // we're empty, code below doesn't make sense in this case
221 return false;
222 }
223
224 // determine the real first line to scroll to: we shouldn't scroll beyond
225 // the end
226 size_t lineFirstLast = FindFirstFromBottom(m_lineMax - 1);
227 if ( line > lineFirstLast )
228 line = lineFirstLast;
229
230 // anything to do?
231 if ( line == m_lineFirst )
232 {
233 // no
234 return false;
235 }
236
237
238 // remember the currently shown lines for the refresh code below
239 size_t lineFirstOld = GetFirstVisibleLine(),
240 lineLastOld = GetLastVisibleLine();
241
242 m_lineFirst = line;
243
244
245 // the size of scrollbar thumb could have changed
246 UpdateScrollbar();
247
248
249 // finally refresh the display -- but only redraw as few lines as possible
250 // to avoid flicker
251 if ( GetFirstVisibleLine() > lineLastOld ||
252 GetLastVisibleLine() < lineFirstOld )
253 {
254 // the simplest case: we don't have any old lines left, just redraw
255 // everything
256 Refresh();
257 }
258 else // overlap between the lines we showed before and should show now
259 {
260 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld));
261 }
262
263 return true;
264 }
265
266 bool wxVScrolledWindow::ScrollLines(int lines)
267 {
268 lines += m_lineFirst;
269 if ( lines < 0 )
270 lines = 0;
271
272 return ScrollToLine(lines);
273 }
274
275 bool wxVScrolledWindow::ScrollPages(int pages)
276 {
277 bool didSomething = false;
278
279 while ( pages )
280 {
281 int line;
282 if ( pages > 0 )
283 {
284 line = GetLastVisibleLine();
285 pages--;
286 }
287 else // pages < 0
288 {
289 line = FindFirstFromBottom(GetFirstVisibleLine());
290 pages++;
291 }
292
293 didSomething = ScrollToLine(line);
294 }
295
296 return didSomething;
297 }
298
299 // ----------------------------------------------------------------------------
300 // event handling
301 // ----------------------------------------------------------------------------
302
303 void wxVScrolledWindow::OnSize(wxSizeEvent& event)
304 {
305 UpdateScrollbar();
306
307 event.Skip();
308 }
309
310 void wxVScrolledWindow::OnScroll(wxScrollWinEvent& event)
311 {
312 size_t lineFirstNew;
313
314 const wxEventType evtType = event.GetEventType();
315 if ( evtType == wxEVT_SCROLLWIN_TOP )
316 {
317 lineFirstNew = 0;
318 }
319 else if ( evtType == wxEVT_SCROLLWIN_BOTTOM )
320 {
321 lineFirstNew = m_lineMax;
322 }
323 else if ( evtType == wxEVT_SCROLLWIN_LINEUP )
324 {
325 lineFirstNew = m_lineFirst ? m_lineFirst - 1 : 0;
326 }
327 else if ( evtType == wxEVT_SCROLLWIN_LINEDOWN )
328 {
329 lineFirstNew = m_lineFirst + 1;
330 }
331 else if ( evtType == wxEVT_SCROLLWIN_PAGEUP )
332 {
333 lineFirstNew = FindFirstFromBottom(m_lineFirst);
334 }
335 else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN )
336 {
337 lineFirstNew = GetLastVisibleLine();
338 }
339 else // unknown scroll event?
340 {
341 if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE )
342 {
343 lineFirstNew = event.GetPosition();
344 }
345 else
346 {
347 wxASSERT_MSG( evtType == wxEVT_SCROLLWIN_THUMBTRACK,
348 _T("unknown scroll event type?") );
349
350 // don't do anything, otherwise dragging the thumb around would
351 // be too slow
352 return;
353 }
354 }
355
356 ScrollToLine(lineFirstNew);
357 }
358