VisualTreeHelper.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 / Core / CSharp / System / Windows / Media / VisualTreeHelper.cs / 1305600 / VisualTreeHelper.cs

                            //------------------------------------------------------------------------------ 
//  Microsoft Avalon
//  Copyright (c) Microsoft Corporation, 2003
//
//  File:       VisualTreeHelper 
//-----------------------------------------------------------------------------
using System; 
using System.Windows.Media; 
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation; 
using System.Windows.Threading;
using System.Security.Permissions;
using System.Windows.Media.Effects;
 
using System.Collections;
using System.Diagnostics; 
using MS.Internal; 
using MS.Internal.Media;
using MS.Internal.PresentationCore; 

using SR=MS.Internal.PresentationCore.SR;
using SRID=MS.Internal.PresentationCore.SRID;
 
namespace System.Windows.Media
{ 
    ///  
    /// The VisualTreeHelper class contains static methods that are useful for performing
    /// common tasks with visual tree nodes.  Nodes in the visual tree may be Visual or 
    /// Visual3Ds.
    ///
    /// When possible methods are typed to DependencyObject and may accept or
    /// return either type of visual tree node (e.g. GetParent). 
    ///
    /// When the result of the operation is specific to 2D or 3D the methods are typed 
    /// to Visual or Visual3D (e.g., GetDescendantBounds()). 
    /// 
    public static class VisualTreeHelper 
    {
        private static void CheckVisualReferenceArgument(DependencyObject reference)
        {
            if (reference == null) 
            {
                throw new ArgumentNullException("reference"); 
            } 
        }
 
        /// 
        /// Return true if the given DependencyObject is a non-null visual type.
        /// This is useful as a precondition for many of the VisualTreeHelper methods,
        /// to avoid exceptions. 
        /// 
        // 
        [FriendAccessAllowed] 
        internal static bool IsVisualType(DependencyObject reference)
        { 
            return (reference is Visual) || (reference is Visual3D);
        }

        ///  
        /// Get the number of children of the specified Visual.
        ///  
        public static int GetChildrenCount(DependencyObject reference) 
        {
            Visual visual; 
            Visual3D visual3D;

            VisualTreeUtils.AsNonNullVisual(reference, out visual, out visual3D);
 
            // x86 branch prediction skips the branch on first encounter.  We favor 2D.
            if (visual3D != null) 
            { 
                return visual3D.InternalVisual2DOr3DChildrenCount;
            } 

            //
            // Even though visual is a 2D visual, it still may have
            // 3D children. 
            //
            return visual.InternalVisual2DOr3DChildrenCount; 
        } 

        ///  
        /// Returns the child of Visual visual at the specified index.
        /// 
        public static DependencyObject GetChild(DependencyObject reference, int childIndex)
        { 
            Visual visual;
            Visual3D visual3D; 
 
            VisualTreeUtils.AsNonNullVisual(reference, out visual, out visual3D);
 
            // x86 branch prediction skips the branch on first encounter.  We favor 2D.
            if (visual3D != null)
            {
                return visual3D.InternalGet2DOr3DVisualChild(childIndex); 
            }
 
            // 
            // Even though visual is a 2D visual, it still may have
            // 3D children. 
            //
            return visual.InternalGet2DOr3DVisualChild(childIndex);
        }
 
        /// 
        /// Visual parent of this Visual. 
        ///  
        public static DependencyObject GetParent(DependencyObject reference)
        { 
            Visual visual;
            Visual3D visual3D;

            VisualTreeUtils.AsNonNullVisual(reference, out visual, out visual3D); 

            // x86 branch prediction skips the branch on first encounter.  We favor 2D. 
            if (visual3D != null) 
            {
                return visual3D.InternalVisualParent; 
            }

            return visual.InternalVisualParent;
        } 

        ///  
        /// Equivalent to GetParent except that it does not VerifyAccess and only asserts 
        /// in
 



        [FriendAccessAllowed] 
        internal static DependencyObject GetParentInternal(DependencyObject reference)
        { 
            Visual visual; 
            Visual3D visual3D;
 
            VisualTreeUtils.AsVisualInternal(reference, out visual, out visual3D);

            if (visual != null)
            { 
                return visual.InternalVisualParent;
            } 
 
            if (visual3D != null)
            { 
                return visual3D.InternalVisualParent;
            }

            Debug.Assert(reference == null); 

            return null; 
        } 

