public void Reset()
 {
     this.GrabHandleBeingMoved = GrabHandles.None;
     this.SelectedControl = null;
     this.SelectionBounds = Rectangle.Empty;
     this.SelectedCenter = Point.Empty;
 }
        void SelectionOverlay_MouseMove(object sender, MouseEventArgs mevent)
        {
            if (SelectedControl == null) return;

            //Debug.WriteLine(String.Format("Selection bounds is {0}", SelectionBounds));
            GrabHandles handle = SelectableControlHelper.OnMouseMove(SelectionBounds, mevent.Location);

            this.Cursor = SelectableControlHelper.CursorsForCorners[(int)handle];
            if (mevent.Button == MouseButtons.Left &&
                SelectionBounds != null)
            {
                if (handle != GrabHandles.None)
                {
                    trace("Resizing");
                    MoveGrabHandle(handle, mevent.Location);
                    GrabHandleBeingMoved = handle;
                }
                else if (SelectionBounds.Contains(mevent.Location) || this.NewSelectionBounds != null)
                {
                    if (NewSelectionBounds == null)
                    {
                        SelectedCenter = new Point(mevent.Location.X - SelectionBounds.Left, mevent.Location.Y - SelectionBounds.Top);
                    }

                    Point newCenter = new Point(mevent.Location.X - SelectedCenter.X, mevent.Location.Y - SelectedCenter.Y);

                    trace("New center is " + newCenter);
                    NewSelectionBounds = new Rectangle(newCenter, this.SelectionBounds.Size);
                    trace("Bounds were " + NewSelectionBounds);
                    this.GrabHandleBeingMoved = GrabHandles.None;
                }
                else
                {
                    trace("Location: " + mevent.Location);
                    trace("Client loc: " + this.PointToClient(mevent.Location));
                    trace("Bounds: " + SelectionBounds);
                }
            }
        }
        void SelectionOverlay_MouseUp(object sender, MouseEventArgs e)
        {
            if (GrabHandleBeingMoved != GrabHandles.None)
            {
                MoveGrabHandle(GrabHandleBeingMoved, e.Location);
                Debug.WriteLine("SelectionBounds is " + this.SelectionBounds);
                Rectangle screenRect = _parent.RectangleToScreen(this.SelectionBounds);
                this.SelectedControl.UpdateProperties(screenRect.X + SelectableControlHelper.GrabHandleLength,
                    screenRect.Y + SelectableControlHelper.GrabHandleLength,
                    SelectionBounds.Width - SelectableControlHelper.GrabHandleLength * 2,
                    SelectionBounds.Height - SelectableControlHelper.GrabHandleLength * 2);

                this.Invalidate();
                this.GrabHandleBeingMoved = GrabHandles.None;
            }
            else if (NewSelectionBounds != null)
            {
                this.SelectionBounds = this.NewSelectionBounds.Value;
                this.SelectionBounds.Inflate(SelectableControlHelper.GrabHandleLength, SelectableControlHelper.GrabHandleLength);
                Rectangle screenRect = _parent.RectangleToScreen(this.SelectionBounds);
                this.SelectedControl.UpdateProperties(screenRect.X + SelectableControlHelper.GrabHandleLength,
                    screenRect.Y + SelectableControlHelper.GrabHandleLength,
                    SelectionBounds.Width - SelectableControlHelper.GrabHandleLength * 2,
                    SelectionBounds.Height - SelectableControlHelper.GrabHandleLength * 2);
                this.SelectedCenter = new Point(SelectionBounds.Left + (SelectionBounds.Width / 2), SelectionBounds.Top + (SelectionBounds.Height + 2));
                this.NewSelectionBounds = null;
                this.Invalidate();
            }
            else
            {
                TampaController.GetInstance().SelectControlAt(this.PointToScreen(e.Location));
                if (e.Button == MouseButtons.Right)
                {
                    TampaController.GetInstance().ShowContextMenu(this.PointToScreen(e.Location));
                }
            }
        }
        void MoveGrabHandle(GrabHandles g, Point p)
        {
            trace("Grab g picked: " + g);

            Rectangle modifiedRect = this.SelectionBounds;

            if (GrabHandleBeingMoved == GrabHandles.None)
                trace(string.Format("Starting with {0}", modifiedRect));

            if (g == GrabHandles.TopLeft || g == GrabHandles.TopMiddle || g == GrabHandles.TopRight)
            {
                trace("MoveY: " + p.Y);
                modifiedRect.Height += (modifiedRect.Y - p.Y);
                modifiedRect.Y = p.Y;
            }
            if (g == GrabHandles.TopLeft || g == GrabHandles.BottomLeft || g == GrabHandles.Left)
            {
                trace("MoveX: " + p.X);
                modifiedRect.Width += (modifiedRect.X - p.X);
                modifiedRect.X = p.X;
            }
            if (g == GrabHandles.Right || g == GrabHandles.TopRight || g == GrabHandles.BottomRight)
            {
                trace(string.Format("Fattening: {0}, {1}", p.X, modifiedRect.X));
                modifiedRect.Width = p.X - modifiedRect.X;
            }
            if (g == GrabHandles.BottomLeft || g == GrabHandles.BottomMiddle || g == GrabHandles.BottomRight)
            {
                trace(string.Format("Growing: {0}, {1}", p.Y, modifiedRect.Y));
                modifiedRect.Height = p.Y - modifiedRect.Y;
            }

            trace(String.Format("Modified rect: {0}", modifiedRect));
            this.SelectionBounds = modifiedRect;
        }