MarginsConverter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Drawing.MarginsConverter.cs
  3. //
  4. // Author:
  5. // Dennis Hayes ([email protected])
  6. // Herve Poussineau ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.ComponentModel;
  34. using System.Globalization;
  35. using System.Text.RegularExpressions;
  36. using System.ComponentModel.Design.Serialization;
  37. using System.Reflection;
  38. namespace System.Drawing.Printing {
  39. /// <summary>
  40. /// Summary description for MarginsConverter.
  41. /// </summary>
  42. public class MarginsConverter : ExpandableObjectConverter {
  43. public MarginsConverter() {
  44. }
  45. #region Methods
  46. public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType) {
  47. if (sourceType == typeof(string))
  48. return true;
  49. return base.CanConvertFrom(context, sourceType);
  50. }
  51. public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType) {
  52. if (destinationType == typeof(string))
  53. return true;
  54. if (destinationType == typeof (InstanceDescriptor))
  55. return true;
  56. return base.CanConvertTo(context, destinationType);
  57. }
  58. public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,object value) {
  59. if (value is string)
  60. {
  61. if (value == null)
  62. return new Margins();
  63. // format [left];[right];[top];[bottom]
  64. string separator = @"( |\t)*";
  65. separator = separator + ";" + separator;
  66. string regex = @"(?<left>\d+)" + separator + @"(?<right>\d+)" + separator + @"(?<top>\d+)" + separator + @"(?<bottom>\d+)";
  67. Match match = new Regex(regex).Match(value as string);
  68. if (!match.Success)
  69. throw new ArgumentException("value");
  70. int left, right, top, bottom;
  71. try
  72. {
  73. left = int.Parse(match.Groups["left"].Value);
  74. right = int.Parse(match.Groups["right"].Value);
  75. top = int.Parse(match.Groups["top"].Value);
  76. bottom = int.Parse(match.Groups["bottom"].Value);
  77. }
  78. catch (Exception e)
  79. {
  80. throw new ArgumentException("value", e);
  81. }
  82. return new Margins(left, right, top, bottom);
  83. } else
  84. return base.ConvertFrom(context, culture, value);
  85. }
  86. public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value,Type destinationType) {
  87. if (destinationType == typeof(string) && value is Margins)
  88. {
  89. Margins source = value as Margins;
  90. string ret = "{0}; {1}; {2}; {3}";
  91. return String.Format(ret, source.Left, source.Right, source.Top, source.Bottom);
  92. }
  93. if (destinationType == typeof (InstanceDescriptor) && value is Margins) {
  94. Margins c = (Margins) value;
  95. ConstructorInfo ctor = typeof(Margins).GetConstructor (new Type[] {typeof(int), typeof(int), typeof(int), typeof(int)} );
  96. return new InstanceDescriptor (ctor, new object[] {c.Left, c.Right, c.Top, c.Bottom});
  97. }
  98. return base.ConvertTo(context, culture, value, destinationType);
  99. }
  100. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  101. {
  102. return true;
  103. }
  104. public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
  105. {
  106. try
  107. {
  108. Margins margins = new Margins();
  109. margins.Left = int.Parse(propertyValues["Left"].ToString());
  110. margins.Right = int.Parse(propertyValues["Right"].ToString());
  111. margins.Top = int.Parse(propertyValues["Top"].ToString());
  112. margins.Bottom = int.Parse(propertyValues["Bottom"].ToString());
  113. return margins;
  114. }
  115. catch (Exception)
  116. {
  117. // in case of error, return null
  118. return null;
  119. }
  120. }
  121. #endregion
  122. }
  123. }