ScrollData.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / MS / Internal / documents / ScrollData.cs / 1 / ScrollData.cs

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// File: ScrollData.cs 
//
// Description: IScrollInfo implementation helper for FlowDocumentView, TextBoxView. 
// 
//---------------------------------------------------------------------------
 
namespace MS.Internal.Documents
{
    using System;
    using System.Windows; 
    using System.Windows.Controls;
    using System.Windows.Media; 
    using MS.Internal; 

    // IScrollInfo implementation helper for FlowDocumentView, TextBoxView. 
    internal class ScrollData
    {
        //-------------------------------------------------------------------
        // 
        //  Internal Methods
        // 
        //------------------------------------------------------------------- 

        #region Internal Methods 

        /// 
        /// 
        ///  
        internal void LineUp(UIElement owner)
        { 
            SetVerticalOffset(owner, _offset.Y - ScrollViewer._scrollLineDelta); 
        }
 
        /// 
        /// 
        /// 
        internal void LineDown(UIElement owner) 
        {
            SetVerticalOffset(owner, _offset.Y + ScrollViewer._scrollLineDelta); 
        } 

        ///  
        /// 
        /// 
        internal void LineLeft(UIElement owner)
        { 
            SetHorizontalOffset(owner, _offset.X - ScrollViewer._scrollLineDelta);
        } 
 
        /// 
        ///  
        /// 
        internal void LineRight(UIElement owner)
        {
            SetHorizontalOffset(owner, _offset.X + ScrollViewer._scrollLineDelta); 
        }
 
        ///  
        /// 
        ///  
        internal void PageUp(UIElement owner)
        {
            SetVerticalOffset(owner, _offset.Y - _viewport.Height);
        } 

        ///  
        ///  
        /// 
        internal void PageDown(UIElement owner) 
        {
            SetVerticalOffset(owner, _offset.Y + _viewport.Height);
        }
 
        /// 
        ///  
        ///  
        internal void PageLeft(UIElement owner)
        { 
            SetHorizontalOffset(owner, _offset.X - _viewport.Width);
        }

        ///  
        /// 
        ///  
        internal void PageRight(UIElement owner) 
        {
            SetHorizontalOffset(owner, _offset.X + _viewport.Width); 
        }

        /// 
        ///  
        /// 
        internal void MouseWheelUp(UIElement owner) 
        { 
            SetVerticalOffset(owner, _offset.Y - ScrollViewer._mouseWheelDelta);
        } 

        /// 
        /// 
        ///  
        internal void MouseWheelDown(UIElement owner)
        { 
            SetVerticalOffset(owner, _offset.Y + ScrollViewer._mouseWheelDelta); 
        }
 
        /// 
        /// 
        /// 
        internal void MouseWheelLeft(UIElement owner) 
        {
            SetHorizontalOffset(owner, _offset.X - ScrollViewer._mouseWheelDelta); 
        } 

        ///  
        /// 
        /// 
        internal void MouseWheelRight(UIElement owner)
        { 
            SetHorizontalOffset(owner, _offset.X + ScrollViewer._mouseWheelDelta);
        } 
 
        /// 
        ///  
        /// 
        internal void SetHorizontalOffset(UIElement owner, double offset)
        {
            if (!this.CanHorizontallyScroll) 
            {
                return; 
            } 

            offset = Math.Max(0, Math.Min(_extent.Width - _viewport.Width, offset)); 
            if (!DoubleUtil.AreClose(offset, _offset.X))
            {
                _offset.X = offset;
                owner.InvalidateArrange(); 
                if (_scrollOwner != null)
                { 
                    _scrollOwner.InvalidateScrollInfo(); 
                }
            } 
        }

        /// 
        ///  
        /// 
        internal void SetVerticalOffset(UIElement owner, double offset) 
        { 
            if (!this.CanVerticallyScroll)
            { 
                return;
            }

            offset = Math.Max(0, Math.Min(_extent.Height - _viewport.Height, offset)); 
            if (!DoubleUtil.AreClose(offset, _offset.Y))
            { 
                _offset.Y = offset; 
                owner.InvalidateArrange();
                if (_scrollOwner != null) 
                {
                    _scrollOwner.InvalidateScrollInfo();
                }
            } 
        }
 
