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

using SftDirectoryLib25;
using AxSftDirectoryLib25;
using Softelvdm.OLEConvert;

namespace SimpleExplorer
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button buttonUp;
        private System.Windows.Forms.Button buttonProp;
        private System.Windows.Forms.StatusBar statusBar;
        private System.Windows.Forms.Panel panel1;
        private AxSftDirectoryLib25.AxSftDirectory axSftDirectoryLeft;
        private System.Windows.Forms.Splitter splitter1;
        private AxSftDirectoryLib25.AxSftDirectory axSftDirectoryRight;
        private System.Windows.Forms.Button buttonNewFolder;
        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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.label1 = new System.Windows.Forms.Label();
            this.buttonUp = new System.Windows.Forms.Button();
            this.buttonProp = new System.Windows.Forms.Button();
            this.statusBar = new System.Windows.Forms.StatusBar();
            this.panel1 = new System.Windows.Forms.Panel();
            this.axSftDirectoryRight = new AxSftDirectoryLib25.AxSftDirectory();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.axSftDirectoryLeft = new AxSftDirectoryLib25.AxSftDirectory();
            this.buttonNewFolder = new System.Windows.Forms.Button();
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.axSftDirectoryRight)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftDirectoryLeft)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(232, 32);
            this.label1.TabIndex = 0;
            this.label1.Text = "Simple Explorer Sample";
            // 
            // buttonUp
            // 
            this.buttonUp.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.buttonUp.Location = new System.Drawing.Point(248, 8);
            this.buttonUp.Name = "buttonUp";
            this.buttonUp.Size = new System.Drawing.Size(32, 24);
            this.buttonUp.TabIndex = 2;
            this.buttonUp.Text = "Up";
            this.buttonUp.Click += new System.EventHandler(this.buttonUp_Click);
            // 
            // buttonProp
            // 
            this.buttonProp.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.buttonProp.Location = new System.Drawing.Point(288, 8);
            this.buttonProp.Name = "buttonProp";
            this.buttonProp.Size = new System.Drawing.Size(72, 24);
            this.buttonProp.TabIndex = 3;
            this.buttonProp.Text = "Properties";
            this.buttonProp.Click += new System.EventHandler(this.buttonProp_Click);
            // 
            // statusBar
            // 
            this.statusBar.Location = new System.Drawing.Point(0, 438);
            this.statusBar.Name = "statusBar";
            this.statusBar.Size = new System.Drawing.Size(704, 16);
            this.statusBar.TabIndex = 5;
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.axSftDirectoryRight);
            this.panel1.Controls.Add(this.splitter1);
            this.panel1.Controls.Add(this.axSftDirectoryLeft);
            this.panel1.Location = new System.Drawing.Point(0, 40);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(704, 392);
            this.panel1.TabIndex = 6;
            // 
            // axSftDirectoryRight
            // 
            this.axSftDirectoryRight.Dock = System.Windows.Forms.DockStyle.Fill;
            this.axSftDirectoryRight.Location = new System.Drawing.Point(208, 0);
            this.axSftDirectoryRight.Name = "axSftDirectoryRight";
            this.axSftDirectoryRight.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftDirectoryRight.OcxState")));
            this.axSftDirectoryRight.Size = new System.Drawing.Size(492, 388);
            this.axSftDirectoryRight.TabIndex = 1;
            this.axSftDirectoryRight.KeyDownEvent += new AxSftDirectoryLib25._ISftDirectoryEvents_KeyDownEventHandler(this.axSftDirectoryRight_KeyDownEvent);
            this.axSftDirectoryRight.SelectionFinal += new System.EventHandler(this.axSftDirectoryRight_SelectionFinal);
            this.axSftDirectoryRight.Opening += new AxSftDirectoryLib25._ISftDirectoryEvents_OpeningEventHandler(this.axSftDirectoryRight_Opening);
            // 
            // splitter1
            // 
            this.splitter1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.splitter1.Location = new System.Drawing.Point(200, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(8, 388);
            this.splitter1.TabIndex = 12;
            this.splitter1.TabStop = false;
            // 
            // axSftDirectoryLeft
            // 
            this.axSftDirectoryLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.axSftDirectoryLeft.Location = new System.Drawing.Point(0, 0);
            this.axSftDirectoryLeft.Name = "axSftDirectoryLeft";
            this.axSftDirectoryLeft.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftDirectoryLeft.OcxState")));
            this.axSftDirectoryLeft.Size = new System.Drawing.Size(200, 388);
            this.axSftDirectoryLeft.TabIndex = 0;
            this.axSftDirectoryLeft.KeyDownEvent += new AxSftDirectoryLib25._ISftDirectoryEvents_KeyDownEventHandler(this.axSftDirectoryLeft_KeyDownEvent);
            this.axSftDirectoryLeft.SelectionFinal += new System.EventHandler(this.axSftDirectoryLeft_SelectionFinal);
            // 
            // buttonNewFolder
            // 
            this.buttonNewFolder.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.buttonNewFolder.Location = new System.Drawing.Point(368, 8);
            this.buttonNewFolder.Name = "buttonNewFolder";
            this.buttonNewFolder.Size = new System.Drawing.Size(72, 24);
            this.buttonNewFolder.TabIndex = 4;
            this.buttonNewFolder.Text = "New Folder";
            this.buttonNewFolder.Click += new System.EventHandler(this.buttonNewFolder_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(704, 454);
            this.Controls.Add(this.buttonNewFolder);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.statusBar);
            this.Controls.Add(this.buttonProp);
            this.Controls.Add(this.buttonUp);
            this.Controls.Add(this.label1);
            this.MinimumSize = new System.Drawing.Size(300, 250);
            this.Name = "Form1";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
            this.Text = "SimpleExplorer Sample - Softel vdm, Inc.";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.axSftDirectoryRight)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftDirectoryLeft)).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 Form1_Load(object sender, System.EventArgs e) {
            UpdateLeftButtons();
            UpdateRightButtons();
            axSftDirectoryRight.MakeColumnsOptimal();
        }

        private void UpdateLeftButtons()
        {
            // Update the buttons dependent on the left tree
            SftDirectoryFolder f;
            // Enable/disable Up button
            f = axSftDirectoryLeft.CurrentFolder;
            if (f != null) 
                f = f.Parent;
            buttonUp.Enabled = (f != null);
        }

        private void UpdateRightButtons()
        {
            // Update the buttons dependent on the right side detaillist
            SftDirectoryFolder f;
            // Enable/disable properties button
            f = axSftDirectoryRight.CurrentFolder;
            buttonProp.Enabled = false;
            if (axSftDirectoryRight.SelectionCount > 0 && f != null) {
                if (f.Can("properties"))
                    buttonProp.Enabled = true;
            }

            // Enable/disable New Folder button
            f = axSftDirectoryRight.RootFolder;
            buttonNewFolder.Enabled = false;
            if (f != null) {
                if (f.Can("NewFolder"))
                    buttonNewFolder.Enabled = true;
            }
        }

        private void axSftDirectoryLeft_SelectionFinal(object sender, System.EventArgs e) {
            // If the selection in the left tree changes, update right side detaillist
            UpdateLeftButtons();
            axSftDirectoryRight.Clear();
            axSftDirectoryRight.CancelMode();
            axSftDirectoryRight.Refresh();
            if (axSftDirectoryLeft.CurrentFolder != null)
                axSftDirectoryRight.TopMostFolderIDL = axSftDirectoryLeft.CurrentFolder.ItemIDList;
            else
                axSftDirectoryRight.TopMostFolderSpecial = SftDirectorySpecialFolderConstants.specialSftDirectoryEmpty;
        }

        private void axSftDirectoryRight_Opening(object sender, AxSftDirectoryLib25._ISftDirectoryEvents_OpeningEvent e) {
            SftDirectoryFolder f;
            String IDL;
            // we're about to open a file/folder.  If we can add the folder on the
            // left side, that means we don't need to open it in a separate window
            
            IDL = e.folder.ItemIDList; // the folder we are looking for
            // this will add the complete hierarchy down to the folder in
            // left tree (if possible)
            f = axSftDirectoryLeft.get_FolderUsingIDL(IDL, false);
            // this will locate the exact item if we just added it
            f = axSftDirectoryLeft.get_FolderUsingIDL(IDL, true);
            if (f != null) { 
                // The folder has been added on the left side
                f.MakeCurrent(); // make it the current item
                f.MakeVisible(); // make sure it's scrolled into view
                e.allow = false; // no need to open it
            }
        }

        private void axSftDirectoryRight_SelectionFinal(object sender, System.EventArgs e) {
            UpdateRightButtons();
            if (axSftDirectoryRight.CurrentFolder != null)
                statusBar.Text = axSftDirectoryRight.CurrentFolder.InfoTip;
            else
                statusBar.Text = "";
        }

        private void buttonUp_Click(object sender, System.EventArgs e) {
            // Move up on level in left side tree control
            SftDirectoryFolder f;
            f = axSftDirectoryLeft.CurrentFolder;
            if (f != null) {
                f = f.Parent;
                if (f != null) {
                    f.MakeCurrent(); // make it the current item
                    f.MakeVisible(); // make sure it's scrolled into view
                }
            }
        }

        private void buttonProp_Click(object sender, System.EventArgs e) {
            // Display properties dialog for the currently selected item
            SftDirectoryFolder f;
            f = axSftDirectoryRight.CurrentFolder;
            if (axSftDirectoryRight.SelectionCount > 0 && f != null)
                f.Do("properties");
        }

        private void buttonNewFolder_Click(object sender, System.EventArgs e) {
            // Create a new folder
            SftDirectoryFolder f;
            f = axSftDirectoryRight.RootFolder;
            if (f != null) {
                try {
                    f.Do("NewFolder");
                } catch  { }
            }
        }

        private void axSftDirectoryLeft_KeyDownEvent(object sender, AxSftDirectoryLib25._ISftDirectoryEvents_KeyDownEvent e) {
            // F5 automatically reloads left side, but we also want right side updated
            if (e.keyCode == (short) Keys.F5) axSftDirectoryRight.Reload(true, true);
        }

        private void axSftDirectoryRight_KeyDownEvent(object sender, AxSftDirectoryLib25._ISftDirectoryEvents_KeyDownEvent e) {
            // F5 automatically reloads right side, but we also want left side updated
            if (e.keyCode == (short) Keys.F5) axSftDirectoryLeft.Reload(true, true);
        }

    }
}