Table.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 / Documents / Table.cs / 1305600 / Table.cs

                            //---------------------------------------------------------------------------- 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//  
//
// 
// Description: Table implementation 
//
//              See spec at http://avalon/layout/Tables/WPP%20TableOM.doc 
//
// History:
//  05/19/2003 : olego - created
// 
//---------------------------------------------------------------------------
using MS.Internal; 
using MS.Internal.PtsHost; 
using MS.Internal.PtsTable;
using MS.Utility; 
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics; 
using System.Windows.Threading;
using System.Windows; 
using System.Windows.Automation.Peers; 
using System.Windows.Media;
using System.Windows.Markup; 
using MS.Internal.PtsHost.UnsafeNativeMethods;
using MS.Internal.Documents;

#pragma warning disable 1634, 1691  // suppressing PreSharp warnings 

namespace System.Windows.Documents 
{ 
    /// 
    /// Table implements 
    /// 
    [ContentProperty("RowGroups")]
    public class Table : Block, IAddChild, IAcceptInsertion
    { 
        //-----------------------------------------------------
        // 
        //  Constructors 
        //
        //----------------------------------------------------- 

        #region Constructors

        ///  
        /// Static ctor.  Initializes property metadata.
        ///  
        static Table() 
        {
            MarginProperty.OverrideMetadata(typeof(Table), new FrameworkPropertyMetadata(new Thickness(Double.NaN))); 
        }

        /// 
        /// Table constructor. 
        /// 
        public Table() 
        { 
            PrivateInitialize();
        } 

        #endregion Constructors

        //------------------------------------------------------ 
        //
        //  Public Methods 
        // 
        //-----------------------------------------------------
 
        #region Public Methods

        /// 
        ///  
        /// 
        void IAddChild.AddChild(object value) 
        { 
            if (value == null)
            { 
                throw new ArgumentNullException("value");
            }

            TableRowGroup rowGroup = value as TableRowGroup; 
            if (rowGroup != null)
            { 
                RowGroups.Add(rowGroup); 
                return;
            } 

            throw (new ArgumentException(SR.Get(SRID.UnexpectedParameterType, value.GetType(), typeof(TableRowGroup)), "value"));
        }
 
        /// 
        ///  
        ///  
        void IAddChild.AddText(string text)
        { 
            XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText(text, this);
        }

        ///  
        ///     Initialization of this element is about to begin
        ///  
        public override void BeginInit() 
        {
            base.BeginInit(); 
            _initializing = true;
        }

        ///  
        ///     Initialization of this element has completed
        ///  
        public override void EndInit() 
        {
            _initializing = false; 
            OnStructureChanged();
            base.EndInit();
        }
 
        /// 
        ///  
        ///  
        /// 
        /// children enumerated in the following order: 
        /// * columns
        /// * rowgroups
        /// 
        protected internal override IEnumerator LogicalChildren 
        {
            get { return (new TableChildrenCollectionEnumeratorSimple(this)); } 
        } 

        #endregion Public Methods 

        //------------------------------------------------------
        //
        //  Public Properties 
        //
        //------------------------------------------------------ 
 
        #region Public Properties
 
        /// 
        /// Returns table column collection.
        /// 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
        public TableColumnCollection Columns { get { return (_columns); } }
 
        ///  
        /// This method is used by TypeDescriptor to determine if this property should
        /// be serialized. 
        /// 
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeColumns()
        { 
            return (Columns.Count > 0);
        } 
 
        /// 
        /// Returns table row group. 
        /// 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TableRowGroupCollection RowGroups
        { 
            get { return (_rowGroups); }
        } 
 
        /// 
        /// Cell spacing property. 
        /// 
        [TypeConverter(typeof(LengthConverter))]
        public double CellSpacing
        { 
            get { return (double) GetValue(CellSpacingProperty); }
            set { SetValue(CellSpacingProperty, value); } 
        } 

        #endregion Public Properties 

        //-----------------------------------------------------
        //
        //  Protected Methods 
        //
        //------------------------------------------------------ 
 
        #region Protected Methods
 
        /// 
        /// Creates AutomationPeer ()
        /// 
        protected override AutomationPeer OnCreateAutomationPeer() 
        {
            return new TableAutomationPeer(this); 
        } 

        #endregion Protected Methods 


        //-----------------------------------------------------
        // 
        //  Internal Properties
        // 
        //----------------------------------------------------- 

        #region Internal Properties 

        /// 
        /// Internal cell spacing getter
        ///  
        internal double InternalCellSpacing
        { 
            get { return Math.Max(CellSpacing, 0); } 
        }
 
