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