        ///  
        /// Returns the closest Visual that contains the given DependencyObject
        /// 
        internal static Visual GetContainingVisual2D(DependencyObject reference)
        { 
            Visual visual = null;
 
            while (reference != null) 
            {
                visual = reference as Visual; 

                if (visual != null) break;

                reference = VisualTreeHelper.GetParent(reference); 
            }
 
            return visual; 
        }
 
        /// 
        /// Returns the closest Visual3D that contains the given DependencyObject
        /// 
        internal static Visual3D GetContainingVisual3D(DependencyObject reference) 
        {
            Visual3D visual3D = null; 
 
            while (reference != null)
            { 
                visual3D = reference as Visual3D;

                if (visual3D != null) break;
 
                reference = VisualTreeHelper.GetParent(reference);
            } 
 
            return visual3D;
        } 

        internal static bool IsAncestorOf(DependencyObject reference, DependencyObject descendant)
        {
            Visual visual; 
            Visual3D visual3D;
 
            VisualTreeUtils.AsNonNullVisual(reference, out visual, out visual3D); 

            // x86 branch prediction skips the branch on first encounter.  We favor 2D. 
            if (visual3D != null)
            {
                return visual3D.IsAncestorOf(descendant);
            } 

            return visual.IsAncestorOf(descendant); 
        } 

        // a version of IsAncestorOf that stops looking when it finds an ancestor 
        // of the given type
        internal static bool IsAncestorOf(DependencyObject ancestor, DependencyObject descendant, Type stopType)
        {
            if (ancestor == null) 
            {
                throw new ArgumentNullException("ancestor"); 
            } 
            if (descendant == null)
            { 
                throw new ArgumentNullException("descendant");
            }

            VisualTreeUtils.EnsureVisual(ancestor); 
            VisualTreeUtils.EnsureVisual(descendant);
 
            // Walk up the parent chain of the descendant until we run out of parents, 
            // or we find the ancestor, or we reach a node of the given type.
            DependencyObject current = descendant; 

            while ((current != null) && (current != ancestor) && !stopType.IsInstanceOfType(current))
            {
                Visual visual; 
                Visual3D visual3D;
 
                if ((visual = current as Visual) != null) 
                {
                    current = visual.InternalVisualParent; 
                }
                else if ((visual3D = current as Visual3D) != null)
                {
                    current = visual3D.InternalVisualParent; 
                }
                else 
                { 
                    current = null;
                } 
            }

            return current == ancestor;
        } 

        internal static DependencyObject FindCommonAncestor(DependencyObject reference, DependencyObject otherVisual) 
        { 
            Visual visual;
            Visual3D visual3D; 

            VisualTreeUtils.AsNonNullVisual(reference, out visual, out visual3D);

            // x86 branch prediction skips the branch on first encounter.  We favor 2D. 
            if (visual3D != null)
            { 
                return visual3D.FindCommonVisualAncestor(otherVisual); 
            }
 
            return visual.FindCommonVisualAncestor(otherVisual);
        }

        ///  
        /// Gets the clip of this Visual.
        ///  
        public static Geometry GetClip(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualClip;
        }

        ///  
        /// Gets the opacity of the Visual.
        ///  
        public static double GetOpacity(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualOpacity;
        }

        ///  
        /// Gets the OpacityMask.
        ///  
        public static Brush GetOpacityMask(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualOpacityMask;
        }

        ///  
        /// Returns the offset of the Visual.
        ///  
        public static Vector GetOffset(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualOffset;
        }

         ///  
        /// Returns the Visual transform.
        ///  
        public static Transform GetTransform(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualTransform;
        }

        ///  
        /// Returns X-coordinate (vertical) guideline collection.
        ///  
        public static DoubleCollection GetXSnappingGuidelines(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualXSnappingGuidelines;
        }

         ///  
        /// Returns Y-coordinate (horizontal) guideline collection.
        ///  
        public static DoubleCollection GetYSnappingGuidelines(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualYSnappingGuidelines;
        }

        ///  
        /// GetDrawing returns the drawing content of the reference Visual
        ///  
        public static DrawingGroup GetDrawing(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.GetDrawing();
        }

