using System; using System.Drawing; using System.ComponentModel; using System.Reflection; using FastReport.Utils; using System.Windows.Forms; using System.Drawing.Design; namespace FastReport.Dialog { /// /// Base class for all dialog controls such as ButtonControl, TextBoxControl. /// public abstract partial class DialogControl : DialogComponentBase { #region Fields private Control control; private string clickEvent; private string doubleClickEvent; private string enterEvent; private string leaveEvent; private string keyDownEvent; private string keyPressEvent; private string keyUpEvent; private string mouseDownEvent; private string mouseMoveEvent; private string mouseUpEvent; private string mouseEnterEvent; private string mouseLeaveEvent; private string resizeEvent; private string textChangedEvent; private string paintEvent; private PropertyInfo bindableProperty; #endregion #region Properties /// /// Occurs when the control is clicked. /// Wraps the event. /// public event EventHandler Click; /// /// Occurs when the control is double-clicked. /// Wraps the event. /// public event EventHandler DoubleClick; /// /// Occurs when the control is entered. /// Wraps the event. /// public event EventHandler Enter; /// /// Occurs when the input focus leaves the control. /// Wraps the event. /// public event EventHandler Leave; /// /// Occurs when a key is pressed while the control has focus. /// Wraps the event. /// public event KeyEventHandler KeyDown; /// /// Occurs when a key is pressed while the control has focus. /// Wraps the event. /// public event KeyPressEventHandler KeyPress; /// /// Occurs when a key is released while the control has focus. /// Wraps the event. /// public event KeyEventHandler KeyUp; /// /// Occurs when the mouse pointer is over the control and a mouse button is pressed. /// Wraps the event. /// public event MouseEventHandler MouseDown; /// /// Occurs when the mouse pointer is moved over the control. /// Wraps the event. /// public event MouseEventHandler MouseMove; /// /// Occurs when the mouse pointer is over the control and a mouse button is released. /// Wraps the event. /// public event MouseEventHandler MouseUp; /// /// Occurs when the mouse pointer enters the control. /// Wraps the event. /// public event EventHandler MouseEnter; /// /// Occurs when the mouse pointer leaves the control. /// Wraps the event. /// public event EventHandler MouseLeave; /// /// Occurs when the control is resized. /// Wraps the event. /// public event EventHandler Resize; /// /// Occurs when the Text property value changes. /// Wraps the event. /// public event EventHandler TextChanged; /// /// Occurs when the control is redrawn. /// Wraps the event. /// public event PaintEventHandler Paint; /// /// Gets an internal Control. /// [Browsable(false)] public Control Control { get { return control; } set { control = value; } } /// /// Gets or sets the background color for the control. /// Wraps the property. /// [Category("Appearance")] [Editor("FastReport.TypeEditors.ColorEditor, FastReport", typeof(UITypeEditor))] public virtual Color BackColor { get { return Control.BackColor; } set { Control.BackColor = value; } } /// /// Gets or sets the cursor that is displayed when the mouse pointer is over the control. /// Wraps the property. /// [Category("Appearance")] public Cursor Cursor { get { return Control.Cursor; } set { Control.Cursor = value; } } /// /// Gets or sets a value indicating whether the control can respond to user interaction. /// Wraps the property. /// [DefaultValue(true)] [Category("Behavior")] public bool Enabled { get { return Control.Enabled; } set { Control.Enabled = value; OnEnabledChanged(); } } /// /// Gets or sets the font of the text displayed by the control. /// Wraps the property. /// [Category("Appearance")] public Font Font { get { return Control.Font; } set { Control.Font = value; } } /// /// Gets or sets the foreground color of the control. /// Wraps the property. /// [Category("Appearance")] [Editor("FastReport.TypeEditors.ColorEditor, FastReport", typeof(UITypeEditor))] public virtual Color ForeColor { get { return Control.ForeColor; } set { Control.ForeColor = value; } } /// /// Gets or sets a value indicating whether control's elements are aligned to support locales using right-to-left fonts. /// Wraps the property. /// [DefaultValue(RightToLeft.No)] [Category("Appearance")] public RightToLeft RightToLeft { get { return Control.RightToLeft; } set { Control.RightToLeft = value; } } /// /// Gets or sets the tab order of the control within its container. /// Wraps the property. /// [DefaultValue(0)] [Category("Behavior")] public int TabIndex { get { return Control.TabIndex; } set { Control.TabIndex = value; } } /// /// Gets or sets a value indicating whether the user can give the focus to this control using the TAB key. /// Wraps the property. /// [DefaultValue(true)] [Category("Behavior")] public bool TabStop { get { return Control.TabStop; } set { Control.TabStop = value; } } /// /// Gets or sets the text associated with this control. /// Wraps the property. /// [Category("Data")] [Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] public virtual string Text { get { return Control.Text; } set { Control.Text = value; } } /// /// Gets or sets which control borders are docked to its parent control and determines how a control is resized with its parent. /// Wraps the property. /// [Category("Layout")] public override DockStyle Dock { get { return Control.Dock; } set { Control.Dock = value; } } /// /// Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent. /// Wraps the property. /// [Category("Layout")] public override AnchorStyles Anchor { get { return Control.Anchor; } set { Control.Anchor = value; } } /// /// Gets or sets a value indicating whether the control is displayed. /// Wraps the property. /// [Category("Behavior")] public override bool Visible { get { return base.Visible; } set { base.Visible = value; Control.Visible = value; } } /// /// Gets or sets a property that returns actual data contained in a control. This value is used /// in the "Data" window. /// [Browsable(false)] public PropertyInfo BindableProperty { get { return bindableProperty; } set { bindableProperty = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string ClickEvent { get { return clickEvent; } set { clickEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string DoubleClickEvent { get { return doubleClickEvent; } set { doubleClickEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string EnterEvent { get { return enterEvent; } set { enterEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string LeaveEvent { get { return leaveEvent; } set { leaveEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string KeyDownEvent { get { return keyDownEvent; } set { keyDownEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string KeyPressEvent { get { return keyPressEvent; } set { keyPressEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string KeyUpEvent { get { return keyUpEvent; } set { keyUpEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string MouseDownEvent { get { return mouseDownEvent; } set { mouseDownEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string MouseMoveEvent { get { return mouseMoveEvent; } set { mouseMoveEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string MouseUpEvent { get { return mouseUpEvent; } set { mouseUpEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string MouseEnterEvent { get { return mouseEnterEvent; } set { mouseEnterEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string MouseLeaveEvent { get { return mouseLeaveEvent; } set { mouseLeaveEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string ResizeEvent { get { return resizeEvent; } set { resizeEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string TextChangedEvent { get { return textChangedEvent; } set { textChangedEvent = value; } } /// /// Gets or sets a script method name that will be used to handle the /// event. /// [Category("Events")] public string PaintEvent { get { return paintEvent; } set { paintEvent = value; } } /// public override float Left { get { return Control.Left; } set { if (!IsDesigning || !HasRestriction(Restrictions.DontMove)) Control.Left = (int)value; } } /// public override float Top { get { return Control.Top; } set { if (!IsDesigning || !HasRestriction(Restrictions.DontMove)) Control.Top = (int)value; } } /// public override float Width { get { return Control.Width; } set { if (!IsDesigning || !HasRestriction(Restrictions.DontResize)) Control.Width = (int)value; } } /// public override float Height { get { return Control.Height; } set { if (!IsDesigning || !HasRestriction(Restrictions.DontResize)) Control.Height = (int)value; } } #endregion #region Private Methods private void Control_Click(object sender, EventArgs e) { OnClick(e); } private void Control_DoubleClick(object sender, EventArgs e) { OnDoubleClick(e); } private void Control_Enter(object sender, EventArgs e) { OnEnter(e); } private void Control_Leave(object sender, EventArgs e) { OnLeave(e); } private void Control_KeyDown(object sender, KeyEventArgs e) { OnKeyDown(e); } private void Control_KeyPress(object sender, KeyPressEventArgs e) { OnKeyPress(e); } private void Control_KeyUp(object sender, KeyEventArgs e) { OnKeyUp(e); } private void Control_MouseDown(object sender, MouseEventArgs e) { OnMouseDown(e); } private void Control_MouseMove(object sender, MouseEventArgs e) { OnMouseMove(e); } private void Control_MouseUp(object sender, MouseEventArgs e) { OnMouseUp(e); } private void Control_MouseEnter(object sender, EventArgs e) { OnMouseEnter(e); } private void Control_MouseLeave(object sender, EventArgs e) { OnMouseLeave(e); } private void Control_Resize(object sender, EventArgs e) { OnResize(e); } private void Control_TextChanged(object sender, EventArgs e) { OnTextChanged(e); } private void Control_Paint(object sender, PaintEventArgs e) { OnPaint(e); } #endregion #region Protected Methods /// protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) Control.Dispose(); } /// /// Called when the control's Enabled state changed. /// protected virtual void OnEnabledChanged() { } /// /// Attaches Control events to its event handlers. /// /// /// Override this method if your custom control has own events. /// /// See the example of AttachEvents implementation used in the CheckBoxControl: /// /// protected override void AttachEvents() /// { /// base.AttachEvents(); /// CheckBox.CheckedChanged += new EventHandler(CheckBox_CheckedChanged); /// } /// /// private void CheckBox_CheckedChanged(object sender, EventArgs e) /// { /// if (CheckedChanged != null) /// CheckedChanged(this, e); /// InvokeEvent(CheckedChangedEvent, e); /// } /// /// protected virtual void AttachEvents() { Control.Click += new EventHandler(Control_Click); Control.DoubleClick += new EventHandler(Control_DoubleClick); Control.Enter += new EventHandler(Control_Enter); Control.Leave += new EventHandler(Control_Leave); Control.KeyDown += new KeyEventHandler(Control_KeyDown); Control.KeyPress += new KeyPressEventHandler(Control_KeyPress); Control.KeyUp += new KeyEventHandler(Control_KeyUp); Control.MouseDown += new MouseEventHandler(Control_MouseDown); Control.MouseMove += new MouseEventHandler(Control_MouseMove); Control.MouseUp += new MouseEventHandler(Control_MouseUp); Control.MouseEnter += new EventHandler(Control_MouseEnter); Control.MouseLeave += new EventHandler(Control_MouseLeave); Control.Resize += new EventHandler(Control_Resize); Control.TextChanged += new EventHandler(Control_TextChanged); Control.Paint += new PaintEventHandler(Control_Paint); } /// /// Detaches Control events from its event handlers. /// /// /// Override this method if your custom control has own events. In this method, you should /// detach control's events that were attached in the method. /// /// See the example of DetachEvents implementation used in the CheckBoxControl: /// /// protected override void DetachEvents() /// { /// base.DetachEvents(); /// CheckBox.CheckedChanged -= new EventHandler(CheckBox_CheckedChanged); /// } /// /// protected virtual void DetachEvents() { Control.Click -= new EventHandler(Control_Click); Control.DoubleClick -= new EventHandler(Control_DoubleClick); Control.Enter -= new EventHandler(Control_Enter); Control.Leave -= new EventHandler(Control_Leave); Control.KeyDown -= new KeyEventHandler(Control_KeyDown); Control.KeyPress -= new KeyPressEventHandler(Control_KeyPress); Control.KeyUp -= new KeyEventHandler(Control_KeyUp); Control.MouseDown -= new MouseEventHandler(Control_MouseDown); Control.MouseMove -= new MouseEventHandler(Control_MouseMove); Control.MouseUp -= new MouseEventHandler(Control_MouseUp); Control.MouseEnter -= new EventHandler(Control_MouseEnter); Control.MouseLeave -= new EventHandler(Control_MouseLeave); Control.Resize -= new EventHandler(Control_Resize); Control.TextChanged -= new EventHandler(Control_TextChanged); Control.Paint -= new PaintEventHandler(Control_Paint); } #endregion #region Public Methods /// public override void SetParent(Base value) { base.SetParent(value); if (Parent is DialogPage) Control.Parent = (Parent as DialogPage).Form; else if (Parent is ParentControl) Control.Parent = (Parent as ParentControl).Control; else if (Parent == null) Control.Parent = null; #if !FRCORE if (Control.Parent != null) { // in winforms, the controls are added in opposite order Control.BringToFront(); // this will reset the font to form's font. We need this to ensure the automatic form scaling will do the work if (Control.Font.Equals(Control.Parent.Font)) Control.Font = null; } #endif } /// public override void Serialize(FRWriter writer) { if (Page == null) return; DialogControl c = writer.DiffObject as DialogControl; base.Serialize(writer); if (BackColor != c.BackColor) writer.WriteValue("BackColor", BackColor); if (Cursor != c.Cursor) writer.WriteValue("Cursor", Cursor); if (Enabled != c.Enabled) writer.WriteBool("Enabled", Enabled); Form form = (Page as DialogPage).Form; // fixed 27.08.2021 for FR.Core, if uncomment this condition - font doesn't serialise => next open report with font = null if (form != null /*&& !Font.Equals(Control.Parent.Font)*/ && writer.ItemName != "inherited") { #if !FRCORE float fontDpiMultiplier = form.FontDpiMultiplier(); #else float fontDpiMultiplier = 1; #endif using (Font f = new Font(Font.FontFamily, Font.Size / fontDpiMultiplier, Font.Style)) writer.WriteValue("Font", f); } if (ForeColor != c.ForeColor) writer.WriteValue("ForeColor", ForeColor); if (RightToLeft != c.RightToLeft) writer.WriteValue("RightToLeft", RightToLeft); writer.WriteInt("TabIndex", TabIndex); if (TabStop != c.TabStop) writer.WriteBool("TabStop", TabStop); if (Text != c.Text) writer.WriteStr("Text", Text); if (ClickEvent != c.ClickEvent) writer.WriteStr("ClickEvent", ClickEvent); if (DoubleClickEvent != c.DoubleClickEvent) writer.WriteStr("DoubleClickEvent", DoubleClickEvent); if (EnterEvent != c.EnterEvent) writer.WriteStr("EnterEvent", EnterEvent); if (LeaveEvent != c.LeaveEvent) writer.WriteStr("LeaveEvent", LeaveEvent); if (KeyDownEvent != c.KeyDownEvent) writer.WriteStr("KeyDownEvent", KeyDownEvent); if (KeyPressEvent != c.KeyPressEvent) writer.WriteStr("KeyPressEvent", KeyPressEvent); if (KeyUpEvent != c.KeyUpEvent) writer.WriteStr("KeyUpEvent", KeyUpEvent); if (MouseDownEvent != c.MouseDownEvent) writer.WriteStr("MouseDownEvent", MouseDownEvent); if (MouseMoveEvent != c.MouseMoveEvent) writer.WriteStr("MouseMoveEvent", MouseMoveEvent); if (MouseUpEvent != c.MouseUpEvent) writer.WriteStr("MouseUpEvent", MouseUpEvent); if (MouseEnterEvent != c.MouseEnterEvent) writer.WriteStr("MouseEnterEvent", MouseEnterEvent); if (MouseLeaveEvent != c.MouseLeaveEvent) writer.WriteStr("MouseLeaveEvent", MouseLeaveEvent); if (ResizeEvent != c.ResizeEvent) writer.WriteStr("ResizeEvent", ResizeEvent); if (TextChangedEvent != c.TextChangedEvent) writer.WriteStr("TextChangedEvent", TextChangedEvent); if (PaintEvent != c.PaintEvent) writer.WriteStr("PaintEvent", PaintEvent); } /// /// Initializes the control before display it in the dialog form. /// /// /// This method is called when report is run. /// public virtual void InitializeControl() { AttachEvents(); } /// /// Finalizes the control after its parent form is closed. /// /// /// This method is called when report is run. /// public virtual void FinalizeControl() { DetachEvents(); } /// /// Sets input focus to the control. /// public void Focus() { Control.Focus(); } /// /// Conceals the control from the user. /// public void Hide() { Control.Hide(); } /// /// Displays the control to the user. /// public void Show() { Control.Show(); } #endregion #region Invoke Events /// /// This method fires the Click event and the script code connected to the ClickEvent. /// /// Event data. public virtual void OnClick(EventArgs e) { if (Click != null) Click(this, e); InvokeEvent(ClickEvent, e); } /// /// This method fires the DoubleClick event and the script code connected to the DoubleClickEvent. /// /// Event data. public virtual void OnDoubleClick(EventArgs e) { if (DoubleClick != null) DoubleClick(this, e); InvokeEvent(DoubleClickEvent, e); } /// /// This method fires the Enter event and the script code connected to the EnterEvent. /// /// Event data. public virtual void OnEnter(EventArgs e) { if (Enter != null) Enter(this, e); InvokeEvent(EnterEvent, e); } /// /// This method fires the Leave event and the script code connected to the LeaveEvent. /// /// Event data. public virtual void OnLeave(EventArgs e) { if (Leave != null) Leave(this, e); InvokeEvent(LeaveEvent, e); } /// /// This method fires the KeyDown event and the script code connected to the KeyDownEvent. /// /// Event data. public virtual void OnKeyDown(KeyEventArgs e) { if (KeyDown != null) KeyDown(this, e); InvokeEvent(KeyDownEvent, e); } /// /// This method fires the KeyPress event and the script code connected to the KeyPressEvent. /// /// Event data. public virtual void OnKeyPress(KeyPressEventArgs e) { if (KeyPress != null) KeyPress(this, e); InvokeEvent(KeyPressEvent, e); } /// /// This method fires the KeyUp event and the script code connected to the KeyUpEvent. /// /// Event data. public virtual void OnKeyUp(KeyEventArgs e) { if (KeyUp != null) KeyUp(this, e); InvokeEvent(KeyUpEvent, e); } /// /// This method fires the MouseDown event and the script code connected to the MouseDownEvent. /// /// Event data. public virtual void OnMouseDown(MouseEventArgs e) { if (MouseDown != null) MouseDown(this, e); InvokeEvent(MouseDownEvent, e); } /// /// This method fires the MouseMove event and the script code connected to the MouseMoveEvent. /// /// Event data. public virtual void OnMouseMove(MouseEventArgs e) { if (MouseMove != null) MouseMove(this, e); InvokeEvent(MouseMoveEvent, e); } /// /// This method fires the MouseUp event and the script code connected to the MouseUpEvent. /// /// Event data. public virtual void OnMouseUp(MouseEventArgs e) { if (MouseUp != null) MouseUp(this, e); InvokeEvent(MouseUpEvent, e); } /// /// This method fires the MouseEnter event and the script code connected to the MouseEnterEvent. /// /// Event data. public virtual void OnMouseEnter(EventArgs e) { if (MouseEnter != null) MouseEnter(this, e); InvokeEvent(MouseEnterEvent, e); } /// /// This method fires the MouseLeave event and the script code connected to the MouseLeaveEvent. /// /// Event data. public virtual void OnMouseLeave(EventArgs e) { if (MouseLeave != null) MouseLeave(this, e); InvokeEvent(MouseLeaveEvent, e); } /// /// This method fires the Resize event and the script code connected to the ResizeEvent. /// /// Event data. public virtual void OnResize(EventArgs e) { if (Resize != null) Resize(this, e); InvokeEvent(ResizeEvent, e); } /// /// This method fires the TextChanged event and the script code connected to the TextChangedEvent. /// /// Event data. public virtual void OnTextChanged(EventArgs e) { if (TextChanged != null) TextChanged(this, e); InvokeEvent(TextChangedEvent, e); } /// /// This method fires the Paint event and the script code connected to the PaintEvent. /// /// Event data. public virtual void OnPaint(PaintEventArgs e) { if (Paint != null) Paint(this, e); InvokeEvent(PaintEvent, e); } #endregion } }