using System; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; using FastReport.Utils; namespace FastReport.Controls { internal class StyleListBox : ListBox { private bool updating; private StyleCollection styles; public event EventHandler StyleSelected; public string Style { get { if (SelectedIndex < 1) return ""; return (string)Items[SelectedIndex]; } set { updating = true; int i = Items.IndexOf(value); SelectedIndex = i != -1 ? i : 0; updating = false; } } public StyleCollection Styles { get { return styles; } set { styles = value; if (value != null) UpdateItems(); } } protected override void OnDrawItem(DrawItemEventArgs e) { e.DrawBackground(); Graphics g = e.Graphics; #if AVALONIA g.FontScale = 1; #endif if (e.Index >= 0) { string name = (string)Items[e.Index]; float scale = this.DpiMultiplier(); using (TextObject sample = new TextObject()) { 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); sample.Text = name; sample.HorzAlign = HorzAlign.Center; sample.VertAlign = VertAlign.Center; int index = Styles.IndexOf(name); if (index != -1) sample.ApplyStyle(Styles[index]); using (var cache = new GraphicCache()) { sample.Draw(new FRPaintEventArgs(g, scale, scale, cache)); } } } } protected override void OnSelectedIndexChanged(EventArgs e) { base.OnSelectedIndexChanged(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 styles) { Items.Add(s.Name); } } public void UpdateDpiDependencies(Control owner) { #if MONO // WPF: fix issues with multi-monitor dpi. Dropdown will be scaled automatically owner = this; #endif if (owner == null) return; ItemHeight = owner.LogicalToDevice(32); Width = owner.LogicalToDevice(150); Height = (Items.Count < 8 ? Items.Count : 8) * ItemHeight + 4; } public StyleListBox() { DrawMode = DrawMode.OwnerDrawFixed; IntegralHeight = false; } } }