StyleComboBoxItem.cs 4.3 KB

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