ScrollViewerAutomationPeer.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / Automation / Peers / ScrollViewerAutomationPeer.cs / 1305600 / ScrollViewerAutomationPeer.cs

                            using System; 
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security;
using System.Text; 
using System.Windows;
using System.Windows.Automation.Provider; 
using System.Windows.Controls; 
using System.Windows.Interop;
using System.Windows.Media; 

using MS.Internal;
using MS.Win32;
 
namespace System.Windows.Automation.Peers
{ 
 
    ///
    public class ScrollViewerAutomationPeer : FrameworkElementAutomationPeer, IScrollProvider 
    {
        ///
        public ScrollViewerAutomationPeer(ScrollViewer owner): base(owner)
        {} 

        /// 
        override protected string GetClassNameCore() 
        {
            return "ScrollViewer"; 
        }

        ///
        override protected AutomationControlType GetAutomationControlTypeCore() 
        {
            return AutomationControlType.Pane; 
        } 

        /// 
        override protected bool IsControlElementCore()
        {
            // Return true if ScrollViewer is not part of the style
            return ((ScrollViewer)Owner).TemplatedParent == null; 
        }
 
        /// 
        override public object GetPattern(PatternInterface patternInterface)
        { 
            if (patternInterface == PatternInterface.Scroll)
            {
                return this;
            } 
            else
            { 
                return base.GetPattern(patternInterface); 
            }
        } 


        //--------------------------------------------------------------------
        // 
        //  IScrollProvider
        // 
        //------------------------------------------------------------------- 

        ///  
        /// Request to scroll horizontally and vertically by the specified amount.
        /// The ability to call this method and simultaneously scroll horizontally
        /// and vertically provides simple panning support.
        ///  
        /// 
        void IScrollProvider.Scroll(ScrollAmount horizontalAmount, ScrollAmount verticalAmount) 
        { 
            if(!IsEnabled())
                throw new ElementNotEnabledException(); 

            bool scrollHorizontally = (horizontalAmount != ScrollAmount.NoAmount);
            bool scrollVertically = (verticalAmount != ScrollAmount.NoAmount);
 
            ScrollViewer owner = (ScrollViewer)Owner;
 
            if (scrollHorizontally && !HorizontallyScrollable || scrollVertically && !VerticallyScrollable) 
            {
                throw new InvalidOperationException(SR.Get(SRID.UIA_OperationCannotBePerformed)); 
            }

            switch (horizontalAmount)
            { 
                case ScrollAmount.LargeDecrement:
                    owner.PageLeft(); 
                    break; 
                case ScrollAmount.SmallDecrement:
                    owner.LineLeft(); 
                    break;
                case ScrollAmount.SmallIncrement:
                    owner.LineRight();
                    break; 
                case ScrollAmount.LargeIncrement:
                    owner.PageRight(); 
                    break; 
                case ScrollAmount.NoAmount:
                    break; 
                default:
                    throw new InvalidOperationException(SR.Get(SRID.UIA_OperationCannotBePerformed));
            }
 
            switch (verticalAmount)
            { 
                case ScrollAmount.LargeDecrement: 
                    owner.PageUp();
                    break; 
                case ScrollAmount.SmallDecrement:
                    owner.LineUp();
                    break;
                case ScrollAmount.SmallIncrement: 
                    owner.LineDown();
                    break; 
                case ScrollAmount.LargeIncrement: 
                    owner.PageDown();
                    break; 
                case ScrollAmount.NoAmount:
                    break;
                default:
                    throw new InvalidOperationException(SR.Get(SRID.UIA_OperationCannotBePerformed)); 
            }
        } 
 
