]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/vscroll.cpp | |
3 | // Purpose: wxVScrolledWindow implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: Brad Anderson, David Warkentin | |
6 | // Created: 30.05.03 | |
7 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/dc.h" | |
28 | #include "wx/sizer.h" | |
29 | #endif | |
30 | ||
31 | #include "wx/vscroll.h" | |
32 | ||
33 | #include "wx/utils.h" // For wxMin/wxMax(). | |
34 | ||
35 | // ============================================================================ | |
36 | // wxVarScrollHelperEvtHandler declaration | |
37 | // ============================================================================ | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // wxScrollHelperEvtHandler: intercept the events from the window and forward | |
41 | // them to wxVarScrollHelperBase | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | class WXDLLEXPORT wxVarScrollHelperEvtHandler : public wxEvtHandler | |
45 | { | |
46 | public: | |
47 | wxVarScrollHelperEvtHandler(wxVarScrollHelperBase *scrollHelper) | |
48 | { | |
49 | m_scrollHelper = scrollHelper; | |
50 | } | |
51 | ||
52 | virtual bool ProcessEvent(wxEvent& event); | |
53 | ||
54 | private: | |
55 | wxVarScrollHelperBase *m_scrollHelper; | |
56 | ||
57 | wxDECLARE_NO_COPY_CLASS(wxVarScrollHelperEvtHandler); | |
58 | }; | |
59 | ||
60 | // ============================================================================ | |
61 | // wxVarScrollHelperEvtHandler implementation | |
62 | // ============================================================================ | |
63 | ||
64 | // FIXME: This method totally duplicates a method with the same name in | |
65 | // wxScrollHelperEvtHandler, we really should merge them by reusing the | |
66 | // common parts in wxAnyScrollHelperBase. | |
67 | bool wxVarScrollHelperEvtHandler::ProcessEvent(wxEvent& event) | |
68 | { | |
69 | wxEventType evType = event.GetEventType(); | |
70 | ||
71 | // Pass it on to the real handler: notice that we must not call | |
72 | // ProcessEvent() on this object itself as it wouldn't pass it to the next | |
73 | // handler (i.e. the real window) if we're called from a previous handler | |
74 | // (as indicated by "process here only" flag being set) and we do want to | |
75 | // execute the handler defined in the window we're associated with right | |
76 | // now, without waiting until TryAfter() is called from wxEvtHandler. | |
77 | bool processed = m_nextHandler->ProcessEvent(event); | |
78 | ||
79 | // always process the size events ourselves, even if the user code handles | |
80 | // them as well, as we need to AdjustScrollbars() | |
81 | // | |
82 | // NB: it is important to do it after processing the event in the normal | |
83 | // way as HandleOnSize() may generate a wxEVT_SIZE itself if the | |
84 | // scrollbar[s] (dis)appear and it should be seen by the user code | |
85 | // after this one | |
86 | if ( evType == wxEVT_SIZE ) | |
87 | { | |
88 | m_scrollHelper->HandleOnSize((wxSizeEvent &)event); | |
89 | return true; | |
90 | } | |
91 | ||
92 | if ( processed && event.IsCommandEvent()) | |
93 | return true; | |
94 | ||
95 | // For wxEVT_PAINT the user code can either handle this event as usual or | |
96 | // override virtual OnDraw(), so if the event hasn't been handled we need | |
97 | // to call this virtual function ourselves. | |
98 | if ( | |
99 | #ifndef __WXUNIVERSAL__ | |
100 | // in wxUniversal "processed" will always be true, because | |
101 | // all windows use the paint event to draw themselves. | |
102 | // In this case we can't use this flag to determine if a custom | |
103 | // paint event handler already drew our window and we just | |
104 | // call OnDraw() anyway. | |
105 | !processed && | |
106 | #endif // !__WXUNIVERSAL__ | |
107 | evType == wxEVT_PAINT ) | |
108 | { | |
109 | m_scrollHelper->HandleOnPaint((wxPaintEvent &)event); | |
110 | return true; | |
111 | } | |
112 | ||
113 | // reset the skipped flag (which might have been set to true in | |
114 | // ProcessEvent() above) to be able to test it below | |
115 | bool wasSkipped = event.GetSkipped(); | |
116 | if ( wasSkipped ) | |
117 | event.Skip(false); | |
118 | ||
119 | if ( evType == wxEVT_SCROLLWIN_TOP || | |
120 | evType == wxEVT_SCROLLWIN_BOTTOM || | |
121 | evType == wxEVT_SCROLLWIN_LINEUP || | |
122 | evType == wxEVT_SCROLLWIN_LINEDOWN || | |
123 | evType == wxEVT_SCROLLWIN_PAGEUP || | |
124 | evType == wxEVT_SCROLLWIN_PAGEDOWN || | |
125 | evType == wxEVT_SCROLLWIN_THUMBTRACK || | |
126 | evType == wxEVT_SCROLLWIN_THUMBRELEASE ) | |
127 | { | |
128 | m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event); | |
129 | if ( !event.GetSkipped() ) | |
130 | { | |
131 | // it makes sense to indicate that we processed the message as we | |
132 | // did scroll the window (and also notice that wxAutoScrollTimer | |
133 | // relies on our return value to stop scrolling when we are at top | |
134 | // or bottom already) | |
135 | processed = true; | |
136 | wasSkipped = false; | |
137 | } | |
138 | } | |
139 | #if wxUSE_MOUSEWHEEL | |
140 | // Use GTK's own scroll wheel handling in GtkScrolledWindow | |
141 | #ifndef __WXGTK20__ | |
142 | else if ( evType == wxEVT_MOUSEWHEEL ) | |
143 | { | |
144 | m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event); | |
145 | } | |
146 | #endif | |
147 | #endif // wxUSE_MOUSEWHEEL | |
148 | else if ( evType == wxEVT_CHAR && | |
149 | (m_scrollHelper->GetOrientation() == wxVERTICAL) ) | |
150 | { | |
151 | m_scrollHelper->HandleOnChar((wxKeyEvent &)event); | |
152 | if ( !event.GetSkipped() ) | |
153 | { | |
154 | processed = true; | |
155 | wasSkipped = false; | |
156 | } | |
157 | } | |
158 | ||
159 | event.Skip(wasSkipped); | |
160 | ||
161 | // We called ProcessEvent() on the next handler, meaning that we explicitly | |
162 | // worked around the request to process the event in this handler only. As | |
163 | // explained above, this is unfortunately really necessary but the trouble | |
164 | // is that the event will continue to be post-processed by the previous | |
165 | // handler resulting in duplicate calls to event handlers. Call the special | |
166 | // function below to prevent this from happening, base class DoTryChain() | |
167 | // will check for it and behave accordingly. | |
168 | // | |
169 | // And if we're not called from DoTryChain(), this won't do anything anyhow. | |
170 | event.DidntHonourProcessOnlyIn(); | |
171 | ||
172 | return processed; | |
173 | } | |
174 | ||
175 | ||
176 | // ============================================================================ | |
177 | // wxVarScrollHelperBase implementation | |
178 | // ============================================================================ | |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // wxVarScrollHelperBase initialization | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | wxVarScrollHelperBase::wxVarScrollHelperBase(wxWindow *win) | |
185 | : wxAnyScrollHelperBase(win) | |
186 | { | |
187 | #if wxUSE_MOUSEWHEEL | |
188 | m_sumWheelRotation = 0; | |
189 | #endif | |
190 | ||
191 | m_unitMax = 0; | |
192 | m_sizeTotal = 0; | |
193 | m_unitFirst = 0; | |
194 | ||
195 | m_physicalScrolling = true; | |
196 | m_handler = NULL; | |
197 | ||
198 | // by default, the associated window is also the target window | |
199 | DoSetTargetWindow(win); | |
200 | } | |
201 | ||
202 | wxVarScrollHelperBase::~wxVarScrollHelperBase() | |
203 | { | |
204 | DeleteEvtHandler(); | |
205 | } | |
206 | ||
207 | // ---------------------------------------------------------------------------- | |
208 | // wxVarScrollHelperBase various helpers | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
211 | void | |
212 | wxVarScrollHelperBase::AssignOrient(wxCoord& x, | |
213 | wxCoord& y, | |
214 | wxCoord first, | |
215 | wxCoord second) | |
216 | { | |
217 | if ( GetOrientation() == wxVERTICAL ) | |
218 | { | |
219 | x = first; | |
220 | y = second; | |
221 | } | |
222 | else // horizontal | |
223 | { | |
224 | x = second; | |
225 | y = first; | |
226 | } | |
227 | } | |
228 | ||
229 | void | |
230 | wxVarScrollHelperBase::IncOrient(wxCoord& x, wxCoord& y, wxCoord inc) | |
231 | { | |
232 | if ( GetOrientation() == wxVERTICAL ) | |
233 | y += inc; | |
234 | else | |
235 | x += inc; | |
236 | } | |
237 | ||
238 | wxCoord wxVarScrollHelperBase::DoEstimateTotalSize() const | |
239 | { | |
240 | // estimate the total height: it is impossible to call | |
241 | // OnGetUnitSize() for every unit because there may be too many of | |
242 | // them, so we just make a guess using some units in the beginning, | |
243 | // some in the end and some in the middle | |
244 | static const size_t NUM_UNITS_TO_SAMPLE = 10; | |
245 | ||
246 | wxCoord sizeTotal; | |
247 | if ( m_unitMax < 3*NUM_UNITS_TO_SAMPLE ) | |
248 | { | |
249 | // in this case, full calculations are faster and more correct than | |
250 | // guessing | |
251 | sizeTotal = GetUnitsSize(0, m_unitMax); | |
252 | } | |
253 | else // too many units to calculate exactly | |
254 | { | |
255 | // look at some units in the beginning/middle/end | |
256 | sizeTotal = | |
257 | GetUnitsSize(0, NUM_UNITS_TO_SAMPLE) + | |
258 | GetUnitsSize(m_unitMax - NUM_UNITS_TO_SAMPLE, | |
259 | m_unitMax) + | |
260 | GetUnitsSize(m_unitMax/2 - NUM_UNITS_TO_SAMPLE/2, | |
261 | m_unitMax/2 + NUM_UNITS_TO_SAMPLE/2); | |
262 | ||
263 | // use the height of the units we looked as the average | |
264 | sizeTotal = (wxCoord) | |
265 | (((float)sizeTotal / (3*NUM_UNITS_TO_SAMPLE)) * m_unitMax); | |
266 | } | |
267 | ||
268 | return sizeTotal; | |
269 | } | |
270 | ||
271 | wxCoord wxVarScrollHelperBase::GetUnitsSize(size_t unitMin, size_t unitMax) const | |
272 | { | |
273 | if ( unitMin == unitMax ) | |
274 | return 0; | |
275 | else if ( unitMin > unitMax ) | |
276 | return -GetUnitsSize(unitMax, unitMin); | |
277 | //else: unitMin < unitMax | |
278 | ||
279 | // let the user code know that we're going to need all these units | |
280 | OnGetUnitsSizeHint(unitMin, unitMax); | |
281 | ||
282 | // sum up their sizes | |
283 | wxCoord size = 0; | |
284 | for ( size_t unit = unitMin; unit < unitMax; ++unit ) | |
285 | { | |
286 | size += OnGetUnitSize(unit); | |
287 | } | |
288 | ||
289 | return size; | |
290 | } | |
291 | ||
292 | size_t wxVarScrollHelperBase::FindFirstVisibleFromLast(size_t unitLast, bool full) const | |
293 | { | |
294 | const wxCoord sWindow = GetOrientationTargetSize(); | |
295 | ||
296 | // go upwards until we arrive at a unit such that unitLast is not visible | |
297 | // any more when it is shown | |
298 | size_t unitFirst = unitLast; | |
299 | wxCoord s = 0; | |
300 | for ( ;; ) | |
301 | { | |
302 | s += OnGetUnitSize(unitFirst); | |
303 | ||
304 | if ( s > sWindow ) | |
305 | { | |
306 | // for this unit to be fully visible we need to go one unit | |
307 | // down, but if it is enough for it to be only partly visible then | |
308 | // this unit will do as well | |
309 | if ( full ) | |
310 | { | |
311 | ++unitFirst; | |
312 | } | |
313 | ||
314 | break; | |
315 | } | |
316 | ||
317 | if ( !unitFirst ) | |
318 | break; | |
319 | ||
320 | --unitFirst; | |
321 | } | |
322 | ||
323 | return unitFirst; | |
324 | } | |
325 | ||
326 | size_t wxVarScrollHelperBase::GetNewScrollPosition(wxScrollWinEvent& event) const | |
327 | { | |
328 | wxEventType evtType = event.GetEventType(); | |
329 | ||
330 | if ( evtType == wxEVT_SCROLLWIN_TOP ) | |
331 | { | |
332 | return 0; | |
333 | } | |
334 | else if ( evtType == wxEVT_SCROLLWIN_BOTTOM ) | |
335 | { | |
336 | return m_unitMax; | |
337 | } | |
338 | else if ( evtType == wxEVT_SCROLLWIN_LINEUP ) | |
339 | { | |
340 | return m_unitFirst ? m_unitFirst - 1 : 0; | |
341 | } | |
342 | else if ( evtType == wxEVT_SCROLLWIN_LINEDOWN ) | |
343 | { | |
344 | return m_unitFirst + 1; | |
345 | } | |
346 | else if ( evtType == wxEVT_SCROLLWIN_PAGEUP ) | |
347 | { | |
348 | // Page up should do at least as much as line up. | |
349 | return wxMin(FindFirstVisibleFromLast(m_unitFirst), | |
350 | m_unitFirst ? m_unitFirst - 1 : 0); | |
351 | } | |
352 | else if ( evtType == wxEVT_SCROLLWIN_PAGEDOWN ) | |
353 | { | |
354 | // And page down should do at least as much as line down. | |
355 | if ( GetVisibleEnd() ) | |
356 | return wxMax(GetVisibleEnd() - 1, m_unitFirst + 1); | |
357 | else | |
358 | return wxMax(GetVisibleEnd(), m_unitFirst + 1); | |
359 | } | |
360 | else if ( evtType == wxEVT_SCROLLWIN_THUMBRELEASE ) | |
361 | { | |
362 | return event.GetPosition(); | |
363 | } | |
364 | else if ( evtType == wxEVT_SCROLLWIN_THUMBTRACK ) | |
365 | { | |
366 | return event.GetPosition(); | |
367 | } | |
368 | ||
369 | // unknown scroll event? | |
370 | wxFAIL_MSG( wxT("unknown scroll event type?") ); | |
371 | return 0; | |
372 | } | |
373 | ||
374 | void wxVarScrollHelperBase::UpdateScrollbar() | |
375 | { | |
376 | // if there is nothing to scroll, remove the scrollbar | |
377 | if ( !m_unitMax ) | |
378 | { | |
379 | RemoveScrollbar(); | |
380 | return; | |
381 | } | |
382 | ||
383 | // see how many units can we fit on screen | |
384 | const wxCoord sWindow = GetOrientationTargetSize(); | |
385 | ||
386 | // do vertical calculations | |
387 | wxCoord s = 0; | |
388 | size_t unit; | |
389 | for ( unit = m_unitFirst; unit < m_unitMax; ++unit ) | |
390 | { | |
391 | if ( s > sWindow ) | |
392 | break; | |
393 | ||
394 | s += OnGetUnitSize(unit); | |
395 | } | |
396 | ||
397 | m_nUnitsVisible = unit - m_unitFirst; | |
398 | ||
399 | int unitsPageSize = m_nUnitsVisible; | |
400 | if ( s > sWindow ) | |
401 | { | |
402 | // last unit is only partially visible, we still need the scrollbar and | |
403 | // so we have to "fix" pageSize because if it is equal to m_unitMax | |
404 | // the scrollbar is not shown at all under MSW | |
405 | --unitsPageSize; | |
406 | } | |
407 | ||
408 | // set the scrollbar parameters to reflect this | |
409 | m_win->SetScrollbar(GetOrientation(), m_unitFirst, unitsPageSize, m_unitMax); | |
410 | } | |
411 | ||
412 | void wxVarScrollHelperBase::RemoveScrollbar() | |
413 | { | |
414 | m_unitFirst = 0; | |
415 | m_nUnitsVisible = m_unitMax; | |
416 | m_win->SetScrollbar(GetOrientation(), 0, 0, 0); | |
417 | } | |
418 | ||
419 | void wxVarScrollHelperBase::DeleteEvtHandler() | |
420 | { | |
421 | // search for m_handler in the handler list | |
422 | if ( m_win && m_handler ) | |
423 | { | |
424 | if ( m_win->RemoveEventHandler(m_handler) ) | |
425 | { | |
426 | delete m_handler; | |
427 | } | |
428 | //else: something is very wrong, so better [maybe] leak memory than | |
429 | // risk a crash because of double deletion | |
430 | ||
431 | m_handler = NULL; | |
432 | } | |
433 | } | |
434 | ||
435 | void wxVarScrollHelperBase::DoSetTargetWindow(wxWindow *target) | |
436 | { | |
437 | m_targetWindow = target; | |
438 | #ifdef __WXMAC__ | |
439 | target->MacSetClipChildren( true ) ; | |
440 | #endif | |
441 | ||
442 | // install the event handler which will intercept the events we're | |
443 | // interested in (but only do it for our real window, not the target window | |
444 | // which we scroll - we don't need to hijack its events) | |
445 | if ( m_targetWindow == m_win ) | |
446 | { | |
447 | // if we already have a handler, delete it first | |
448 | DeleteEvtHandler(); | |
449 | ||
450 | m_handler = new wxVarScrollHelperEvtHandler(this); | |
451 | m_targetWindow->PushEventHandler(m_handler); | |
452 | } | |
453 | } | |
454 | ||
455 | // ---------------------------------------------------------------------------- | |
456 | // wxVarScrollHelperBase operations | |
457 | // ---------------------------------------------------------------------------- | |
458 | ||
459 | void wxVarScrollHelperBase::SetTargetWindow(wxWindow *target) | |
460 | { | |
461 | wxCHECK_RET( target, wxT("target window must not be NULL") ); | |
462 | ||
463 | if ( target == m_targetWindow ) | |
464 | return; | |
465 | ||
466 | DoSetTargetWindow(target); | |
467 | } | |
468 | ||
469 | void wxVarScrollHelperBase::SetUnitCount(size_t count) | |
470 | { | |
471 | // save the number of units | |
472 | m_unitMax = count; | |
473 | ||
474 | // and our estimate for their total height | |
475 | m_sizeTotal = EstimateTotalSize(); | |
476 | ||
477 | // ScrollToUnit() will update the scrollbar itself if it changes the unit | |
478 | // we pass to it because it's out of [new] range | |
479 | size_t oldScrollPos = m_unitFirst; | |
480 | DoScrollToUnit(m_unitFirst); | |
481 | if ( oldScrollPos == m_unitFirst ) | |
482 | { | |
483 | // but if it didn't do it, we still need to update the scrollbar to | |
484 | // reflect the changed number of units ourselves | |
485 | UpdateScrollbar(); | |
486 | } | |
487 | } | |
488 | ||
489 | void wxVarScrollHelperBase::RefreshUnit(size_t unit) | |
490 | { | |
491 | // is this unit visible? | |
492 | if ( !IsVisible(unit) ) | |
493 | { | |
494 | // no, it is useless to do anything | |
495 | return; | |
496 | } | |
497 | ||
498 | // calculate the rect occupied by this unit on screen | |
499 | wxRect rect; | |
500 | AssignOrient(rect.width, rect.height, | |
501 | GetNonOrientationTargetSize(), OnGetUnitSize(unit)); | |
502 | ||
503 | for ( size_t n = GetVisibleBegin(); n < unit; ++n ) | |
504 | { | |
505 | IncOrient(rect.x, rect.y, OnGetUnitSize(n)); | |
506 | } | |
507 | ||
508 | // do refresh it | |
509 | m_targetWindow->RefreshRect(rect); | |
510 | } | |
511 | ||
512 | void wxVarScrollHelperBase::RefreshUnits(size_t from, size_t to) | |
513 | { | |
514 | wxASSERT_MSG( from <= to, wxT("RefreshUnits(): empty range") ); | |
515 | ||
516 | // clump the range to just the visible units -- it is useless to refresh | |
517 | // the other ones | |
518 | if ( from < GetVisibleBegin() ) | |
519 | from = GetVisibleBegin(); | |
520 | ||
521 | if ( to > GetVisibleEnd() ) | |
522 | to = GetVisibleEnd(); | |
523 | ||
524 | // calculate the rect occupied by these units on screen | |
525 | int orient_size = 0, | |
526 | orient_pos = 0; | |
527 | ||
528 | int nonorient_size = GetNonOrientationTargetSize(); | |
529 | ||
530 | for ( size_t nBefore = GetVisibleBegin(); | |
531 | nBefore < from; | |
532 | nBefore++ ) | |
533 | { | |
534 | orient_pos += OnGetUnitSize(nBefore); | |
535 | } | |
536 | ||
537 | for ( size_t nBetween = from; nBetween <= to; nBetween++ ) | |
538 | { | |
539 | orient_size += OnGetUnitSize(nBetween); | |
540 | } | |
541 | ||
542 | wxRect rect; | |
543 | AssignOrient(rect.x, rect.y, 0, orient_pos); | |
544 | AssignOrient(rect.width, rect.height, nonorient_size, orient_size); | |
545 | ||
546 | // do refresh it | |
547 | m_targetWindow->RefreshRect(rect); | |
548 | } | |
549 | ||
550 | void wxVarScrollHelperBase::RefreshAll() | |
551 | { | |
552 | UpdateScrollbar(); | |
553 | ||
554 | m_targetWindow->Refresh(); | |
555 | } | |
556 | ||
557 | bool wxVarScrollHelperBase::ScrollLayout() | |
558 | { | |
559 | if ( m_targetWindow->GetSizer() && m_physicalScrolling ) | |
560 | { | |
561 | // adjust the sizer dimensions/position taking into account the | |
562 | // virtual size and scrolled position of the window. | |
563 | ||
564 | int x, y; | |
565 | AssignOrient(x, y, 0, -GetScrollOffset()); | |
566 | ||
567 | int w, h; | |
568 | m_targetWindow->GetVirtualSize(&w, &h); | |
569 | ||
570 | m_targetWindow->GetSizer()->SetDimension(x, y, w, h); | |
571 | return true; | |
572 | } | |
573 | ||
574 | // fall back to default for LayoutConstraints | |
575 | return m_targetWindow->wxWindow::Layout(); | |
576 | } | |
577 | ||
578 | int wxVarScrollHelperBase::VirtualHitTest(wxCoord coord) const | |
579 | { | |
580 | const size_t unitMax = GetVisibleEnd(); | |
581 | for ( size_t unit = GetVisibleBegin(); unit < unitMax; ++unit ) | |
582 | { | |
583 | coord -= OnGetUnitSize(unit); | |
584 | if ( coord < 0 ) | |
585 | return unit; | |
586 | } | |
587 | ||
588 | return wxNOT_FOUND; | |
589 | } | |
590 | ||
591 | // ---------------------------------------------------------------------------- | |
592 | // wxVarScrollHelperBase scrolling | |
593 | // ---------------------------------------------------------------------------- | |
594 | ||
595 | bool wxVarScrollHelperBase::DoScrollToUnit(size_t unit) | |
596 | { | |
597 | if ( !m_unitMax ) | |
598 | { | |
599 | // we're empty, code below doesn't make sense in this case | |
600 | return false; | |
601 | } | |
602 | ||
603 | // determine the real first unit to scroll to: we shouldn't scroll beyond | |
604 | // the end | |
605 | size_t unitFirstLast = FindFirstVisibleFromLast(m_unitMax - 1, true); | |
606 | if ( unit > unitFirstLast ) | |
607 | unit = unitFirstLast; | |
608 | ||
609 | // anything to do? | |
610 | if ( unit == m_unitFirst ) | |
611 | { | |
612 | // no | |
613 | return false; | |
614 | } | |
615 | ||
616 | ||
617 | // remember the currently shown units for the refresh code below | |
618 | size_t unitFirstOld = GetVisibleBegin(), | |
619 | unitLastOld = GetVisibleEnd(); | |
620 | ||
621 | m_unitFirst = unit; | |
622 | ||
623 | ||
624 | // the size of scrollbar thumb could have changed | |
625 | UpdateScrollbar(); | |
626 | ||
627 | // finally refresh the display -- but only redraw as few units as possible | |
628 | // to avoid flicker. We can't do this if we have children because they | |
629 | // won't be scrolled | |
630 | if ( m_targetWindow->GetChildren().empty() && | |
631 | (GetVisibleBegin() >= unitLastOld || GetVisibleEnd() <= unitFirstOld) ) | |
632 | { | |
633 | // the simplest case: we don't have any old units left, just redraw | |
634 | // everything | |
635 | m_targetWindow->Refresh(); | |
636 | } | |
637 | else // scroll the window | |
638 | { | |
639 | // Avoid scrolling visible parts of the screen on Mac | |
640 | #ifdef __WXMAC__ | |
641 | if (m_physicalScrolling && m_targetWindow->IsShownOnScreen()) | |
642 | #else | |
643 | if ( m_physicalScrolling ) | |
644 | #endif | |
645 | { | |
646 | wxCoord dx = 0, | |
647 | dy = GetUnitsSize(GetVisibleBegin(), unitFirstOld); | |
648 | ||
649 | if ( GetOrientation() == wxHORIZONTAL ) | |
650 | { | |
651 | wxCoord tmp = dx; | |
652 | dx = dy; | |
653 | dy = tmp; | |
654 | } | |
655 | ||
656 | m_targetWindow->ScrollWindow(dx, dy); | |
657 | } | |
658 | else // !m_physicalScrolling | |
659 | { | |
660 | // we still need to invalidate but we can't use ScrollWindow | |
661 | // because physical scrolling is disabled (the user either didn't | |
662 | // want children scrolled and/or doesn't want pixels to be | |
663 | // physically scrolled). | |
664 | m_targetWindow->Refresh(); | |
665 | } | |
666 | } | |
667 | ||
668 | return true; | |
669 | } | |
670 | ||
671 | bool wxVarScrollHelperBase::DoScrollUnits(int units) | |
672 | { | |
673 | units += m_unitFirst; | |
674 | if ( units < 0 ) | |
675 | units = 0; | |
676 | ||
677 | return DoScrollToUnit(units); | |
678 | } | |
679 | ||
680 | bool wxVarScrollHelperBase::DoScrollPages(int pages) | |
681 | { | |
682 | bool didSomething = false; | |
683 | ||
684 | while ( pages ) | |
685 | { | |
686 | int unit; | |
687 | if ( pages > 0 ) | |
688 | { | |
689 | unit = GetVisibleEnd(); | |
690 | if ( unit ) | |
691 | --unit; | |
692 | --pages; | |
693 | } | |
694 | else // pages < 0 | |
695 | { | |
696 | unit = FindFirstVisibleFromLast(GetVisibleEnd()); | |
697 | ++pages; | |
698 | } | |
699 | ||
700 | didSomething = DoScrollToUnit(unit); | |
701 | } | |
702 | ||
703 | return didSomething; | |
704 | } | |
705 | ||
706 | // ---------------------------------------------------------------------------- | |
707 | // event handling | |
708 | // ---------------------------------------------------------------------------- | |
709 | ||
710 | void wxVarScrollHelperBase::HandleOnSize(wxSizeEvent& event) | |
711 | { | |
712 | if ( m_unitMax ) | |
713 | { | |
714 | // sometimes change in varscrollable window's size can result in | |
715 | // unused empty space after the last item. Fix it by decrementing | |
716 | // first visible item position according to the available space. | |
717 | ||
718 | // determine free space | |
719 | const wxCoord sWindow = GetOrientationTargetSize(); | |
720 | wxCoord s = 0; | |
721 | size_t unit; | |
722 | for ( unit = m_unitFirst; unit < m_unitMax; ++unit ) | |
723 | { | |
724 | if ( s > sWindow ) | |
725 | break; | |
726 | ||
727 | s += OnGetUnitSize(unit); | |
728 | } | |
729 | wxCoord freeSpace = sWindow - s; | |
730 | ||
731 | // decrement first visible item index as long as there is free space | |
732 | size_t idealUnitFirst; | |
733 | for ( idealUnitFirst = m_unitFirst; | |
734 | idealUnitFirst > 0; | |
735 | idealUnitFirst-- ) | |
736 | { | |
737 | wxCoord us = OnGetUnitSize(idealUnitFirst-1); | |
738 | if ( freeSpace < us ) | |
739 | break; | |
740 | freeSpace -= us; | |
741 | } | |
742 | m_unitFirst = idealUnitFirst; | |
743 | } | |
744 | ||
745 | UpdateScrollbar(); | |
746 | ||
747 | event.Skip(); | |
748 | } | |
749 | ||
750 | void wxVarScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event) | |
751 | { | |
752 | if (GetOrientation() != event.GetOrientation()) | |
753 | { | |
754 | event.Skip(); | |
755 | return; | |
756 | } | |
757 | ||
758 | DoScrollToUnit(GetNewScrollPosition(event)); | |
759 | ||
760 | #ifdef __WXMAC__ | |
761 | UpdateMacScrollWindow(); | |
762 | #endif // __WXMAC__ | |
763 | } | |
764 | ||
765 | void wxVarScrollHelperBase::DoPrepareDC(wxDC& dc) | |
766 | { | |
767 | if ( m_physicalScrolling ) | |
768 | { | |
769 | wxPoint pt = dc.GetDeviceOrigin(); | |
770 | ||
771 | IncOrient(pt.x, pt.y, -GetScrollOffset()); | |
772 | ||
773 | dc.SetDeviceOrigin(pt.x, pt.y); | |
774 | } | |
775 | } | |
776 | ||
777 | int wxVarScrollHelperBase::DoCalcScrolledPosition(int coord) const | |
778 | { | |
779 | return coord - GetScrollOffset(); | |
780 | } | |
781 | ||
782 | int wxVarScrollHelperBase::DoCalcUnscrolledPosition(int coord) const | |
783 | { | |
784 | return coord + GetScrollOffset(); | |
785 | } | |
786 | ||
787 | #if wxUSE_MOUSEWHEEL | |
788 | ||
789 | void wxVarScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event) | |
790 | { | |
791 | // we only want to process wheel events for vertical implementations. | |
792 | // There is no way to determine wheel orientation (and on MSW horizontal | |
793 | // wheel rotation just fakes scroll events, rather than sending a MOUSEWHEEL | |
794 | // event). | |
795 | if ( GetOrientation() != wxVERTICAL ) | |
796 | return; | |
797 | ||
798 | m_sumWheelRotation += event.GetWheelRotation(); | |
799 | int delta = event.GetWheelDelta(); | |
800 | ||
801 | // how much to scroll this time | |
802 | int units_to_scroll = -(m_sumWheelRotation/delta); | |
803 | if ( !units_to_scroll ) | |
804 | return; | |
805 | ||
806 | m_sumWheelRotation += units_to_scroll*delta; | |
807 | ||
808 | if ( !event.IsPageScroll() ) | |
809 | DoScrollUnits( units_to_scroll*event.GetLinesPerAction() ); | |
810 | else // scroll pages instead of units | |
811 | DoScrollPages( units_to_scroll ); | |
812 | } | |
813 | ||
814 | #endif // wxUSE_MOUSEWHEEL | |
815 | ||
816 | ||
817 | // ============================================================================ | |
818 | // wxVarHVScrollHelper implementation | |
819 | // ============================================================================ | |
820 | ||
821 | // ---------------------------------------------------------------------------- | |
822 | // wxVarHVScrollHelper operations | |
823 | // ---------------------------------------------------------------------------- | |
824 | ||
825 | void wxVarHVScrollHelper::SetRowColumnCount(size_t rowCount, size_t columnCount) | |
826 | { | |
827 | SetRowCount(rowCount); | |
828 | SetColumnCount(columnCount); | |
829 | } | |
830 | ||
831 | bool wxVarHVScrollHelper::ScrollToRowColumn(size_t row, size_t column) | |
832 | { | |
833 | bool result = false; | |
834 | result |= ScrollToRow(row); | |
835 | result |= ScrollToColumn(column); | |
836 | return result; | |
837 | } | |
838 | ||
839 | void wxVarHVScrollHelper::RefreshRowColumn(size_t row, size_t column) | |
840 | { | |
841 | // is this unit visible? | |
842 | if ( !IsRowVisible(row) || !IsColumnVisible(column) ) | |
843 | { | |
844 | // no, it is useless to do anything | |
845 | return; | |
846 | } | |
847 | ||
848 | // calculate the rect occupied by this cell on screen | |
849 | wxRect v_rect, h_rect; | |
850 | v_rect.height = OnGetRowHeight(row); | |
851 | h_rect.width = OnGetColumnWidth(column); | |
852 | ||
853 | size_t n; | |
854 | ||
855 | for ( n = GetVisibleRowsBegin(); n < row; n++ ) | |
856 | { | |
857 | v_rect.y += OnGetRowHeight(n); | |
858 | } | |
859 | ||
860 | for ( n = GetVisibleColumnsBegin(); n < column; n++ ) | |
861 | { | |
862 | h_rect.x += OnGetColumnWidth(n); | |
863 | } | |
864 | ||
865 | // refresh but specialize the behaviour if we have a single target window | |
866 | if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() ) | |
867 | { | |
868 | v_rect.x = h_rect.x; | |
869 | v_rect.width = h_rect.width; | |
870 | wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect); | |
871 | } | |
872 | else | |
873 | { | |
874 | v_rect.x = 0; | |
875 | v_rect.width = wxVarVScrollHelper::GetNonOrientationTargetSize(); | |
876 | h_rect.y = 0; | |
877 | h_rect.width = wxVarHScrollHelper::GetNonOrientationTargetSize(); | |
878 | ||
879 | wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect); | |
880 | wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect); | |
881 | } | |
882 | } | |
883 | ||
884 | void wxVarHVScrollHelper::RefreshRowsColumns(size_t fromRow, size_t toRow, | |
885 | size_t fromColumn, size_t toColumn) | |
886 | { | |
887 | wxASSERT_MSG( fromRow <= toRow || fromColumn <= toColumn, | |
888 | wxT("RefreshRowsColumns(): empty range") ); | |
889 | ||
890 | // clump the range to just the visible units -- it is useless to refresh | |
891 | // the other ones | |
892 | if ( fromRow < GetVisibleRowsBegin() ) | |
893 | fromRow = GetVisibleRowsBegin(); | |
894 | ||
895 | if ( toRow > GetVisibleRowsEnd() ) | |
896 | toRow = GetVisibleRowsEnd(); | |
897 | ||
898 | if ( fromColumn < GetVisibleColumnsBegin() ) | |
899 | fromColumn = GetVisibleColumnsBegin(); | |
900 | ||
901 | if ( toColumn > GetVisibleColumnsEnd() ) | |
902 | toColumn = GetVisibleColumnsEnd(); | |
903 | ||
904 | // calculate the rect occupied by these units on screen | |
905 | wxRect v_rect, h_rect; | |
906 | size_t nBefore, nBetween; | |
907 | ||
908 | for ( nBefore = GetVisibleRowsBegin(); | |
909 | nBefore < fromRow; | |
910 | nBefore++ ) | |
911 | { | |
912 | v_rect.y += OnGetRowHeight(nBefore); | |
913 | } | |
914 | ||
915 | for ( nBetween = fromRow; nBetween <= toRow; nBetween++ ) | |
916 | { | |
917 | v_rect.height += OnGetRowHeight(nBetween); | |
918 | } | |
919 | ||
920 | for ( nBefore = GetVisibleColumnsBegin(); | |
921 | nBefore < fromColumn; | |
922 | nBefore++ ) | |
923 | { | |
924 | h_rect.x += OnGetColumnWidth(nBefore); | |
925 | } | |
926 | ||
927 | for ( nBetween = fromColumn; nBetween <= toColumn; nBetween++ ) | |
928 | { | |
929 | h_rect.width += OnGetColumnWidth(nBetween); | |
930 | } | |
931 | ||
932 | // refresh but specialize the behaviour if we have a single target window | |
933 | if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() ) | |
934 | { | |
935 | v_rect.x = h_rect.x; | |
936 | v_rect.width = h_rect.width; | |
937 | wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect); | |
938 | } | |
939 | else | |
940 | { | |
941 | v_rect.x = 0; | |
942 | v_rect.width = wxVarVScrollHelper::GetNonOrientationTargetSize(); | |
943 | h_rect.y = 0; | |
944 | h_rect.width = wxVarHScrollHelper::GetNonOrientationTargetSize(); | |
945 | ||
946 | wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect); | |
947 | wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect); | |
948 | } | |
949 | } | |
950 | ||
951 | wxPosition wxVarHVScrollHelper::VirtualHitTest(wxCoord x, wxCoord y) const | |
952 | { | |
953 | return wxPosition(wxVarVScrollHelper::VirtualHitTest(y), | |
954 | wxVarHScrollHelper::VirtualHitTest(x)); | |
955 | } | |
956 | ||
957 | void wxVarHVScrollHelper::DoPrepareDC(wxDC& dc) | |
958 | { | |
959 | wxVarVScrollHelper::DoPrepareDC(dc); | |
960 | wxVarHScrollHelper::DoPrepareDC(dc); | |
961 | } | |
962 | ||
963 | bool wxVarHVScrollHelper::ScrollLayout() | |
964 | { | |
965 | bool layout_result = false; | |
966 | layout_result |= wxVarVScrollHelper::ScrollLayout(); | |
967 | layout_result |= wxVarHScrollHelper::ScrollLayout(); | |
968 | return layout_result; | |
969 | } | |
970 | ||
971 | wxSize wxVarHVScrollHelper::GetRowColumnCount() const | |
972 | { | |
973 | return wxSize(GetColumnCount(), GetRowCount()); | |
974 | } | |
975 | ||
976 | wxPosition wxVarHVScrollHelper::GetVisibleBegin() const | |
977 | { | |
978 | return wxPosition(GetVisibleRowsBegin(), GetVisibleColumnsBegin()); | |
979 | } | |
980 | ||
981 | wxPosition wxVarHVScrollHelper::GetVisibleEnd() const | |
982 | { | |
983 | return wxPosition(GetVisibleRowsEnd(), GetVisibleColumnsEnd()); | |
984 | } | |
985 | ||
986 | bool wxVarHVScrollHelper::IsVisible(size_t row, size_t column) const | |
987 | { | |
988 | return IsRowVisible(row) && IsColumnVisible(column); | |
989 | } | |
990 | ||
991 | ||
992 | // ============================================================================ | |
993 | // wx[V/H/HV]ScrolledWindow implementations | |
994 | // ============================================================================ | |
995 | ||
996 | IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow, wxPanel) | |
997 | IMPLEMENT_ABSTRACT_CLASS(wxHScrolledWindow, wxPanel) | |
998 | IMPLEMENT_ABSTRACT_CLASS(wxHVScrolledWindow, wxPanel) | |
999 | ||
1000 | ||
1001 | #if WXWIN_COMPATIBILITY_2_8 | |
1002 | ||
1003 | // =========================================================================== | |
1004 | // wxVarVScrollLegacyAdaptor | |
1005 | // =========================================================================== | |
1006 | ||
1007 | size_t wxVarVScrollLegacyAdaptor::GetFirstVisibleLine() const | |
1008 | { return GetVisibleRowsBegin(); } | |
1009 | ||
1010 | size_t wxVarVScrollLegacyAdaptor::GetLastVisibleLine() const | |
1011 | { return GetVisibleRowsEnd() - 1; } | |
1012 | ||
1013 | size_t wxVarVScrollLegacyAdaptor::GetLineCount() const | |
1014 | { return GetRowCount(); } | |
1015 | ||
1016 | void wxVarVScrollLegacyAdaptor::SetLineCount(size_t count) | |
1017 | { SetRowCount(count); } | |
1018 | ||
1019 | void wxVarVScrollLegacyAdaptor::RefreshLine(size_t line) | |
1020 | { RefreshRow(line); } | |
1021 | ||
1022 | void wxVarVScrollLegacyAdaptor::RefreshLines(size_t from, size_t to) | |
1023 | { RefreshRows(from, to); } | |
1024 | ||
1025 | bool wxVarVScrollLegacyAdaptor::ScrollToLine(size_t line) | |
1026 | { return ScrollToRow(line); } | |
1027 | ||
1028 | bool wxVarVScrollLegacyAdaptor::ScrollLines(int lines) | |
1029 | { return ScrollRows(lines); } | |
1030 | ||
1031 | bool wxVarVScrollLegacyAdaptor::ScrollPages(int pages) | |
1032 | { return ScrollRowPages(pages); } | |
1033 | ||
1034 | wxCoord wxVarVScrollLegacyAdaptor::OnGetLineHeight(size_t WXUNUSED(n)) const | |
1035 | { | |
1036 | wxFAIL_MSG( wxT("OnGetLineHeight() must be overridden if OnGetRowHeight() isn't!") ); | |
1037 | return -1; | |
1038 | } | |
1039 | ||
1040 | void wxVarVScrollLegacyAdaptor::OnGetLinesHint(size_t WXUNUSED(lineMin), | |
1041 | size_t WXUNUSED(lineMax)) const | |
1042 | { | |
1043 | } | |
1044 | ||
1045 | wxCoord wxVarVScrollLegacyAdaptor::OnGetRowHeight(size_t n) const | |
1046 | { | |
1047 | return OnGetLineHeight(n); | |
1048 | } | |
1049 | ||
1050 | void wxVarVScrollLegacyAdaptor::OnGetRowsHeightHint(size_t rowMin, | |
1051 | size_t rowMax) const | |
1052 | { | |
1053 | OnGetLinesHint(rowMin, rowMax); | |
1054 | } | |
1055 | ||
1056 | #endif // WXWIN_COMPATIBILITY_2_8 |