PaddingConverter.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.ComponentModel.Design.Serialization;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Collections;
  5. namespace System.Windows.Forms
  6. {
  7. public class PaddingConverter : TypeConverter
  8. {
  9. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  10. {
  11. if (sourceType == typeof(string))
  12. {
  13. return true;
  14. }
  15. return base.CanConvertFrom(context, sourceType);
  16. }
  17. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  18. {
  19. if (destinationType == typeof(InstanceDescriptor))
  20. {
  21. return true;
  22. }
  23. return base.CanConvertTo(context, destinationType);
  24. }
  25. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  26. {
  27. string text = value as string;
  28. if (text != null)
  29. {
  30. text = text.Trim();
  31. if (text.Length == 0)
  32. {
  33. return null;
  34. }
  35. if (culture == null)
  36. {
  37. culture = CultureInfo.CurrentCulture;
  38. }
  39. char c = culture.TextInfo.ListSeparator[0];
  40. string[] array = text.Split(c);
  41. int[] array2 = new int[array.Length];
  42. TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
  43. for (int i = 0; i < array2.Length; i++)
  44. {
  45. array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
  46. }
  47. if (array2.Length == 4)
  48. {
  49. return new Padding(array2[0], array2[1], array2[2], array2[3]);
  50. }
  51. throw new ArgumentException("Parse Failed " + text);
  52. }
  53. return base.ConvertFrom(context, culture, value);
  54. }
  55. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  56. {
  57. if (destinationType == null)
  58. {
  59. throw new ArgumentNullException("destinationType");
  60. }
  61. if (value is Padding)
  62. {
  63. if (destinationType == typeof(string))
  64. {
  65. Padding padding = (Padding)value;
  66. if (culture == null)
  67. {
  68. culture = CultureInfo.CurrentCulture;
  69. }
  70. string separator = culture.TextInfo.ListSeparator + " ";
  71. TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
  72. string[] array = new string[4];
  73. int num = 0;
  74. array[num++] = converter.ConvertToString(context, culture, padding.Left);
  75. array[num++] = converter.ConvertToString(context, culture, padding.Top);
  76. array[num++] = converter.ConvertToString(context, culture, padding.Right);
  77. array[num++] = converter.ConvertToString(context, culture, padding.Bottom);
  78. return string.Join(separator, array);
  79. }
  80. if (destinationType == typeof(InstanceDescriptor))
  81. {
  82. Padding padding2 = (Padding)value;
  83. if (padding2.ShouldSerializeAll())
  84. {
  85. return new InstanceDescriptor(typeof(Padding).GetConstructor(new Type[1] { typeof(int) }), new object[1] { padding2.All });
  86. }
  87. return new InstanceDescriptor(typeof(Padding).GetConstructor(new Type[4]
  88. {
  89. typeof(int),
  90. typeof(int),
  91. typeof(int),
  92. typeof(int)
  93. }), new object[4] { padding2.Left, padding2.Top, padding2.Right, padding2.Bottom });
  94. }
  95. }
  96. return base.ConvertTo(context, culture, value, destinationType);
  97. }
  98. public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
  99. {
  100. if (context == null)
  101. {
  102. throw new ArgumentNullException("context");
  103. }
  104. if (propertyValues == null)
  105. {
  106. throw new ArgumentNullException("propertyValues");
  107. }
  108. Padding padding = (Padding)context.PropertyDescriptor.GetValue(context.Instance);
  109. int num = (int)propertyValues["All"];
  110. if (padding.All != num)
  111. {
  112. return new Padding(num);
  113. }
  114. return new Padding((int)propertyValues["Left"], (int)propertyValues["Top"], (int)propertyValues["Right"], (int)propertyValues["Bottom"]);
  115. }
  116. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  117. {
  118. return true;
  119. }
  120. public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
  121. {
  122. PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Padding), attributes);
  123. return properties.Sort(new string[5] { "All", "Left", "Top", "Right", "Bottom" });
  124. }
  125. public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  126. {
  127. return true;
  128. }
  129. public PaddingConverter()
  130. {
  131. }
  132. }
  133. }