        /// 
        /// Request to set the current horizontal and Vertical scroll position by percent (0-100). 
        /// Passing in the value of "-1", represented by the constant "NoScroll", will indicate that scrolling
        /// in that direction should be ignored.
        /// The ability to call this method and simultaneously scroll horizontally and vertically provides simple panning support.
        ///  
        /// 
        void IScrollProvider.SetScrollPercent(double horizontalPercent, double verticalPercent) 
        { 
            if(!IsEnabled())
                throw new ElementNotEnabledException(); 

            bool scrollHorizontally = (horizontalPercent != (double)ScrollPatternIdentifiers.NoScroll);
            bool scrollVertically = (verticalPercent != (double)ScrollPatternIdentifiers.NoScroll);
 
            ScrollViewer owner = (ScrollViewer)Owner;
 
            if (scrollHorizontally && !HorizontallyScrollable || scrollVertically && !VerticallyScrollable) 
            {
                throw new InvalidOperationException(SR.Get(SRID.UIA_OperationCannotBePerformed)); 
            }

            if (scrollHorizontally && (horizontalPercent < 0.0) || (horizontalPercent > 100.0))
            { 
                throw new ArgumentOutOfRangeException("horizontalPercent", SR.Get(SRID.ScrollViewer_OutOfRange, "horizontalPercent", horizontalPercent.ToString(CultureInfo.InvariantCulture), "0", "100"));
            } 
            if (scrollVertically && (verticalPercent < 0.0) || (verticalPercent > 100.0)) 
            {
                throw new ArgumentOutOfRangeException("verticalPercent", SR.Get(SRID.ScrollViewer_OutOfRange, "verticalPercent", verticalPercent.ToString(CultureInfo.InvariantCulture), "0", "100")); 
            }

            if (scrollHorizontally)
            { 
                owner.ScrollToHorizontalOffset((owner.ExtentWidth - owner.ViewportWidth) * (double)horizontalPercent * 0.01);
            } 
            if (scrollVertically) 
            {
                owner.ScrollToVerticalOffset((owner.ExtentHeight - owner.ViewportHeight) * (double)verticalPercent * 0.01); 
            }
        }

        ///  
        /// Get the current horizontal scroll position
        ///  
        ///  
        double IScrollProvider.HorizontalScrollPercent
        { 
            get
            {
                if (!HorizontallyScrollable) { return ScrollPatternIdentifiers.NoScroll; }
                ScrollViewer owner = (ScrollViewer)Owner; 
                return (double)(owner.HorizontalOffset * 100.0 / (owner.ExtentWidth - owner.ViewportWidth));
            } 
        } 

        ///  
        /// Get the current vertical scroll position
        /// 
        /// 
        double IScrollProvider.VerticalScrollPercent 
        {
            get 
            { 
                if (!VerticallyScrollable) { return ScrollPatternIdentifiers.NoScroll; }
                ScrollViewer owner = (ScrollViewer)Owner; 
                return (double)(owner.VerticalOffset * 100.0 / (owner.ExtentHeight - owner.ViewportHeight));
            }
        }
 
        /// 
        /// Equal to the horizontal percentage of the entire control that is currently viewable. 
        ///  
        /// 
        double IScrollProvider.HorizontalViewSize 
        {
            get
            {
                ScrollViewer owner = (ScrollViewer)Owner; 
                if (owner.ScrollInfo == null || DoubleUtil.IsZero(owner.ExtentWidth)) { return 100.0; }
                return Math.Min(100.0, (double)(owner.ViewportWidth * 100.0 / owner.ExtentWidth)); 
            } 
        }
 
        /// 
        /// Equal to the vertical percentage of the entire control that is currently viewable.
        /// 
        ///  
        double IScrollProvider.VerticalViewSize
        { 
            get 
            {
                ScrollViewer owner = (ScrollViewer)Owner; 
                if (owner.ScrollInfo == null || DoubleUtil.IsZero(owner.ExtentHeight)) { return 100f; }
                return Math.Min(100.0, (double)(owner.ViewportHeight * 100.0 / owner.ExtentHeight));
            }
        } 

        ///  
        /// True if control can scroll horizontally 
        /// 
        ///  
        bool IScrollProvider.HorizontallyScrollable
        {
            get { return HorizontallyScrollable; }
        } 

