using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace RadGridView_WPF_AR_5
{
	public class DoubleClick
	{
		private UIElement element;
		private DispatcherTimer doubleClickTimer;
		private int clickCount;

		internal event EventHandler DoubleClickEvent;

		public DoubleClick(UIElement element)
		{
			this.element = element;
			this.element.AddHandler(UIElement.MouseLeftButtonDownEvent,
				new MouseButtonEventHandler(element_MouseLeftButtonDown), true);
		}

		void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			this.StartDoubleClickTime();
			if (this.clickCount > 0)
				this.OnDoubleClick();

			this.clickCount++;
		}

		private void StartDoubleClickTime()
		{
			if (this.doubleClickTimer != null)
			{
				this.doubleClickTimer.Stop();
			}

			this.doubleClickTimer = new DispatcherTimer();
			this.doubleClickTimer.Interval = TimeSpan.FromMilliseconds(300);
			this.doubleClickTimer.Tick += new EventHandler(doubleClickTimer_Tick);
			this.doubleClickTimer.Start();
		}

		void doubleClickTimer_Tick(object sender, EventArgs e)
		{
			this.doubleClickTimer.Stop();
			this.clickCount = 0;
		}

		private void OnDoubleClick()
		{
			if (this.DoubleClickEvent != null)
				this.DoubleClickEvent(this.element, new EventArgs());
		}
	}
}
