GridLengthConverter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / GridLengthConverter.cs / 1 / GridLengthConverter.cs

                            //---------------------------------------------------------------------------- 
//
// 
//    Copyright (C) Microsoft Corporation.  All rights reserved.
//  
//
// 
// Description: Grid length converter implementation 
//
//              See spec at http://avalon/layout/Specs/Star%20LengthUnit.mht 
//
// History:
//  09/24/2003 : olego - Created (in griddy prototype branch);
//  10/27/2003 : olego - Ported from griddy prototype branch; 
//
//--------------------------------------------------------------------------- 
 
using MS.Internal;
using MS.Utility; 
using System.ComponentModel;
using System.Windows;
using System;
using System.Security; 
using System.ComponentModel.Design.Serialization;
using System.Diagnostics; 
using System.Globalization; 
using System.Reflection;
using System.Windows.Markup; 

namespace System.Windows
{
    ///  
    /// GridLengthConverter - Converter class for converting
    /// instances of other types to and from GridLength instances. 
    ///  
    public class GridLengthConverter: TypeConverter
    { 
        //-------------------------------------------------------------------
        //
        //  Public Methods
        // 
        //-------------------------------------------------------------------
 
        #region Public Methods 

        ///  
        /// Checks whether or not this class can convert from a given type.
        /// 
        /// The ITypeDescriptorContext
        /// for this call. 
        /// The Type being queried for support.
        ///  
        /// true if thie converter can convert from the provided type, 
        /// false otherwise.
        ///  
        public override bool CanConvertFrom(
            ITypeDescriptorContext typeDescriptorContext,
            Type sourceType)
        { 
            // We can only handle strings, integral and floating types
            TypeCode tc = Type.GetTypeCode(sourceType); 
            switch (tc) 
            {
                case TypeCode.String: 
                case TypeCode.Decimal:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Int16: 
                case TypeCode.Int32:
                case TypeCode.Int64: 
                case TypeCode.UInt16: 
                case TypeCode.UInt32:
                case TypeCode.UInt64: 
                    return true;
                default:
                    return false;
            } 
        }
 
        ///  
        /// Checks whether or not this class can convert to a given type.
        ///  
        /// The ITypeDescriptorContext
        /// for this call.
        /// The Type being queried for support.
        ///  
        /// true if this converter can convert to the provided type,
        /// false otherwise. 
        ///  
        public override bool CanConvertTo(
            ITypeDescriptorContext typeDescriptorContext, 
            Type destinationType)
        {
            return (    destinationType == typeof(InstanceDescriptor)
                    ||  destinationType == typeof(string)   ); 
        }
 
        ///  
        /// Attempts to convert to a GridLength from the given object.
        ///  
        /// The ITypeDescriptorContext for this call.
        /// The CultureInfo which is respected when converting.
        /// The object to convert to a GridLength.
        ///  
        /// The GridLength instance which was constructed.
        ///  
        ///  
        /// An ArgumentNullException is thrown if the example object is null.
        ///  
        /// 
        /// An ArgumentException is thrown if the example object is not null
        /// and is not a valid type which can be converted to a GridLength.
        ///  
        public override object ConvertFrom(
            ITypeDescriptorContext typeDescriptorContext, 
            CultureInfo cultureInfo, 
            object source)
        { 
            if (source != null)
            {
                if (source is string)
                { 
                    return (FromString((string)source, cultureInfo));
                } 
                else 
                {
                    //  conversion from numeric type 
                    double value;
                    GridUnitType type;

                    value = Convert.ToDouble(source, cultureInfo); 

                    if (DoubleUtil.IsNaN(value)) 
                    { 
                        //  this allows for conversion from Width / Height = "Auto"
                        value = 1.0; 
                        type = GridUnitType.Auto;
                    }
                    else
                    { 
                        type = GridUnitType.Pixel;
                    } 
 
                    return new GridLength(value, type);
                } 
            }
            throw GetConvertFromException(source);
        }
 
        /// 
        /// Attempts to convert a GridLength instance to the given type. 
        ///  
        /// The ITypeDescriptorContext for this call.
        /// The CultureInfo which is respected when converting. 
        /// The GridLength to convert.
        /// The type to which to convert the GridLength instance.
        /// 
        /// The object which was constructed. 
        /// 
        ///  
        /// An ArgumentNullException is thrown if the example object is null. 
        /// 
        ///  
        /// An ArgumentException is thrown if the object is not null and is not a GridLength,
        /// or if the destinationType isn't one of the valid destination types.
        /// 
        /// 
        ///     Critical: calls InstanceDescriptor ctor which LinkDemands
        ///     PublicOK: can only make an InstanceDescriptor for GridLength, not an arbitrary class 
        /// 
        [SecurityCritical]
        public override object ConvertTo( 
            ITypeDescriptorContext typeDescriptorContext,
            CultureInfo cultureInfo,
            object value,
            Type destinationType) 
        {
            if (destinationType == null) 
            { 
                throw new ArgumentNullException("destinationType");
            } 

            if (    value != null
                &&  value is GridLength )
            { 
                GridLength gl = (GridLength)value;
 
                if (destinationType == typeof(string)) 
                {
                    return (ToString(gl, cultureInfo)); 
                }

                if (destinationType == typeof(InstanceDescriptor))
                { 
                    ConstructorInfo ci = typeof(GridLength).GetConstructor(new Type[] { typeof(double), typeof(GridUnitType) });
                    return (new InstanceDescriptor(ci, new object[] { gl.Value, gl.GridUnitType })); 
                } 
            }
            throw GetConvertToException(value, destinationType); 
        }

