using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ToolBox
{
    public class EditorReceiver
    {

        private Control control;
        private EditorReceiver(Control control)
        {
            this.control = control;
        }
        private void dragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent("Text"))
            {
                return;
            }
            string str = e.Data.GetData("Text") as string;
            if (!str.Equals("MovedEditor"))
            {
                return;
            }
            e.Effect = DragDropEffects.Copy;
        }
        private void dragDrop(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent("Text"))
            {
                return;
            }
            string str = e.Data.GetData("Text") as string;
            if (!str.Equals("MovedEditor"))
            {
                return;
            }
            TextBox tf = new MovedTextBox();
            int x = 0, y = 0;
            FormTools.GetCoordinates(control, ref x, ref y);
            tf.Left = e.X - x;
            tf.Top = e.Y - y;
            control.Controls.Add(tf);
        }

        private void dragLeave(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent("Text"))
            {
                return;
            }
            string str = e.Data.GetData("Text") as string;
            if (str.Equals("MovedEditor"))
            {
                e.Effect = DragDropEffects.None;
            }
        }

        public static void AddEditorDrag(Control control)
        {
            EditorReceiver r = new EditorReceiver(control);
            control.AllowDrop = true;
            control.DragEnter += new DragEventHandler(r.dragEnter);
            control.DragDrop += new DragEventHandler(r.dragDrop);
            //control.DragLeave += new DragEventHandler(r.dragLeave);
        }
    }
}
