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 CellEditing
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        public System.Windows.Forms.ComboBox Combo1;
        public System.Windows.Forms.TextBox Text1;
        public System.Windows.Forms.ToolTip ToolTip1;
        public System.Windows.Forms.Button Command1;
        public System.Windows.Forms.Label Label5;
        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.Combo1 = new System.Windows.Forms.ComboBox();
            this.Text1 = new System.Windows.Forms.TextBox();
            this.ToolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.Command1 = new System.Windows.Forms.Button();
            this.Label5 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.axSftTree1 = new AxSftTreeLib70.AxSftTree();
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).BeginInit();
            this.SuspendLayout();
            // 
            // Combo1
            // 
            this.Combo1.BackColor = System.Drawing.SystemColors.Window;
            this.Combo1.Cursor = System.Windows.Forms.Cursors.Default;
            this.Combo1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Combo1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.Combo1.Location = new System.Drawing.Point(502, 143);
            this.Combo1.Name = "Combo1";
            this.Combo1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Combo1.Size = new System.Drawing.Size(81, 21);
            this.Combo1.TabIndex = 12;
            this.Combo1.Text = "Combo1";
            this.Combo1.Visible = false;
            // 
            // Text1
            // 
            this.Text1.AcceptsReturn = true;
            this.Text1.AutoSize = false;
            this.Text1.BackColor = System.Drawing.SystemColors.Window;
            this.Text1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            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(502, 111);
            this.Text1.MaxLength = 0;
            this.Text1.Name = "Text1";
            this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Text1.Size = new System.Drawing.Size(81, 20);
            this.Text1.TabIndex = 11;
            this.Text1.Text = "Text1";
            this.Text1.Visible = false;
            // 
            // Command1
            // 
            this.Command1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
            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(502, 7);
            this.Command1.Name = "Command1";
            this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command1.Size = new System.Drawing.Size(81, 25);
            this.Command1.TabIndex = 8;
            this.Command1.Text = "Close";
            this.Command1.Click += new System.EventHandler(this.Command1_Click);
            // 
            // 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.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label5.Location = new System.Drawing.Point(502, 191);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(81, 201);
            this.Label5.TabIndex = 13;
            this.Label5.Text = "Click on the SftTree/OCX control and hit F1 to access online help for SftTree/OCX" +
                " 6.0";
            this.Label5.Visible = false;
            // 
            // 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(6, 7);
            this.Label1.Name = "Label1";
            this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label1.Size = new System.Drawing.Size(489, 16);
            this.Label1.TabIndex = 9;
            this.Label1.Text = "Simple Cell Editing Sample with edit controls and combo boxes.";
            // 
            // axSftTree1
            // 
            this.axSftTree1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.axSftTree1.Location = new System.Drawing.Point(8, 24);
            this.axSftTree1.Name = "axSftTree1";
            this.axSftTree1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftTree1.OcxState")));
            this.axSftTree1.Size = new System.Drawing.Size(489, 553);
            this.axSftTree1.TabIndex = 14;
            this.axSftTree1.ItemDblClick += new AxSftTreeLib70._DSftTreeEvents_ItemDblClickEventHandler(this.axSftTree1_ItemDblClick);
            this.axSftTree1.EditAllowed += new AxSftTreeLib70._DSftTreeEvents_EditAllowedEventHandler(this.axSftTree1_EditAllowed);
            this.axSftTree1.EditEnding += new AxSftTreeLib70._DSftTreeEvents_EditEndingEventHandler(this.axSftTree1_EditEnding);
            this.axSftTree1.ItemClick += new AxSftTreeLib70._DSftTreeEvents_ItemClickEventHandler(this.axSftTree1_ItemClick);
            this.axSftTree1.EditInitializing += new AxSftTreeLib70._DSftTreeEvents_EditInitializingEventHandler(this.axSftTree1_EditInitializing);
            this.axSftTree1.ToolTipVScroll += new AxSftTreeLib70._DSftTreeEvents_ToolTipVScrollEventHandler(this.axSftTree1_ToolTipVScroll);
            this.axSftTree1.EditValidate += new AxSftTreeLib70._DSftTreeEvents_EditValidateEventHandler(this.axSftTree1_EditValidate);
            this.axSftTree1.EditInitialized += new AxSftTreeLib70._DSftTreeEvents_EditInitializedEventHandler(this.axSftTree1_EditInitialized);
            this.axSftTree1.EditNavigating += new AxSftTreeLib70._DSftTreeEvents_EditNavigatingEventHandler(this.axSftTree1_EditNavigating);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.Command1;
            this.ClientSize = new System.Drawing.Size(589, 590);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.axSftTree1,
                                                                          this.Combo1,
                                                                          this.Text1,
                                                                          this.Command1,
                                                                          this.Label5,
                                                                          this.Label1});
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - CellEditing 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) {
            int ItemIndex;

            ItemIndex = axSftTree1.Items.Add("Click on a cell to edit");
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Use Tab/Return keys";
            axSftTree1.get_Cell(ItemIndex, 2).Text = "Use arrow keys";
            
            ItemIndex = axSftTree1.Items.Add("This sample supports cell navigation");
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Ctrl+Home and Ctrl+End";
            
            int i;
            SftTreeItem Item;
            SftTreeCell Cell;

            for (i = 1 ; i < 50 ; ++i) {
                
                ItemIndex = axSftTree1.Items.Add("An item");
                axSftTree1.get_Cell(ItemIndex, 1).Text = "2nd Column";
                axSftTree1.get_Cell(ItemIndex, 2).Text = "3rd Column";
                
                ItemIndex = axSftTree1.Items.Add("Another item");
                Item = axSftTree1.get_Item(ItemIndex);
                Item.Level = 1;
                Item.get_Cell(1).Text = "2nd Column";
                Item.get_Cell(2).Text = "3rd Column";
                
                ItemIndex = axSftTree1.Items.Add("This item can't be edited");
                Item = axSftTree1.get_Item(ItemIndex);
                Item.Level = 2;
                Item.EditIgnore = true;
                Item.get_Cell(0).Image.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                Item.get_Cell(1).Text = "2nd Column (can't edit this item)";
                Item.get_Cell(2).Text = "3rd Column (can't edit this item)";
                
                ItemIndex = axSftTree1.Items.Add("A fourth item");
                Item = axSftTree1.get_Item(ItemIndex);
                Item.Level = 1;
                Cell = axSftTree1.get_Cell(ItemIndex, 1);
                Cell.Text = "This cell can't be edited";
                Cell.EditIgnore = true;
                Cell.Image.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                axSftTree1.get_Cell(ItemIndex, 2).Text = "3rd Column";
                
            }
            
            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.RowHeaders.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
        }

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemClickEvent e) {
            SftPictureObject Img;
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeCellText)
                // User clicked on a cell
                axSftTree1.get_Cell(e.itemIndex, e.colIndex).Edit(0, 0);
            else if (area == SftTreeAreaTypeConstants.constSftTreeCellGraphic) {
                // check if check box - toggle
                Img = axSftTree1.get_Cell(e.itemIndex, e.colIndex).Image;
                if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxNo)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                else if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxYes)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxNo;
            }
        }

        private void axSftTree1_ItemDblClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemDblClickEvent e) {
            SftPictureObject Img;
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeColumnRes)
                axSftTree1.get_Column(e.colIndex).MakeOptimal();
            else if (area == SftTreeAreaTypeConstants.constSftTreeCellGraphic) {
                // check if check box - toggle
                Img = axSftTree1.get_Cell(e.itemIndex, e.colIndex).Image;
                if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxNo)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                else if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxYes)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxNo;
            }
        }

        private void axSftTree1_ToolTipVScroll(object sender, AxSftTreeLib70._DSftTreeEvents_ToolTipVScrollEvent e) {
            e.text = "Item " + e.ItemIndex.ToString() + " - " + e.text;
        }

        private void axSftTree1_EditAllowed(object sender, AxSftTreeLib70._DSftTreeEvents_EditAllowedEvent e) {
            // Last chance to suppress cell editing for a cell
            //if (e.itemIndex == 1 && e.colIndex == 1)
            //    e.allowed = false;
        }

        private void axSftTree1_EditInitializing(object sender, AxSftTreeLib70._DSftTreeEvents_EditInitializingEvent e) {
            System.Windows.Forms.Control ctrl;
            
            // Choose a control based on current column being edited
            if (e.editCol == 1)
                ctrl = Combo1;
            else
                ctrl = Text1;
            
            // LeftPix/TopPix/WidthPix/HeightPix describes the current cell area
            // we need to return the position and size needed for editing.
            // In this example, we use the height of the control on the form
            // and center it over the cell.
            
            e.topPix = e.topPix + (e.heightPix - ctrl.Height) / 2;
            e.heightPix = ctrl.Height;
            
            // Set the text in the control used for cell editing and
            // set other control-specific properties
            if (ctrl == Text1) {
                Text1.Width = 200;
                Text1.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
                Text1.SelectionStart = 0;
                Text1.SelectionLength = 999;
            } else {
                Combo1.Items.Clear();
                Combo1.Items.Add("Option 1");
                Combo1.Items.Add("Option 2");
                Combo1.Items.Add("Option 3");
                Combo1.Items.Add(axSftTree1.get_Cell(e.editIndex, e.editCol).Text);
                Combo1.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
            }
            
            // Return the control's window handle
            e.window = (int) ctrl.Handle;
            e.vData = ctrl;

            // Define navigation keys
            // VK_TAB
            axSftTree1.CellEditIntercept(9, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_RETURN
            axSftTree1.CellEditIntercept(13, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_HOME
            axSftTree1.CellEditIntercept(36, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar);
            // VK_END
            axSftTree1.CellEditIntercept(35, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar);
            
            if (ctrl == Text1) {
                // We want these keys just for the edit control.
                // VK_UP
                axSftTree1.CellEditIntercept(38, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
                // VK_DOWN
                axSftTree1.CellEditIntercept(40, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            }
        }

        private void axSftTree1_EditInitialized(object sender, AxSftTreeLib70._DSftTreeEvents_EditInitializedEvent e) {
            // Show the combo box dropdown portion
            if (e.vData == Combo1) {
                // We're taking over positioning, so we can drop down the dropdown portion of the control
                e.positioned = true;
                Combo1.Left = e.leftPix;
                Combo1.Top = e.topPix;
                Combo1.Width = e.widthPix;
                Combo1.Height = e.heightPix;
                Combo1.Enabled = true;
                Combo1.Visible = true;
                Combo1.Focus();
                Combo1.DroppedDown = true;
            }
        }

        private void axSftTree1_EditNavigating(object sender, AxSftTreeLib70._DSftTreeEvents_EditNavigatingEvent e) {
            // Process key pressed
            axSftTree1.EditNavigate(e.key, e.shift);
        }

        private void axSftTree1_EditValidate(object sender, AxSftTreeLib70._DSftTreeEvents_EditValidateEvent e) {
            // Validate the new cell contents
            string s;
            if (e.vData == Text1)
                s = Text1.Text;
            else
                s = Combo1.Text;
            s = s.Trim();
            if (s.Length <= 0) {
                MessageBox.Show("Just to demonstrate data input validation, this example rejects empty cells.  Please enter some data.");
                e.inputValid = false;
            }
        }

        private void axSftTree1_EditEnding(object sender, AxSftTreeLib70._DSftTreeEvents_EditEndingEvent e) {
            // Save the new cell contents
            if (e.saveInput) {
                if (e.vData == Text1) {
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = Text1.Text;
                } else {
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = Combo1.Text;
                }
            }
            Control ctrl = (Control) e.vData;
            ctrl.Visible = false;
            ctrl.Enabled = false;
        }

    }
}