        #endregion Public Methods
 
        //--------------------------------------------------------------------
        // 
        //  Internal Methods 
        //
        //------------------------------------------------------------------- 

        #region Internal Methods

        ///  
        /// Converts a GridLength instance to a String given the CultureInfo.
        ///  
        /// GridLength instance to convert. 
        /// Culture Info.
        /// String representation of the object. 
        static internal string ToString(GridLength gl, CultureInfo cultureInfo)
        {
            switch (gl.GridUnitType)
            { 
                //  for Auto print out "Auto". value is always "1.0"
                case (GridUnitType.Auto): 
                    return ("Auto"); 

                //  Star has one special case when value is "1.0". 
                //  in this case drop value part and print only "Star"
                case (GridUnitType.Star):
                    return (
                        DoubleUtil.IsOne(gl.Value) 
                        ? "*"
                        : Convert.ToString(gl.Value, cultureInfo) + "*"); 
 
                //  for Pixel print out the numeric value. "px" can be omitted.
                default: 
                    return (Convert.ToString(gl.Value, cultureInfo));

            }
        } 

        ///  
        /// Parses a GridLength from a string given the CultureInfo. 
        /// 
        /// String to parse from. 
        /// Culture Info.
        /// Newly created GridLength instance.
        /// 
        /// Formats: 
        /// "[value][unit]"
        ///     [value] is a double 
        ///     [unit] is a string in GridLength._unitTypes connected to a GridUnitType 
        /// "[value]"
        ///     As above, but the GridUnitType is assumed to be GridUnitType.Pixel 
        /// "[unit]"
        ///     As above, but the value is assumed to be 1.0
        ///     This is only acceptable for a subset of GridUnitType: Auto
        ///  
        static internal GridLength FromString(string s, CultureInfo cultureInfo)
        { 
            double value; 
            GridUnitType unit;
            XamlGridLengthSerializer.FromString(s, cultureInfo, 
                out value, out unit);

            return (new GridLength(value, unit));
        } 

        #endregion Internal Methods 
 
    }
} 

// 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.
//  
//
// 
// Description: Grid length converter implementation 
//
//              See spec at http://avalon/layout/Specs/Star%20LengthUnit.mht 
//
// History:
//  09/24/2003 : olego - Created (in griddy prototype branch);
//  10/27/2003 : olego - Ported from griddy prototype branch; 
//
//--------------------------------------------------------------------------- 
 
using MS.Internal;
using MS.Utility; 
using System.ComponentModel;
using System.Windows;
using System;
using System.Security; 
using System.ComponentModel.Design.Serialization;
using System.Diagnostics; 
using System.Globalization; 
using System.Reflection;
using System.Windows.Markup; 

namespace System.Windows
{
    ///  
    /// GridLengthConverter - Converter class for converting
    /// instances of other types to and from GridLength instances. 
    ///  
    public class GridLengthConverter: TypeConverter
    { 
        //-------------------------------------------------------------------
        //
        //  Public Methods
        // 
        //-------------------------------------------------------------------
 
        #region Public Methods 

        ///  
        /// Checks whether or not this class can convert from a given type.
        /// 
        /// The ITypeDescriptorContext
        /// for this call. 
        /// The Type being queried for support.
        ///  
        /// true if thie converter can convert from the provided type, 
        /// false otherwise.
        ///  
        public override bool CanConvertFrom(
            ITypeDescriptorContext typeDescriptorContext,
            Type sourceType)
        { 
            // We can only handle strings, integral and floating types
            TypeCode tc = Type.GetTypeCode(sourceType); 
            switch (tc) 
            {
                case TypeCode.String: 
                case TypeCode.Decimal:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Int16: 
                case TypeCode.Int32:
                case TypeCode.Int64: 
                case TypeCode.UInt16: 
                case TypeCode.UInt32:
                case TypeCode.UInt64: 
                    return true;
                default:
                    return false;
            } 
        }
 
        ///  
        /// Checks whether or not this class can convert to a given type.
        ///  
        /// The ITypeDescriptorContext
        /// for this call.
        /// The Type being queried for support.
        ///  
        /// true if this converter can convert to the provided type,
        /// false otherwise. 
        ///  
        public override bool CanConvertTo(
            ITypeDescriptorContext typeDescriptorContext, 
            Type destinationType)
        {
            return (    destinationType == typeof(InstanceDescriptor)
                    ||  destinationType == typeof(string)   ); 
        }
 
