using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using SftTreeLib70;
using AxSftTreeLib70;
using Softelvdm.OLEConvert;

namespace OLEDrag
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        public System.Windows.Forms.PictureBox Picture2;
        public System.Windows.Forms.TextBox Text1;
        public System.Windows.Forms.Button Command1;
        public System.Windows.Forms.PictureBox Picture1;
        public System.Windows.Forms.Label Label5;
        public System.Windows.Forms.Label DropTarget;
        public System.Windows.Forms.PictureBox DropTargetPic;
        public System.Windows.Forms.Label Label4;
        public System.Windows.Forms.Label Label3;
        public System.Windows.Forms.Label Label2;
        public System.Windows.Forms.Label Label1;
        private AxSftTreeLib70.AxSftTree axSftTree1;
        private Softelvdm.OCXHelper.SftHelperComponent sftHelperComponent1;
        private System.ComponentModel.IContainer components;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.axSftTree1 = new AxSftTreeLib70.AxSftTree();
            this.Picture2 = new System.Windows.Forms.PictureBox();
            this.Text1 = new System.Windows.Forms.TextBox();
            this.Command1 = new System.Windows.Forms.Button();
            this.Picture1 = new System.Windows.Forms.PictureBox();
            this.Label5 = new System.Windows.Forms.Label();
            this.DropTarget = new System.Windows.Forms.Label();
            this.DropTargetPic = new System.Windows.Forms.PictureBox();
            this.Label4 = new System.Windows.Forms.Label();
            this.Label3 = new System.Windows.Forms.Label();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).BeginInit();
            this.SuspendLayout();
            // 
            // axSftTree1
            // 
            this.axSftTree1.Location = new System.Drawing.Point(8, 8);
            this.axSftTree1.Name = "axSftTree1";
            this.axSftTree1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftTree1.OcxState")));
            this.axSftTree1.Size = new System.Drawing.Size(257, 329);
            this.axSftTree1.TabIndex = 12;
            this.axSftTree1.DragStarting += new AxSftTreeLib70._DSftTreeEvents_DragStartingEventHandler(this.axSftTree1_DragStarting);
            this.axSftTree1.OLEStartDrag += new AxSftTreeLib70._DSftTreeEvents_OLEStartDragEventHandler(this.axSftTree1_OLEStartDrag);
            this.axSftTree1.OLEDragDrop += new AxSftTreeLib70._DSftTreeEvents_OLEDragDropEventHandler(this.axSftTree1_OLEDragDrop);
            // 
            // Picture2
            // 
            this.Picture2.BackColor = System.Drawing.SystemColors.Control;
            this.Picture2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Picture2.Cursor = System.Windows.Forms.Cursors.Default;
            this.Picture2.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Picture2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Picture2.Image = ((System.Drawing.Bitmap)(resources.GetObject("Picture2.Image")));
            this.Picture2.Location = new System.Drawing.Point(302, 165);
            this.Picture2.Name = "Picture2";
            this.Picture2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Picture2.Size = new System.Drawing.Size(25, 17);
            this.Picture2.TabIndex = 18;
            this.Picture2.TabStop = false;
            this.Picture2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Picture2_MouseDown);
            // 
            // Text1
            // 
            this.Text1.AcceptsReturn = true;
            this.Text1.AutoSize = false;
            this.Text1.BackColor = System.Drawing.SystemColors.Window;
            this.Text1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.Text1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Text1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.Text1.Location = new System.Drawing.Point(336, 165);
            this.Text1.MaxLength = 0;
            this.Text1.Name = "Text1";
            this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Text1.Size = new System.Drawing.Size(96, 19);
            this.Text1.TabIndex = 16;
            this.Text1.Text = "Text1";
            this.Text1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Text1_MouseDown);
            // 
            // Command1
            // 
            this.Command1.BackColor = System.Drawing.SystemColors.Control;
            this.Command1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Command1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Command1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command1.Location = new System.Drawing.Point(366, 8);
            this.Command1.Name = "Command1";
            this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command1.Size = new System.Drawing.Size(73, 27);
            this.Command1.TabIndex = 13;
            this.Command1.Text = "Close";
            this.Command1.Click += new System.EventHandler(this.Command1_Click);
            // 
            // Picture1
            // 
            this.Picture1.BackColor = System.Drawing.SystemColors.Control;
            this.Picture1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.Picture1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Picture1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Picture1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Picture1.Image = ((System.Drawing.Bitmap)(resources.GetObject("Picture1.Image")));
            this.Picture1.Location = new System.Drawing.Point(270, 165);
            this.Picture1.Name = "Picture1";
            this.Picture1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Picture1.Size = new System.Drawing.Size(25, 17);
            this.Picture1.TabIndex = 15;
            this.Picture1.TabStop = false;
            this.Picture1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Picture1_MouseDown);
            // 
            // Label5
            // 
            this.Label5.BackColor = System.Drawing.SystemColors.Control;
            this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label5.Location = new System.Drawing.Point(6, 341);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(433, 49);
            this.Label5.TabIndex = 22;
            this.Label5.Text = "Drag items from the tree control to the drop target shown above.  Or drag the ima" +
                "ges or text from the drag sources above.  You can also use Windows Explorer as a" +
                " drag source or simply drag && drop within the tree control.";
            // 
            // DropTarget
            // 
            this.DropTarget.AllowDrop = true;
            this.DropTarget.BackColor = System.Drawing.SystemColors.Control;
            this.DropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.DropTarget.Cursor = System.Windows.Forms.Cursors.Default;
            this.DropTarget.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.DropTarget.ForeColor = System.Drawing.SystemColors.ControlText;
            this.DropTarget.Location = new System.Drawing.Point(294, 301);
            this.DropTarget.Name = "DropTarget";
            this.DropTarget.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.DropTarget.Size = new System.Drawing.Size(145, 25);
            this.DropTarget.TabIndex = 21;
            this.DropTarget.DragEnter += new System.Windows.Forms.DragEventHandler(this.DropTarget_DragEnter);
            this.DropTarget.DragDrop += new System.Windows.Forms.DragEventHandler(this.DropTarget_DragDrop);
            // 
            // DropTargetPic
            // 
            this.DropTargetPic.Cursor = System.Windows.Forms.Cursors.Default;
            this.DropTargetPic.Location = new System.Drawing.Point(270, 301);
            this.DropTargetPic.Name = "DropTargetPic";
            this.DropTargetPic.Size = new System.Drawing.Size(25, 25);
            this.DropTargetPic.TabIndex = 23;
            this.DropTargetPic.TabStop = false;
            this.DropTargetPic.DragEnter += new System.Windows.Forms.DragEventHandler(this.DropTargetPic_DragEnter);
            this.DropTargetPic.DragDrop += new System.Windows.Forms.DragEventHandler(this.DropTargetPic_DragDrop);
            // 
            // Label4
            // 
            this.Label4.BackColor = System.Drawing.SystemColors.Control;
            this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label4.Location = new System.Drawing.Point(270, 240);
            this.Label4.Name = "Label4";
            this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label4.Size = new System.Drawing.Size(161, 56);
            this.Label4.TabIndex = 20;
            this.Label4.Text = "Drag items from the tree control to the drop target below.  In this example, only" +
                " one item is shown.";
            // 
            // Label3
            // 
            this.Label3.BackColor = System.Drawing.SystemColors.Control;
            this.Label3.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label3.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label3.Location = new System.Drawing.Point(270, 213);
            this.Label3.Name = "Label3";
            this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label3.Size = new System.Drawing.Size(161, 25);
            this.Label3.TabIndex = 19;
            this.Label3.Text = "Drop Target";
            // 
            // Label2
            // 
            this.Label2.BackColor = System.Drawing.SystemColors.Control;
            this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label2.Location = new System.Drawing.Point(270, 53);
            this.Label2.Name = "Label2";
            this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label2.Size = new System.Drawing.Size(161, 25);
            this.Label2.TabIndex = 17;
            this.Label2.Text = "Drag Sources";
            // 
            // Label1
            // 
            this.Label1.BackColor = System.Drawing.SystemColors.Control;
            this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label1.Location = new System.Drawing.Point(270, 85);
            this.Label1.Name = "Label1";
            this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label1.Size = new System.Drawing.Size(161, 73);
            this.Label1.TabIndex = 14;
            this.Label1.Text = "Drag the images or text shown below to the tree control or use Windows Explorer t" +
                "o drag one or more file names and drop them on the tree control.";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.Command1;
            this.ClientSize = new System.Drawing.Size(445, 395);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Picture2,
                                                                          this.Text1,
                                                                          this.Command1,
                                                                          this.Picture1,
                                                                          this.Label5,
                                                                          this.DropTarget,
                                                                          this.DropTargetPic,
                                                                          this.Label4,
                                                                          this.Label3,
                                                                          this.Label2,
                                                                          this.Label1,
                                                                          this.axSftTree1});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - DragDrop Sample";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).EndInit();
            this.ResumeLayout(false);

        }
		#endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        //--locate-marker--

        private void Command1_Click(object sender, System.EventArgs e) {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e) {
            DropTargetPic.AllowDrop = true;
            int i;
            axSftTree1.Items.Add("Item 0");
            i = axSftTree1.Items.Add("Item 1");
            axSftTree1.get_Item(i).Level = 1;
            i = axSftTree1.Items.Add("Item 2");
            axSftTree1.get_Item(i).Level = 2;
            i = axSftTree1.Items.Add("Item 3");
            axSftTree1.get_Item(i).Level = 1;
        }

        private void axSftTree1_DragStarting(object sender, AxSftTreeLib70._DSftTreeEvents_DragStartingEvent e) {

            axSftTree1.CancelMode();    // .NET controls drag&drop, so tree control needs to cancel

            // This section demonstrates drag (&drop) using the .NET DoDragDrop method
            // THIS CODE IS ONLY USED IF DRAGMETHOD IS SET TO  ( 1 - Manual ) or SftTreeDragMethodConstants.dragSftTreeManual

            int curr = axSftTree1.Items.Current;
            // cell text
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Text, axSftTree1.get_Cell(curr, 0).Text);

            // item picture
            SftPictureObject img = null;
            if (axSftTree1.get_Item(curr).Image.Type == SftPictureTypeConstants.sftTypeIDispatch) {
                img = axSftTree1.get_Item(curr).Image;
            } else {
                if (axSftTree1.get_Item(curr).Expanded)
                    img = axSftTree1.Items.ItemImageExpanded;
                else if (axSftTree1.get_Item(curr).DependentAllCount > 0)
                    img = axSftTree1.Items.ItemImageExpandable;
                else
                    img = axSftTree1.Items.ItemImageLeaf;
            }
            Image sendingImage = OLECvt.ToImage(img.Picture);
            data.SetData(DataFormats.Bitmap, true, sendingImage);
            DoDragDrop(data, DragDropEffects.Copy);
        }

        private void axSftTree1_OLEStartDrag(object sender, AxSftTreeLib70._DSftTreeEvents_OLEStartDragEvent e) {

            // This section demonstrates drag&drop) using OLE mechanisms built into this control
            // THIS CODE IS ONLY USED IF DRAGMETHOD IS SET TO  ( 3 - OLEDrag ) or SftTreeDragMethodConstants.dragSftTreeOLE

            int curr = axSftTree1.Items.Current;
            // cell text
            e.data.SetData(axSftTree1.get_Cell(curr, 0).Text, SftOLEClipboardConstants.sftCFText);
            // item picture
            SftPictureObject img = null;
            if (axSftTree1.get_Item(curr).Image.Type == SftPictureTypeConstants.sftTypeIDispatch) {
                img = axSftTree1.get_Item(curr).Image;
            } else {
                if (axSftTree1.get_Item(curr).Expanded)
                    img = axSftTree1.Items.ItemImageExpanded;
                else if (axSftTree1.get_Item(curr).DependentAllCount > 0)
                    img = axSftTree1.Items.ItemImageExpandable;
                else
                    img = axSftTree1.Items.ItemImageLeaf;
            }
            e.data.SetData(img.Picture, SftOLEClipboardConstants.sftCFDIB);
            e.data.SetData(img.Picture, SftOLEClipboardConstants.sftCFBitmap);
            e.allowedEffects = 3;
        }

        private void DropTarget_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.Text, true) || e.Data.GetDataPresent(DataFormats.Bitmap, true))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void DropTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
            String[] arrayOfFormats = e.Data.GetFormats(true); // informational
            if (e.Data.GetDataPresent(DataFormats.Text, true))
                DropTarget.Text = (string) e.Data.GetData(DataFormats.Text, true);
            if (e.Data.GetDataPresent(DataFormats.Bitmap, true))
                DropTargetPic.Image = (Image) e.Data.GetData(DataFormats.Bitmap, true);
        }

        private void DropTargetPic_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.Bitmap, true))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void DropTargetPic_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
            String[] arrayOfFormats = e.Data.GetFormats(true); // informational
            if (e.Data.GetDataPresent(DataFormats.Bitmap, true)) {
                object o = e.Data.GetData(DataFormats.Bitmap, true);
                DropTargetPic.Image = (Image) o;
            }
        }

        private void axSftTree1_OLEDragDrop(object sender, AxSftTreeLib70._DSftTreeEvents_OLEDragDropEvent e) {

            // get horizontal extent and width of column 0
            int horzExtent = axSftTree1.Items.HorizontalExtentPix;
            int firstColumnWidth = axSftTree1.get_Column(0).WidthPix;
            
            int insertAt = axSftTree1.Items.DropHighlight;
            if (insertAt < 0) return;
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFText)) {
                short lvl = axSftTree1.get_Item(insertAt).Level;
                string str = (string) e.data.GetData(SftOLEClipboardConstants.sftCFText);
                int newItem = axSftTree1.Items.Insert(insertAt + 1, str);
                axSftTree1.get_Item(newItem).Level = (short) (lvl + 1);
            }
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFDIB)) {
                axSftTree1.get_Cell(insertAt, 0).Image.Picture = (stdole.IPictureDisp)e.data.GetData((short) SftOLEClipboardConstants.sftCFDIB);
                axSftTree1.get_Cell(insertAt, 0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            }
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFBitmap)) {
                axSftTree1.get_Cell(insertAt, 0).Image.Picture = (stdole.IPictureDisp)e.data.GetData((short) SftOLEClipboardConstants.sftCFBitmap);
                axSftTree1.get_Cell(insertAt, 0).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            }
            if (e.data.GetFormat((short) SftOLEClipboardConstants.sftCFFiles)) {
                axSftTree1.BulkUpdate = true;
                short lvl = axSftTree1.get_Item(insertAt).Level;
                int newItem = insertAt + 1;
                for (int i = e.data.Files.Count ; i >= 1 ; --i) {
                    newItem = axSftTree1.Items.Insert(newItem, e.data.Files[i]);
                    axSftTree1.get_Item(newItem).Level = (short) (lvl + 1);
                }
                axSftTree1.BulkUpdate = false;
            }

            // make horizontal extent and width of column 0 wider than previous
            // setting, but never smaller
            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
            
            if (horzExtent > axSftTree1.Items.HorizontalExtent)
                axSftTree1.Items.HorizontalExtent = horzExtent;
            if (firstColumnWidth > axSftTree1.get_Column(0).WidthPix)
                axSftTree1.get_Column(0).WidthPix = firstColumnWidth;
        }

        private void Picture1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Bitmap, true, Picture1.Image);
            DoDragDrop(data, DragDropEffects.Copy);
        }

        private void Picture2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Dib, true, Picture2.Image);
            DoDragDrop(data, DragDropEffects.Copy);
        }

        private void Text1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
            System.Windows.Forms.DataObject data = new System.Windows.Forms.DataObject();
            data.SetData(DataFormats.Text, Text1.Text);
            DoDragDrop(data, DragDropEffects.Copy);
        }

    }
}