ToolStripLineStyleButton.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Windows.Forms;
  6. namespace FastReport.Controls
  7. {
  8. internal class ToolStripLineStyleButton : ToolStripDropDownButton
  9. {
  10. private Control owner; // not used in Mono/WPF
  11. private LineStyleControl selector;
  12. public event EventHandler StyleSelected;
  13. public LineStyle LineStyle
  14. {
  15. get => selector.LineStyle;
  16. set => selector.LineStyle = value;
  17. }
  18. private void selector_ItemSelected(object sender, EventArgs e)
  19. {
  20. DropDown.Close();
  21. if (StyleSelected != null)
  22. StyleSelected(this, EventArgs.Empty);
  23. }
  24. private void UpdateDpiDependencies()
  25. {
  26. selector.Size = selector.LogicalToDevice(new Size(100, 127));
  27. }
  28. public ToolStripLineStyleButton(Control owner)
  29. {
  30. this.owner = owner;
  31. DisplayStyle = ToolStripItemDisplayStyle.Image;
  32. ImageAlign = ContentAlignment.MiddleLeft;
  33. if (!Config.IsRunningOnMono)
  34. {
  35. // better looking in Mono/Win. WPF will override this size
  36. AutoSize = false;
  37. Size = new Size(32, 22);
  38. }
  39. selector = new LineStyleControl(owner);
  40. selector.ItemSelected += selector_ItemSelected;
  41. DropDown = new FRToolStripDropDown(selector);
  42. DropDown.Opening += (s, e) => UpdateDpiDependencies();
  43. }
  44. private class LineStyleControl : ItemSelector
  45. {
  46. private LineStyle[] styles;
  47. public LineStyle LineStyle
  48. {
  49. get => (LineStyle)SelectedIndex;
  50. set => SelectedIndex = (int)value;
  51. }
  52. protected override void DrawItem(Graphics g, Rectangle rect, int index)
  53. {
  54. using (Pen pen = new Pen(Color.Black, Owner.LogicalToDevice(2)))
  55. {
  56. int y = rect.Top + rect.Height / 2;
  57. int _2 = Owner.LogicalToDevice(2);
  58. if (styles[index] == LineStyle.Double)
  59. {
  60. y -= _2;
  61. g.DrawLine(pen, rect.Left + _2, y, rect.Right - _2, y);
  62. y += _2 + _2;
  63. }
  64. else
  65. {
  66. pen.DashStyle = (DashStyle)styles[index];
  67. }
  68. g.DrawLine(pen, rect.Left + _2, y, rect.Right - _2, y);
  69. }
  70. }
  71. public LineStyleControl(Control owner) : base(owner)
  72. {
  73. styles = new LineStyle[] { LineStyle.Solid, LineStyle.Dash, LineStyle.Dot, LineStyle.DashDot, LineStyle.DashDotDot, LineStyle.Double };
  74. ItemsCount = styles.Length;
  75. ItemHeight = 20;
  76. }
  77. }
  78. }
  79. }