/// <summary>
        /// Handles the right mouse button up event (dragging stopped).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">Event arguments.</param>
        private void mainGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.IsViewOnly)
            {
                return;
            }

            // do not start in a new point unless last polygon is closed
            if (rightBtnIsDragging || !lastPolygonClosed)
            {
                return;
            }

            this.rightBtnIsDragging = true;
            this.lastPolygonClosed  = false;
            System.Windows.Point position = e.GetPosition(this);

            double x = 0, y = 0;

            FromMonitorToCoordinate(position.X, position.Y, ref x, ref y);

            this.dragStartPoint = new CGUtilities.Point(x, y);
            this.dragEndPoint   = this.dragStartPoint;
            this.UpdateSelectionLine();
        }
        /// <summary>
        /// Handles the left mouse button up event (dragging stopped).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">Event arguments.</param>
        void mainGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.IsViewOnly || dragStartPoint == null)
            {
                return;
            }

            if (!this.isDragging || rightBtnIsDragging)
            {
                return;
            }

            this.isDragging = false;
            this.selectionGrid.Visibility = System.Windows.Visibility.Collapsed;
            System.Windows.Point position = e.GetPosition(this);

            double x = 0, y = 0;

            FromMonitorToCoordinate(position.X, position.Y, ref x, ref y);

            this.dragEndPoint = new CGUtilities.Point(x, y);

            if (this.dragStartPoint.Equals(this.dragEndPoint))
            {
                this.Points.Add(this.dragStartPoint);
            }
            else
            {
                this.Lines.Add(new Line(this.dragStartPoint, this.dragEndPoint));
            }
        }
        /// <summary>
        /// Adds animation element for a point.
        /// </summary>
        /// <param name="point">The point.</param>
        private void AddAnimationElement(CGUtilities.Point point)
        {
            var element = this.CreateElement(point);

            this.AnimateAnimationElement(element, point);
            this.AddAnimationElement(element);
        }
        /// <summary>
        /// Handles the right mouse button up event (dragging stopped).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">Event arguments.</param>
        private void mainGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.IsViewOnly || dragStartPoint == null)
            {
                return;
            }

            double x = 0, y = 0;

            System.Windows.Point position = e.GetPosition(this);
            FromMonitorToCoordinate(position.X, position.Y, ref x, ref y);

            var EndPoint = new CGUtilities.Point(x, y);

            this.selectionGrid.Visibility = System.Windows.Visibility.Collapsed;

            if (this.dragStartPoint.Equals(EndPoint))
            {
                return;
            }

            if (this.rightBtnIsDragging) // first line in a new polygon
            {
                this.rightBtnIsDragging = false;

                if (!this.dragStartPoint.Equals(EndPoint))
                {
                    var CurrentPolygon = new ObservableCollection <Line>();
                    CurrentPolygon.CollectionChanged += lines_CollectionChanged;

                    CurrentPolygon.Add(new Line(this.dragStartPoint, EndPoint));
                    polygonsUI.Add(CurrentPolygon);
                }
            }
            else // new line in last polygon
            {
                if (polygonsUI.Count == 0)
                {
                    return;
                }

                var CurrentPolygon = polygonsUI[polygonsUI.Count - 1];
                var firstPoint     = CurrentPolygon[CurrentPolygon.Count - 1].End.Clone();

                if (!firstPoint.Equals(EndPoint))
                {
                    CurrentPolygon.Add(new Line((CGUtilities.Point)firstPoint, EndPoint));
                }
            }
        }
        internal void Reset()
        {
            this.Points.Clear();
            this.Lines.Clear();
            this.polygons.Clear();

            this.selectionLine.X1 = 0;
            this.selectionLine.Y1 = 0;
            this.selectionLine.X2 = 0;
            this.selectionLine.Y2 = 0;

            lastPolygonClosed  = true;
            rightBtnIsDragging = isDragging = false;
            dragStartPoint     = dragEndPoint = null;
        }
        /// <summary>
        /// Creates a UI element that represents a point.
        /// </summary>
        /// <param name="point">The point.</param>
        /// <returns>Point UI element.</returns>
        private FrameworkElement CreateElement(CGUtilities.Point point)
        {
            double x = point.X, y = point.Y;

            FromCoordinateToMonitor(point.X, point.Y, ref x, ref y);

            var element = new System.Windows.Shapes.Ellipse()
            {
                Width  = pointRadius,
                Height = pointRadius,
                Margin = new Thickness(x - pointRadius / 2, y - pointRadius / 2, 0, 0),
            };

            this.SetElementAlignment(element);

            return(element);
        }
        /// <summary>
        /// Handles the mouse move event (dragging).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">Event arguments.</param>
        void mainGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsViewOnly)
            {
                return;
            }

            if (this.isDragging || this.rightBtnIsDragging)
            {
                this.selectionGrid.Visibility = System.Windows.Visibility.Visible;
                System.Windows.Point position = e.GetPosition(this);

                double x = 0, y = 0;
                FromMonitorToCoordinate(position.X, position.Y, ref x, ref y);

                this.dragEndPoint = new CGUtilities.Point(x, y);
                this.UpdateSelectionLine();
            }
        }
        /// <summary>
        /// Sets the animation for the animated point element.
        /// </summary>
        /// <param name="element">The point element.</param>
        /// <param name="point">The point.</param>
        private void AnimateAnimationElement(FrameworkElement element, CGUtilities.Point point)
        {
            ScaleTransform scaleTransform = new ScaleTransform(1, 1);

            element.RenderTransform       = scaleTransform;
            element.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);

            DoubleAnimation fadeIn = new DoubleAnimation(0, TimeSpan.FromMilliseconds(animationDuration))
            {
                DecelerationRatio = 1,
            };
            DoubleAnimation scaleAnimation = new DoubleAnimation(scaleAnimationScale, TimeSpan.FromMilliseconds(animationDuration))
            {
                AccelerationRatio = 1,
            };

            element.BeginAnimation(FrameworkElement.OpacityProperty, fadeIn);
            scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scaleAnimation);
            scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scaleAnimation);
        }
        /// <summary>
        /// Handles the left mouse button down event (dragging started).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">Event arguments.</param>
        void mainGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.IsViewOnly)
            {
                return;
            }

            if (isDragging || rightBtnIsDragging)
            {
                return;
            }

            this.isDragging = true;
            System.Windows.Point position = e.GetPosition(this);

            double x = 0, y = 0;

            FromMonitorToCoordinate(position.X, position.Y, ref x, ref y);

            this.dragStartPoint = new CGUtilities.Point(x, y);
            this.dragEndPoint   = this.dragStartPoint;
            this.UpdateSelectionLine();
        }