]> git.saurik.com Git - wxWidgets.git/blob - src/generic/vscroll.cpp
Added stub for the new pure virtual DoInsert method
[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, bool full)
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 // for this line to be fully visible we need to go one line
92 // down, but if it is enough for it to be only partly visible then
93 // this line will do as well
94 if ( full )
95 {
96 lineFirst++;
97 }
98
99 break;
100 }
101
102 if ( !lineFirst )
103 break;
104
105 lineFirst--;
106 }
107
108 return lineFirst;
109 }
110
111 void wxVScrolledWindow::UpdateScrollbar()
112 {
113 // see how many lines can we fit on screen
114 const wxCoord hWindow = GetClientSize().y;
115
116 wxCoord h = 0;
117 size_t line;
118 for ( line = m_lineFirst; line < m_lineMax; line++ )
119 {
120 if ( h > hWindow )
121 break;
122
123 h += OnGetLineHeight(line);
124 }
125
126 m_nVisible = line - m_lineFirst;
127
128 int pageSize = m_nVisible;
129 if ( h > hWindow )
130 {
131 // last line is only partially visible, we still need the scrollbar and
132 // so we have to "fix" pageSize because if it is equal to m_lineMax the
133 // scrollbar is not shown at all under MSW
134 pageSize--;
135 }
136
137 // set the scrollbar parameters to reflect this
138 SetScrollbar(wxVERTICAL, m_lineFirst, pageSize, m_lineMax);
139 }
140
141 // ----------------------------------------------------------------------------
142 // operations
143 // ----------------------------------------------------------------------------
144
145 void wxVScrolledWindow::SetLineCount(size_t count)
146 {
147 // save the number of lines
148 m_lineMax = count;
149
150
151 // estimate the total height: it is impossible to call
152 // OnGetLineHeight() for every line because there may be too many of
153 // them, so we just make a guess using some lines in the beginning,
154 // some in the end and some in the middle
155 static const size_t NUM_LINES_TO_SAMPLE = 10;
156
157 if ( count < 3*NUM_LINES_TO_SAMPLE )
158 {
159 // in this case calculating exactly is faster and more correct than
160 // guessing
161 m_heightTotal = GetLinesHeight(0, m_lineMax);
162 }
163 else // too many lines to calculate exactly
164 {
165 // look at some lines in the beginning/middle/end
166 m_heightTotal =
167 GetLinesHeight(0, NUM_LINES_TO_SAMPLE) +
168 GetLinesHeight(count - NUM_LINES_TO_SAMPLE, count) +
169 GetLinesHeight(count/2 - NUM_LINES_TO_SAMPLE/2,
170 count/2 + NUM_LINES_TO_SAMPLE/2);
171
172 // use the height of the lines we looked as the average
173 m_heightTotal = (wxCoord)
174 (((float)m_heightTotal / (3*NUM_LINES_TO_SAMPLE)) * m_lineMax);
175 }
176
177
178 // recalculate the scrollbars parameters
179 m_lineFirst = 1; // make sure it is != 0
180 ScrollToLine(0);
181 }
182
183 void wxVScrolledWindow::RefreshLine(size_t line)
184 {
185 // is this line visible?
186 if ( !IsVisible(line) )
187 {
188 // no, it is useless to do anything
189 return;
190 }
191
192 // calculate the rect occupied by this line on screen
193 wxRect rect;
194 rect.width = GetClientSize().x;
195 rect.height = OnGetLineHeight(line);
196 for ( size_t n = GetFirstVisibleLine(); n < line; n++ )
197 {
198 rect.y += OnGetLineHeight(n);
199 }
200
201 // do refresh it
202 RefreshRect(rect);
203 }
204
205 void wxVScrolledWindow::RefreshAll()
206 {
207 UpdateScrollbar();
208
209 Refresh();
210 }
211
212 int wxVScrolledWindow::HitTest(wxCoord WXUNUSED(x), wxCoord y) const
213 {
214 const size_t lineMax = GetLastVisibleLine();
215 for ( size_t line = GetFirstVisibleLine(); line <= lineMax; line++ )
216 {
217 y -= OnGetLineHeight(line);
218 if ( y < 0 )
219 return line;
220 }
221
222 return wxNOT_FOUND;
223 }
224
225 // ----------------------------------------------------------------------------
226 // scrolling
227 // ----------------------------------------------------------------------------
228
229 bool wxVScrolledWindow::ScrollToLine(size_t line)
230 {
231 if ( !m_lineMax )
232 {
233 // we're empty, code below doesn't make sense in this case
234 return false;
235 }
236
237 // determine the real first line to scroll to: we shouldn't scroll beyond
238 // the end
239 size_t lineFirstLast = FindFirstFromBottom(m_lineMax - 1, true);
240 if ( line > lineFirstLast )
241 line = lineFirstLast;
242
243 // anything to do?
244 if ( line == m_lineFirst )
245 {
246 // no
247 return false;
248 }
249
250
251 // remember the currently shown lines for the refresh code below
252 size_t lineFirstOld = GetFirstVisibleLine(),
253 lineLastOld = GetLastVisibleLine();
254
255 m_lineFirst = line;
256
257
258 // the size of scrollbar thumb could have changed
259 UpdateScrollbar();
260
261
262 // finally refresh the display -- but only redraw as few lines as possible
263 // to avoid flicker
264 if ( GetFirstVisibleLine() > lineLastOld ||
265 GetLastVisibleLine() < lineFirstOld )
266 {
267 // the simplest case: we don't have any old lines left, just redraw
268 // everything
269 Refresh();
270 }
271 else // overlap between the lines we showed before and should show now
272 {
273 ScrollWindow(0, GetLinesHeight(GetFirstVisibleLine(), lineFirstOld));
274 }
275
276 return true;
277 }
278
279 bool wxVScrolledWindow::ScrollLines(int lines)
280 {
281 lines += m_lineFirst;
282 if ( lines < 0 )
283 lines = 0;
284
285 return ScrollToLine(lines);
286 }
287
288 bool wxVScrolledWindow::ScrollPages(int pages)
289 {
290 bool didSomething = false;
291
292 while ( pages )
293 {
294 int line;
295 if ( pages > 0 )
296 {
297 line = GetLastVisibleLine();
298 pages--;
299 }
300 else // pages < 0
301 {
302 line = FindFirstFromBottom(GetFirstVisibleLine());
303 pages++;
304 }
305
306 didSomething = ScrollToLine(line);
307 }
308
309 return didSomething;
310 }
311
312 // ----------------------------------------------------------------------------
313 // event handling
314 // ----------------------------------------------------------------------------
315
316 void wxVScrolledWindow::OnSize(wxSizeEvent& event)
317 {
318 UpdateScrollbar();
319
320 event.Skip();
321 }
322
323 void wxVScrolledWindow::OnScroll(wxScrollWinEvent& event)
324 {
325 size_t lineFirstNew;
326
327 const wxEventType evtType = event.GetEventType();
328 if ( evtType == wxEVT_SCROLLWIN_TOP )
329 {
330 lineFirstNew = 0;
331 }
332 else if ( evtType == wxEVT_SCROLLWIN_BOTTOM )
333 {
334 lineFirstNew = m_lineMax;
335 }
336 else if ( evtType == wxEVT_SCROLLWIN_LINEUP )
337 {
338 lineFirstNew = m_lineFirst ? m_lineFirst - 1 : 0;
339 }
340 else if ( evtType == wxEVT_SCROLLWIN_LINEDOWN )
341 {
342 lineFirstNew = m_lineFirst + 1;
343 }
344 else if ( evtType == wxEVT_SCROLLWIN_PAGEUP )
345 {
346 lineFirstNew = FindFirstFromBottom(m_lineFirst);
347 }
348 else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN )
349 {
350 lineFirstNew = GetLastVisibleLine();
351 }
352 else // unknown scroll event?
353 {
354 if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE )
355 {
356 lineFirstNew = event.GetPosition();
357 }
358 else
359 {
360 wxASSERT_MSG( evtType == wxEVT_SCROLLWIN_THUMBTRACK,
361 _T("unknown scroll event type?") );
362
363 // don't do anything, otherwise dragging the thumb around would
364 // be too slow
365 return;
366 }
367 }
368
369 ScrollToLine(lineFirstNew);
370
371 #ifdef __WXMAC__
372 Update();
373 #endif // __WXMAC__
374 }
375