FlagsEnumEditor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Design-time editor for the enumerations with Flags attribute.
  6. // Editor displays the drop down list with check marks.
  7. //
  8. #if DESIGNER
  9. using System.ComponentModel;
  10. using System.Drawing.Design;
  11. using FastReport.DataVisualization.Charting;
  12. namespace FastReport.Design.DataVisualization.Charting
  13. {
  14. /// <summary>
  15. /// UI type editor for the enumerations with Flags attribute
  16. /// </summary>
  17. internal class FlagsEnumUITypeEditor : System.Drawing.Design.UITypeEditor
  18. {
  19. #region Constructor
  20. /// <summary>
  21. /// Enumeration type.
  22. /// </summary>
  23. private Type _enumType = null;
  24. #endregion
  25. #region Editor methods and properties
  26. private IWindowsFormsEditorService _edSvc = null;
  27. /// <summary>
  28. /// Display a drop down list with check boxes.
  29. /// </summary>
  30. /// <param name="context">Editing context.</param>
  31. /// <param name="provider">Provider.</param>
  32. /// <param name="value">Value to edit.</param>
  33. /// <returns>Result</returns>
  34. public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  35. {
  36. if (context != null && context.Instance != null && provider != null)
  37. {
  38. _edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  39. if (_edSvc != null)
  40. {
  41. // Get enum type
  42. if(value != null)
  43. {
  44. this._enumType = value.GetType();
  45. }
  46. else if(context != null && context.PropertyDescriptor != null)
  47. {
  48. this._enumType = context.PropertyDescriptor.PropertyType;
  49. }
  50. if(this._enumType != null)
  51. {
  52. // Create control for editing
  53. FlagsEnumCheckedListBox control = new FlagsEnumCheckedListBox(value, this._enumType);
  54. // Show drop down control
  55. _edSvc.DropDownControl(control);
  56. // Get new enumeration value
  57. value = control.GetNewValue();
  58. }
  59. }
  60. }
  61. return value;
  62. }
  63. /// <summary>
  64. /// Gets editing style.
  65. /// </summary>
  66. /// <param name="context">Editing context.</param>
  67. /// <returns>Editor style.</returns>
  68. public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
  69. {
  70. if (context != null && context.Instance != null)
  71. {
  72. return UITypeEditorEditStyle.DropDown;
  73. }
  74. return base.GetEditStyle(context);
  75. }
  76. #endregion
  77. }
  78. /// <summary>
  79. /// Checked list box, which is used for the UI type editing.
  80. /// </summary>
  81. internal class FlagsEnumCheckedListBox : CheckedListBox
  82. {
  83. #region Control fields
  84. // Enumeration object to edit
  85. private object _editValue = null;
  86. // Enumeration type to edit
  87. private Type _editType = null;
  88. #endregion
  89. #region Control constructor
  90. /// <summary>
  91. /// Public constructor.
  92. /// </summary>
  93. /// <param name="editValue">Value to edit.</param>
  94. /// <param name="editType">Typpe to edit.</param>
  95. public FlagsEnumCheckedListBox(object editValue, Type editType)
  96. {
  97. // Set editable value
  98. this._editValue = editValue;
  99. this._editType = editType;
  100. // Set control border style
  101. this.BorderStyle = System.Windows.Forms.BorderStyle.None;
  102. // Fill enum items list
  103. this.FillList();
  104. }
  105. #endregion
  106. #region Control methods
  107. /// <summary>
  108. /// Fills checked list items
  109. /// </summary>
  110. private void FillList()
  111. {
  112. // Check if editable object is of type Enum
  113. if(!(this._editType.IsEnum))
  114. {
  115. throw (new ArgumentException(SR.ExceptionEditorUITypeEditorInapplicable));
  116. }
  117. // Check underlying type
  118. if(Enum.GetUnderlyingType(this._editType) != typeof(Int32))
  119. {
  120. throw (new ArgumentException(SR.ExceptionEditorUITypeEditorInt32ApplicableOnly));
  121. }
  122. // Convert enumeration value to Int32
  123. Int32 editValueInt32 = 0;
  124. if(_editValue != null)
  125. {
  126. editValueInt32 = (Int32)_editValue;
  127. }
  128. // Fill list with all possible values in the enumeration
  129. foreach(object enumValue in Enum.GetValues(this._editType))
  130. {
  131. // Add items into list except the enum value zero
  132. Int32 currentValueInt32 = (Int32)enumValue;
  133. if(currentValueInt32 != 0)
  134. {
  135. bool isChecked = (editValueInt32 & currentValueInt32) == currentValueInt32;
  136. this.Items.Add(Enum.GetName(this._editType, enumValue), isChecked);
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Gets new enumeration value.
  142. /// </summary>
  143. /// <returns>New enum value.</returns>
  144. public object GetNewValue()
  145. {
  146. // Update enumeration flags
  147. Int32 editValueInt32 = 0;
  148. foreach(object checkedItem in this.CheckedItems)
  149. {
  150. Int32 currentValueInt32 = (Int32)Enum.Parse(this._editType, (string)checkedItem);
  151. editValueInt32 = editValueInt32 | currentValueInt32;
  152. }
  153. // Return enumeration value
  154. return Enum.ToObject(this._editType, editValueInt32);
  155. }
  156. #endregion
  157. }
  158. }
  159. #endif