1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/vscroll.cpp
3 // Purpose: wxVScrolledWindow implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Brad Anderson
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/vscroll.h"
34 // ============================================================================
35 // wxVarScrollHelperEvtHandler declaration
36 // ============================================================================
38 // ----------------------------------------------------------------------------
39 // wxScrollHelperEvtHandler: intercept the events from the window and forward
40 // them to wxVarScrollHelperBase
41 // ----------------------------------------------------------------------------
43 class WXDLLEXPORT wxVarScrollHelperEvtHandler
: public wxEvtHandler
46 wxVarScrollHelperEvtHandler(wxVarScrollHelperBase
*scrollHelper
)
48 m_scrollHelper
= scrollHelper
;
51 virtual bool ProcessEvent(wxEvent
& event
);
54 wxVarScrollHelperBase
*m_scrollHelper
;
56 DECLARE_NO_COPY_CLASS(wxVarScrollHelperEvtHandler
)
59 // ============================================================================
60 // wxVarScrollHelperEvtHandler implementation
61 // ============================================================================
63 bool wxVarScrollHelperEvtHandler::ProcessEvent(wxEvent
& event
)
65 wxEventType evType
= event
.GetEventType();
67 // pass it on to the real handler
68 bool processed
= wxEvtHandler::ProcessEvent(event
);
70 // always process the size events ourselves, even if the user code handles
71 // them as well, as we need to AdjustScrollbars()
73 // NB: it is important to do it after processing the event in the normal
74 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
75 // scrollbar[s] (dis)appear and it should be seen by the user code
77 if ( evType
== wxEVT_SIZE
)
79 m_scrollHelper
->HandleOnSize((wxSizeEvent
&)event
);
81 return !event
.GetSkipped();
86 // normally, nothing more to do here - except if we have a command
88 if ( event
.IsCommandEvent() )
94 // reset the skipped flag (which might have been set to true in
95 // ProcessEvent() above) to be able to test it below
96 bool wasSkipped
= event
.GetSkipped();
100 // reset the skipped flag to false as it might have been set to true in
101 // ProcessEvent() above
104 if ( evType
== wxEVT_SCROLLWIN_TOP
||
105 evType
== wxEVT_SCROLLWIN_BOTTOM
||
106 evType
== wxEVT_SCROLLWIN_LINEUP
||
107 evType
== wxEVT_SCROLLWIN_LINEDOWN
||
108 evType
== wxEVT_SCROLLWIN_PAGEUP
||
109 evType
== wxEVT_SCROLLWIN_PAGEDOWN
||
110 evType
== wxEVT_SCROLLWIN_THUMBTRACK
||
111 evType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
113 m_scrollHelper
->HandleOnScroll((wxScrollWinEvent
&)event
);
114 if ( !event
.GetSkipped() )
116 // it makes sense to indicate that we processed the message as we
117 // did scroll the window (and also notice that wxAutoScrollTimer
118 // relies on our return value for continuous scrolling)
124 else if ( evType
== wxEVT_MOUSEWHEEL
)
126 m_scrollHelper
->HandleOnMouseWheel((wxMouseEvent
&)event
);
128 #endif // wxUSE_MOUSEWHEEL
131 event
.Skip(wasSkipped
);
137 // ============================================================================
138 // wxVarScrollHelperBase implementation
139 // ============================================================================
141 // ----------------------------------------------------------------------------
142 // wxVarScrollHelperBase initialization
143 // ----------------------------------------------------------------------------
145 wxVarScrollHelperBase::wxVarScrollHelperBase(wxWindow
*win
)
147 wxASSERT_MSG( win
, _T("associated window can't be NULL in wxVarScrollHelperBase") );
150 m_sumWheelRotation
= 0;
158 m_targetWindow
= (wxWindow
*)NULL
;
164 // by default, the associated window is also the target window
165 DoSetTargetWindow(win
);
169 wxVarScrollHelperBase::~wxVarScrollHelperBase()
174 // ----------------------------------------------------------------------------
175 // wxVarScrollHelperBase various helpers
176 // ----------------------------------------------------------------------------
179 wxVarScrollHelperBase::AssignOrient(wxCoord
& x
,
184 if ( GetOrientation() == wxVERTICAL
)
197 wxVarScrollHelperBase::IncOrient(wxCoord
& x
, wxCoord
& y
, wxCoord inc
)
199 if ( GetOrientation() == wxVERTICAL
)
205 wxCoord
wxVarScrollHelperBase::DoEstimateTotalSize() const
207 // estimate the total height: it is impossible to call
208 // OnGetUnitSize() for every unit because there may be too many of
209 // them, so we just make a guess using some units in the beginning,
210 // some in the end and some in the middle
211 static const size_t NUM_UNITS_TO_SAMPLE
= 10;
214 if ( m_unitMax
< 3*NUM_UNITS_TO_SAMPLE
)
216 // in this case, full calculations are faster and more correct than
218 sizeTotal
= GetUnitsSize(0, m_unitMax
);
220 else // too many units to calculate exactly
222 // look at some units in the beginning/middle/end
224 GetUnitsSize(0, NUM_UNITS_TO_SAMPLE
) +
225 GetUnitsSize(m_unitMax
- NUM_UNITS_TO_SAMPLE
,
227 GetUnitsSize(m_unitMax
/2 - NUM_UNITS_TO_SAMPLE
/2,
228 m_unitMax
/2 + NUM_UNITS_TO_SAMPLE
/2);
230 // use the height of the units we looked as the average
231 sizeTotal
= (wxCoord
)
232 (((float)sizeTotal
/ (3*NUM_UNITS_TO_SAMPLE
)) * m_unitMax
);
238 wxCoord
wxVarScrollHelperBase::GetUnitsSize(size_t unitMin
, size_t unitMax
) const
240 if ( unitMin
== unitMax
)
242 else if ( unitMin
> unitMax
)
243 return -GetUnitsSize(unitMax
, unitMin
);
244 //else: unitMin < unitMax
246 // let the user code know that we're going to need all these units
247 OnGetUnitsSizeHint(unitMin
, unitMax
);
249 // sum up their sizes
251 for ( size_t unit
= unitMin
; unit
< unitMax
; ++unit
)
253 size
+= OnGetUnitSize(unit
);
259 size_t wxVarScrollHelperBase::FindFirstVisibleFromLast(size_t unitLast
, bool full
) const
261 const wxCoord sWindow
= GetOrientationTargetSize();
263 // go upwards until we arrive at a unit such that unitLast is not visible
264 // any more when it is shown
265 size_t unitFirst
= unitLast
;
269 s
+= OnGetUnitSize(unitFirst
);
273 // for this unit to be fully visible we need to go one unit
274 // down, but if it is enough for it to be only partly visible then
275 // this unit will do as well
293 size_t wxVarScrollHelperBase::GetNewScrollPosition(wxScrollWinEvent
& event
) const
295 wxEventType evtType
= event
.GetEventType();
297 if ( evtType
== wxEVT_SCROLLWIN_TOP
)
301 else if ( evtType
== wxEVT_SCROLLWIN_BOTTOM
)
305 else if ( evtType
== wxEVT_SCROLLWIN_LINEUP
)
307 return m_unitFirst
? m_unitFirst
- 1 : 0;
309 else if ( evtType
== wxEVT_SCROLLWIN_LINEDOWN
)
311 return m_unitFirst
+ 1;
313 else if ( evtType
== wxEVT_SCROLLWIN_PAGEUP
)
315 return FindFirstVisibleFromLast(m_unitFirst
);
317 else if ( evtType
== wxEVT_SCROLLWIN_PAGEDOWN
)
319 if ( GetVisibleEnd() )
320 return GetVisibleEnd() - 1;
322 return GetVisibleEnd();
324 else if ( evtType
== wxEVT_SCROLLWIN_THUMBRELEASE
)
326 return event
.GetPosition();
328 else if ( evtType
== wxEVT_SCROLLWIN_THUMBTRACK
)
330 return event
.GetPosition();
333 // unknown scroll event?
334 wxFAIL_MSG( _T("unknown scroll event type?") );
338 void wxVarScrollHelperBase::UpdateScrollbar()
340 // if there is nothing to scroll, remove the scrollbar
347 // see how many units can we fit on screen
348 const wxCoord sWindow
= GetOrientationTargetSize();
350 // do vertical calculations
353 for ( unit
= m_unitFirst
; unit
< m_unitMax
; ++unit
)
358 s
+= OnGetUnitSize(unit
);
361 m_nUnitsVisible
= unit
- m_unitFirst
;
363 int unitsPageSize
= m_nUnitsVisible
;
366 // last unit is only partially visible, we still need the scrollbar and
367 // so we have to "fix" pageSize because if it is equal to m_unitMax
368 // the scrollbar is not shown at all under MSW
372 // set the scrollbar parameters to reflect this
373 m_win
->SetScrollbar(GetOrientation(), m_unitFirst
, unitsPageSize
, m_unitMax
);
376 void wxVarScrollHelperBase::RemoveScrollbar()
379 m_nUnitsVisible
= m_unitMax
;
380 m_win
->SetScrollbar(GetOrientation(), 0, 0, 0);
383 void wxVarScrollHelperBase::DeleteEvtHandler()
385 // search for m_handler in the handler list
386 if ( m_win
&& m_handler
)
388 if ( m_win
->RemoveEventHandler(m_handler
) )
392 //else: something is very wrong, so better [maybe] leak memory than
393 // risk a crash because of double deletion
399 void wxVarScrollHelperBase::DoSetTargetWindow(wxWindow
*target
)
401 m_targetWindow
= target
;
403 target
->MacSetClipChildren( true ) ;
406 // install the event handler which will intercept the events we're
407 // interested in (but only do it for our real window, not the target window
408 // which we scroll - we don't need to hijack its events)
409 if ( m_targetWindow
== m_win
)
411 // if we already have a handler, delete it first
414 m_handler
= new wxVarScrollHelperEvtHandler(this);
415 m_targetWindow
->PushEventHandler(m_handler
);
419 // ----------------------------------------------------------------------------
420 // wxVarScrollHelperBase operations
421 // ----------------------------------------------------------------------------
423 void wxVarScrollHelperBase::SetTargetWindow(wxWindow
*target
)
425 wxCHECK_RET( target
, wxT("target window must not be NULL") );
427 if ( target
== m_targetWindow
)
430 DoSetTargetWindow(target
);
433 void wxVarScrollHelperBase::SetUnitCount(size_t count
)
435 // save the number of units
438 // and our estimate for their total height
439 m_sizeTotal
= EstimateTotalSize();
441 // ScrollToUnit() will update the scrollbar itself if it changes the unit
442 // we pass to it because it's out of [new] range
443 size_t oldScrollPos
= m_unitFirst
;
444 DoScrollToUnit(m_unitFirst
);
445 if ( oldScrollPos
== m_unitFirst
)
447 // but if it didn't do it, we still need to update the scrollbar to
448 // reflect the changed number of units ourselves
453 void wxVarScrollHelperBase::RefreshUnit(size_t unit
)
455 // is this unit visible?
456 if ( !IsVisible(unit
) )
458 // no, it is useless to do anything
462 // calculate the rect occupied by this unit on screen
464 AssignOrient(rect
.width
, rect
.height
,
465 GetNonOrientationTargetSize(), OnGetUnitSize(unit
));
467 for ( size_t n
= GetVisibleBegin(); n
< unit
; ++n
)
469 IncOrient(rect
.x
, rect
.y
, OnGetUnitSize(n
));
473 m_targetWindow
->RefreshRect(rect
);
476 void wxVarScrollHelperBase::RefreshUnits(size_t from
, size_t to
)
478 wxASSERT_MSG( from
<= to
, _T("RefreshUnits(): empty range") );
480 // clump the range to just the visible units -- it is useless to refresh
482 if ( from
< GetVisibleBegin() )
483 from
= GetVisibleBegin();
485 if ( to
> GetVisibleEnd() )
486 to
= GetVisibleEnd();
488 // calculate the rect occupied by these units on screen
489 int orient_size
, nonorient_size
, orient_pos
;
490 orient_size
= nonorient_size
= orient_pos
= 0;
492 nonorient_size
= GetNonOrientationTargetSize();
494 for ( size_t nBefore
= GetVisibleBegin();
498 orient_pos
+= OnGetUnitSize(nBefore
);
501 for ( size_t nBetween
= from
; nBetween
<= to
; nBetween
++ )
503 orient_size
+= OnGetUnitSize(nBetween
);
507 AssignOrient(rect
.x
, rect
.y
, 0, orient_pos
);
508 AssignOrient(rect
.width
, rect
.height
, nonorient_size
, orient_size
);
511 m_targetWindow
->RefreshRect(rect
);
514 void wxVarScrollHelperBase::RefreshAll()
518 m_targetWindow
->Refresh();
521 bool wxVarScrollHelperBase::ScrollLayout()
523 if ( m_targetWindow
->GetSizer() && m_physicalScrolling
)
525 // adjust the sizer dimensions/position taking into account the
526 // virtual size and scrolled position of the window.
529 AssignOrient(x
, y
, 0, -GetScrollOffset());
532 m_targetWindow
->GetVirtualSize(&w
, &h
);
534 m_targetWindow
->GetSizer()->SetDimension(x
, y
, w
, h
);
538 // fall back to default for LayoutConstraints
539 return m_targetWindow
->wxWindow::Layout();
542 int wxVarScrollHelperBase::VirtualHitTest(wxCoord coord
) const
544 const size_t unitMax
= GetVisibleEnd();
545 for ( size_t unit
= GetVisibleBegin(); unit
< unitMax
; ++unit
)
547 coord
-= OnGetUnitSize(unit
);
555 // ----------------------------------------------------------------------------
556 // wxVarScrollHelperBase scrolling
557 // ----------------------------------------------------------------------------
559 bool wxVarScrollHelperBase::DoScrollToUnit(size_t unit
)
563 // we're empty, code below doesn't make sense in this case
567 // determine the real first unit to scroll to: we shouldn't scroll beyond
569 size_t unitFirstLast
= FindFirstVisibleFromLast(m_unitMax
- 1, true);
570 if ( unit
> unitFirstLast
)
571 unit
= unitFirstLast
;
574 if ( unit
== m_unitFirst
)
581 // remember the currently shown units for the refresh code below
582 size_t unitFirstOld
= GetVisibleBegin(),
583 unitLastOld
= GetVisibleEnd();
588 // the size of scrollbar thumb could have changed
591 // finally refresh the display -- but only redraw as few units as possible
592 // to avoid flicker. We can't do this if we have children because they
594 if ( m_targetWindow
->GetChildren().empty() &&
595 GetVisibleBegin() >= unitLastOld
|| GetVisibleEnd() <= unitFirstOld
)
597 // the simplest case: we don't have any old units left, just redraw
599 m_targetWindow
->Refresh();
601 else // scroll the window
603 if ( m_physicalScrolling
)
606 dy
= GetUnitsSize(GetVisibleBegin(), unitFirstOld
);
608 if ( GetOrientation() == wxHORIZONTAL
)
615 m_targetWindow
->ScrollWindow(dx
, dy
);
617 else // !m_physicalScrolling
619 // we still need to invalidate but we can't use ScrollWindow
620 // because physical scrolling is disabled (the user either didn't
621 // want children scrolled and/or doesn't want pixels to be
622 // physically scrolled).
623 m_targetWindow
->Refresh();
630 bool wxVarScrollHelperBase::DoScrollUnits(int units
)
632 units
+= m_unitFirst
;
636 return DoScrollToUnit(units
);
639 bool wxVarScrollHelperBase::DoScrollPages(int pages
)
641 bool didSomething
= false;
648 unit
= GetVisibleEnd();
655 unit
= FindFirstVisibleFromLast(GetVisibleEnd());
659 didSomething
= DoScrollToUnit(unit
);
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
669 void wxVarScrollHelperBase::HandleOnSize(wxSizeEvent
& event
)
676 void wxVarScrollHelperBase::HandleOnScroll(wxScrollWinEvent
& event
)
678 if (GetOrientation() != event
.GetOrientation())
684 DoScrollToUnit(GetNewScrollPosition(event
));
687 UpdateMacScrollWindow();
691 void wxVarScrollHelperBase::DoPrepareDC(wxDC
& dc
)
693 if ( m_physicalScrolling
)
695 wxPoint pt
= dc
.GetDeviceOrigin();
697 IncOrient(pt
.x
, pt
.y
, -GetScrollOffset());
699 dc
.SetDeviceOrigin(pt
.x
, pt
.y
);
703 int wxVarScrollHelperBase::DoCalcScrolledPosition(int coord
) const
705 return coord
- GetScrollOffset();
708 int wxVarScrollHelperBase::DoCalcUnscrolledPosition(int coord
) const
710 return coord
+ GetScrollOffset();
715 void wxVarScrollHelperBase::HandleOnMouseWheel(wxMouseEvent
& event
)
717 // we only want to process wheel events for vertical implementations.
718 // There is no way to determine wheel orientation (and on MSW horizontal
719 // wheel rotation just fakes scroll events, rather than sending a MOUSEWHEEL
721 if ( GetOrientation() != wxVERTICAL
)
724 m_sumWheelRotation
+= event
.GetWheelRotation();
725 int delta
= event
.GetWheelDelta();
727 // how much to scroll this time
728 int units_to_scroll
= -(m_sumWheelRotation
/delta
);
729 if ( !units_to_scroll
)
732 m_sumWheelRotation
+= units_to_scroll
*delta
;
734 if ( !event
.IsPageScroll() )
735 DoScrollUnits( units_to_scroll
*event
.GetLinesPerAction() );
736 else // scroll pages instead of units
737 DoScrollPages( units_to_scroll
);
740 #endif // wxUSE_MOUSEWHEEL
743 // ============================================================================
744 // wxVarHVScrollHelper implementation
745 // ============================================================================
747 // ----------------------------------------------------------------------------
748 // wxVarHVScrollHelper operations
749 // ----------------------------------------------------------------------------
751 void wxVarHVScrollHelper::SetRowColumnCount(size_t rowCount
, size_t columnCount
)
753 SetRowCount(rowCount
);
754 SetColumnCount(columnCount
);
757 bool wxVarHVScrollHelper::ScrollToRowColumn(size_t row
, size_t column
)
760 result
|= ScrollToRow(row
);
761 result
|= ScrollToColumn(column
);
765 void wxVarHVScrollHelper::RefreshRowColumn(size_t row
, size_t column
)
767 // is this unit visible?
768 if ( !IsRowVisible(row
) || !IsColumnVisible(column
) )
770 // no, it is useless to do anything
774 // calculate the rect occupied by this cell on screen
775 wxRect v_rect
, h_rect
;
776 v_rect
.height
= OnGetRowHeight(row
);
777 h_rect
.width
= OnGetColumnWidth(column
);
781 for ( n
= GetVisibleRowsBegin(); n
< row
; n
++ )
783 v_rect
.y
+= OnGetRowHeight(n
);
786 for ( n
= GetVisibleColumnsBegin(); n
< column
; n
++ )
788 h_rect
.x
+= OnGetColumnWidth(n
);
791 // refresh but specialize the behavior if we have a single target window
792 if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() )
795 v_rect
.width
= h_rect
.width
;
796 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect
);
801 v_rect
.width
= wxVarVScrollHelper::GetNonOrientationTargetSize();
803 h_rect
.width
= wxVarHScrollHelper::GetNonOrientationTargetSize();
805 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect
);
806 wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect
);
810 void wxVarHVScrollHelper::RefreshRowsColumns(size_t fromRow
, size_t toRow
,
811 size_t fromColumn
, size_t toColumn
)
813 wxASSERT_MSG( fromRow
<= toRow
|| fromColumn
<= toColumn
,
814 _T("RefreshRowsColumns(): empty range") );
816 // clump the range to just the visible units -- it is useless to refresh
818 if ( fromRow
< GetVisibleRowsBegin() )
819 fromRow
= GetVisibleRowsBegin();
821 if ( toRow
> GetVisibleRowsEnd() )
822 toRow
= GetVisibleRowsEnd();
824 if ( fromColumn
< GetVisibleColumnsBegin() )
825 fromColumn
= GetVisibleColumnsBegin();
827 if ( toColumn
> GetVisibleColumnsEnd() )
828 toColumn
= GetVisibleColumnsEnd();
830 // calculate the rect occupied by these units on screen
831 wxRect v_rect
, h_rect
;
832 size_t nBefore
, nBetween
;
834 for ( nBefore
= GetVisibleRowsBegin();
838 v_rect
.y
+= OnGetRowHeight(nBefore
);
841 for ( nBetween
= fromRow
; nBetween
<= toRow
; nBetween
++ )
843 v_rect
.height
+= OnGetRowHeight(nBetween
);
846 for ( nBefore
= GetVisibleColumnsBegin();
847 nBefore
< fromColumn
;
850 h_rect
.x
+= OnGetColumnWidth(nBefore
);
853 for ( nBetween
= fromColumn
; nBetween
<= toColumn
; nBetween
++ )
855 h_rect
.width
+= OnGetColumnWidth(nBetween
);
858 // refresh but specialize the behavior if we have a single target window
859 if ( wxVarVScrollHelper::GetTargetWindow() == wxVarHScrollHelper::GetTargetWindow() )
862 v_rect
.width
= h_rect
.width
;
863 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect
);
868 v_rect
.width
= wxVarVScrollHelper::GetNonOrientationTargetSize();
870 h_rect
.width
= wxVarHScrollHelper::GetNonOrientationTargetSize();
872 wxVarVScrollHelper::GetTargetWindow()->RefreshRect(v_rect
);
873 wxVarHScrollHelper::GetTargetWindow()->RefreshRect(h_rect
);
877 wxPosition
wxVarHVScrollHelper::VirtualHitTest(wxCoord x
, wxCoord y
) const
879 return wxPosition(wxVarVScrollHelper::VirtualHitTest(y
),
880 wxVarHScrollHelper::VirtualHitTest(x
));
883 void wxVarHVScrollHelper::DoPrepareDC(wxDC
& dc
)
885 wxVarVScrollHelper::DoPrepareDC(dc
);
886 wxVarHScrollHelper::DoPrepareDC(dc
);
889 bool wxVarHVScrollHelper::ScrollLayout()
891 bool layout_result
= false;
892 layout_result
|= wxVarVScrollHelper::ScrollLayout();
893 layout_result
|= wxVarHScrollHelper::ScrollLayout();
894 return layout_result
;
897 wxSize
wxVarHVScrollHelper::GetRowColumnCount() const
899 return wxSize(GetColumnCount(), GetRowCount());
902 wxPosition
wxVarHVScrollHelper::GetVisibleBegin() const
904 return wxPosition(GetVisibleRowsBegin(), GetVisibleColumnsBegin());
907 wxPosition
wxVarHVScrollHelper::GetVisibleEnd() const
909 return wxPosition(GetVisibleRowsEnd(), GetVisibleColumnsEnd());
912 bool wxVarHVScrollHelper::IsVisible(size_t row
, size_t column
) const
914 return IsRowVisible(row
) && IsColumnVisible(column
);
918 // ============================================================================
919 // wx[V/H/HV]ScrolledWindow implementations
920 // ============================================================================
922 IMPLEMENT_ABSTRACT_CLASS(wxVScrolledWindow
, wxPanel
)
923 IMPLEMENT_ABSTRACT_CLASS(wxHScrolledWindow
, wxPanel
)
924 IMPLEMENT_ABSTRACT_CLASS(wxHVScrolledWindow
, wxPanel
)