/// <summary>
        /// Adds a layout element to the layout
        /// </summary>
        public void AddToLayout(LayoutElement le)
        {
            string leName = le.Name + " 1";
            int i = 2;
            while (_layoutElements.FindAll(delegate(LayoutElement o) { return (o.Name == leName); }).Count > 0)
            {
                leName = le.Name + " " + i;
                i++;
            }
            le.Name = leName;

            _layoutElements.Insert(0, le);
            OnElementsChanged(null);
            le.Invalidated += LeInvalidated;
            Invalidate(new Region(PaperToScreen(le.Rectangle)));
        }
 /// <summary>
 /// Removes the specified layoutElement from the selection
 /// </summary>
 internal void RemoveFromSelection(LayoutElement le)
 {
     _selectedLayoutElements.Remove(le);
     Invalidate(new Region(PaperToScreen(le.Rectangle)));
     OnSelectionChanged(null);
 }
        void LayoutControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //Handles various different mouse modes
                switch (_mouseMode)
                {
                    //If we are dealing with a selection we look here
                    case MouseMode.CreateSelection:
                        PointF selectBoxTL = ScreenToPaper(_mouseBox.Location);
                        PointF selectBoxBR = ScreenToPaper(_mouseBox.Location.X + _mouseBox.Width, _mouseBox.Location.Y + _mouseBox.Height);
                        RectangleF selectBoxPaper = new RectangleF(selectBoxTL.X, selectBoxTL.Y, selectBoxBR.X - selectBoxTL.X, selectBoxBR.Y - selectBoxTL.Y);

                        if (ModifierKeys == Keys.Control)
                        {
                            foreach (LayoutElement le in _layoutElements)
                            {
                                if (le.IntersectsWith(selectBoxPaper))
                                {
                                    if (_selectedLayoutElements.Contains(le))
                                        _selectedLayoutElements.Remove(le);
                                    else
                                        _selectedLayoutElements.Add(le);
                                    //If the box is just a point only select the top most
                                    if (_mouseBox.Width <= 1 && _mouseBox.Height <= 1)
                                        break;
                                }
                            }
                        }
                        else
                        {
                            _selectedLayoutElements.Clear();
                            foreach (LayoutElement le in _layoutElements)
                            {
                                if (le.IntersectsWith(selectBoxPaper))
                                {
                                    _selectedLayoutElements.Add(le);
                                    //If the box is just a point only select the top most
                                    if (_mouseBox.Width <= 1 && _mouseBox.Height <= 1)
                                        break;
                                }
                            }
                        }
                        OnSelectionChanged(null);
                        _mouseMode = MouseMode.Default;
                        Invalidate();
                        break;

                    //Stops moving the selection
                    case MouseMode.MoveSelection:
                        _mouseMode = MouseMode.Default;
                        Cursor = Cursors.Default;
                        break;

                    //Turns of resize 
                    case MouseMode.ResizeSelected:
                        if (_resizeTempBitmap != null)
                            _resizeTempBitmap.Dispose();
                        _resizeTempBitmap = null;
                        _mouseMode = MouseMode.Default;
                        Cursor = Cursors.Default;
                        Invalidate(new Region(PaperToScreen(_selectedLayoutElements[0].Rectangle)));
                        _selectedLayoutElements[0].Resizing = false;
                        _selectedLayoutElements[0].Size = _selectedLayoutElements[0].Size;
                        break;

                    case MouseMode.InsertNewElement:
                        if (_mouseBox.Width == 0)
                            _mouseBox.Width = 200;                    
                        if (_mouseBox.Height == 0)
                            _mouseBox.Height = 100;
                        if (_mouseBox.Width < 0)
                        {
                            _mouseBox.X = _mouseBox.X + _mouseBox.Width;
                            _mouseBox.Width = -_mouseBox.Width;
                        }
                        if (_mouseBox.Height < 0)
                        {
                            _mouseBox.Y = _mouseBox.Y + _mouseBox.Height;
                            _mouseBox.Height = -_mouseBox.Height;
                        }
                        _elementToAddWithMouse.Rectangle = ScreenToPaper(_mouseBox);
                        AddToLayout(_elementToAddWithMouse);
                        AddToSelection(_elementToAddWithMouse);
                        _elementToAddWithMouse = null;
                        _mouseMode = MouseMode.Default;
                        _mouseBox.Inflate(5, 5);
                        Invalidate(new Region(_mouseBox));
                        break;

                    case MouseMode.PanMap:
                        if (_mouseBox.Width != 0 || _mouseBox.Height != 0)
                            PanMap(_selectedLayoutElements[0] as LayoutMap, _mouseBox.Width, _mouseBox.Height);
                        _mouseMode = MouseMode.StartPanMap;
                        break;

                    case MouseMode.Default:
                        break;
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                switch (_mouseMode)
                {
                    case MouseMode.Default:
                        if (_selectedLayoutElements.Count < 1)
                        {
                            for (int i = 0; i < _contextMenuRight.MenuItems.Count; i++)
                                _contextMenuRight.MenuItems[i].Enabled = false;
                        }
                        else if (_selectedLayoutElements.Count == 1)
                        {
                            _cMnuSelAli.Enabled = false;
                            _cMnuSelFit.Enabled = false;
                        }
                        _contextMenuRight.Show(this, e.Location);
                        for (int i = 0; i < _contextMenuRight.MenuItems.Count; i++)
                            _contextMenuRight.MenuItems[i].Enabled = true;
                        break;
                }   
            }
        }
        void LayoutControl_MouseDown(object sender, MouseEventArgs e)
        {
            //When the user clicks down we start tracking the mouses location
            _mouseStartPoint = new PointF(e.X, e.Y);
            _lastMousePoint = new PointF(e.X, e.Y);
            PointF mousePointPaper = ScreenToPaper(_mouseStartPoint);

            //Deals with left buttons clicks
            if (e.Button == MouseButtons.Left)
            {
                switch (_mouseMode)
                {
                    case MouseMode.Default:

                        //Handles resizing stuff
                        if (_resizeSelectedEdge != Edge.None)
                        {
                            _mouseMode = MouseMode.ResizeSelected;
                            _selectedLayoutElements[0].Resizing = true;
                            if (_selectedLayoutElements[0].ResizeStyle != ResizeStyle.HandledInternally)
                            {
                                RectangleF selecteScreenRect = PaperToScreen(_selectedLayoutElements[0].Rectangle);
                                _resizeTempBitmap = new Bitmap(Convert.ToInt32(selecteScreenRect.Width), Convert.ToInt32(selecteScreenRect.Height),System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                                Graphics graph = Graphics.FromImage(_resizeTempBitmap);
                                graph.SmoothingMode = _drawingQuality;
                                graph.ScaleTransform(96F / 100F * _zoom, 96F / 100F * _zoom);
                                graph.TranslateTransform(-_selectedLayoutElements[0].Rectangle.X, -_selectedLayoutElements[0].Rectangle.Y);
                                _selectedLayoutElements[0].Draw(graph, false);
                                graph.Dispose();
                            }
                            return;
                        }

                        //Starts moving selected elements
                        if (ModifierKeys != Keys.Control)
                        {
                            foreach (LayoutElement le in _selectedLayoutElements)
                            {
                                if (le.IntersectsWith(mousePointPaper))
                                {
                                    _mouseMode = MouseMode.MoveSelection;
                                    Cursor = Cursors.SizeAll;
                                    return;
                                }
                            }
                        }

                        //Starts the selection code.
                        _mouseMode = MouseMode.CreateSelection;
                        _mouseBox = new RectangleF(e.X, e.Y, 0F, 0F);
                        break;

                    //Start drag rectangle insert new element
                    case MouseMode.StartInsertNewElement:
                        _mouseMode = MouseMode.InsertNewElement;
                        _mouseBox = new RectangleF(e.X, e.Y, 0F, 0F);
                        break;

                    //Starts the pan mode for the map
                    case MouseMode.StartPanMap:
                        _mouseMode = MouseMode.PanMap;
                        _mouseBox = new RectangleF(e.X, e.Y, 0F, 0F);
                        break;
                }
            }

            //Deals with right button clicks
            if (e.Button == MouseButtons.Right)
            {
                switch (_mouseMode)
                {
                    //If the user was in insert mode we cancel it
                    case(MouseMode.StartInsertNewElement):
                        _mouseMode = MouseMode.Default;
                        _elementToAddWithMouse = null;
                        Cursor = Cursors.Default;
                        break;
                }
            }
        }
 /// <summary>
 /// Converts a selected layout element into a bitmap and saves it a the specified location removing the old element and replacing it
 /// </summary>
 /// <param name="le"></param>
 /// <param name="filename"></param>
 public virtual void ConvertElementToBitmap(LayoutElement le, string filename)
 {
     if (le is LayoutBitmap) return;
     Bitmap temp = new Bitmap(Convert.ToInt32(le.Size.Width * 3), Convert.ToInt32(le.Size.Height * 3), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
     temp.SetResolution(96, 96);
     temp.MakeTransparent();
     Graphics g = Graphics.FromImage(temp);
     g.PageUnit = GraphicsUnit.Pixel;
     g.ScaleTransform(300F/100F, 300F/100F);
     g.TranslateTransform(-le.LocationF.X,-le.LocationF.Y);
     le.Draw(g,true);
     g.Dispose();
     temp.SetResolution(300, 300);
     temp.Save(filename);
     temp.Dispose();
     LayoutBitmap newLb = new LayoutBitmap();
     newLb.Rectangle = le.Rectangle;
     newLb.Name = le.Name;
     newLb.Filename = filename;
     _layoutElements.Insert(_layoutElements.IndexOf(le), newLb);
     _layoutElements.Remove(le);
     _selectedLayoutElements.Insert(_selectedLayoutElements.IndexOf(le), newLb);
     _selectedLayoutElements.Remove(le);
     OnSelectionChanged(null);
     Invalidate();
 }
 /// <summary>
 /// Allows the user to click on the layout and drag a rectangle where they want to insert an element
 /// </summary>
 public void AddElementWithMouse(LayoutElement le)
 {
     _elementToAddWithMouse = le;
     ClearSelection();
     _mouseMode = MouseMode.StartInsertNewElement;
     Cursor = Cursors.Cross;
 }