GradientEditor.cs 4.4 KB

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