StyleListBox.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. using FastReport.Utils;
  6. namespace FastReport.Controls
  7. {
  8. internal class StyleListBox : ListBox
  9. {
  10. private bool updating;
  11. private StyleCollection styles;
  12. public event EventHandler StyleSelected;
  13. public string Style
  14. {
  15. get
  16. {
  17. if (SelectedIndex < 1)
  18. return "";
  19. return (string)Items[SelectedIndex];
  20. }
  21. set
  22. {
  23. updating = true;
  24. int i = Items.IndexOf(value);
  25. SelectedIndex = i != -1 ? i : 0;
  26. updating = false;
  27. }
  28. }
  29. public StyleCollection Styles
  30. {
  31. get { return styles; }
  32. set
  33. {
  34. styles = value;
  35. if (value != null)
  36. UpdateItems();
  37. }
  38. }
  39. protected override void OnDrawItem(DrawItemEventArgs e)
  40. {
  41. e.DrawBackground();
  42. Graphics g = e.Graphics;
  43. #if AVALONIA
  44. g.FontScale = 1;
  45. #endif
  46. if (e.Index >= 0)
  47. {
  48. string name = (string)Items[e.Index];
  49. float scale = this.DpiMultiplier();
  50. using (TextObject sample = new TextObject())
  51. {
  52. sample.Bounds = new RectangleF((int)(e.Bounds.Left / scale) + 2, (int)(e.Bounds.Top / scale) + 2, (int)(e.Bounds.Width / scale) - 4, (int)(e.Bounds.Height / scale) - 4);
  53. sample.Text = name;
  54. sample.HorzAlign = HorzAlign.Center;
  55. sample.VertAlign = VertAlign.Center;
  56. int index = Styles.IndexOf(name);
  57. if (index != -1)
  58. sample.ApplyStyle(Styles[index]);
  59. using (var cache = new GraphicCache())
  60. {
  61. sample.Draw(new FRPaintEventArgs(g, scale, scale, cache));
  62. }
  63. }
  64. }
  65. }
  66. protected override void OnSelectedIndexChanged(EventArgs e)
  67. {
  68. base.OnSelectedIndexChanged(e);
  69. if (updating)
  70. return;
  71. if (StyleSelected != null)
  72. StyleSelected(this, EventArgs.Empty);
  73. }
  74. private void UpdateItems()
  75. {
  76. Items.Clear();
  77. Items.Add(Res.Get("Designer,Toolbar,Style,NoStyle"));
  78. foreach (Style s in styles)
  79. {
  80. Items.Add(s.Name);
  81. }
  82. }
  83. public void UpdateDpiDependencies(Control owner)
  84. {
  85. #if MONO
  86. // WPF: fix issues with multi-monitor dpi. Dropdown will be scaled automatically
  87. owner = this;
  88. #endif
  89. if (owner == null)
  90. return;
  91. ItemHeight = owner.LogicalToDevice(32);
  92. Width = owner.LogicalToDevice(150);
  93. Height = (Items.Count < 8 ? Items.Count : 8) * ItemHeight + 4;
  94. }
  95. public StyleListBox()
  96. {
  97. DrawMode = DrawMode.OwnerDrawFixed;
  98. IntegralHeight = false;
  99. }
  100. }
  101. }