        ///  
        /// 
        ///  
        internal Rect MakeVisible(UIElement owner, Visual visual, Rect rectangle)
        {
            // We can only work on visuals that are us or children.
            // An empty rect has no size or position.  We can't meaningfully use it. 
            if (rectangle.IsEmpty ||
                visual == null || 
                (visual != owner && !owner.IsAncestorOf(visual))) 
            {
                return Rect.Empty; 
            }

            // Compute the child's rect relative to (0,0) in our coordinate space.
            GeneralTransform childTransform = visual.TransformToAncestor(owner); 
            rectangle = childTransform.TransformBounds(rectangle);
 
            // Initialize the viewport 
            Rect viewport = new Rect(_offset.X, _offset.Y, _viewport.Width, _viewport.Height);
            rectangle.X += viewport.X; 
            rectangle.Y += viewport.Y;

            // Compute the offsets required to minimally scroll the child maximally into view.
            double minX = ComputeScrollOffset(viewport.Left, viewport.Right, rectangle.Left, rectangle.Right); 
            double minY = ComputeScrollOffset(viewport.Top, viewport.Bottom, rectangle.Top, rectangle.Bottom);
 
            // We have computed the scrolling offsets; scroll to them. 
            SetHorizontalOffset(owner, minX);
            SetVerticalOffset(owner, minY); 

            // Compute the visible rectangle of the child relative to the viewport.
            if (this.CanHorizontallyScroll)
            { 
                viewport.X = minX;
            } 
            else 
            {
                // munge the intersection 
                rectangle.X = viewport.X;
            }
            if (this.CanVerticallyScroll)
            { 
                viewport.Y = minY;
            } 
            else 
            {
                // munge the intersection 
                rectangle.Y = viewport.Y;
            }
            rectangle.Intersect(viewport);
            if (!rectangle.IsEmpty) 
            {
                rectangle.X -= viewport.X; 
                rectangle.Y -= viewport.Y; 
            }
 
            // Return the rectangle
            return rectangle;
        }
 
        /// 
        ///  
        ///  
        internal void SetScrollOwner(UIElement owner, ScrollViewer value)
        { 
            if (value != _scrollOwner)
            {
                // Reset cached scroll info.
                _disableHorizonalScroll = false; 
                _disableVerticalScroll = false;
                _offset = new Vector(); 
                _viewport = new Size(); 
                _extent = new Size();
 
                _scrollOwner = value;
                owner.InvalidateArrange();
            }
        } 

        #endregion Internal Methods 
 
        //--------------------------------------------------------------------
        // 
        //  Internal Properties
        //
        //-------------------------------------------------------------------
 
        #region Internal Properties
 
        ///  
        /// 
        ///  
        internal bool CanVerticallyScroll
        {
            get
            { 
                return !_disableVerticalScroll;
            } 
            set 
            {
                _disableVerticalScroll = !value; 
            }
        }

        ///  
        /// 
        ///  
        internal bool CanHorizontallyScroll 
        {
            get 
            {
                return !_disableHorizonalScroll;
            }
            set 
            {
                _disableHorizonalScroll = !value; 
            } 
        }
 
        /// 
        /// 
        /// 
        internal double ExtentWidth 
        {
            get 
            { 
                return _extent.Width;
            } 

            set
            {
                _extent.Width = value; 
            }
        } 
 
        /// 
        ///  
        /// 
        internal double ExtentHeight
        {
            get 
            {
                return _extent.Height; 
            } 

            set 
            {
                _extent.Height = value;
            }
        } 

        ///  
        ///  
        /// 
        internal double ViewportWidth 
        {
            get
            {
                return _viewport.Width; 
            }
        } 
 
        /// 
        ///  
        /// 
        internal double ViewportHeight
        {
            get 
            {
                return _viewport.Height; 
            } 
        }
 
        /// 
        /// 
        /// 
        internal double HorizontalOffset 
        {
            get 
            { 
                return _offset.X;
            } 
        }

        /// 
        ///  
        /// 
        internal double VerticalOffset 
        { 
            get
            { 
                return _offset.Y;
            }
        }
 
        /// 
        ///  
        ///  
        internal ScrollViewer ScrollOwner
        { 
            get
            {
                return _scrollOwner;
            } 
        }
 
        // HorizontalOffset/VerticalOffset as a Vector. 
        internal Vector Offset
        { 
            get
            {
                return _offset;
            } 

            set 
            { 
                _offset = value;
            } 
        }

