private void MainWindow_LeftMouseDown(object sender, MouseEventArgs e)
        {
            // log.write("mouse state set to down");
            mouseState = CGMouseState.CGMouseStateDown;

            if (buttonClicked != this.buttonDrawPolygon &&
                buttonClicked != this.buttonDrawBezier &&
                buttonClicked != this.buttonDrawBStyleCurve)
            {
                // log.write(String.Format("canUpdateGraphics:{0} --> true", canUpdateGraphics));
                canUpdateGraphics = true;
                canClearGraphics  = false;
            }

            // set downpos
            this.downPos.X = e.X;
            this.downPos.Y = e.Y;

            if (buttonClicked == this.buttonMoveGraphics ||
                buttonClicked == this.buttonAdjustGraphics ||
                buttonClicked == this.buttonRotation ||
                buttonClicked == this.buttonZoomGraphics ||
                buttonClicked == null)
            {
                userCanvas.SelectGraphicsByCursor(new Point(e.X, e.Y));
            }

            ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);
        }
        private void MainWindow_LeftMouseUp(object sender, MouseEventArgs e)
        {
            if (buttonClicked == this.buttonDrawPolygon ||
                buttonClicked == this.buttonDrawBezier ||
                buttonClicked == this.buttonDrawBStyleCurve)
            {
                mouseState = CGMouseState.CGMouseStateUp;

                isUpdatingGraphicsWhenMouseUp = true;

                upPos.X = e.X; upPos.Y = e.Y;

                canUpdateGraphics = true;
            }
            else
            {
                if (buttonClicked == this.buttonTrim)
                {
                    userCanvas.trimSelectedGraphics(userCanvas.trimArea);
                    ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);
                }
                // log.write("mouse set to up, canUpdateGraphics --> false");
                mouseState        = CGMouseState.CGMouseStateUp;
                canUpdateGraphics = false;
            }

            if (canClearGraphics)
            {
                // clear current graphics in userCanvas
                // userCanvas.ClearStateOfSelectedGraphics();
                // flip userCanvas to ghs
                ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);
            }

            canClearGraphics = false;
        }
        private void MainWindow_RightMouseDown(object sender, MouseEventArgs e)
        {
            // note: the order of each event when double clicked
            //       down -> up -> down -> dbclick -> up

            if (buttonClicked == this.buttonDrawPolygon)
            {
                // log.write("mouse double click");

                if (polygonEdgeSet.Count == 0)
                {
                    return;
                }

                int dx = e.X - polygonEdgeSet[0].firstPoint.X;
                int dy = e.Y - polygonEdgeSet[0].firstPoint.Y;

                if (dx * dx + dy * dy > 3 * 3)
                {
                    CGUserGraphicsLine pline = new CGUserGraphicsLine(new Point(e.X, e.Y), polygonEdgeSet[0].firstPoint);
                    userCanvas.AddGraphics(pline);
                    polygonEdgeSet.Add(pline);
                }

                mouseState = CGMouseState.CGMouseStateUp;
                // log.write("canUpdateGraphics --> false");
                canUpdateGraphics = false;

                isUpdatingGraphicsWhenMouseUp = false;

                userCanvas.ClearStateOfSelectedGraphics();

                polygonEdgeSet.ForEach((l) => { userCanvas.RemoveGraphics(l); });

                CGUserGraphicsPolygon polygon = new CGUserGraphicsPolygon(polygonEdgeSet);
                userCanvas.AddGraphics(polygon);
                userCanvas.SetGraphicsSelected(polygon);

                ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);
                polygonEdgeSet.Clear();
            }
            else if (buttonClicked == this.buttonDrawBezier)
            {
                if (polygonEdgeSet.Count <= 1)
                {
                    return;
                }

                if (oldSelectedCurve != null)
                {
                    userCanvas.RemoveGraphics(oldSelectedCurve);
                    oldSelectedCurve = null;
                }

                int dx = e.X - polygonEdgeSet[0].firstPoint.X;
                int dy = e.Y - polygonEdgeSet[0].firstPoint.Y;

                List <Point> downPointSet = new List <Point>();

                foreach (CGUserGraphicsLine l in polygonEdgeSet)
                {
                    downPointSet.Add(l.firstPoint);
                }

                if (dx * dx + dy * dy < 3 * 3)
                {
                    downPointSet.Add(polygonEdgeSet[0].firstPoint);
                }
                else
                {
                    downPointSet.Add(polygonEdgeSet[polygonEdgeSet.Count - 1].nextPoint);
                }

                mouseState = CGMouseState.CGMouseStateUp;
                // log.write("canUpdateGraphics --> false");
                canUpdateGraphics = false;

                isUpdatingGraphicsWhenMouseUp = false;

                userCanvas.ClearStateOfSelectedGraphics();

                polygonEdgeSet.ForEach((l) => { userCanvas.RemoveGraphics(l); });
                polygonEdgeSet.Clear();

                CGUserGraphicsBezier curve = new CGUserGraphicsBezier(downPointSet);

                userCanvas.AddGraphics(curve);
                userCanvas.SetGraphicsSelected(curve);

                ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);

                downPointSet.Clear();
            }
            else if (buttonClicked == this.buttonDrawBStyleCurve)
            {
                if (polygonEdgeSet.Count <= 1)
                {
                    return;
                }

                if (oldSelectedCurve != null)
                {
                    userCanvas.RemoveGraphics(oldSelectedCurve);
                    oldSelectedCurve = null;
                }

                int dx = e.X - polygonEdgeSet[0].firstPoint.X;
                int dy = e.Y - polygonEdgeSet[0].firstPoint.Y;

                List <Point> downPointSet = new List <Point>();

                foreach (CGUserGraphicsLine l in polygonEdgeSet)
                {
                    downPointSet.Add(l.firstPoint);
                }

                if (dx * dx + dy * dy < 3 * 3)
                {
                    downPointSet.Add(polygonEdgeSet[0].firstPoint);
                }
                else
                {
                    downPointSet.Add(polygonEdgeSet[polygonEdgeSet.Count - 1].nextPoint);
                }

                mouseState = CGMouseState.CGMouseStateUp;
                // log.write("canUpdateGraphics --> false");
                canUpdateGraphics = false;

                isUpdatingGraphicsWhenMouseUp = false;

                userCanvas.ClearStateOfSelectedGraphics();

                polygonEdgeSet.ForEach((l) => { userCanvas.RemoveGraphics(l); });
                polygonEdgeSet.Clear();

                CGUserGraphicsBStyleCurve curve = new CGUserGraphicsBStyleCurve(downPointSet);

                userCanvas.AddGraphics(curve);
                userCanvas.SetGraphicsSelected(curve);

                ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);

                downPointSet.Clear();
            }
            else if (buttonClicked == this.buttonRotation ||
                     buttonClicked == this.buttonZoomGraphics)
            {
                userCanvas.SetBasePoint(new Point(e.X, e.Y));

                ghs.DrawImage(userCanvas.bmp, this.ClientRectangle);
            }
        }