public static VisualItemCursor NextInputItem(
            this VisualItemCursor Cursor, VisualFeature?Feature = null)
        {
            var  cursor  = Cursor;
            bool gotItem = false;

            while (gotItem == false)
            {
                cursor = cursor.NextItem(true);
                if (cursor == null)
                {
                    break;
                }
                if (cursor.IsInputItem() == true)
                {
                    gotItem = true;
                }

                if ((gotItem == true) &&
                    (Feature != null) && (Feature.Value == VisualFeature.tabTo) &&
                    (cursor.GetVisualItem().IsTabToItem == false))
                {
                    gotItem = false;
                }
            }
            return(cursor);
        }
        /// <summary>
        /// find the first entry field in the list of visual items.
        /// </summary>
        /// <returns></returns>
        public VisualItemCursor FirstInputItem(VisualFeature?Feature = null)
        {
            VisualItemCursor cursor = null;

            foreach (var ic in  VisualItemList( ))
            {
                bool gotItem = false;
                if (ic.IsInputItem( ) == true)
                {
                    gotItem = true;
                }

                if ((gotItem == true) &&
                    (Feature != null) && (Feature.Value == VisualFeature.tabTo) &&
                    (ic.GetVisualItem( ).IsTabToItem == false))
                {
                    gotItem = false;
                }

                if (gotItem == true)
                {
                    cursor = ic;
                    break;
                }
            }
            return(cursor);
        }
        public CanvasPositionCursor FindVisualItemCanvas(IRowCol RowCol)
        {
            CanvasPositionCursor posCursor   = null;
            VisualItemCursor     foundCursor = null;
            int foundPos = 0;

            foreach (var ic in this.InputItemList())
            {
                var ivi = ic.GetVisualItem();
                if (ivi.ContainsLocation(RowCol))
                {
                    foundCursor = ic;
                    foundPos    = RowCol.ColNum - ivi.ShowRowCol().ColNum + 1;
                    break;
                }
            }

            if (foundCursor == null)
            {
                posCursor = new CanvasPositionCursor(RowCol as ZeroRowCol);
            }
            else
            {
                posCursor = new CanvasPositionCursor(foundCursor, foundPos);
            }

            return(posCursor);
        }
        /// <summary>
        /// find the input item on the canvas that preceeds the row/col position.
        /// </summary>
        /// <param name="rowCol"></param>
        /// <returns></returns>
        public VisualItemCursor FindPrevInputItem(
            IRowCol rowCol, VisualFeature?Feature = null)
        {
            VisualItemCursor findCursor = null;

            foreach (var cursor in this.InputItemList(Feature))
            {
                var iItem      = cursor.GetVisualItem();
                var showRowCol = iItem.ShowRowCol();
                if (showRowCol.CompareTo(rowCol) == -1)
                {
                    findCursor = new VisualItemCursor(cursor);
                }
                else
                {
                    break;
                }
            }

            if (findCursor == null)
            {
                findCursor = this.InputItemList(Feature).Last();
            }

            return(findCursor);
        }
        /// <summary>
        /// remove the visualItem from the list of visual items and also from the
        /// ItemCanvas.
        /// </summary>
        /// <param name="itemCursor"></param>
        /// <param name="ItemCanvas"></param>
        public void RemoveItem(VisualItemCursor itemCursor, ItemCanvas ItemCanvas)
        {
            var ivm = itemCursor.GetVisualItem( ) as IVisualItemMore;

            if (ivm.IsOnCanvas == true)
            {
                ivm.RemoveFromCanvas(ItemCanvas);
            }
            itemCursor.RemoveColumnNode();
        }
 public bool IsSameItem(VisualItemCursor Cursor)
 {
     if ((this.RowNode == Cursor.RowNode) &&
         (this.ColumnNode == Cursor.ColumnNode))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        /// <summary>
        /// advance to the next item in collection of items on the canvas.
        /// Use the Reuse flag to reuse the cursor. Update the cursor to the
        /// next item and return it.
        /// </summary>
        /// <param name="Cursor"></param>
        /// <param name="Reuse"></param>
        /// <returns></returns>
        public static VisualItemCursor NextItem(
            this VisualItemCursor Cursor, bool Reuse = false)
        {
            VisualItemCursor             cursor  = null;
            LinkedListNode <IVisualItem> colNode = Cursor.ColumnNode;
            LinkedListNode <VisualRow>   rowNode = Cursor.RowNode;

            // next item in the column.
            if (colNode != null)
            {
                var next = colNode.Next;
                colNode = next;
            }

            // no more column items. get next row.
            while (colNode == null)
            {
                rowNode = rowNode.Next;
                if (rowNode != null)
                {
                    colNode = rowNode.Value.ItemList.First;
                }
                else
                {
                    colNode = null;
                    break;
                }
            }

            if (rowNode == null)
            {
                cursor = null;
            }
            else if (Reuse == true)
            {
                cursor            = Cursor;
                cursor.ColumnNode = colNode;
                cursor.RowNode    = rowNode;
            }
            else
            {
                cursor = new VisualItemCursor()
                {
                    ColumnNode = colNode,
                    RowNode    = rowNode
                };
            }

            return(cursor);
        }
        public VisualItemCursor FindVisualItem(IRowCol FindRowCol)
        {
            LinkedListNode <IVisualItem> colNode   = null;
            LinkedListNode <IVisualItem> foundNode = null;

            // find the row within list of rows that contain items.
            var rowNode = Rows.First;

            while (rowNode != null)
            {
                if (rowNode.Value.RowNum == FindRowCol.RowNum)
                {
                    break;
                }
                rowNode = rowNode.Next;
            }

            // found the row.  now search for the item in the items of the row.
            if (rowNode != null)
            {
                colNode = rowNode.Value.ItemList.First;
                while (colNode != null)
                {
                    if ((colNode.Value as IVisualItem).WithinColumnBounds(FindRowCol.ColNum))
                    {
                        foundNode = colNode;
                        break;
                    }
                    colNode = colNode.Next;
                }
            }

            VisualItemCursor cursor = null;

            if (foundNode != null)
            {
                cursor = new VisualItemCursor()
                {
                    ColumnNode = foundNode,
                    RowNode    = rowNode
                };
            }
            return(cursor);
        }
        public static VisualItemCursor PrevInputItem(
            this VisualItemCursor Cursor, VisualFeature?Feature = null)
        {
            VisualItemCursor fs      = null;
            bool             gotItem = false;
            var cursor = Cursor;

            while (gotItem == false)
            {
                cursor = cursor.PrevItem(true);

                if (cursor.IsInputItem() == true)
                {
                    gotItem = true;
                }

                if ((gotItem == true) &&
                    (Feature != null) && (Feature.Value == VisualFeature.tabTo) &&
                    (cursor.GetVisualItem().IsTabToItem == false))
                {
                    gotItem = false;
                }

                if (gotItem == false)
                {
                    // check that code has looped and no input item found.
                    if (fs == null)
                    {
                        fs = new VisualItemCursor(cursor);
                    }

                    else if (cursor.IsSameItem(fs) == true)
                    {
                        cursor = null;
                        break;
                    }
                }
            }
            return(cursor);
        }
        public VisualItemCursor FirstVisualItem()
        {
            VisualItemCursor             cursor  = null;
            LinkedListNode <IVisualItem> colNode = null;

            var rowNode = this.Rows.First;

            if (rowNode != null)
            {
                colNode = rowNode.Value.ItemList.First;
            }

            if (rowNode != null)
            {
                cursor = new VisualItemCursor()
                {
                    ColumnNode = colNode,
                    RowNode    = rowNode
                };
            }

            return(cursor);
        }
        public static VisualItemCursor PrevItem(this VisualItemCursor Cursor, bool Reuse = false)
        {
            VisualItemCursor             cursor  = null;
            LinkedListNode <IVisualItem> colNode = Cursor.ColumnNode;
            LinkedListNode <VisualRow>   rowNode = Cursor.RowNode;

            // next item in the column.
            if (colNode != null)
            {
                var prev = colNode.Previous;
                colNode = prev;
            }

            // no more column items. get next row.
            if (colNode == null)
            {
                rowNode = rowNode.PrevCircular();
                colNode = rowNode.Value.ItemList.Last;
            }

            if (Reuse == true)
            {
                cursor            = Cursor;
                cursor.ColumnNode = colNode;
                cursor.RowNode    = rowNode;
            }
            else
            {
                cursor = new VisualItemCursor()
                {
                    ColumnNode = colNode,
                    RowNode    = rowNode
                };
            }

            return(cursor);
        }
 public VisualItemCursor(VisualItemCursor Cursor)
 {
     this.ColumnNode = Cursor.ColumnNode;
     this.RowNode    = Cursor.RowNode;
 }