        ///  
        /// GetContentBounds returns the bounding box for the contents of the specified visual.
        ///  
        public static Rect GetContentBounds(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualContentBounds;
        }

        ///  
        /// GetContentBounds returns the bounding box for the contents of the specified visual.
        ///  
        public static Rect3D GetContentBounds(Visual3D reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualContentBounds;
        }

        ///  
        /// GetDescendantBounds returns the union of all of the content bounding
        /// boxes of the specified Visual's sub-graph. 
        ///  
        public static Rect GetDescendantBounds(Visual reference)
        { 
            CheckVisualReferenceArgument(reference);
            return reference.VisualDescendantBounds;
        }
 
        /// 
        /// GetDescendantBounds returns the union of all of the content bounding 
        /// boxes of the specified Visual3D's sub-graph. 
        /// 
        public static Rect3D GetDescendantBounds(Visual3D reference) 
        {
            CheckVisualReferenceArgument(reference);
            return reference.VisualDescendantBounds;
        } 

        ///  
        /// Gets the BitmapEffect. 
        /// 
        public static BitmapEffect GetBitmapEffect(Visual reference) 
        {
            CheckVisualReferenceArgument(reference);
#pragma warning disable 0618
            return reference.VisualBitmapEffect; 
#pragma warning restore 0618
        } 
 
        /// 
        /// Gets the BitmapEffectInput. 
        /// 
        public static BitmapEffectInput GetBitmapEffectInput(Visual reference)
        {
            CheckVisualReferenceArgument(reference); 
#pragma warning disable 0618
            return reference.VisualBitmapEffectInput; 
#pragma warning restore 0618 
        }
 
        /// 
        /// Gets the Effect.
        /// 
        public static Effect GetEffect(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualEffect; 
        }
 
        /// 
        /// Gets the CacheMode.
        /// 
        public static CacheMode GetCacheMode(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualCacheMode; 
        }
 
        /// 
        /// Gets the EdgeMode of the Visual.
        /// 
        public static EdgeMode GetEdgeMode(Visual reference) 
        {
            CheckVisualReferenceArgument(reference); 
            return reference.VisualEdgeMode; 
        }
 
        /// 
        /// Return top most visual of a hit test.
        /// 
        public static HitTestResult HitTest(Visual reference, Point point) 
        {
            return HitTest(reference, point, true); 
        } 

        ///  
        /// 
        [FriendAccessAllowed]
        internal static HitTestResult HitTest(Visual reference, Point point, bool include2DOn3D)
        { 
            CheckVisualReferenceArgument(reference);
 
            return reference.HitTest(point, include2DOn3D); 
        }
 
        /// 
        /// Initiate a hit test using delegates.
        /// 
        public static void HitTest( 
            Visual reference,
            HitTestFilterCallback filterCallback, 
            HitTestResultCallback resultCallback, 
            HitTestParameters hitTestParameters)
        { 
            CheckVisualReferenceArgument(reference);
            reference.HitTest(filterCallback, resultCallback, hitTestParameters);
        }
 
        /// 
        /// Initiate a hit test using delegates. 
        ///  
        public static void HitTest(
            Visual3D reference, 
            HitTestFilterCallback filterCallback,
            HitTestResultCallback resultCallback,
            HitTestParameters3D hitTestParameters)
        { 
            CheckVisualReferenceArgument(reference);
            reference.HitTest(filterCallback, resultCallback, hitTestParameters); 
        } 

#if WCP_MF_ENABLED 
        /// 
        /// Serialize a visual to fixed (S0) XAML
        /// 
        ///  
        /// 
        static internal void SaveAsXml(Visual visual, System.Xml.XmlWriter writer) 
        { 
            VisualTreeFlattener.SaveAsXml(visual, writer, new FixedXamlDesigner());
        } 

        /// 
        /// Walk a visual tree and flatten it to (S0) DrawingContext
        ///  
        /// 
        ///  
        //CASRemoval:[StrongNameIdentityPermission(SecurityAction.LinkDemand, PublicKey=Microsoft.Internal.BuildInfo.WCP_PUBLIC_KEY_STRING)] 
        static public void Walk(Visual visual, DrawingContext dc)
        { 
            VisualTreeFlattener flattener = new VisualTreeFlattener(dc);

            flattener.Walk(visual, null);
        } 
#endif // WCP_MF_ENABLED
 
    } 
}
 



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