StyleComboBoxItem.cs 3.9 KB

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