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