PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Core/IndirectUI/ControlInfo.cs

https://bitbucket.org/tuldok89/openpdn
C# | 149 lines | 114 code | 27 blank | 8 comment | 14 complexity | e4cde36e12e3e35b2830f6f3b6a763c8 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) Rick Brewster, Tom Jackson, and past contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System.Linq;
  10. using PaintDotNet.Base;
  11. using PaintDotNet.Base.PropertySystem;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Windows.Forms;
  15. namespace PaintDotNet.IndirectUI
  16. {
  17. public abstract class ControlInfo
  18. : ICloneable<ControlInfo>
  19. {
  20. private PropertyCollection _controlProperties;
  21. private readonly List<ControlInfo> _childControls = new List<ControlInfo>();
  22. public PropertyCollection ControlProperties
  23. {
  24. get
  25. {
  26. return _controlProperties;
  27. }
  28. protected set
  29. {
  30. _controlProperties = value.Clone();
  31. }
  32. }
  33. public IList<ControlInfo> ChildControls
  34. {
  35. get
  36. {
  37. return new List<ControlInfo>(_childControls);
  38. }
  39. }
  40. private static PropertyControlInfo FindControlForPropertyName(object propertyName, ControlInfo control)
  41. {
  42. var asPCI = control as PropertyControlInfo;
  43. if (asPCI != null && asPCI.Property.Name == propertyName.ToString())
  44. {
  45. return asPCI;
  46. }
  47. return control.GetChildControlsCore().Select(childControl => FindControlForPropertyName(propertyName, childControl)).FirstOrDefault(pci => pci != null);
  48. }
  49. public PropertyControlInfo FindControlForPropertyName(object propertyName)
  50. {
  51. return FindControlForPropertyName(propertyName, this);
  52. }
  53. public bool SetPropertyControlValue(object propertyName, object controlPropertyName, object propertyValue)
  54. {
  55. PropertyControlInfo pci = FindControlForPropertyName(propertyName);
  56. if (pci == null)
  57. {
  58. return false;
  59. }
  60. Property prop = pci.ControlProperties[controlPropertyName];
  61. if (prop == null)
  62. {
  63. return false;
  64. }
  65. prop.Value = propertyValue;
  66. return true;
  67. }
  68. public bool SetPropertyControlType(object propertyName, PropertyControlType newControlType)
  69. {
  70. PropertyControlInfo pci = FindControlForPropertyName(propertyName);
  71. if (pci == null)
  72. {
  73. return false;
  74. }
  75. if (-1 == Array.IndexOf(pci.ControlType.ValueChoices, newControlType))
  76. {
  77. return false;
  78. }
  79. pci.ControlType.Value = newControlType;
  80. return true;
  81. }
  82. protected List<ControlInfo> GetChildControlsCore()
  83. {
  84. return _childControls;
  85. }
  86. internal ControlInfo()
  87. {
  88. _controlProperties = new PropertyCollection(new Property[0], new PropertyCollectionRule[0]);
  89. }
  90. internal ControlInfo(ICloneable<PropertyCollection> controlProperties)
  91. {
  92. _controlProperties = controlProperties.Clone();
  93. }
  94. internal ControlInfo(ControlInfo cloneMe)
  95. {
  96. _controlProperties = cloneMe._controlProperties.Clone();
  97. _childControls = new List<ControlInfo>(cloneMe._childControls.Count);
  98. foreach (ControlInfo ciClone in cloneMe._childControls.Select(ci => ci.Clone()))
  99. {
  100. _childControls.Add(ciClone);
  101. }
  102. }
  103. public object CreateConcreteControl(object uiContainer)
  104. {
  105. return CreateConcreteControl(uiContainer.GetType());
  106. }
  107. public object CreateConcreteControl(Type uiContainerType)
  108. {
  109. if (typeof(Control).IsAssignableFrom(uiContainerType))
  110. {
  111. return CreateWinFormsControl();
  112. }
  113. throw new ArgumentException("uiContainerType is not from a supported UI technology");
  114. }
  115. internal abstract Control CreateWinFormsControl();
  116. public abstract ControlInfo Clone();
  117. object ICloneable.Clone()
  118. {
  119. return Clone();
  120. }
  121. }
  122. }