External/Aga.Controls/Tree/TreeViewAdv.Editor.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/TreeViewAdv.Editor.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,145 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Windows.Forms;
     1.8 +using Aga.Controls.Tree.NodeControls;
     1.9 +using System.Drawing;
    1.10 +
    1.11 +namespace Aga.Controls.Tree
    1.12 +{
    1.13 +	partial class TreeViewAdv
    1.14 +	{
    1.15 +		private TreeNodeAdv _editingNode;
    1.16 +
    1.17 +		public EditableControl CurrentEditorOwner { get; private set; }
    1.18 +		public Control CurrentEditor { get; private set; }
    1.19 +
    1.20 +		public void HideEditor()
    1.21 +		{
    1.22 +			if (CurrentEditorOwner != null)
    1.23 +				CurrentEditorOwner.EndEdit(false);
    1.24 +		}
    1.25 +
    1.26 +		internal void DisplayEditor(Control editor, EditableControl owner)
    1.27 +		{
    1.28 +			if (editor == null || owner == null || CurrentNode == null)
    1.29 +				throw new ArgumentNullException();
    1.30 +
    1.31 +			HideEditor(false);
    1.32 +
    1.33 +			CurrentEditor = editor;
    1.34 +			CurrentEditorOwner = owner;
    1.35 +			_editingNode = CurrentNode;
    1.36 +
    1.37 +			editor.Validating += EditorValidating;
    1.38 +			UpdateEditorBounds();
    1.39 +			UpdateView();
    1.40 +			editor.Parent = this;
    1.41 +			editor.Focus();
    1.42 +			owner.UpdateEditor(editor);
    1.43 +		}
    1.44 +
    1.45 +		internal bool HideEditor(bool applyChanges)
    1.46 +		{
    1.47 +			if (CurrentEditor != null)
    1.48 +			{
    1.49 +				if (applyChanges)
    1.50 +				{
    1.51 +					if (!ApplyChanges())
    1.52 +						return false;
    1.53 +				}
    1.54 +
    1.55 +				//Check once more if editor was closed in ApplyChanges
    1.56 +				if (CurrentEditor != null)
    1.57 +				{
    1.58 +					CurrentEditor.Validating -= EditorValidating;
    1.59 +					CurrentEditorOwner.DoDisposeEditor(CurrentEditor);
    1.60 +
    1.61 +					CurrentEditor.Parent = null;
    1.62 +					CurrentEditor.Dispose();
    1.63 +
    1.64 +					CurrentEditor = null;
    1.65 +					CurrentEditorOwner = null;
    1.66 +					_editingNode = null;
    1.67 +				}
    1.68 +			}
    1.69 +			return true;
    1.70 +		}
    1.71 +
    1.72 +		private bool ApplyChanges()
    1.73 +		{
    1.74 +			try
    1.75 +			{
    1.76 +				CurrentEditorOwner.ApplyChanges(_editingNode, CurrentEditor);
    1.77 +				_errorProvider.Clear();
    1.78 +				return true;
    1.79 +			}
    1.80 +			catch (ArgumentException ex)
    1.81 +			{
    1.82 +				_errorProvider.SetError(CurrentEditor, ex.Message);
    1.83 +				/*CurrentEditor.Validating -= EditorValidating;
    1.84 +				MessageBox.Show(this, ex.Message, "Value is not valid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    1.85 +				CurrentEditor.Focus();
    1.86 +				CurrentEditor.Validating += EditorValidating;*/
    1.87 +				return false;
    1.88 +			}
    1.89 +		}
    1.90 +
    1.91 +		void EditorValidating(object sender, System.ComponentModel.CancelEventArgs e)
    1.92 +		{
    1.93 +			e.Cancel = !ApplyChanges();
    1.94 +		}
    1.95 +
    1.96 +		public void UpdateEditorBounds()
    1.97 +		{
    1.98 +			if (CurrentEditor != null)
    1.99 +			{
   1.100 +				EditorContext context = new EditorContext();
   1.101 +				context.Owner = CurrentEditorOwner;
   1.102 +				context.CurrentNode = CurrentNode;
   1.103 +				context.Editor = CurrentEditor;
   1.104 +				context.DrawContext = _measureContext;
   1.105 +				SetEditorBounds(context);
   1.106 +			}
   1.107 +		}
   1.108 +
   1.109 +		private void SetEditorBounds(EditorContext context)
   1.110 +		{
   1.111 +			foreach (NodeControlInfo info in GetNodeControls(context.CurrentNode))
   1.112 +			{
   1.113 +				if (context.Owner == info.Control && info.Control is EditableControl)
   1.114 +				{
   1.115 +					Point p = info.Bounds.Location;
   1.116 +					p.X += info.Control.LeftMargin;
   1.117 +					p.X -= OffsetX;
   1.118 +					p.Y -= (_rowLayout.GetRowBounds(FirstVisibleRow).Y - ColumnHeaderHeight);
   1.119 +					int width = DisplayRectangle.Width - p.X;
   1.120 +					if (UseColumns && info.Control.ParentColumn != null && Columns.Contains(info.Control.ParentColumn))
   1.121 +					{
   1.122 +						Rectangle rect = GetColumnBounds(info.Control.ParentColumn.Index);
   1.123 +						width = rect.Right - OffsetX - p.X;
   1.124 +					}
   1.125 +					context.Bounds = new Rectangle(p.X, p.Y, width, info.Bounds.Height);
   1.126 +					((EditableControl)info.Control).SetEditorBounds(context);
   1.127 +					return;
   1.128 +				}
   1.129 +			}
   1.130 +		}
   1.131 +
   1.132 +		private Rectangle GetColumnBounds(int column)
   1.133 +		{
   1.134 +			int x = 0;
   1.135 +			for (int i = 0; i < Columns.Count; i++)
   1.136 +			{
   1.137 +				if (Columns[i].IsVisible)
   1.138 +				{
   1.139 +					if (i < column)
   1.140 +						x += Columns[i].Width;
   1.141 +					else
   1.142 +						return new Rectangle(x, 0, Columns[i].Width, 0);
   1.143 +				}
   1.144 +			}
   1.145 +			return Rectangle.Empty;
   1.146 +		}
   1.147 +	}
   1.148 +}