        int IAcceptInsertion.InsertionIndex
        {
            get { return this.InsertionIndex; }
            set { this.InsertionIndex = value; } 
        }
        ///  
        /// Stores temporary data for where to insert a new row group 
        /// 
        internal int InsertionIndex 
        {
            get { return _rowGroupInsertionIndex; }
            set { _rowGroupInsertionIndex = value; }
        } 

        ///  
        /// Count of columns in the table 
        /// 
        internal int ColumnCount 
        {
            get
            {
                return (_columnCount); 
            }
        } 
 
        #endregion Internal Properties
 

        //-----------------------------------------------------
        //
        //  Internal Methods 
        //
        //------------------------------------------------------ 
 
        #region Internal Methods
 
        /// 
        /// Updates table actual column count
        /// 
        /// Count of column to account for 
        internal void EnsureColumnCount(int columnCount)
        { 
            if (_columnCount < columnCount) 
                _columnCount = columnCount;
        } 

        /// 
        /// OnStructureChanged - Called to rebuild structure.
        ///  
        internal void OnStructureChanged()
        { 
            if (!_initializing) 
            {
                if (TableStructureChanged != null) 
                {
                    TableStructureChanged(this, EventArgs.Empty);
                }
 
                ValidateStructure();
 
                // Table structure changes affect number of rows and colums. Need to notify peer about it. 
                TableAutomationPeer peer = ContentElementAutomationPeer.FromElement(this) as TableAutomationPeer;
                if (peer != null) 
                {
                    peer.OnStructureInvalidated();
                }
            } 
        }
 
        ///  
        /// ValidateStructure
        ///  
        internal void ValidateStructure()
        {
            if (!_initializing)
            { 
                //
                //  validate row groups structural cache 
                // 
                _columnCount = 0;
                for (int i = 0; i < _rowGroups.Count; ++i) 
                {
                    _rowGroups[i].ValidateStructure();
                }
 
                _version++;
            } 
        } 

        ///  
        /// Notifies the text container that some property change has occurred, requiring a revalidation of table.
        /// 
        internal void InvalidateColumns()
        { 
            NotifyTypographicPropertyChanged(true /* affectsMeasureOrArrange */, true /* localValueChanged */, null);
        } 
 
        /// 
        /// Returns true if the given rowGroupIndex is the first non-empty one 
        /// 
        internal bool IsFirstNonEmptyRowGroup(int rowGroupIndex)
        {
            rowGroupIndex--; 

            while(rowGroupIndex >= 0) 
            { 
                if(RowGroups[rowGroupIndex].Rows.Count > 0)
                { 
                    return false;
                }

                rowGroupIndex--; 
            }
 
            return true; 
        }
 
        /// 
        /// Returns true if the given rowGroupIndex is the last non-empty one
        /// 
        internal bool IsLastNonEmptyRowGroup(int rowGroupIndex) 
        {
            rowGroupIndex++; 
 
            while(rowGroupIndex < RowGroups.Count)
            { 
                if(RowGroups[rowGroupIndex].Rows.Count > 0)
                {
                    return false;
                } 

                rowGroupIndex++; 
            } 

            return true; 
        }


        #endregion Internal Methods 

        //----------------------------------------------------- 
        // 
        //  Internal Events
        // 
        //------------------------------------------------------

        #region Internal Events
 
        /// 
        /// Fired when the table changes structurally 
        ///  
        internal event EventHandler TableStructureChanged;
 
        #endregion

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

        #region Private Methods 

        /// 
        /// Private ctor time initialization.
        ///  
        private void PrivateInitialize()
        { 
            // Acquire new PTS Context. 
            _columns = new TableColumnCollection(this);
            _rowGroups = new TableRowGroupCollection(this); 
            _rowGroupInsertionIndex = -1;
        }

        private static bool IsValidCellSpacing(object o) 
        {
            double spacing = (double)o; 
            double maxSpacing = Math.Min(1000000, PTS.MaxPageSize); 
            if (Double.IsNaN(spacing))
            { 
                return false;
            }
            if (spacing < 0 || spacing > maxSpacing)
            { 
                return false;
            } 
            return true; 
        }
 
        #endregion Private Methods

        //------------------------------------------------------
        // 
        //  Private Fields
        // 
        //----------------------------------------------------- 

