moel@345: using System; moel@345: using System.Collections.Generic; moel@345: using System.Text; moel@345: using System.Windows.Forms; moel@345: using System.Drawing; moel@345: using System.ComponentModel; moel@345: moel@345: namespace Aga.Controls.Tree.NodeControls moel@345: { moel@345: public abstract class EditableControl : InteractiveControl moel@345: { moel@345: private Timer _timer; moel@345: private bool _editFlag; moel@345: moel@345: #region Properties moel@345: moel@345: private bool _editOnClick = false; moel@345: [DefaultValue(false)] moel@345: public bool EditOnClick moel@345: { moel@345: get { return _editOnClick; } moel@345: set { _editOnClick = value; } moel@345: } moel@345: moel@345: #endregion moel@345: moel@345: protected EditableControl() moel@345: { moel@345: _timer = new Timer(); moel@345: _timer.Interval = 500; moel@345: _timer.Tick += new EventHandler(TimerTick); moel@345: } moel@345: moel@345: private void TimerTick(object sender, EventArgs e) moel@345: { moel@345: _timer.Stop(); moel@345: if (_editFlag) moel@345: BeginEdit(); moel@345: _editFlag = false; moel@345: } moel@345: moel@345: public void SetEditorBounds(EditorContext context) moel@345: { moel@345: Size size = CalculateEditorSize(context); moel@345: context.Editor.Bounds = new Rectangle(context.Bounds.X, context.Bounds.Y, moel@345: Math.Min(size.Width, context.Bounds.Width), moel@345: Math.Min(size.Height, Parent.ClientSize.Height - context.Bounds.Y) moel@345: ); moel@345: } moel@345: moel@345: protected abstract Size CalculateEditorSize(EditorContext context); moel@345: moel@345: protected virtual bool CanEdit(TreeNodeAdv node) moel@345: { moel@345: return (node.Tag != null) && IsEditEnabled(node); moel@345: } moel@345: moel@345: public void BeginEdit() moel@345: { moel@345: if (Parent != null && Parent.CurrentNode != null && CanEdit(Parent.CurrentNode)) moel@345: { moel@345: CancelEventArgs args = new CancelEventArgs(); moel@345: OnEditorShowing(args); moel@345: if (!args.Cancel) moel@345: { moel@345: var editor = CreateEditor(Parent.CurrentNode); moel@345: Parent.DisplayEditor(editor, this); moel@345: } moel@345: } moel@345: } moel@345: moel@345: public void EndEdit(bool applyChanges) moel@345: { moel@345: if (Parent != null) moel@345: if (Parent.HideEditor(applyChanges)) moel@345: OnEditorHided(); moel@345: } moel@345: moel@345: public virtual void UpdateEditor(Control control) moel@345: { moel@345: } moel@345: moel@345: internal void ApplyChanges(TreeNodeAdv node, Control editor) moel@345: { moel@345: DoApplyChanges(node, editor); moel@345: OnChangesApplied(); moel@345: } moel@345: moel@345: internal void DoDisposeEditor(Control editor) moel@345: { moel@345: DisposeEditor(editor); moel@345: } moel@345: moel@345: protected abstract void DoApplyChanges(TreeNodeAdv node, Control editor); moel@345: moel@345: protected abstract Control CreateEditor(TreeNodeAdv node); moel@345: moel@345: protected abstract void DisposeEditor(Control editor); moel@345: moel@345: public virtual void Cut(Control control) moel@345: { moel@345: } moel@345: moel@345: public virtual void Copy(Control control) moel@345: { moel@345: } moel@345: moel@345: public virtual void Paste(Control control) moel@345: { moel@345: } moel@345: moel@345: public virtual void Delete(Control control) moel@345: { moel@345: } moel@345: moel@345: public override void MouseDown(TreeNodeAdvMouseEventArgs args) moel@345: { moel@345: _editFlag = (!EditOnClick && args.Button == MouseButtons.Left moel@345: && args.ModifierKeys == Keys.None && args.Node.IsSelected); moel@345: } moel@345: moel@345: public override void MouseUp(TreeNodeAdvMouseEventArgs args) moel@345: { moel@345: if (args.Node.IsSelected) moel@345: { moel@345: if (EditOnClick && args.Button == MouseButtons.Left && args.ModifierKeys == Keys.None) moel@345: { moel@345: Parent.ItemDragMode = false; moel@345: BeginEdit(); moel@345: args.Handled = true; moel@345: } moel@345: else if (_editFlag)// && args.Node.IsSelected) moel@345: _timer.Start(); moel@345: } moel@345: } moel@345: moel@345: public override void MouseDoubleClick(TreeNodeAdvMouseEventArgs args) moel@345: { moel@345: _editFlag = false; moel@345: _timer.Stop(); moel@345: } moel@345: moel@345: protected override void Dispose(bool disposing) moel@345: { moel@345: base.Dispose(disposing); moel@345: if (disposing) moel@345: _timer.Dispose(); moel@345: } moel@345: moel@345: #region Events moel@345: moel@345: public event CancelEventHandler EditorShowing; moel@345: protected void OnEditorShowing(CancelEventArgs args) moel@345: { moel@345: if (EditorShowing != null) moel@345: EditorShowing(this, args); moel@345: } moel@345: moel@345: public event EventHandler EditorHided; moel@345: protected void OnEditorHided() moel@345: { moel@345: if (EditorHided != null) moel@345: EditorHided(this, EventArgs.Empty); moel@345: } moel@345: moel@345: public event EventHandler ChangesApplied; moel@345: protected void OnChangesApplied() moel@345: { moel@345: if (ChangesApplied != null) moel@345: ChangesApplied(this, EventArgs.Empty); moel@345: } moel@345: moel@345: #endregion moel@345: } moel@345: }