        ///  
        /// True if control can scroll vertically 
        /// 
        ///  
        bool IScrollProvider.VerticallyScrollable
        {
            get { return VerticallyScrollable; }
        } 

        private static bool AutomationIsScrollable(double extent, double viewport) 
        { 
            return DoubleUtil.GreaterThan(extent, viewport);
        } 

        private static double AutomationGetScrollPercent(double extent, double viewport, double actualOffset)
        {
            if (!AutomationIsScrollable(extent, viewport)) { return ScrollPatternIdentifiers.NoScroll; } 
            return (double)(actualOffset * 100.0 / (extent - viewport));
        } 
 
        private static double AutomationGetViewSize(double extent, double viewport)
        { 
            if (DoubleUtil.IsZero(extent)) { return 100.0; }
            return Math.Min(100.0, (double)(viewport * 100.0 / extent));
        }
 
        // Private *Scrollable properties used to determine scrollability for IScrollProvider implemenation.
        private bool HorizontallyScrollable 
        { 
            get
            { 
                ScrollViewer owner = (ScrollViewer)Owner;
                return owner.ScrollInfo != null && DoubleUtil.GreaterThan(owner.ExtentWidth, owner.ViewportWidth);
            }
        } 
        private bool VerticallyScrollable
        { 
            get 
            {
                ScrollViewer owner = (ScrollViewer)Owner; 
                return owner.ScrollInfo != null && DoubleUtil.GreaterThan(owner.ExtentHeight, owner.ViewportHeight);
            }
        }
 
        // This helper synchronously fires automation PropertyChange events for every IScrollProvider property that has changed.
        // 
        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] 
        internal void RaiseAutomationEvents(double extentX,
                                          double extentY, 
                                          double viewportX,
                                          double viewportY,
                                          double offsetX,
                                          double offsetY) 
        {
            IScrollProvider isp = (IScrollProvider)this; 
 
            if (AutomationIsScrollable(extentX, viewportX) != isp.HorizontallyScrollable)
            { 
                RaisePropertyChangedEvent(
                    ScrollPatternIdentifiers.HorizontallyScrollableProperty,
                    AutomationIsScrollable(extentX, viewportX),
                    isp.HorizontallyScrollable); 
            }
            if (AutomationIsScrollable(extentY, viewportY) != isp.VerticallyScrollable) 
            { 
                RaisePropertyChangedEvent(
                    ScrollPatternIdentifiers.VerticallyScrollableProperty, 
                    AutomationIsScrollable(extentY, viewportY),
                    isp.VerticallyScrollable);
            }
            if (AutomationGetViewSize(extentX, viewportX) != isp.HorizontalViewSize) 
            {
                RaisePropertyChangedEvent( 
                    ScrollPatternIdentifiers.HorizontalViewSizeProperty, 
                    AutomationGetViewSize(extentX, viewportX),
                    isp.HorizontalViewSize); 
            }
            if (AutomationGetViewSize(extentY, viewportY) != isp.VerticalViewSize)
            {
                RaisePropertyChangedEvent( 
                    ScrollPatternIdentifiers.VerticalViewSizeProperty,
                    AutomationGetViewSize(extentY, viewportY), 
                    isp.VerticalViewSize); 
            }
            if (AutomationGetScrollPercent(extentX, viewportX, offsetX) != isp.HorizontalScrollPercent) 
            {
                RaisePropertyChangedEvent(
                    ScrollPatternIdentifiers.HorizontalScrollPercentProperty,
                    AutomationGetScrollPercent(extentX, viewportX, offsetX), 
                    isp.HorizontalScrollPercent);
            } 
            if (AutomationGetScrollPercent(extentY, viewportY, offsetY) != isp.VerticalScrollPercent) 
            {
                RaisePropertyChangedEvent( 
                    ScrollPatternIdentifiers.VerticalScrollPercentProperty,
                    AutomationGetScrollPercent(extentY, viewportY, offsetY),
                    isp.VerticalScrollPercent);
            } 
        }
 
 
    }
} 



// 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