using System; using System.Windows.Forms; using System.Drawing; using FastReport.Utils; using FastReport.Design; namespace FastReport.Controls { internal class ToolStripStyleComboBox : ToolStripComboBox { private bool updating; private Report report; public event EventHandler StyleSelected; public string Style { get { if (ComboBox.Text == Res.Get("Designer,Toolbar,Style,NoStyle")) return ""; return ComboBox.Text; } set { updating = true; if (value == null) value = ""; int i = ComboBox.Items.IndexOf(value); if (i != -1) ComboBox.SelectedIndex = i; else { if (String.IsNullOrEmpty(value)) value = Res.Get("Designer,Toolbar,Style,NoStyle"); ComboBox.Text = value; } updating = false; } } public Report Report { get { return report; } set { report = value; if (value != null) UpdateItems(); } } private void ComboBox_MeasureItem(object sender, MeasureItemEventArgs e) { e.ItemHeight = ComboBox.LogicalToDevice(32); } private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); Graphics g = e.Graphics; if (e.Index >= 0) { string name = (string)Items[e.Index]; float scale = ComboBox.DpiMultiplier(); using (TextObject sample = new TextObject()) { sample.Bounds = new RectangleF(e.Bounds.Left + 2, e.Bounds.Top + 2, (int)(e.Bounds.Width / scale) - 4, (int)(e.Bounds.Height / scale) - 4); sample.Text = name; sample.HorzAlign = HorzAlign.Center; sample.VertAlign = VertAlign.Center; if (report != null) { int index = report.Styles.IndexOf(name); if (index != -1) sample.ApplyStyle(report.Styles[index]); } using (GraphicCache cache = new GraphicCache()) { sample.Draw(new FRPaintEventArgs(g, scale, scale, cache)); } } } } private void ComboBox_TextChanged(object sender, EventArgs e) { if (updating) return; if (StyleSelected != null) StyleSelected(this, EventArgs.Empty); } private void UpdateItems() { Items.Clear(); Items.Add(Res.Get("Designer,Toolbar,Style,NoStyle")); foreach (Style s in report.Styles) { Items.Add(s.Name); } } public void UpdateDpiDependencies(Designer designer) { Width = designer.LogicalToDevice(100); ComboBox.ItemHeight = designer.LogicalToDevice(15); DropDownWidth = designer.LogicalToDevice(150); DropDownHeight = designer.LogicalToDevice(300); } public ToolStripStyleComboBox() { AutoSize = false; ComboBox.DrawMode = DrawMode.OwnerDrawVariable; ComboBox.ItemHeight = 15; ComboBox.DrawItem += ComboBox_DrawItem; ComboBox.MeasureItem += ComboBox_MeasureItem; ComboBox.TextChanged += ComboBox_TextChanged; Size = new Size(100, 25); DropDownWidth = 150; DropDownHeight = 300; } } }