        #region Private Fields 
        private TableColumnCollection _columns;         //  collection of columns
        private TableRowGroupCollection _rowGroups;     //  collection of row groups
        private int _rowGroupInsertionIndex;            //  insertion index used by row group collection
        private const double c_defaultCellSpacing = 2;  //  default value of cell spacing 
        private int _columnCount;
        private int _version = 0; 
        private bool _initializing;                     //  True if the table is being initialized 

        #endregion Private Fields 

        //-----------------------------------------------------
        //
        //  Private Structures / Classes 
        //
        //----------------------------------------------------- 
 
        #region Private Structures Classes
 
        /// 
        /// Implementation of a simple enumerator of table's children
        /// 
        private class TableChildrenCollectionEnumeratorSimple : IEnumerator, ICloneable 
        {
            internal TableChildrenCollectionEnumeratorSimple(Table table) 
            { 
                Debug.Assert(table != null);
                _table = table; 
                _version = _table._version;
                _columns = ((IEnumerable)_table._columns).GetEnumerator();
                _rowGroups = ((IEnumerable)_table._rowGroups).GetEnumerator();
            } 

            public Object Clone() 
            { 
                return (MemberwiseClone());
            } 

            public bool MoveNext()
            {
                if (_version != _table._version) 
                {
                    throw new InvalidOperationException(SR.Get(SRID.EnumeratorVersionChanged)); 
                } 

                // Strange design, but iterator must spin on contained column iterator 
                if ((_currentChildType != ChildrenTypes.Columns) && (_currentChildType != ChildrenTypes.RowGroups))
                    _currentChildType++;

                Object currentChild = null; 

                while (_currentChildType < ChildrenTypes.AfterLast) 
                { 
                    switch (_currentChildType)
                    { 
                        case (ChildrenTypes.Columns):
                            if (_columns.MoveNext())
                            {
                                currentChild = _columns.Current; 
                            }
                            break; 
                        case (ChildrenTypes.RowGroups): 
                            if (_rowGroups.MoveNext())
                            { 
                                currentChild = _rowGroups.Current;
                            }
                            break;
                    } 

                    if (currentChild != null) 
                    { 
                        _currentChild = currentChild;
                        break; 
                    }

                    _currentChildType++;
                } 

                Debug.Assert(_currentChildType != ChildrenTypes.BeforeFirst); 
                return (_currentChildType != ChildrenTypes.AfterLast); 
            }
 
            public Object Current
            {
                get
                { 
                    if (_currentChildType == ChildrenTypes.BeforeFirst)
                    { 
                        #pragma warning suppress 6503 // IEnumerator.Current is documented to throw this exception 
                        throw new InvalidOperationException(SR.Get(SRID.EnumeratorNotStarted));
                    } 
                    if (_currentChildType == ChildrenTypes.AfterLast)
                    {
                        #pragma warning suppress 6503 // IEnumerator.Current is documented to throw this exception
                        throw new InvalidOperationException(SR.Get(SRID.EnumeratorReachedEnd)); 
                    }
 
                    return (_currentChild); 
                }
            } 

            public void Reset()
            {
                if (_version != _table._version) 
                {
                    throw new InvalidOperationException(SR.Get(SRID.EnumeratorVersionChanged)); 
                } 

                _columns.Reset(); 
                _rowGroups.Reset();
                _currentChildType = ChildrenTypes.BeforeFirst;
                _currentChild = null;
            } 

            private Table _table; 
            private int _version; 
            private IEnumerator _columns;
            private IEnumerator _rowGroups; 
            private ChildrenTypes _currentChildType;
            private Object _currentChild;

            private enum ChildrenTypes : int 
            {
                BeforeFirst     = 0, 
                Columns         = 1, 
                RowGroups       = 2,
                AfterLast       = 3, 
            }
        }

        #endregion Private Structures Classes 

        //------------------------------------------------------ 
        // 
        //  Properties
        // 
        //-----------------------------------------------------

        #region Properties
 
        /// 
        /// Cell spacing property. 
        ///  
        public static readonly DependencyProperty CellSpacingProperty =
                DependencyProperty.Register( 
                        "CellSpacing",
                        typeof(double),
                        typeof(Table),
                        new FrameworkPropertyMetadata( 
                                c_defaultCellSpacing,
                                FrameworkPropertyMetadataOptions.AffectsMeasure), 
                        new ValidateValueCallback(IsValidCellSpacing)); 

        #endregion Properties 

        //------------------------------------------------------
        //
        //  Debug / Performance 
        //
        //------------------------------------------------------ 
 
        #if TABLEPARANOIA
        internal int ParanoiaVersion = 0; 
        #endif // TABLEPARANOIA
    }
}
 

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