        ///  
        /// Attempts to convert to a GridLength from the given object.
        ///  
        /// The ITypeDescriptorContext for this call.
        /// The CultureInfo which is respected when converting.
        /// The object to convert to a GridLength.
        ///  
        /// The GridLength instance which was constructed.
        ///  
        ///  
        /// An ArgumentNullException is thrown if the example object is null.
        ///  
        /// 
        /// An ArgumentException is thrown if the example object is not null
        /// and is not a valid type which can be converted to a GridLength.
        ///  
        public override object ConvertFrom(
            ITypeDescriptorContext typeDescriptorContext, 
            CultureInfo cultureInfo, 
            object source)
        { 
            if (source != null)
            {
                if (source is string)
                { 
                    return (FromString((string)source, cultureInfo));
                } 
                else 
                {
                    //  conversion from numeric type 
                    double value;
                    GridUnitType type;

                    value = Convert.ToDouble(source, cultureInfo); 

                    if (DoubleUtil.IsNaN(value)) 
                    { 
                        //  this allows for conversion from Width / Height = "Auto"
                        value = 1.0; 
                        type = GridUnitType.Auto;
                    }
                    else
                    { 
                        type = GridUnitType.Pixel;
                    } 
 
                    return new GridLength(value, type);
                } 
            }
            throw GetConvertFromException(source);
        }
 
        /// 
        /// Attempts to convert a GridLength instance to the given type. 
        ///  
        /// The ITypeDescriptorContext for this call.
        /// The CultureInfo which is respected when converting. 
        /// The GridLength to convert.
        /// The type to which to convert the GridLength instance.
        /// 
        /// The object which was constructed. 
        /// 
        ///  
        /// An ArgumentNullException is thrown if the example object is null. 
        /// 
        ///  
        /// An ArgumentException is thrown if the object is not null and is not a GridLength,
        /// or if the destinationType isn't one of the valid destination types.
        /// 
        /// 
        ///     Critical: calls InstanceDescriptor ctor which LinkDemands
        ///     PublicOK: can only make an InstanceDescriptor for GridLength, not an arbitrary class 
        /// 
        [SecurityCritical]
        public override object ConvertTo( 
            ITypeDescriptorContext typeDescriptorContext,
            CultureInfo cultureInfo,
            object value,
            Type destinationType) 
        {
            if (destinationType == null) 
            { 
                throw new ArgumentNullException("destinationType");
            } 

            if (    value != null
                &&  value is GridLength )
            { 
                GridLength gl = (GridLength)value;
 
                if (destinationType == typeof(string)) 
                {
                    return (ToString(gl, cultureInfo)); 
                }

                if (destinationType == typeof(InstanceDescriptor))
                { 
                    ConstructorInfo ci = typeof(GridLength).GetConstructor(new Type[] { typeof(double), typeof(GridUnitType) });
                    return (new InstanceDescriptor(ci, new object[] { gl.Value, gl.GridUnitType })); 
                } 
            }
            throw GetConvertToException(value, destinationType); 
        }

        #endregion Public Methods
 
        //--------------------------------------------------------------------
        // 
        //  Internal Methods 
        //
        //------------------------------------------------------------------- 

        #region Internal Methods

        ///  
        /// Converts a GridLength instance to a String given the CultureInfo.
        ///  
        /// GridLength instance to convert. 
        /// Culture Info.
        /// String representation of the object. 
        static internal string ToString(GridLength gl, CultureInfo cultureInfo)
        {
            switch (gl.GridUnitType)
            { 
                //  for Auto print out "Auto". value is always "1.0"
                case (GridUnitType.Auto): 
                    return ("Auto"); 

                //  Star has one special case when value is "1.0". 
                //  in this case drop value part and print only "Star"
                case (GridUnitType.Star):
                    return (
                        DoubleUtil.IsOne(gl.Value) 
                        ? "*"
                        : Convert.ToString(gl.Value, cultureInfo) + "*"); 
 
                //  for Pixel print out the numeric value. "px" can be omitted.
                default: 
                    return (Convert.ToString(gl.Value, cultureInfo));

            }
        } 

        ///  
        /// Parses a GridLength from a string given the CultureInfo. 
        /// 
        /// String to parse from. 
        /// Culture Info.
        /// Newly created GridLength instance.
        /// 
        /// Formats: 
        /// "[value][unit]"
        ///     [value] is a double 
        ///     [unit] is a string in GridLength._unitTypes connected to a GridUnitType 
        /// "[value]"
        ///     As above, but the GridUnitType is assumed to be GridUnitType.Pixel 
        /// "[unit]"
        ///     As above, but the value is assumed to be 1.0
        ///     This is only acceptable for a subset of GridUnitType: Auto
        ///  
        static internal GridLength FromString(string s, CultureInfo cultureInfo)
        { 
            double value; 
            GridUnitType unit;
            XamlGridLengthSerializer.FromString(s, cultureInfo, 
                out value, out unit);

            return (new GridLength(value, unit));
        } 

        #endregion Internal Methods 
 
    }
} 

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