/// <summary>
        /// Grup selected objs
        /// </summary>
        public void UnGroupSelected()
        {
            var tmpList = SelectedElements;

            if (selectedElement != null)
            {
                //this.List.Remove(selectedElement);
                selectedElement = null;
                sRec            = null;
            }
            bool found = false;

            foreach (var tmpElem in tmpList)
            {
                var ungroupedElements = tmpElem.UnGroup();
                if (ungroupedElements == null || ungroupedElements.Count <= 0)
                {
                    continue;
                }
                List.AddRange(ungroupedElements);
                List.Remove(tmpElem);
                found = true;
            }
            if (found)      // when grouping / ungrouping reset undoBuffer
            {
                undoB = new UndoBuffer(20);
            }
        }
        /// <summary>
        /// Group selected objs
        /// </summary>
        public void GroupSelected()
        {
            var tmpList = SelectedElements;

            if (selectedElement != null)
            {
                //this.List.Remove(selectedElement);
                selectedElement = null;
                sRec            = null;
            }

            foreach (var tmpElem in tmpList)
            {
                List.Remove(tmpElem);

                // store the operation in undo/redo buffer
                //storeDo("D", tmpElem);
            }

            var g = new Group(tmpList);

            List.Add(g);
            // store the operation in undo/redo buffer
            //storeDo("I", g);

            sRec = new SelRect(g);
            //sRec.showHandles(true);
            selectedElement = g;
            selectedElement.Select();

            // when grouping / ungrouping, reset the undoBuffer
            undoB = new UndoBuffer(20);
        }
 /// <summary>
 /// Selects last shape containing x,y
 /// </summary>
 public void click(int x, int y, RichTextBox r)
 {
     sRec            = null;
     selectedElement = null;
     foreach (var obj in List)
     {
         obj.IsSelected = false;
         obj.DeSelect();
         if (obj.Contains(x, y))
         {
             selectedElement = obj; //salvo il riferimento dell'ogg trovato
         }
     }
     if (selectedElement == null)
     {
         return;
     }
     selectedElement.IsSelected = true;
     selectedElement.Select();
     selectedElement.Select(r);
     sRec = selectedElement is PointSet
                ? (AbstractSel) new SelPoly(selectedElement)
                : new SelRect(selectedElement);
     //sRec.IsLine = selectedElement.IsLine;
     //sRec.showHandles(selectedElement.AmIaGroup());
 }
        /// <summary>
        /// Adds RoundRect
        /// </summary>
        public void addRRect(int x, int y, int x1, int y1, Color penC, Color fillC, float penW, bool filled)
        {
            if (x1 - minDim <= x)
            {
                x1 = x + minDim;
            }
            if (y1 - minDim <= y)
            {
                y1 = y + minDim;
            }

            deSelect();
            var r = new RoundedRect(x, y, x1, y1)
            {
                PenColor = penC, PenWidth = penW, FillColor = fillC, IsFilled = filled
            };

            List.Add(r);
            // store the operation in undo/redo buffer
            storeDo("I", r);

            sRec            = new SelRect(r);
            selectedElement = r;
            selectedElement.Select();
        }
        /// <summary>
        /// Adds Rect
        /// </summary>
        public void addLine(int x, int y, int x1, int y1, Color penC, float penW)
        {
            /*
             * if ((x1 == x) && (y1 == y))
             * {
             *  x1 = x + minDim;
             *  y1 = y + minDim;
             * }
             */

            deSelect();
            var r = new Line(x, y, x1, y1)
            {
                PenColor = penC, PenWidth = penW
            };

            List.Add(r);
            // store the operation in undo/redo buffer
            storeDo("I", r);

            sRec = new SelRect(r);

            selectedElement = r;
            selectedElement.Select();
        }
        public void addImgBox(int x, int y, int x1, int y1, Bitmap bitmap, Color penC, float penW)
        {
            if (bitmap == null)
            {
                return;
            }

            if (x1 - minDim <= x)
            {
                x1 = x + minDim;
            }
            if (y1 - minDim <= y)
            {
                y1 = y + minDim;
            }

            deSelect();
            var r = new ImageBox(x, y, x1, y1)
            {
                PenColor = penC, PenWidth = penW
            };

            List.Add(r);
            // store the operation in undo/redo buffer
            storeDo("I", r);
            r.img = bitmap;

            sRec            = new SelRect(r);
            selectedElement = r;
            selectedElement.Select();
        }
        /// <summary>
        /// Adds SimpleTextBox
        /// </summary>
        public void addSimpleTextBox(int x, int y, int x1, int y1, RichTextBox t, Color penC, Color fillC, float penW,
                                     bool filled)
        {
            if (x1 - minDim <= x)
            {
                x1 = x + minDim;
            }
            if (y1 - minDim <= y)
            {
                y1 = y + minDim;
            }

            deSelect();
            var r = new SimpleText(x, y, x1, y1)
            {
                Text      = t.Text,
                CharFont  = t.SelectionFont,
                PenColor  = penC,
                PenWidth  = penW,
                FillColor = fillC,
                IsFilled  = filled
            };

            //r.CharFont = (Font)t.Font.Clone();


            List.Add(r);
            // store the operation in undo/redo buffer
            storeDo("I", r);

            sRec            = new SelRect(r);
            selectedElement = r;
            selectedElement.Select();
        }
        /// <summary>
        /// Adds Polygon
        /// </summary>
        public void addPoly(int x, int y, int x1, int y1, Color penC, Color fillC, float penW, bool filled,
                            List <PointWr> aa,
                            bool curv)
        {
            /*if (x1 - minDim <= x)
             *  x1 = x + minDim;
             * if (y1 - minDim <= y)
             *  y1 = y + minDim;*/

            deSelect();
            var r = new PointSet(x, y, x1, y1, aa)
            {
                PenColor  = penC,
                PenWidth  = penW,
                FillColor = fillC,
                IsFilled  = filled,
                Curved    = curv
            };

            List.Add(r);
            // store the operation in undo/redo buffer
            storeDo("I", r);

            sRec            = new SelPoly(r);
            selectedElement = r;
            selectedElement.Select();
        }
        /// <summary>
        /// Copy all selected Items
        /// </summary>
        public void CopyMultiSelected(int dx, int dy)
        {
            var tmpList = new List <Element>();

            foreach (var elem in List)
            {
                if (!elem.IsSelected)
                {
                    continue;
                }
                var eL = elem.Copy();
                elem.IsSelected = false;
                eL.move(dx, dy);
                tmpList.Add(eL);
                sRec            = new SelRect(eL);
                selectedElement = eL;
                selectedElement.endMoveRedim();
            }
            foreach (var tmpElem in tmpList)
            {
                List.Add(tmpElem);
                // store the operation in undo/redo buffer
                storeDo("I", tmpElem);
            }
        }
 /// <summary>
 /// Deselect
 /// </summary>
 public void deSelect()
 {
     foreach (var obj in List)
     {
         obj.IsSelected = false;
     }
     selectedElement = null;
     sRec            = null;
 }
 public void YMirror()
 {
     if (!(selectedElement is PointSet))
     {
         return;
     }
     ((PointSet)selectedElement).CommitMirror(false, true);
     sRec = new SelPoly(selectedElement); //create handling rect
 }
        /// <summary>
        /// Selects all shapes in imput rectangle
        /// </summary>
        public void multiSelect(int startX, int startY, int endX, int endY, RichTextBox r)
        {
            SanitizeRect(ref startX, ref startY, ref endX, ref endY);

            sRec            = null;
            selectedElement = null;
            foreach (var obj in List)
            {
                obj.IsSelected = false;
                obj.DeSelect(); // to deselect points in polys
                int x  = obj.getX();
                int x1 = obj.getX1();
                int y  = obj.getY();
                int y1 = obj.getY1();
                int c;
                if (x > x1)
                {
                    c  = x;
                    x  = x1;
                    x1 = c;
                }
                if (y > y1)
                {
                    c  = y;
                    y  = y1;
                    y1 = c;
                }
                //if (obj.getX() <= endX && obj.getX1() >= Start.X && obj.getY() <= endY && obj.getY1() >= Start.Y)
                if (!(x <= endX && x1 >= startX && y <= endY && y1 >= startY))
                {
                    continue;
                }
                selectedElement = obj;  //salvo il riferimento dell'ogg trovato
                obj.IsSelected  = true; //indico l'oggetto trovato come selezionato
                obj.Select();
                obj.Select(r);
                obj.Select(startX, startY, endX, endY);
            }
            if (selectedElement != null)
            {
                if (selectedElement is PointSet)
                {
                    sRec = new SelPoly(selectedElement); //create handling rect
                }
                else
                {
                    sRec = new SelRect(selectedElement); //creo un gestore con maniglie
                }
                //sRec.IsLine = selectedElement.IsLine;//indico se il gestore è per una linea
                //sRec.showHandles(selectedElement.AmIaGroup());
            }
        }
        public void AddPoint()
        {
            if (!(sRec is SelPoly))
            {
                return;
            }
            var p = ((SelPoly)sRec).getNewPoint();
            int i = ((SelPoly)sRec).getIndex();

            if (i <= 0)
            {
                return;
            }
            ((PointSet)selectedElement).points.Insert(i - 1, p);
            sRec = new SelPoly(selectedElement); //create handling rect
        }
        public void RemovePoint()
        {
            if (!(sRec is SelPoly))
            {
                return;
            }
            var tmp = ((SelPoly)sRec).getSelPoints();

            if (tmp.Count < ((PointSet)selectedElement).points.Count - 1)
            {
                foreach (var p in tmp)
                {
                    ((PointSet)selectedElement).points.Remove(p);
                }
            }
            sRec = new SelPoly(selectedElement); //create handling rect
        }
        /// <summary>
        /// Elimina oggetto selezioanto
        /// </summary>
        public void RemoveSelected()
        {
            var tmpList = SelectedElements;

            if (selectedElement != null)
            {
                //this.List.Remove(selectedElement);
                selectedElement = null;
                sRec            = null;
            }

            foreach (var tmpElem in tmpList)
            {
                List.Remove(tmpElem);

                // store the operation in undo/redo buffer
                storeDo("D", tmpElem);
            }
        }
        /// <summary>
        /// Copy selected Item
        /// </summary>
        public void CopySelected(int dx, int dy)
        {
            if (selectedElement == null)
            {
                return;
            }
            var element = GetCopyOfSelectedElement();

            element.move(dx, dy);
            deSelect();
            List.Add(element);

            // store the operation in undo/redo buffer
            storeDo("I", element);

            sRec = new SelRect(element);
            //sRec.IsLine = L.IsLine;
            selectedElement = element;
            selectedElement.endMoveRedim();
        }
        /// <summary>
        /// Adds Polygon
        /// </summary>
        public void addColorPoinySet(int x, int y, int x1, int y1, Color penC, Color fillC, float penW, bool filled,
                                     List <PointWr> aa, bool curv)
        {
            deSelect();
            var r = new PointColorSet(x, y, x1, y1, aa)
            {
                PenColor  = penC,
                PenWidth  = penW,
                FillColor = fillC,
                IsFilled  = filled,
                Curved    = curv
            };

            List.Add(r);
            // store the operation in undo/redo buffer
            storeDo("I", r);

            sRec            = new SelPoly(r);
            selectedElement = r;
            selectedElement.Select();
        }