ToolStripStyleComboBox.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using FastReport.Design;
  6. namespace FastReport.Controls
  7. {
  8. internal class ToolStripStyleComboBox : ToolStripComboBox
  9. {
  10. private bool updating;
  11. private Report report;
  12. public event EventHandler StyleSelected;
  13. public string Style
  14. {
  15. get
  16. {
  17. if (ComboBox.Text == Res.Get("Designer,Toolbar,Style,NoStyle"))
  18. return "";
  19. return ComboBox.Text;
  20. }
  21. set
  22. {
  23. updating = true;
  24. if (value == null)
  25. value = "";
  26. int i = ComboBox.Items.IndexOf(value);
  27. if (i != -1)
  28. ComboBox.SelectedIndex = i;
  29. else
  30. {
  31. if (String.IsNullOrEmpty(value))
  32. value = Res.Get("Designer,Toolbar,Style,NoStyle");
  33. ComboBox.Text = value;
  34. }
  35. updating = false;
  36. }
  37. }
  38. public Report Report
  39. {
  40. get { return report; }
  41. set
  42. {
  43. report = value;
  44. if (value != null)
  45. UpdateItems();
  46. }
  47. }
  48. private void ComboBox_MeasureItem(object sender, MeasureItemEventArgs e)
  49. {
  50. e.ItemHeight = ComboBox.LogicalToDevice(32);
  51. }
  52. private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
  53. {
  54. e.DrawBackground();
  55. Graphics g = e.Graphics;
  56. if (e.Index >= 0)
  57. {
  58. string name = (string)Items[e.Index];
  59. float scale = ComboBox.DpiMultiplier();
  60. using (TextObject sample = new TextObject())
  61. {
  62. sample.Bounds = new RectangleF(e.Bounds.Left + 2, e.Bounds.Top + 2, (int)(e.Bounds.Width / scale) - 4, (int)(e.Bounds.Height / scale) - 4);
  63. sample.Text = name;
  64. sample.HorzAlign = HorzAlign.Center;
  65. sample.VertAlign = VertAlign.Center;
  66. if (report != null)
  67. {
  68. int index = report.Styles.IndexOf(name);
  69. if (index != -1)
  70. sample.ApplyStyle(report.Styles[index]);
  71. }
  72. using (GraphicCache cache = new GraphicCache())
  73. {
  74. sample.Draw(new FRPaintEventArgs(g, scale, scale, cache));
  75. }
  76. }
  77. }
  78. }
  79. private void ComboBox_TextChanged(object sender, EventArgs e)
  80. {
  81. if (updating)
  82. return;
  83. if (StyleSelected != null)
  84. StyleSelected(this, EventArgs.Empty);
  85. }
  86. private void UpdateItems()
  87. {
  88. Items.Clear();
  89. Items.Add(Res.Get("Designer,Toolbar,Style,NoStyle"));
  90. foreach (Style s in report.Styles)
  91. {
  92. Items.Add(s.Name);
  93. }
  94. }
  95. public void UpdateDpiDependencies(Designer designer)
  96. {
  97. Width = designer.LogicalToDevice(100);
  98. ComboBox.ItemHeight = designer.LogicalToDevice(15);
  99. DropDownWidth = designer.LogicalToDevice(150);
  100. DropDownHeight = designer.LogicalToDevice(300);
  101. }
  102. public ToolStripStyleComboBox()
  103. {
  104. AutoSize = false;
  105. ComboBox.DrawMode = DrawMode.OwnerDrawVariable;
  106. ComboBox.ItemHeight = 15;
  107. ComboBox.DrawItem += ComboBox_DrawItem;
  108. ComboBox.MeasureItem += ComboBox_MeasureItem;
  109. ComboBox.TextChanged += ComboBox_TextChanged;
  110. Size = new Size(100, 25);
  111. DropDownWidth = 150;
  112. DropDownHeight = 300;
  113. }
  114. }
  115. }