        // ExtenttWidth/ExtentHeight as a Size.
        internal Size Extent 
        {
            get 
            { 
                return _extent;
            } 

            set
            {
                _extent = value; 
            }
        } 
 
        // ViewportWidth/ViewportHeight as a Size.
        internal Size Viewport 
        {
            get
            {
                return _viewport; 
            }
 
            set 
            {
                _viewport = value; 
            }
        }

        #endregion Internal Properties 

        //-------------------------------------------------------------------- 
        // 
        //  Private Methods
        // 
        //--------------------------------------------------------------------

        #region Private Methods
 
        /// 
        /// Compute scroll offset for child rectangle. 
        ///  
        private double ComputeScrollOffset(double topView, double bottomView, double topChild, double bottomChild)
        { 
            // # CHILD POSITION             REMEDY
            // 1 Above viewport             Align top edge of child & viewport
            // 2 Below viewport             Align top edge of child & viewport
            // 3 Entirely within viewport   No scroll 
            // 4 Spanning viewport          Align top edge of child & viewport
            // 
            // Note: "Above viewport" = childTop above viewportTop, childBottom above viewportBottom 
            //       "Below viewport" = childTop below viewportTop, childBottom below viewportBottom
            // These child thus may overlap with the viewport, but will scroll the same direction/ 

            bool topInView = DoubleUtil.GreaterThanOrClose(topChild, topView) && DoubleUtil.LessThan(topChild, bottomView);
            bool bottomInView = DoubleUtil.LessThanOrClose(bottomChild, bottomView) && DoubleUtil.GreaterThan(bottomChild, topView);
 
            double position;
            if (topInView && bottomInView) 
            { 
                position = topView;
            } 
            else
            {
                position = topChild;
            } 
            return position;
        } 
 
        #endregion Private Methods
 
        //-------------------------------------------------------------------
        //
        //  Private Fields
        // 
        //--------------------------------------------------------------------
 
        #region Private Fields 

        private bool _disableHorizonalScroll; 
        private bool _disableVerticalScroll;
        private Vector _offset;
        private Size _viewport;
        private Size _extent; 
        private ScrollViewer _scrollOwner;
 
        #endregion Private Fields 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// File: ScrollData.cs 
//
// Description: IScrollInfo implementation helper for FlowDocumentView, TextBoxView. 
// 
//---------------------------------------------------------------------------
 
namespace MS.Internal.Documents
{
    using System;
    using System.Windows; 
    using System.Windows.Controls;
    using System.Windows.Media; 
    using MS.Internal; 

    // IScrollInfo implementation helper for FlowDocumentView, TextBoxView. 
    internal class ScrollData
    {
        //-------------------------------------------------------------------
        // 
        //  Internal Methods
        // 
        //------------------------------------------------------------------- 

        #region Internal Methods 

        /// 
        /// 
        ///  
        internal void LineUp(UIElement owner)
        { 
            SetVerticalOffset(owner, _offset.Y - ScrollViewer._scrollLineDelta); 
        }
 
        /// 
        /// 
        /// 
        internal void LineDown(UIElement owner) 
        {
            SetVerticalOffset(owner, _offset.Y + ScrollViewer._scrollLineDelta); 
        } 

        ///  
        /// 
        /// 
        internal void LineLeft(UIElement owner)
        { 
            SetHorizontalOffset(owner, _offset.X - ScrollViewer._scrollLineDelta);
        } 
 
        /// 
        ///  
        /// 
        internal void LineRight(UIElement owner)
        {
            SetHorizontalOffset(owner, _offset.X + ScrollViewer._scrollLineDelta); 
        }
 
        ///  
        /// 
        ///  
        internal void PageUp(UIElement owner)
        {
            SetVerticalOffset(owner, _offset.Y - _viewport.Height);
        } 

        ///  
        ///  
        /// 
        internal void PageDown(UIElement owner) 
        {
            SetVerticalOffset(owner, _offset.Y + _viewport.Height);
        }
 
        /// 
        ///  
        ///  
        internal void PageLeft(UIElement owner)
        { 
            SetHorizontalOffset(owner, _offset.X - _viewport.Width);
        }

        ///  
        /// 
        ///  
        internal void PageRight(UIElement owner) 
        {
            SetHorizontalOffset(owner, _offset.X + _viewport.Width); 
        }

