123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System.ComponentModel.Design.Serialization;
- using System.ComponentModel;
- using System.Globalization;
- using System.Collections;
- namespace System.Windows.Forms
- {
- public class PaddingConverter : TypeConverter
- {
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- if (sourceType == typeof(string))
- {
- return true;
- }
- return base.CanConvertFrom(context, sourceType);
- }
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- if (destinationType == typeof(InstanceDescriptor))
- {
- return true;
- }
- return base.CanConvertTo(context, destinationType);
- }
- public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
- {
- string text = value as string;
- if (text != null)
- {
- text = text.Trim();
- if (text.Length == 0)
- {
- return null;
- }
- if (culture == null)
- {
- culture = CultureInfo.CurrentCulture;
- }
- char c = culture.TextInfo.ListSeparator[0];
- string[] array = text.Split(c);
- int[] array2 = new int[array.Length];
- TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
- for (int i = 0; i < array2.Length; i++)
- {
- array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
- }
- if (array2.Length == 4)
- {
- return new Padding(array2[0], array2[1], array2[2], array2[3]);
- }
- throw new ArgumentException("Parse Failed " + text);
- }
- return base.ConvertFrom(context, culture, value);
- }
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if (destinationType == null)
- {
- throw new ArgumentNullException("destinationType");
- }
- if (value is Padding)
- {
- if (destinationType == typeof(string))
- {
- Padding padding = (Padding)value;
- if (culture == null)
- {
- culture = CultureInfo.CurrentCulture;
- }
- string separator = culture.TextInfo.ListSeparator + " ";
- TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
- string[] array = new string[4];
- int num = 0;
- array[num++] = converter.ConvertToString(context, culture, padding.Left);
- array[num++] = converter.ConvertToString(context, culture, padding.Top);
- array[num++] = converter.ConvertToString(context, culture, padding.Right);
- array[num++] = converter.ConvertToString(context, culture, padding.Bottom);
- return string.Join(separator, array);
- }
- if (destinationType == typeof(InstanceDescriptor))
- {
- Padding padding2 = (Padding)value;
- if (padding2.ShouldSerializeAll())
- {
- return new InstanceDescriptor(typeof(Padding).GetConstructor(new Type[1] { typeof(int) }), new object[1] { padding2.All });
- }
- return new InstanceDescriptor(typeof(Padding).GetConstructor(new Type[4]
- {
- typeof(int),
- typeof(int),
- typeof(int),
- typeof(int)
- }), new object[4] { padding2.Left, padding2.Top, padding2.Right, padding2.Bottom });
- }
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
- {
- if (context == null)
- {
- throw new ArgumentNullException("context");
- }
- if (propertyValues == null)
- {
- throw new ArgumentNullException("propertyValues");
- }
- Padding padding = (Padding)context.PropertyDescriptor.GetValue(context.Instance);
- int num = (int)propertyValues["All"];
- if (padding.All != num)
- {
- return new Padding(num);
- }
- return new Padding((int)propertyValues["Left"], (int)propertyValues["Top"], (int)propertyValues["Right"], (int)propertyValues["Bottom"]);
- }
- public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
- {
- PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Padding), attributes);
- return properties.Sort(new string[5] { "All", "Left", "Top", "Right", "Bottom" });
- }
- public override bool GetPropertiesSupported(ITypeDescriptorContext context)
- {
- return true;
- }
- public PaddingConverter()
- {
- }
- }
- }
|