moel@345: using System; moel@345: using System.Collections.Generic; moel@345: using System.Text; moel@345: using System.Windows.Forms; moel@345: using Aga.Controls.Tree.NodeControls; moel@345: using System.Drawing; moel@345: moel@345: namespace Aga.Controls.Tree moel@345: { moel@345: partial class TreeViewAdv moel@345: { moel@345: private TreeNodeAdv _editingNode; moel@345: moel@345: public EditableControl CurrentEditorOwner { get; private set; } moel@345: public Control CurrentEditor { get; private set; } moel@345: moel@345: public void HideEditor() moel@345: { moel@345: if (CurrentEditorOwner != null) moel@345: CurrentEditorOwner.EndEdit(false); moel@345: } moel@345: moel@345: internal void DisplayEditor(Control editor, EditableControl owner) moel@345: { moel@345: if (editor == null || owner == null || CurrentNode == null) moel@345: throw new ArgumentNullException(); moel@345: moel@345: HideEditor(false); moel@345: moel@345: CurrentEditor = editor; moel@345: CurrentEditorOwner = owner; moel@345: _editingNode = CurrentNode; moel@345: moel@345: editor.Validating += EditorValidating; moel@345: UpdateEditorBounds(); moel@345: UpdateView(); moel@345: editor.Parent = this; moel@345: editor.Focus(); moel@345: owner.UpdateEditor(editor); moel@345: } moel@345: moel@345: internal bool HideEditor(bool applyChanges) moel@345: { moel@345: if (CurrentEditor != null) moel@345: { moel@345: if (applyChanges) moel@345: { moel@345: if (!ApplyChanges()) moel@345: return false; moel@345: } moel@345: moel@345: //Check once more if editor was closed in ApplyChanges moel@345: if (CurrentEditor != null) moel@345: { moel@345: CurrentEditor.Validating -= EditorValidating; moel@345: CurrentEditorOwner.DoDisposeEditor(CurrentEditor); moel@345: moel@345: CurrentEditor.Parent = null; moel@345: CurrentEditor.Dispose(); moel@345: moel@345: CurrentEditor = null; moel@345: CurrentEditorOwner = null; moel@345: _editingNode = null; moel@345: } moel@345: } moel@345: return true; moel@345: } moel@345: moel@345: private bool ApplyChanges() moel@345: { moel@345: try moel@345: { moel@345: CurrentEditorOwner.ApplyChanges(_editingNode, CurrentEditor); moel@345: _errorProvider.Clear(); moel@345: return true; moel@345: } moel@345: catch (ArgumentException ex) moel@345: { moel@345: _errorProvider.SetError(CurrentEditor, ex.Message); moel@345: /*CurrentEditor.Validating -= EditorValidating; moel@345: MessageBox.Show(this, ex.Message, "Value is not valid", MessageBoxButtons.OK, MessageBoxIcon.Warning); moel@345: CurrentEditor.Focus(); moel@345: CurrentEditor.Validating += EditorValidating;*/ moel@345: return false; moel@345: } moel@345: } moel@345: moel@345: void EditorValidating(object sender, System.ComponentModel.CancelEventArgs e) moel@345: { moel@345: e.Cancel = !ApplyChanges(); moel@345: } moel@345: moel@345: public void UpdateEditorBounds() moel@345: { moel@345: if (CurrentEditor != null) moel@345: { moel@345: EditorContext context = new EditorContext(); moel@345: context.Owner = CurrentEditorOwner; moel@345: context.CurrentNode = CurrentNode; moel@345: context.Editor = CurrentEditor; moel@345: context.DrawContext = _measureContext; moel@345: SetEditorBounds(context); moel@345: } moel@345: } moel@345: moel@345: private void SetEditorBounds(EditorContext context) moel@345: { moel@345: foreach (NodeControlInfo info in GetNodeControls(context.CurrentNode)) moel@345: { moel@345: if (context.Owner == info.Control && info.Control is EditableControl) moel@345: { moel@345: Point p = info.Bounds.Location; moel@345: p.X += info.Control.LeftMargin; moel@345: p.X -= OffsetX; moel@345: p.Y -= (_rowLayout.GetRowBounds(FirstVisibleRow).Y - ColumnHeaderHeight); moel@345: int width = DisplayRectangle.Width - p.X; moel@345: if (UseColumns && info.Control.ParentColumn != null && Columns.Contains(info.Control.ParentColumn)) moel@345: { moel@345: Rectangle rect = GetColumnBounds(info.Control.ParentColumn.Index); moel@345: width = rect.Right - OffsetX - p.X; moel@345: } moel@345: context.Bounds = new Rectangle(p.X, p.Y, width, info.Bounds.Height); moel@345: ((EditableControl)info.Control).SetEditorBounds(context); moel@345: return; moel@345: } moel@345: } moel@345: } moel@345: moel@345: private Rectangle GetColumnBounds(int column) moel@345: { moel@345: int x = 0; moel@345: for (int i = 0; i < Columns.Count; i++) moel@345: { moel@345: if (Columns[i].IsVisible) moel@345: { moel@345: if (i < column) moel@345: x += Columns[i].Width; moel@345: else moel@345: return new Rectangle(x, 0, Columns[i].Width, 0); moel@345: } moel@345: } moel@345: return Rectangle.Empty; moel@345: } moel@345: } moel@345: }