        /// 
        ///  
        /// 
        internal void MouseWheelUp(UIElement owner) 
        { 
            SetVerticalOffset(owner, _offset.Y - ScrollViewer._mouseWheelDelta);
        } 

        /// 
        /// 
        ///  
        internal void MouseWheelDown(UIElement owner)
        { 
            SetVerticalOffset(owner, _offset.Y + ScrollViewer._mouseWheelDelta); 
        }
 
        /// 
        /// 
        /// 
        internal void MouseWheelLeft(UIElement owner) 
        {
            SetHorizontalOffset(owner, _offset.X - ScrollViewer._mouseWheelDelta); 
        } 

        ///  
        /// 
        /// 
        internal void MouseWheelRight(UIElement owner)
        { 
            SetHorizontalOffset(owner, _offset.X + ScrollViewer._mouseWheelDelta);
        } 
 
        /// 
        ///  
        /// 
        internal void SetHorizontalOffset(UIElement owner, double offset)
        {
            if (!this.CanHorizontallyScroll) 
            {
                return; 
            } 

            offset = Math.Max(0, Math.Min(_extent.Width - _viewport.Width, offset)); 
            if (!DoubleUtil.AreClose(offset, _offset.X))
            {
                _offset.X = offset;
                owner.InvalidateArrange(); 
                if (_scrollOwner != null)
                { 
                    _scrollOwner.InvalidateScrollInfo(); 
                }
            } 
        }

        /// 
        ///  
        /// 
        internal void SetVerticalOffset(UIElement owner, double offset) 
        { 
            if (!this.CanVerticallyScroll)
            { 
                return;
            }

            offset = Math.Max(0, Math.Min(_extent.Height - _viewport.Height, offset)); 
            if (!DoubleUtil.AreClose(offset, _offset.Y))
            { 
                _offset.Y = offset; 
                owner.InvalidateArrange();
                if (_scrollOwner != null) 
                {
                    _scrollOwner.InvalidateScrollInfo();
                }
            } 
        }
 
        ///  
        /// 
        ///  
        internal Rect MakeVisible(UIElement owner, Visual visual, Rect rectangle)
        {
            // We can only work on visuals that are us or children.
            // An empty rect has no size or position.  We can't meaningfully use it. 
            if (rectangle.IsEmpty ||
                visual == null || 
                (visual != owner && !owner.IsAncestorOf(visual))) 
            {
                return Rect.Empty; 
            }

            // Compute the child's rect relative to (0,0) in our coordinate space.
            GeneralTransform childTransform = visual.TransformToAncestor(owner); 
            rectangle = childTransform.TransformBounds(rectangle);
 
            // Initialize the viewport 
            Rect viewport = new Rect(_offset.X, _offset.Y, _viewport.Width, _viewport.Height);
            rectangle.X += viewport.X; 
            rectangle.Y += viewport.Y;

            // Compute the offsets required to minimally scroll the child maximally into view.
            double minX = ComputeScrollOffset(viewport.Left, viewport.Right, rectangle.Left, rectangle.Right); 
            double minY = ComputeScrollOffset(viewport.Top, viewport.Bottom, rectangle.Top, rectangle.Bottom);
 
            // We have computed the scrolling offsets; scroll to them. 
            SetHorizontalOffset(owner, minX);
            SetVerticalOffset(owner, minY); 

            // Compute the visible rectangle of the child relative to the viewport.
            if (this.CanHorizontallyScroll)
            { 
                viewport.X = minX;
            } 
            else 
            {
                // munge the intersection 
                rectangle.X = viewport.X;
            }
            if (this.CanVerticallyScroll)
            { 
                viewport.Y = minY;
            } 
            else 
            {
                // munge the intersection 
                rectangle.Y = viewport.Y;
            }
            rectangle.Intersect(viewport);
            if (!rectangle.IsEmpty) 
            {
                rectangle.X -= viewport.X; 
                rectangle.Y -= viewport.Y; 
            }
 
            // Return the rectangle
            return rectangle;
        }
 
        /// 
        ///  
        ///  
        internal void SetScrollOwner(UIElement owner, ScrollViewer value)
        { 
            if (value != _scrollOwner)
            {
                // Reset cached scroll info.
                _disableHorizonalScroll = false; 
                _disableVerticalScroll = false;
                _offset = new Vector(); 
                _viewport = new Size(); 
                _extent = new Size();
 
                _scrollOwner = value;
                owner.InvalidateArrange();
            }
        } 

