External/Aga.Controls/Tree/TreeViewAdv.Editor.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 Aga.Controls.Tree.NodeControls;
moel@345
     6
using System.Drawing;
moel@345
     7
moel@345
     8
namespace Aga.Controls.Tree
moel@345
     9
{
moel@345
    10
	partial class TreeViewAdv
moel@345
    11
	{
moel@345
    12
		private TreeNodeAdv _editingNode;
moel@345
    13
moel@345
    14
		public EditableControl CurrentEditorOwner { get; private set; }
moel@345
    15
		public Control CurrentEditor { get; private set; }
moel@345
    16
moel@345
    17
		public void HideEditor()
moel@345
    18
		{
moel@345
    19
			if (CurrentEditorOwner != null)
moel@345
    20
				CurrentEditorOwner.EndEdit(false);
moel@345
    21
		}
moel@345
    22
moel@345
    23
		internal void DisplayEditor(Control editor, EditableControl owner)
moel@345
    24
		{
moel@345
    25
			if (editor == null || owner == null || CurrentNode == null)
moel@345
    26
				throw new ArgumentNullException();
moel@345
    27
moel@345
    28
			HideEditor(false);
moel@345
    29
moel@345
    30
			CurrentEditor = editor;
moel@345
    31
			CurrentEditorOwner = owner;
moel@345
    32
			_editingNode = CurrentNode;
moel@345
    33
moel@345
    34
			editor.Validating += EditorValidating;
moel@345
    35
			UpdateEditorBounds();
moel@345
    36
			UpdateView();
moel@345
    37
			editor.Parent = this;
moel@345
    38
			editor.Focus();
moel@345
    39
			owner.UpdateEditor(editor);
moel@345
    40
		}
moel@345
    41
moel@345
    42
		internal bool HideEditor(bool applyChanges)
moel@345
    43
		{
moel@345
    44
			if (CurrentEditor != null)
moel@345
    45
			{
moel@345
    46
				if (applyChanges)
moel@345
    47
				{
moel@345
    48
					if (!ApplyChanges())
moel@345
    49
						return false;
moel@345
    50
				}
moel@345
    51
moel@345
    52
				//Check once more if editor was closed in ApplyChanges
moel@345
    53
				if (CurrentEditor != null)
moel@345
    54
				{
moel@345
    55
					CurrentEditor.Validating -= EditorValidating;
moel@345
    56
					CurrentEditorOwner.DoDisposeEditor(CurrentEditor);
moel@345
    57
moel@345
    58
					CurrentEditor.Parent = null;
moel@345
    59
					CurrentEditor.Dispose();
moel@345
    60
moel@345
    61
					CurrentEditor = null;
moel@345
    62
					CurrentEditorOwner = null;
moel@345
    63
					_editingNode = null;
moel@345
    64
				}
moel@345
    65
			}
moel@345
    66
			return true;
moel@345
    67
		}
moel@345
    68
moel@345
    69
		private bool ApplyChanges()
moel@345
    70
		{
moel@345
    71
			try
moel@345
    72
			{
moel@345
    73
				CurrentEditorOwner.ApplyChanges(_editingNode, CurrentEditor);
moel@345
    74
				_errorProvider.Clear();
moel@345
    75
				return true;
moel@345
    76
			}
moel@345
    77
			catch (ArgumentException ex)
moel@345
    78
			{
moel@345
    79
				_errorProvider.SetError(CurrentEditor, ex.Message);
moel@345
    80
				/*CurrentEditor.Validating -= EditorValidating;
moel@345
    81
				MessageBox.Show(this, ex.Message, "Value is not valid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
moel@345
    82
				CurrentEditor.Focus();
moel@345
    83
				CurrentEditor.Validating += EditorValidating;*/
moel@345
    84
				return false;
moel@345
    85
			}
moel@345
    86
		}
moel@345
    87
moel@345
    88
		void EditorValidating(object sender, System.ComponentModel.CancelEventArgs e)
moel@345
    89
		{
moel@345
    90
			e.Cancel = !ApplyChanges();
moel@345
    91
		}
moel@345
    92
moel@345
    93
		public void UpdateEditorBounds()
moel@345
    94
		{
moel@345
    95
			if (CurrentEditor != null)
moel@345
    96
			{
moel@345
    97
				EditorContext context = new EditorContext();
moel@345
    98
				context.Owner = CurrentEditorOwner;
moel@345
    99
				context.CurrentNode = CurrentNode;
moel@345
   100
				context.Editor = CurrentEditor;
moel@345
   101
				context.DrawContext = _measureContext;
moel@345
   102
				SetEditorBounds(context);
moel@345
   103
			}
moel@345
   104
		}
moel@345
   105
moel@345
   106
		private void SetEditorBounds(EditorContext context)
moel@345
   107
		{
moel@345
   108
			foreach (NodeControlInfo info in GetNodeControls(context.CurrentNode))
moel@345
   109
			{
moel@345
   110
				if (context.Owner == info.Control && info.Control is EditableControl)
moel@345
   111
				{
moel@345
   112
					Point p = info.Bounds.Location;
moel@345
   113
					p.X += info.Control.LeftMargin;
moel@345
   114
					p.X -= OffsetX;
moel@345
   115
					p.Y -= (_rowLayout.GetRowBounds(FirstVisibleRow).Y - ColumnHeaderHeight);
moel@345
   116
					int width = DisplayRectangle.Width - p.X;
moel@345
   117
					if (UseColumns && info.Control.ParentColumn != null && Columns.Contains(info.Control.ParentColumn))
moel@345
   118
					{
moel@345
   119
						Rectangle rect = GetColumnBounds(info.Control.ParentColumn.Index);
moel@345
   120
						width = rect.Right - OffsetX - p.X;
moel@345
   121
					}
moel@345
   122
					context.Bounds = new Rectangle(p.X, p.Y, width, info.Bounds.Height);
moel@345
   123
					((EditableControl)info.Control).SetEditorBounds(context);
moel@345
   124
					return;
moel@345
   125
				}
moel@345
   126
			}
moel@345
   127
		}
moel@345
   128
moel@345
   129
		private Rectangle GetColumnBounds(int column)
moel@345
   130
		{
moel@345
   131
			int x = 0;
moel@345
   132
			for (int i = 0; i < Columns.Count; i++)
moel@345
   133
			{
moel@345
   134
				if (Columns[i].IsVisible)
moel@345
   135
				{
moel@345
   136
					if (i < column)
moel@345
   137
						x += Columns[i].Width;
moel@345
   138
					else
moel@345
   139
						return new Rectangle(x, 0, Columns[i].Width, 0);
moel@345
   140
				}
moel@345
   141
			}
moel@345
   142
			return Rectangle.Empty;
moel@345
   143
		}
moel@345
   144
	}
moel@345
   145
}