HatchStyleEditor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 hatch style editor class.
  6. //
  7. using System.ComponentModel;
  8. using System.Drawing;
  9. using System.Drawing.Design;
  10. using System.Reflection;
  11. using FastReport.DataVisualization.Charting;
  12. namespace FastReport.Design.DataVisualization.Charting
  13. {
  14. #if DESIGNER
  15. /// <summary>
  16. /// AxisName editor for the hatch type.
  17. /// Paints a rectangle with hatch sample.
  18. /// </summary>
  19. internal class HatchStyleEditor : UITypeEditor, IDisposable
  20. {
  21. #region Editor methods and properties
  22. // Reference to chart graphics object
  23. ChartGraphics _chartGraph = null;
  24. private bool _disposed;
  25. /// <summary>
  26. /// Override this function to support palette colors drawing
  27. /// </summary>
  28. /// <param name="context">Descriptor context.</param>
  29. /// <returns>Can paint values.</returns>
  30. public override bool GetPaintValueSupported(ITypeDescriptorContext context)
  31. {
  32. return true;
  33. }
  34. /// <summary>
  35. /// Override this function to support palette colors drawing
  36. /// </summary>
  37. /// <param name="e">Paint value event arguments.</param>
  38. public override void PaintValue(PaintValueEventArgs e)
  39. {
  40. if(e.Value is ChartHatchStyle)
  41. {
  42. // Create chart graphics object
  43. if(_chartGraph == null)
  44. {
  45. _chartGraph = new ChartGraphics(null);
  46. }
  47. _chartGraph.Graphics = e.Graphics;
  48. // Try to get original color from the object
  49. Color color1 = Color.Black;
  50. Color color2 = Color.White;
  51. if(e.Context != null && e.Context.Instance != null)
  52. {
  53. // Get color properties using reflection
  54. PropertyInfo propertyInfo = e.Context.Instance.GetType().GetProperty("BackColor");
  55. if(propertyInfo != null)
  56. {
  57. color1 = (Color)propertyInfo.GetValue(e.Context.Instance, null);
  58. }
  59. else
  60. {
  61. propertyInfo = e.Context.Instance.GetType().GetProperty("BackColor");
  62. if(propertyInfo != null)
  63. {
  64. color1 = (Color)propertyInfo.GetValue(e.Context.Instance, null);
  65. }
  66. else
  67. {
  68. // If object do not have "BackColor" property try using "Color" property
  69. propertyInfo = e.Context.Instance.GetType().GetProperty("Color");
  70. if(propertyInfo != null)
  71. {
  72. color1 = (Color)propertyInfo.GetValue(e.Context.Instance, null);
  73. }
  74. }
  75. }
  76. propertyInfo = e.Context.Instance.GetType().GetProperty("BackSecondaryColor");
  77. if(propertyInfo != null)
  78. {
  79. color2 = (Color)propertyInfo.GetValue(e.Context.Instance, null);
  80. }
  81. else
  82. {
  83. propertyInfo = e.Context.Instance.GetType().GetProperty("BackSecondaryColor");
  84. if(propertyInfo != null)
  85. {
  86. color2 = (Color)propertyInfo.GetValue(e.Context.Instance, null);
  87. }
  88. }
  89. }
  90. // Check if colors are valid
  91. if(color1 == Color.Empty)
  92. {
  93. color1 = Color.Black;
  94. }
  95. if(color2 == Color.Empty)
  96. {
  97. color2 = Color.White;
  98. }
  99. if(color1 == color2)
  100. {
  101. color2 = Color.FromArgb(color1.B, color1.R, color1.G);
  102. }
  103. // Draw hatch sample
  104. if((ChartHatchStyle)e.Value != ChartHatchStyle.None)
  105. {
  106. Brush brush = _chartGraph.GetHatchBrush((ChartHatchStyle)e.Value,color1, color2);
  107. e.Graphics.FillRectangle( brush, e.Bounds);
  108. brush.Dispose();
  109. }
  110. }
  111. }
  112. #endregion
  113. #region IDisposable Members
  114. /// <summary>
  115. /// Finalizer for the HatchStyleEditor, disposes any remaining
  116. /// resources if it has not already been disposed.
  117. /// </summary>
  118. ~HatchStyleEditor()
  119. {
  120. Dispose(false);
  121. }
  122. /// <summary>
  123. /// Disposes resources used by this object.
  124. /// </summary>
  125. /// <param name="disposing">Whether this method was called form Dispose() or the finalizer.</param>
  126. protected virtual void Dispose(bool disposing)
  127. {
  128. if (!_disposed)
  129. {
  130. if (disposing)
  131. {
  132. _chartGraph.Dispose();
  133. }
  134. _disposed = true;
  135. }
  136. }
  137. /// <summary>
  138. /// Disposes all resources used by this object.
  139. /// </summary>
  140. public void Dispose()
  141. {
  142. Dispose(true);
  143. GC.SuppressFinalize(this);
  144. }
  145. #endregion
  146. }
  147. #endif
  148. }