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