        #endregion Internal Methods 
 
        //--------------------------------------------------------------------
        // 
        //  Internal Properties
        //
        //-------------------------------------------------------------------
 
        #region Internal Properties
 
        ///  
        /// 
        ///  
        internal bool CanVerticallyScroll
        {
            get
            { 
                return !_disableVerticalScroll;
            } 
            set 
            {
                _disableVerticalScroll = !value; 
            }
        }

        ///  
        /// 
        ///  
        internal bool CanHorizontallyScroll 
        {
            get 
            {
                return !_disableHorizonalScroll;
            }
            set 
            {
                _disableHorizonalScroll = !value; 
            } 
        }
 
        /// 
        /// 
        /// 
        internal double ExtentWidth 
        {
            get 
            { 
                return _extent.Width;
            } 

            set
            {
                _extent.Width = value; 
            }
        } 
 
        /// 
        ///  
        /// 
        internal double ExtentHeight
        {
            get 
            {
                return _extent.Height; 
            } 

            set 
            {
                _extent.Height = value;
            }
        } 

        ///  
        ///  
        /// 
        internal double ViewportWidth 
        {
            get
            {
                return _viewport.Width; 
            }
        } 
 
        /// 
        ///  
        /// 
        internal double ViewportHeight
        {
            get 
            {
                return _viewport.Height; 
            } 
        }
 
        /// 
        /// 
        /// 
        internal double HorizontalOffset 
        {
            get 
            { 
                return _offset.X;
            } 
        }

        /// 
        ///  
        /// 
        internal double VerticalOffset 
        { 
            get
            { 
                return _offset.Y;
            }
        }
 
        /// 
        ///  
        ///  
        internal ScrollViewer ScrollOwner
        { 
            get
            {
                return _scrollOwner;
            } 
        }
 
        // HorizontalOffset/VerticalOffset as a Vector. 
        internal Vector Offset
        { 
            get
            {
                return _offset;
            } 

            set 
            { 
                _offset = value;
            } 
        }

        // ExtenttWidth/ExtentHeight as a Size.
        internal Size Extent 
        {
            get 
            { 
                return _extent;
            } 

            set
            {
                _extent = value; 
            }
        } 
 
        // ViewportWidth/ViewportHeight as a Size.
        internal Size Viewport 
        {
            get
            {
                return _viewport; 
            }
 
            set 
            {
                _viewport = value; 
            }
        }

        #endregion Internal Properties 

        //-------------------------------------------------------------------- 
        // 
        //  Private Methods
        // 
        //--------------------------------------------------------------------

        #region Private Methods
 
        /// 
        /// Compute scroll offset for child rectangle. 
        ///  
        private double ComputeScrollOffset(double topView, double bottomView, double topChild, double bottomChild)
        { 
            // # CHILD POSITION             REMEDY
            // 1 Above viewport             Align top edge of child & viewport
            // 2 Below viewport             Align top edge of child & viewport
            // 3 Entirely within viewport   No scroll 
            // 4 Spanning viewport          Align top edge of child & viewport
            // 
            // Note: "Above viewport" = childTop above viewportTop, childBottom above viewportBottom 
            //       "Below viewport" = childTop below viewportTop, childBottom below viewportBottom
            // These child thus may overlap with the viewport, but will scroll the same direction/ 

            bool topInView = DoubleUtil.GreaterThanOrClose(topChild, topView) && DoubleUtil.LessThan(topChild, bottomView);
            bool bottomInView = DoubleUtil.LessThanOrClose(bottomChild, bottomView) && DoubleUtil.GreaterThan(bottomChild, topView);
 
            double position;
            if (topInView && bottomInView) 
            { 
                position = topView;
            } 
            else
            {
                position = topChild;
            } 
            return position;
        } 
 
        #endregion Private Methods
 
        //-------------------------------------------------------------------
        //
        //  Private Fields
        // 
        //--------------------------------------------------------------------
 
        #region Private Fields 

        private bool _disableHorizonalScroll; 
        private bool _disableVerticalScroll;
        private Vector _offset;
        private Size _viewport;
        private Size _extent; 
        private ScrollViewer _scrollOwner;
 
        #endregion Private Fields 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK