MarginsConverter.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. namespace System.Drawing.Printing {
  37. /// <summary>
  38. /// Summary description for MarginsConverter.
  39. /// </summary>
  40. public class MarginsConverter : ExpandableObjectConverter {
  41. public MarginsConverter() {
  42. }
  43. #region Methods
  44. public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType) {
  45. if (sourceType == typeof(string))
  46. return true;
  47. return base.CanConvertFrom(context, sourceType);
  48. }
  49. public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType) {
  50. if (destinationType == typeof(string))
  51. return true;
  52. return base.CanConvertTo(context, destinationType);
  53. }
  54. public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,object value) {
  55. if (value is string)
  56. {
  57. if (value == null)
  58. return new Margins();
  59. // format [left];[right];[top];[bottom]
  60. string separator = @"( |\t)*";
  61. separator = separator + ";" + separator;
  62. string regex = @"(?<left>\d+)" + separator + @"(?<right>\d+)" + separator + @"(?<top>\d+)" + separator + @"(?<bottom>\d+)";
  63. Match match = new Regex(regex).Match(value as string);
  64. if (!match.Success)
  65. throw new ArgumentException("value");
  66. int left, right, top, bottom;
  67. try
  68. {
  69. left = int.Parse(match.Groups["left"].Value);
  70. right = int.Parse(match.Groups["right"].Value);
  71. top = int.Parse(match.Groups["top"].Value);
  72. bottom = int.Parse(match.Groups["bottom"].Value);
  73. }
  74. catch (Exception e)
  75. {
  76. throw new ArgumentException("value", e);
  77. }
  78. return new Margins(left, right, top, bottom);
  79. } else
  80. return base.ConvertFrom(context, culture, value);
  81. }
  82. public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value,Type destinationType) {
  83. if (destinationType == typeof(string) && value is Margins)
  84. {
  85. Margins source = value as Margins;
  86. string ret = "{0}; {1}; {2}; {3}";
  87. return String.Format(ret, source.Left, source.Right, source.Top, source.Bottom);
  88. } else
  89. return base.ConvertTo(context, culture, value, destinationType);
  90. }
  91. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  92. {
  93. return true;
  94. }
  95. public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
  96. {
  97. try
  98. {
  99. Margins margins = new Margins();
  100. margins.Left = int.Parse(propertyValues["Left"].ToString());
  101. margins.Right = int.Parse(propertyValues["Right"].ToString());
  102. margins.Top = int.Parse(propertyValues["Top"].ToString());
  103. margins.Bottom = int.Parse(propertyValues["Bottom"].ToString());
  104. return margins;
  105. }
  106. catch (Exception)
  107. {
  108. // in case of error, return null
  109. return null;
  110. }
  111. }
  112. #endregion
  113. }
  114. }