Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / System / Windows / ThicknessConverter.cs / 1 / ThicknessConverter.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// File: ThicknessConverter.cs
//
// Description: Contains the ThicknessConverter: TypeConverter for the Thicknessclass.
//
// History:
// 07/09/2003 : greglett - Added to WCP branch (was contained in BoxUnitThickness.cs in old Avalon)
//
//---------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Security;
using MS.Internal;
using MS.Utility;
#pragma warning disable 1634, 1691 // suppressing PreSharp warnings
namespace System.Windows
{
///
/// ThicknessConverter - Converter class for converting instances of other types to and from Thickness instances.
///
public class ThicknessConverter : TypeConverter
{
#region Public Methods
///
/// CanConvertFrom - Returns whether or not this class can convert from a given type.
///
///
/// bool - True if thie converter can convert from the provided type, false if not.
///
/// The ITypeDescriptorContext for this call.
/// The Type being queried for support.
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;
}
}
///
/// CanConvertTo - Returns whether or not this class can convert to a given type.
///
///
/// bool - True if this converter can convert to the provided type, false if not.
///
/// The ITypeDescriptorContext for this call.
/// The Type being queried for support.
public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
{
// We can convert to an InstanceDescriptor or to a string.
if ( destinationType == typeof(InstanceDescriptor)
|| destinationType == typeof(string))
{
return true;
}
else
{
return false;
}
}
///
/// ConvertFrom - Attempt to convert to a Thickness from the given object
///
///
/// The Thickness 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 Thickness.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The object to convert to a Thickness.
public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
{
if (source != null)
{
if (source is string) { return FromString((string)source, cultureInfo); }
else if (source is double) { return new Thickness((double)source); }
else { return new Thickness(Convert.ToDouble(source, cultureInfo)); }
}
throw GetConvertFromException(source);
}
///
/// ConvertTo - Attempt to convert a Thickness to the given type
///
///
/// The object which was constructoed.
///
///
/// An ArgumentNullException is thrown if the example object is null.
///
///
/// An ArgumentException is thrown if the object is not null and is not a Thickness,
/// or if the destinationType isn't one of the valid destination types.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The Thickness to convert.
/// The type to which to convert the Thickness instance.
///
/// Critical: calls InstanceDescriptor ctor which LinkDemands
/// PublicOK: can only make an InstanceDescriptor for Thickness, not an arbitrary class
///
[SecurityCritical]
public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
{
if (null == value)
{
throw new ArgumentNullException("value");
}
if (null == destinationType)
{
throw new ArgumentNullException("destinationType");
}
if (!(value is Thickness))
{
#pragma warning suppress 6506 // value is obviously not null
throw new ArgumentException(SR.Get(SRID.UnexpectedParameterType, value.GetType(), typeof(Thickness)), "value");
}
Thickness th = (Thickness)value;
if (destinationType == typeof(string)) { return ToString(th, cultureInfo); }
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(Thickness).GetConstructor(new Type[] { typeof(double), typeof(double), typeof(double), typeof(double) });
return new InstanceDescriptor(ci, new object[] { th.Left, th.Top, th.Right, th.Bottom });
}
throw new ArgumentException(SR.Get(SRID.CannotConvertType, typeof(Thickness), destinationType.FullName));
}
#endregion Public Methods
//-------------------------------------------------------------------
//
// Internal Methods
//
//-------------------------------------------------------------------
#region Internal Methods
static internal string ToString(Thickness th, CultureInfo cultureInfo)
{
char listSeparator = TokenizerHelper.GetNumericListSeparator(cultureInfo);
// Initial capacity [64] is an estimate based on a sum of:
// 48 = 4x double (twelve digits is generous for the range of values likely)
// 8 = 4x Unit Type string (approx two characters)
// 4 = 4x separator characters
StringBuilder sb = new StringBuilder(64);
sb.Append(LengthConverter.ToString(th.Left, cultureInfo));
sb.Append(listSeparator);
sb.Append(LengthConverter.ToString(th.Top, cultureInfo));
sb.Append(listSeparator);
sb.Append(LengthConverter.ToString(th.Right, cultureInfo));
sb.Append(listSeparator);
sb.Append(LengthConverter.ToString(th.Bottom, cultureInfo));
return sb.ToString();
}
static internal Thickness FromString(string s, CultureInfo cultureInfo)
{
TokenizerHelper th = new TokenizerHelper(s, cultureInfo);
double[] lengths = new double[4];
int i = 0;
// Peel off each double in the delimited list.
while (th.NextToken())
{
if (i >= 4)
{
i = 5; // Set i to a bad value.
break;
}
lengths[i] = LengthConverter.FromString(th.GetCurrentToken(), cultureInfo);
i++;
}
// We have a reasonable interpreation for one value (all four edges), two values (horizontal, vertical),
// and four values (left, top, right, bottom).
switch (i)
{
case 1:
return new Thickness(lengths[0]);
case 2:
return new Thickness(lengths[0], lengths[1], lengths[0], lengths[1]);
case 4:
return new Thickness(lengths[0], lengths[1], lengths[2], lengths[3]);
}
throw new FormatException(SR.Get(SRID.InvalidStringThickness, s));
}
#endregion
}
}
// 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: ThicknessConverter.cs
//
// Description: Contains the ThicknessConverter: TypeConverter for the Thicknessclass.
//
// History:
// 07/09/2003 : greglett - Added to WCP branch (was contained in BoxUnitThickness.cs in old Avalon)
//
//---------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Security;
using MS.Internal;
using MS.Utility;
#pragma warning disable 1634, 1691 // suppressing PreSharp warnings
namespace System.Windows
{
///
/// ThicknessConverter - Converter class for converting instances of other types to and from Thickness instances.
///
public class ThicknessConverter : TypeConverter
{
#region Public Methods
///
/// CanConvertFrom - Returns whether or not this class can convert from a given type.
///
///
/// bool - True if thie converter can convert from the provided type, false if not.
///
/// The ITypeDescriptorContext for this call.
/// The Type being queried for support.
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;
}
}
///
/// CanConvertTo - Returns whether or not this class can convert to a given type.
///
///
/// bool - True if this converter can convert to the provided type, false if not.
///
/// The ITypeDescriptorContext for this call.
/// The Type being queried for support.
public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
{
// We can convert to an InstanceDescriptor or to a string.
if ( destinationType == typeof(InstanceDescriptor)
|| destinationType == typeof(string))
{
return true;
}
else
{
return false;
}
}
///
/// ConvertFrom - Attempt to convert to a Thickness from the given object
///
///
/// The Thickness 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 Thickness.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The object to convert to a Thickness.
public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
{
if (source != null)
{
if (source is string) { return FromString((string)source, cultureInfo); }
else if (source is double) { return new Thickness((double)source); }
else { return new Thickness(Convert.ToDouble(source, cultureInfo)); }
}
throw GetConvertFromException(source);
}
///
/// ConvertTo - Attempt to convert a Thickness to the given type
///
///
/// The object which was constructoed.
///
///
/// An ArgumentNullException is thrown if the example object is null.
///
///
/// An ArgumentException is thrown if the object is not null and is not a Thickness,
/// or if the destinationType isn't one of the valid destination types.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The Thickness to convert.
/// The type to which to convert the Thickness instance.
///
/// Critical: calls InstanceDescriptor ctor which LinkDemands
/// PublicOK: can only make an InstanceDescriptor for Thickness, not an arbitrary class
///
[SecurityCritical]
public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
{
if (null == value)
{
throw new ArgumentNullException("value");
}
if (null == destinationType)
{
throw new ArgumentNullException("destinationType");
}
if (!(value is Thickness))
{
#pragma warning suppress 6506 // value is obviously not null
throw new ArgumentException(SR.Get(SRID.UnexpectedParameterType, value.GetType(), typeof(Thickness)), "value");
}
Thickness th = (Thickness)value;
if (destinationType == typeof(string)) { return ToString(th, cultureInfo); }
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(Thickness).GetConstructor(new Type[] { typeof(double), typeof(double), typeof(double), typeof(double) });
return new InstanceDescriptor(ci, new object[] { th.Left, th.Top, th.Right, th.Bottom });
}
throw new ArgumentException(SR.Get(SRID.CannotConvertType, typeof(Thickness), destinationType.FullName));
}
#endregion Public Methods
//-------------------------------------------------------------------
//
// Internal Methods
//
//-------------------------------------------------------------------
#region Internal Methods
static internal string ToString(Thickness th, CultureInfo cultureInfo)
{
char listSeparator = TokenizerHelper.GetNumericListSeparator(cultureInfo);
// Initial capacity [64] is an estimate based on a sum of:
// 48 = 4x double (twelve digits is generous for the range of values likely)
// 8 = 4x Unit Type string (approx two characters)
// 4 = 4x separator characters
StringBuilder sb = new StringBuilder(64);
sb.Append(LengthConverter.ToString(th.Left, cultureInfo));
sb.Append(listSeparator);
sb.Append(LengthConverter.ToString(th.Top, cultureInfo));
sb.Append(listSeparator);
sb.Append(LengthConverter.ToString(th.Right, cultureInfo));
sb.Append(listSeparator);
sb.Append(LengthConverter.ToString(th.Bottom, cultureInfo));
return sb.ToString();
}
static internal Thickness FromString(string s, CultureInfo cultureInfo)
{
TokenizerHelper th = new TokenizerHelper(s, cultureInfo);
double[] lengths = new double[4];
int i = 0;
// Peel off each double in the delimited list.
while (th.NextToken())
{
if (i >= 4)
{
i = 5; // Set i to a bad value.
break;
}
lengths[i] = LengthConverter.FromString(th.GetCurrentToken(), cultureInfo);
i++;
}
// We have a reasonable interpreation for one value (all four edges), two values (horizontal, vertical),
// and four values (left, top, right, bottom).
switch (i)
{
case 1:
return new Thickness(lengths[0]);
case 2:
return new Thickness(lengths[0], lengths[1], lengths[0], lengths[1]);
case 4:
return new Thickness(lengths[0], lengths[1], lengths[2], lengths[3]);
}
throw new FormatException(SR.Get(SRID.InvalidStringThickness, s));
}
#endregion
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SupportingTokenChannel.cs
- SchemaElementLookUpTable.cs
- TextFormatterHost.cs
- ComNativeDescriptor.cs
- DataSourceControlBuilder.cs
- PageRequestManager.cs
- PropertyManager.cs
- XmlDataProvider.cs
- DataControlField.cs
- BaseCodeDomTreeGenerator.cs
- Query.cs
- XmlSignificantWhitespace.cs
- XamlTreeBuilderBamlRecordWriter.cs
- StyleReferenceConverter.cs
- IdleTimeoutMonitor.cs
- DecimalConstantAttribute.cs
- GACMembershipCondition.cs
- SqlDataSourceQuery.cs
- SecurityTokenSerializer.cs
- DbBuffer.cs
- CustomCredentialPolicy.cs
- PasswordDeriveBytes.cs
- SoapHeaders.cs
- XmlAutoDetectWriter.cs
- NamedPipeProcessProtocolHandler.cs
- AudienceUriMode.cs
- HtmlTable.cs
- HttpHandlerAction.cs
- ValidationErrorEventArgs.cs
- DateTimeParse.cs
- TextServicesHost.cs
- ChannelDispatcher.cs
- xml.cs
- SafeReadContext.cs
- DataGridViewCellValueEventArgs.cs
- XmlWrappingWriter.cs
- WebPartDisplayMode.cs
- SlipBehavior.cs
- GrammarBuilder.cs
- BaseAsyncResult.cs
- AppDomainFactory.cs
- DbDataReader.cs
- ReflectPropertyDescriptor.cs
- ManifestResourceInfo.cs
- AdvancedBindingPropertyDescriptor.cs
- ObjectQueryState.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- ImageAnimator.cs
- TriggerCollection.cs
- MetadataArtifactLoaderCompositeResource.cs
- ReflectEventDescriptor.cs
- DataGridViewSelectedCellCollection.cs
- FreezableOperations.cs
- DesignDataSource.cs
- LayoutTableCell.cs
- ExpandSegmentCollection.cs
- AssemblyNameProxy.cs
- DelegateSerializationHolder.cs
- ConstructorArgumentAttribute.cs
- ApplicationManager.cs
- AnonymousIdentificationModule.cs
- FormViewInsertEventArgs.cs
- SvcMapFileSerializer.cs
- UpdateCommand.cs
- MimeObjectFactory.cs
- PropertyIDSet.cs
- CompleteWizardStep.cs
- EventHandlerList.cs
- SendKeys.cs
- LambdaCompiler.Address.cs
- ObjectStateEntryDbUpdatableDataRecord.cs
- WaitingCursor.cs
- EntityDataSource.cs
- Rules.cs
- MailHeaderInfo.cs
- NullableIntMinMaxAggregationOperator.cs
- HandleRef.cs
- BindingNavigator.cs
- VisualStyleRenderer.cs
- XmlBinaryReader.cs
- Panel.cs
- SaveWorkflowCommand.cs
- SynchronizationLockException.cs
- TextProviderWrapper.cs
- EmbossBitmapEffect.cs
- SpellCheck.cs
- FullTextLine.cs
- ParsedAttributeCollection.cs
- RemoteArgument.cs
- SourceItem.cs
- DataGridViewUtilities.cs
- SQLInt16Storage.cs
- KnownTypeAttribute.cs
- EntityDescriptor.cs
- RequestCachingSection.cs
- RequestCacheEntry.cs
- BinaryParser.cs
- AmbientValueAttribute.cs
- EdmProperty.cs
- MasterPage.cs