context: /* This file is part of the db4o object database http://www.db4o.com
  
  Copyright (C) 2004 - 2011  Versant Corporation http://www.versant.com
  
  db4o is free software; you can redistribute it and/or modify it under
  the terms of version 3 of the GNU General Public License as published
  by the Free Software Foundation.
  
  db4o is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  for more details.
  
  You should have received a copy of the GNU General Public License along
  with this program.  If not, see http://www.gnu.org/licenses/. */
  using System;
  using System.Windows.Forms;
  using System.Drawing;
  using System.Diagnostics;
  using System.Windows.Forms.VisualStyles;
  using System.ComponentModel;
  using System.ComponentModel.Design;
  using System.ComponentModel.Design.Serialization;
  using System.Drawing.Design;
  
  using OME.Logging.Common;
  using OME.Logging.Tracing;
  
  namespace OME.AdvancedDataGridView
  {
  	/// <summary>
  	/// Summary description for TreeGridView.
  	/// </summary>
      [System.ComponentModel.DesignerCategory("code"),
      Designer(typeof(System.Windows.Forms.Design.ControlDesigner)),
  	ComplexBindingProperties(),
      Docking(DockingBehavior.Ask)]
  	public class TreeGridView:DataGridView
  	{
          #region Member Variables
          private TreeGridNode _root;
          private TreeGridColumn _expandableColumn;
          internal ImageList _imageList;
          private bool _inExpandCollapse = false;
          internal bool _inExpandCollapseMouseCapture = false;
          private Control hideScrollBarControl;
          private bool _showLines = true;
          private bool _virtualNodes = false;
  
          public ContextMenuStrip m_deleteContextMenuStrip;
          private const string CONTEXTMENU_CAPTION_SETTONULL = "Set to Null";
          private const string CONTEXTMENU_NAME_DELETE = "MenuDelete";
  
          public event EventHandler<ContextItemClickedEventArg> OnContextMenuItemClicked;
          public event EventHandler<ContextItemClickedEventArg> OnContextMenuOpening;
          TreeGridView.HitTestInfo hitTestInfo;
  
          #endregion
  
          #region Constructor
          public TreeGridView()
  		{
              this.SetStyle(ControlStyles.CacheText |
                  ControlStyles.AllPaintingInWmPaint |
                  ControlStyles.UserPaint |
                  ControlStyles.OptimizedDoubleBuffer |
                  ControlStyles.Opaque, true);
  
  			// Control when edit occurs because edit mode shouldn't start when expanding/collapsing
  			this.EditMode = DataGridViewEditMode.EditProgrammatically;
              this.RowTemplate = new TreeGridNode() as DataGridViewRow;
  			// This sample does not support adding or deleting rows by the user.
  			this.AllowUserToAddRows = false;
  			this.AllowUserToDeleteRows = false;
  			this._root = new TreeGridNode(this);
  			this._root.IsRoot = true;
              
             
              SetDefaultProperties();
  			// Ensures that all rows are added unshared by listening to the CollectionChanged event.
  			base.Rows.CollectionChanged += delegate(object sender, System.ComponentModel.CollectionChangeEventArgs e){};
          }
         
          private void SetDefaultProperties()
          {
              this.BackgroundColor = Color.White;
              this.RowHeadersVisible = false;
              this.AutoGenerateColumns = false;
              this.AllowUserToResizeRows = false;
              this.AllowUserToAddRows = false;
              this.AllowUserToOrderColumns = false;
              this.AllowDrop = true;
              this.MultiSelect = false;
              this.GridColor = SystemColors.ControlDark;
              this.ScrollBars = ScrollBars.Both;
              this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
              this.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
              this.AllowUserToDeleteRows = false;
              this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
              this.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
              this.Visible = true;
              this.AutoGenerateColumns = false;
              this.EnableHeadersVisualStyles = false;
              this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
              DataGridViewCellStyle cellstyle = new DataGridViewCellStyle();
              cellstyle.BackColor = Color.FromArgb(218, 232, 241);
          }
          
          #endregion
  
          #region Keyboard F2 to begin edit support
          protected override void OnKeyDown(KeyEventArgs e)
  		{
  			// Cause edit mode to begin since edit mode is disabled to support 
  			// expanding/collapsing 
  			base.OnKeyDown(e);
  			if (!e.Handled)
  			{
  				if (e.KeyCode == Keys.F2 && this.CurrentCellAddress.X > -1 && this.CurrentCellAddress.Y >-1)
  				{
  					if (!this.CurrentCell.Displayed)
  					{
  						this.FirstDisplayedScrollingRowIndex = this.CurrentCellAddress.Y;
  					}
  					else
  					{
  						// TODO:calculate if the cell is partially offscreen and if so scroll into view
  					}
  					//this.SelectionMode = DataGridViewSelectionMode.CellSelect;
  					this.BeginEdit(true);
  				}
  				else if (e.KeyCode == Keys.Enter && !this.IsCurrentCellInEditMode)
  				{
  					this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  					this.CurrentCell.OwningRow.Selected = true;
  				}
  			}
          }
          #endregion
  
          #region Shadow and hide DGV properties
  
          // This sample does not support databinding
  		[Browsable(false),
  		DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  		EditorBrowsable(EditorBrowsableState.Never)]
  		public new object DataSource
  		{
  			get { return null; }
  			set { throw new NotSupportedException("The TreeGridView does not support databinding"); }
  		}
  
  		[Browsable(false),
  	    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  		EditorBrowsable(EditorBrowsableState.Never)]
  		public new object DataMember
  		{
  			get { return null; }
  			set { throw new NotSupportedException("The TreeGridView does not support databinding"); }
  		}
  
          [Browsable(false),
          DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  		EditorBrowsable(EditorBrowsableState.Never)]
          public new DataGridViewRowCollection Rows
          {
              get { return base.Rows; }
          }
  
          [Browsable(false),
          DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
          EditorBrowsable(EditorBrowsableState.Never)]
          public new bool VirtualMode
          {
              get { return false; }
              set { throw new NotSupportedException("The TreeGridView does not support virtual mode"); }
          }
  
          // none of the rows/nodes created use the row template, so it is hidden.
          [Browsable(false),
          DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
          EditorBrowsable(EditorBrowsableState.Never)]
          public new DataGridViewRow RowTemplate
          {
              get { return base.RowTemplate; }
              set { base.RowTemplate = value; }
          }
  
          #endregion
  
          #region Public methods
          [Description("Returns the TreeGridNode for the given DataGridViewRow")]
          public TreeGridNode GetNodeForRow(DataGridViewRow row)
          {
              return row as TreeGridNode;
          }
  
          [Description("Returns the TreeGridNode for the given DataGridViewRow")]
          public TreeGridNode GetNodeForRow(int index)
          {
              return GetNodeForRow(base.Rows[index]);
          }
          #endregion
  
          #region Public properties
          [Category("Data"),
  		Description("The collection of root nodes in the treelist."),
  		DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
  		Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
  		public TreeGridNodeCollection Nodes
  		{
  			get
  			{
  				return this._root.Nodes;
  			}
  		}
  
  		public new TreeGridNode CurrentRow
  		{
  			get
  			{
  				return base.CurrentRow as TreeGridNode;
  			}
  		}
  
          [DefaultValue(false),
          Description("Causes nodes to always show as expandable. Use the NodeExpanding event to add nodes.")]
          public bool VirtualNodes
          {
              get { return _virtualNodes; }
              set { _virtualNodes = value; }
          }
  	
  		public TreeGridNode CurrentNode
  		{
  			get
  			{
  				return this.CurrentRow;
  			}
  		}
  
          [DefaultValue(true)]
          public bool ShowLines
          {
  
gt:             get { return this._showLines; }