External/Aga.Controls/Tree/NodeControls/EditableControl.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/NodeControls/EditableControl.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,175 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Windows.Forms;
     1.8 +using System.Drawing;
     1.9 +using System.ComponentModel;
    1.10 +
    1.11 +namespace Aga.Controls.Tree.NodeControls
    1.12 +{
    1.13 +	public abstract class EditableControl : InteractiveControl
    1.14 +	{
    1.15 +		private Timer _timer;
    1.16 +		private bool _editFlag;
    1.17 +
    1.18 +		#region Properties
    1.19 +
    1.20 +		private bool _editOnClick = false;
    1.21 +		[DefaultValue(false)]
    1.22 +		public bool EditOnClick
    1.23 +		{
    1.24 +			get { return _editOnClick; }
    1.25 +			set { _editOnClick = value; }
    1.26 +		}
    1.27 +
    1.28 +		#endregion
    1.29 +
    1.30 +		protected EditableControl()
    1.31 +		{
    1.32 +			_timer = new Timer();
    1.33 +			_timer.Interval = 500;
    1.34 +			_timer.Tick += new EventHandler(TimerTick);
    1.35 +		}
    1.36 +
    1.37 +		private void TimerTick(object sender, EventArgs e)
    1.38 +		{
    1.39 +			_timer.Stop();
    1.40 +			if (_editFlag)
    1.41 +				BeginEdit();
    1.42 +			_editFlag = false;
    1.43 +		}
    1.44 +
    1.45 +		public void SetEditorBounds(EditorContext context)
    1.46 +		{
    1.47 +			Size size = CalculateEditorSize(context);
    1.48 +			context.Editor.Bounds = new Rectangle(context.Bounds.X, context.Bounds.Y,
    1.49 +				Math.Min(size.Width, context.Bounds.Width),
    1.50 +				Math.Min(size.Height, Parent.ClientSize.Height - context.Bounds.Y)
    1.51 +			);
    1.52 +		}
    1.53 +
    1.54 +		protected abstract Size CalculateEditorSize(EditorContext context);
    1.55 +
    1.56 +		protected virtual bool CanEdit(TreeNodeAdv node)
    1.57 +		{
    1.58 +			return (node.Tag != null) && IsEditEnabled(node);
    1.59 +		}
    1.60 +
    1.61 +		public void BeginEdit()
    1.62 +		{
    1.63 +			if (Parent != null && Parent.CurrentNode != null && CanEdit(Parent.CurrentNode))
    1.64 +			{
    1.65 +				CancelEventArgs args = new CancelEventArgs();
    1.66 +				OnEditorShowing(args);
    1.67 +				if (!args.Cancel)
    1.68 +				{
    1.69 +					var editor = CreateEditor(Parent.CurrentNode);
    1.70 +					Parent.DisplayEditor(editor, this);
    1.71 +				}
    1.72 +			}
    1.73 +		}
    1.74 +
    1.75 +		public void EndEdit(bool applyChanges)
    1.76 +		{
    1.77 +			if (Parent != null)
    1.78 +				if (Parent.HideEditor(applyChanges))
    1.79 +					OnEditorHided();
    1.80 +		}
    1.81 +
    1.82 +		public virtual void UpdateEditor(Control control)
    1.83 +		{
    1.84 +		}
    1.85 +
    1.86 +		internal void ApplyChanges(TreeNodeAdv node, Control editor)
    1.87 +		{
    1.88 +			DoApplyChanges(node, editor);
    1.89 +			OnChangesApplied();
    1.90 +		}
    1.91 +
    1.92 +		internal void DoDisposeEditor(Control editor)
    1.93 +		{
    1.94 +			DisposeEditor(editor);
    1.95 +		}
    1.96 +
    1.97 +		protected abstract void DoApplyChanges(TreeNodeAdv node, Control editor);
    1.98 +
    1.99 +		protected abstract Control CreateEditor(TreeNodeAdv node);
   1.100 +
   1.101 +		protected abstract void DisposeEditor(Control editor);
   1.102 +
   1.103 +		public virtual void Cut(Control control)
   1.104 +		{
   1.105 +		}
   1.106 +
   1.107 +		public virtual void Copy(Control control)
   1.108 +		{
   1.109 +		}
   1.110 +
   1.111 +		public virtual void Paste(Control control)
   1.112 +		{
   1.113 +		}
   1.114 +
   1.115 +		public virtual void Delete(Control control)
   1.116 +		{
   1.117 +		}
   1.118 +
   1.119 +		public override void MouseDown(TreeNodeAdvMouseEventArgs args)
   1.120 +		{
   1.121 +			_editFlag = (!EditOnClick && args.Button == MouseButtons.Left
   1.122 +				&& args.ModifierKeys == Keys.None && args.Node.IsSelected);
   1.123 +		}
   1.124 +
   1.125 +		public override void MouseUp(TreeNodeAdvMouseEventArgs args)
   1.126 +		{
   1.127 +			if (args.Node.IsSelected)
   1.128 +			{
   1.129 +				if (EditOnClick && args.Button == MouseButtons.Left && args.ModifierKeys == Keys.None)
   1.130 +				{
   1.131 +					Parent.ItemDragMode = false;
   1.132 +					BeginEdit();
   1.133 +					args.Handled = true;
   1.134 +				}
   1.135 +				else if (_editFlag)// && args.Node.IsSelected)
   1.136 +					_timer.Start();
   1.137 +			}
   1.138 +		}
   1.139 +
   1.140 +		public override void MouseDoubleClick(TreeNodeAdvMouseEventArgs args)
   1.141 +		{
   1.142 +			_editFlag = false;
   1.143 +			_timer.Stop();
   1.144 +		}
   1.145 +
   1.146 +		protected override void Dispose(bool disposing)
   1.147 +		{
   1.148 +			base.Dispose(disposing);
   1.149 +			if (disposing)
   1.150 +				_timer.Dispose();
   1.151 +		}
   1.152 +
   1.153 +		#region Events
   1.154 +
   1.155 +		public event CancelEventHandler EditorShowing;
   1.156 +		protected void OnEditorShowing(CancelEventArgs args)
   1.157 +		{
   1.158 +			if (EditorShowing != null)
   1.159 +				EditorShowing(this, args);
   1.160 +		}
   1.161 +
   1.162 +		public event EventHandler EditorHided;
   1.163 +		protected void OnEditorHided()
   1.164 +		{
   1.165 +			if (EditorHided != null)
   1.166 +				EditorHided(this, EventArgs.Empty);
   1.167 +		}
   1.168 +
   1.169 +		public event EventHandler ChangesApplied;
   1.170 +		protected void OnChangesApplied()
   1.171 +		{
   1.172 +			if (ChangesApplied != null)
   1.173 +				ChangesApplied(this, EventArgs.Empty);
   1.174 +		}
   1.175 +
   1.176 +		#endregion
   1.177 +	}
   1.178 +}