123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- using System;
- using System.Windows.Forms;
- using System.Drawing;
- using FastReport.Utils;
- using System.ComponentModel;
- using FastReport.Data;
- namespace FastReport.Controls
- {
- /// <summary>
- /// Represents the combobox used to select a data column.
- /// </summary>
- #if !DEBUG
- [DesignTimeVisible(false)]
- #endif
- public class DataColumnComboBox : UserControl
- {
- private TextBox textBox;
- private Button button;
- private Button comboButton;
- private Report report;
- private DataColumnDropDown dropDown;
- private Timer timer;
- private int closedTicks;
- private int dpi;
- /// <summary>
- /// Occurs when the text portion of the combobox is changed.
- /// </summary>
- public new event EventHandler TextChanged;
- /// <inheritdoc/>
- public override string Text
- {
- get { return textBox.Text; }
- set { textBox.Text = value; }
- }
- /// <summary>
- /// Gets or sets the data source.
- /// </summary>
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public DataSourceBase DataSource
- {
- get { return dropDown.DataSource; }
- set
- {
- // to recreate datasource list
- Report = Report;
- dropDown.DataSource = value;
- }
- }
- /// <summary>
- /// Gets or sets the Report.
- /// </summary>
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public Report Report
- {
- get { return report; }
- set
- {
- report = value;
- dropDown.CreateNodes(value);
- }
- }
- private void FTextBox_TextChanged(object sender, EventArgs e)
- {
- if (TextChanged != null)
- TextChanged(this, EventArgs.Empty);
- }
- private void FDropDown_ColumnSelected(object sender, EventArgs e)
- {
- Text = String.IsNullOrEmpty(dropDown.Column) ? "" : "[" + dropDown.Column + "]";
- timer.Start();
- }
- private void FDropDown_Closed(object sender, ToolStripDropDownClosedEventArgs e)
- {
- closedTicks = Environment.TickCount;
- }
- private void FComboButton_Click(object sender, EventArgs e)
- {
- if (Math.Abs(Environment.TickCount - closedTicks) < 100)
- return;
- string column = Text.Replace("[", "");
- column = column.Replace("]", "");
- dropDown.Column = column;
- dropDown.SetSize(Width, 250);
- dropDown.RightToLeft = RightToLeft;
- dropDown.Owner = this;
- dropDown.Show(this, new Point(RightToLeft == RightToLeft.Yes ? Width : 0, Height));
- }
- private void FButton_Click(object sender, EventArgs e)
- {
- Text = Editors.EditExpression(Report, Text);
- timer.Start();
- }
- private void FTimer_Tick(object sender, EventArgs e)
- {
- FindForm().BringToFront();
- textBox.Focus();
- textBox.Select(Text.Length, 0);
- timer.Stop();
- }
- private void LayoutControls()
- {
- int _1 = (int)Math.Round(this.LogicalToDevice(1f));
- int _2 = _1 + _1;
- int btnWidth = Height - _2;
- if (button != null)
- button.SetBounds(RightToLeft == RightToLeft.Yes ? _1 : Width - btnWidth - _1, _1, btnWidth, btnWidth);
- if (comboButton != null)
- comboButton.SetBounds(RightToLeft == RightToLeft.Yes ? btnWidth + _1 : Width - btnWidth * 2 - _1, _1, btnWidth, btnWidth);
- if (textBox != null)
- textBox.SetBounds(RightToLeft == RightToLeft.Yes ? btnWidth * 2 + 1 : 3, (Height - textBox.PreferredHeight) / 2, Width - btnWidth * 2 - 3, textBox.PreferredHeight);
- }
- private void UpdateImages()
- {
- #if AVALONIA
- // doing this in the render code will result in InvalidOp exception
- Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
- {
- button.Image = this.GetImage(52);
- comboButton.Image = this.GetImage(182);
- });
- #else
- button.Image = this.GetImage(52);
- comboButton.Image = this.GetImage(182);
- #endif
- }
- /// <inheritdoc/>
- protected override void OnPaint(PaintEventArgs e)
- {
- if (dpi != this.Dpi())
- {
- UpdateImages();
- dpi = this.Dpi();
- }
- Graphics g = e.Graphics;
- g.FillRectangle(Enabled ? SystemBrushes.Window : SystemBrushes.Control, DisplayRectangle);
- if (Enabled)
- this.DrawVisualStyleBorder(g, new Rectangle(0, 0, Width - 1, Height - 1));
- else
- g.DrawRectangle(SystemPens.InactiveBorder, new Rectangle(0, 0, Width - 1, Height - 1));
- }
- /// <inheritdoc/>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (timer != null)
- timer.Dispose();
- timer = null;
- }
- base.Dispose(disposing);
- }
- /// <inheritdoc/>
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- LayoutControls();
- }
- /// <inheritdoc/>
- protected override void OnLayout(LayoutEventArgs e)
- {
- base.OnLayout(e);
- LayoutControls();
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="DataColumnComboBox"/> class.
- /// </summary>
- public DataColumnComboBox()
- {
- button = new Button();
- button.FlatStyle = FlatStyle.Flat;
- button.FlatAppearance.BorderSize = 0;
- button.Click += new EventHandler(FButton_Click);
- Controls.Add(button);
- comboButton = new Button();
- comboButton.FlatStyle = FlatStyle.Flat;
- comboButton.FlatAppearance.BorderSize = 0;
- comboButton.Click += new EventHandler(FComboButton_Click);
- Controls.Add(comboButton);
- textBox = new TextBox();
- textBox.BorderStyle = BorderStyle.None;
- textBox.TextChanged += new EventHandler(FTextBox_TextChanged);
- Controls.Add(textBox);
- dropDown = new DataColumnDropDown();
- dropDown.ColumnSelected += new EventHandler(FDropDown_ColumnSelected);
- dropDown.Closed += new ToolStripDropDownClosedEventHandler(FDropDown_Closed);
- timer = new Timer();
- timer.Interval = 50;
- timer.Tick += new EventHandler(FTimer_Tick);
- // prevent autoscale of child controls
- AutoScaleMode = AutoScaleMode.None;
- SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
- }
- }
- }
|