External/Aga.Controls/Tree/NodeControls/EditableControl.cs
author moel.mich
Sun, 27 May 2012 16:08:54 +0000
changeset 346 f652ab1e06e2
permissions -rw-r--r--
Added the small modifications to the Aga.Controls required by the Open Hardware Monitor.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Windows.Forms;
     5 using System.Drawing;
     6 using System.ComponentModel;
     7 
     8 namespace Aga.Controls.Tree.NodeControls
     9 {
    10 	public abstract class EditableControl : InteractiveControl
    11 	{
    12 		private Timer _timer;
    13 		private bool _editFlag;
    14 
    15 		#region Properties
    16 
    17 		private bool _editOnClick = false;
    18 		[DefaultValue(false)]
    19 		public bool EditOnClick
    20 		{
    21 			get { return _editOnClick; }
    22 			set { _editOnClick = value; }
    23 		}
    24 
    25 		#endregion
    26 
    27 		protected EditableControl()
    28 		{
    29 			_timer = new Timer();
    30 			_timer.Interval = 500;
    31 			_timer.Tick += new EventHandler(TimerTick);
    32 		}
    33 
    34 		private void TimerTick(object sender, EventArgs e)
    35 		{
    36 			_timer.Stop();
    37 			if (_editFlag)
    38 				BeginEdit();
    39 			_editFlag = false;
    40 		}
    41 
    42 		public void SetEditorBounds(EditorContext context)
    43 		{
    44 			Size size = CalculateEditorSize(context);
    45 			context.Editor.Bounds = new Rectangle(context.Bounds.X, context.Bounds.Y,
    46 				Math.Min(size.Width, context.Bounds.Width),
    47 				Math.Min(size.Height, Parent.ClientSize.Height - context.Bounds.Y)
    48 			);
    49 		}
    50 
    51 		protected abstract Size CalculateEditorSize(EditorContext context);
    52 
    53 		protected virtual bool CanEdit(TreeNodeAdv node)
    54 		{
    55 			return (node.Tag != null) && IsEditEnabled(node);
    56 		}
    57 
    58 		public void BeginEdit()
    59 		{
    60 			if (Parent != null && Parent.CurrentNode != null && CanEdit(Parent.CurrentNode))
    61 			{
    62 				CancelEventArgs args = new CancelEventArgs();
    63 				OnEditorShowing(args);
    64 				if (!args.Cancel)
    65 				{
    66 					var editor = CreateEditor(Parent.CurrentNode);
    67 					Parent.DisplayEditor(editor, this);
    68 				}
    69 			}
    70 		}
    71 
    72 		public void EndEdit(bool applyChanges)
    73 		{
    74 			if (Parent != null)
    75 				if (Parent.HideEditor(applyChanges))
    76 					OnEditorHided();
    77 		}
    78 
    79 		public virtual void UpdateEditor(Control control)
    80 		{
    81 		}
    82 
    83 		internal void ApplyChanges(TreeNodeAdv node, Control editor)
    84 		{
    85 			DoApplyChanges(node, editor);
    86 			OnChangesApplied();
    87 		}
    88 
    89 		internal void DoDisposeEditor(Control editor)
    90 		{
    91 			DisposeEditor(editor);
    92 		}
    93 
    94 		protected abstract void DoApplyChanges(TreeNodeAdv node, Control editor);
    95 
    96 		protected abstract Control CreateEditor(TreeNodeAdv node);
    97 
    98 		protected abstract void DisposeEditor(Control editor);
    99 
   100 		public virtual void Cut(Control control)
   101 		{
   102 		}
   103 
   104 		public virtual void Copy(Control control)
   105 		{
   106 		}
   107 
   108 		public virtual void Paste(Control control)
   109 		{
   110 		}
   111 
   112 		public virtual void Delete(Control control)
   113 		{
   114 		}
   115 
   116 		public override void MouseDown(TreeNodeAdvMouseEventArgs args)
   117 		{
   118 			_editFlag = (!EditOnClick && args.Button == MouseButtons.Left
   119 				&& args.ModifierKeys == Keys.None && args.Node.IsSelected);
   120 		}
   121 
   122 		public override void MouseUp(TreeNodeAdvMouseEventArgs args)
   123 		{
   124 			if (args.Node.IsSelected)
   125 			{
   126 				if (EditOnClick && args.Button == MouseButtons.Left && args.ModifierKeys == Keys.None)
   127 				{
   128 					Parent.ItemDragMode = false;
   129 					BeginEdit();
   130 					args.Handled = true;
   131 				}
   132 				else if (_editFlag)// && args.Node.IsSelected)
   133 					_timer.Start();
   134 			}
   135 		}
   136 
   137 		public override void MouseDoubleClick(TreeNodeAdvMouseEventArgs args)
   138 		{
   139 			_editFlag = false;
   140 			_timer.Stop();
   141 		}
   142 
   143 		protected override void Dispose(bool disposing)
   144 		{
   145 			base.Dispose(disposing);
   146 			if (disposing)
   147 				_timer.Dispose();
   148 		}
   149 
   150 		#region Events
   151 
   152 		public event CancelEventHandler EditorShowing;
   153 		protected void OnEditorShowing(CancelEventArgs args)
   154 		{
   155 			if (EditorShowing != null)
   156 				EditorShowing(this, args);
   157 		}
   158 
   159 		public event EventHandler EditorHided;
   160 		protected void OnEditorHided()
   161 		{
   162 			if (EditorHided != null)
   163 				EditorHided(this, EventArgs.Empty);
   164 		}
   165 
   166 		public event EventHandler ChangesApplied;
   167 		protected void OnChangesApplied()
   168 		{
   169 			if (ChangesApplied != null)
   170 				ChangesApplied(this, EventArgs.Empty);
   171 		}
   172 
   173 		#endregion
   174 	}
   175 }