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 CellEditingII
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private AxSftMaskLib60.AxSftMask SftMaskDateTime;
        private AxSftMaskLib60.AxSftMask SftMaskAmount;
        public System.Windows.Forms.ToolTip ToolTip1;
        public System.Windows.Forms.Button Command1;
        public System.Windows.Forms.Label Label1;
        private AxSftTreeLib70.AxSftTree axSftTree1;
        public System.Windows.Forms.TextBox Text1;
        private AxSftBoxLib40.AxSftBox SftBox1;
        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.SftMaskDateTime = new AxSftMaskLib60.AxSftMask();
            this.SftMaskAmount = new AxSftMaskLib60.AxSftMask();
            this.ToolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.Command1 = new System.Windows.Forms.Button();
            this.Label1 = new System.Windows.Forms.Label();
            this.axSftTree1 = new AxSftTreeLib70.AxSftTree();
            this.Text1 = new System.Windows.Forms.TextBox();
            this.SftBox1 = new AxSftBoxLib40.AxSftBox();
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.SftMaskDateTime)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.SftMaskAmount)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.SftBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // SftMaskDateTime
            // 
            this.SftMaskDateTime.DataSource = null;
            this.SftMaskDateTime.Location = new System.Drawing.Point(408, 400);
            this.SftMaskDateTime.Name = "SftMaskDateTime";
            this.SftMaskDateTime.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("SftMaskDateTime.OcxState")));
            this.SftMaskDateTime.Size = new System.Drawing.Size(177, 19);
            this.SftMaskDateTime.TabIndex = 14;
            this.SftMaskDateTime.Visible = false;
            // 
            // SftMaskAmount
            // 
            this.SftMaskAmount.DataSource = null;
            this.SftMaskAmount.Location = new System.Drawing.Point(408, 336);
            this.SftMaskAmount.Name = "SftMaskAmount";
            this.SftMaskAmount.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("SftMaskAmount.OcxState")));
            this.SftMaskAmount.Size = new System.Drawing.Size(177, 19);
            this.SftMaskAmount.TabIndex = 12;
            this.SftMaskAmount.Visible = false;
            this.SftMaskAmount.UpDownPress += new AxSftMaskLib60._ISftMaskEvents_UpDownPressEventHandler(this.SftMaskAmount_UpDownPress);
            // 
            // 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", 8F);
            this.Command1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Command1.Location = new System.Drawing.Point(504, 8);
            this.Command1.Name = "Command1";
            this.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Command1.Size = new System.Drawing.Size(81, 25);
            this.Command1.TabIndex = 9;
            this.Command1.Text = "Close";
            this.Command1.Click += new System.EventHandler(this.Command1_Click);
            // 
            // 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", 8F);
            this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
            this.Label1.Location = new System.Drawing.Point(8, 8);
            this.Label1.Name = "Label1";
            this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label1.Size = new System.Drawing.Size(489, 25);
            this.Label1.TabIndex = 10;
            this.Label1.Text = "Cell Editing with separately available ActiveX controls.";
            // 
            // axSftTree1
            // 
            this.axSftTree1.Location = new System.Drawing.Point(8, 40);
            this.axSftTree1.Name = "axSftTree1";
            this.axSftTree1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftTree1.OcxState")));
            this.axSftTree1.Size = new System.Drawing.Size(489, 264);
            this.axSftTree1.TabIndex = 16;
            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);
            // 
            // Text1
            // 
            this.Text1.AcceptsReturn = true;
            this.Text1.AutoSize = false;
            this.Text1.BackColor = System.Drawing.SystemColors.Control;
            this.Text1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.Text1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.Text1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Text1.ForeColor = System.Drawing.SystemColors.WindowText;
            this.Text1.Location = new System.Drawing.Point(8, 312);
            this.Text1.MaxLength = 0;
            this.Text1.Multiline = true;
            this.Text1.Name = "Text1";
            this.Text1.ReadOnly = true;
            this.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Text1.Size = new System.Drawing.Size(488, 177);
            this.Text1.TabIndex = 19;
            this.Text1.TabStop = false;
            this.Text1.Text = "\r\nThis sample can only be opened and modified if SftMask/OCX 6.0 and SftBox/OCX 4" +
                ".0 \r\n(demo or product) are also installed.\r\n\r\n\r\nClick on the SftTree/OCX control" +
                " and hit F1 to access online help for SftTree/OCX 7.0.\r\n";
            // 
            // SftBox1
            // 
            this.SftBox1.Location = new System.Drawing.Point(392, 368);
            this.SftBox1.Name = "SftBox1";
            this.SftBox1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("SftBox1.OcxState")));
            this.SftBox1.Size = new System.Drawing.Size(192, 19);
            this.SftBox1.TabIndex = 27;
            this.SftBox1.Visible = false;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.Command1;
            this.ClientSize = new System.Drawing.Size(592, 497);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.SftBox1,
                                                                          this.Text1,
                                                                          this.axSftTree1,
                                                                          this.SftMaskDateTime,
                                                                          this.SftMaskAmount,
                                                                          this.Command1,
                                                                          this.Label1});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - CellEditingII Sample";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.SftMaskDateTime)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.SftMaskAmount)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.SftBox1)).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) {
            int ItemIndex;
            
            Text1.Text = "In this example, the first column uses a SftMask/OCX Masked Edit control to enter an amount.  Try entering a new amount, then type + or - to access the built-in popup calculator.\r\n\r\nThe second column uses a SftBox/OCX Combo Box control.\r\n\r\nThe last column again uses a SftMask/OCX Masked Edit control to enter a date.  Click on the drop down button to access the popup calendar.";
            
            ItemIndex = axSftTree1.Items.Add("Edit cells\r\nusing SftMask/OCX:");
            axSftTree1.get_Item(ItemIndex).EditIgnore = true;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "\r\nusing SftBox/OCX:";
            axSftTree1.get_Cell(ItemIndex, 2).Text = "and again\r\nusing SftMask/OCX:";
            
            DateTime Dt;

            ItemIndex = axSftTree1.Items.Add("5.33");
            axSftTree1.get_Cell(ItemIndex, 0).TextHAlign = SftTreeHAlignDefaultConstants.halignSftTreeDefaultRight;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Option 1";
            Dt = DateTime.Today.AddDays(-1);
            string s;
            s = "short";
            SftMaskDateTime.Calendar.FormatDate(Dt, ref  s);
            axSftTree1.get_Cell(ItemIndex, 2).Text = s;
            axSftTree1.get_Cell(ItemIndex, 2).DataTag = Dt;
            
            ItemIndex = axSftTree1.Items.Add("122.33");
            axSftTree1.get_Cell(ItemIndex, 0).TextHAlign = SftTreeHAlignDefaultConstants.halignSftTreeDefaultRight;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Option 3";
            Dt = DateTime.Today;
            s = "short";
            SftMaskDateTime.Calendar.FormatDate(Dt, ref  s);
            axSftTree1.get_Cell(ItemIndex, 2).Text = s;
            axSftTree1.get_Cell(ItemIndex, 2).DataTag = Dt;
            
            ItemIndex = axSftTree1.Items.Add("66.05");
            axSftTree1.get_Cell(ItemIndex, 0).TextHAlign = SftTreeHAlignDefaultConstants.halignSftTreeDefaultRight;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Option 2";
            Dt = DateTime.Today.AddDays(1);
            s = "short";
            SftMaskDateTime.Calendar.FormatDate(Dt, ref  s);
            axSftTree1.get_Cell(ItemIndex, 2).Text = s;
            axSftTree1.get_Cell(ItemIndex, 2).DataTag = Dt;
            
            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.RowHeaders.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
            
            axSftTree1.get_Item(0).Selected = true;
        }

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

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemClickEvent e)
        {
            // User clicked on a cell
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeCellText)
                axSftTree1.get_Cell(e.itemIndex, e.colIndex).Edit(0, 0);
        }

        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 == 0)
                ctrl = SftMaskAmount;
            else if (e.editCol == 1)
                ctrl = SftBox1;
            else
                ctrl = SftMaskDateTime;
            
            // 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 == SftMaskAmount) {
                SftMaskAmount.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
                SftMaskAmount.SelStart = 0;
                SftMaskAmount.SelLength = 999;
            } else if (ctrl == SftMaskDateTime) {
                DateTime Dt = (DateTime) axSftTree1.get_Cell(e.editIndex, e.editCol).DataTag;
                SftMaskDateTime.Contents.DateTime = Dt;
            } else {
                SftBox1.Items.Clear();
                int ItemIndex = SftBox1.Items.Add("Option 1");
                SftBox1.get_Cell(ItemIndex, 1).Text = "Description for option 1";
                ItemIndex = SftBox1.Items.Add("Option 2");
                SftBox1.get_Cell(ItemIndex, 1).Text = "Description for option 2";
                ItemIndex = SftBox1.Items.Add("Option 3");
                SftBox1.get_Cell(ItemIndex, 1).Text = "Description for option 3";
                string s = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
                if (SftBox1.Items.Find(s, 0, 0, false, true, true) < 0) {
                    ItemIndex = SftBox1.Items.Add(s);
                    SftBox1.get_Cell(ItemIndex, 1).Text = "Description for " + s;
                }
                SftBox1.Columns.MakeOptimal(0);
                SftBox1.RecalcHorizontalExtent(0);
                SftBox1.Edit.Text = s;
            }
            
            // 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 == SftMaskAmount) {
                // We want these keys just for the amount 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) {
            // We position the control (ActiveX controls must be positioned using the EditInitialized event)
            e.positioned = true;
            
            Control ctrl = (Control) e.vData;
            if (ctrl == SftMaskAmount) {
                SftMaskAmount.Left = e.leftPix;
                SftMaskAmount.Top = e.topPix;
                SftMaskAmount.Width = e.widthPix;
                SftMaskAmount.Height = e.heightPix;
                SftMaskAmount.Enabled = true;
                SftMaskAmount.Visible = true;
                SftMaskAmount.Focus();
            } else if (ctrl == SftMaskDateTime) {
                SftMaskDateTime.Left = e.leftPix;
                SftMaskDateTime.Top = e.topPix;
                SftMaskDateTime.Width = e.widthPix;
                SftMaskDateTime.Height = e.heightPix;
                SftMaskDateTime.Enabled = true;
                SftMaskDateTime.Visible = true;
                SftMaskDateTime.Focus();
            } else {
                SftBox1.Left = e.leftPix;
                SftBox1.Top = e.topPix;
                SftBox1.Width = e.widthPix;
                SftBox1.Height = e.heightPix;
                SftBox1.Enabled = true;
                SftBox1.Visible = true;
                SftBox1.Focus();
                SftBox1.Edit.SetSelection(0, -1);
                SftBox1.DropDown.Dropped = 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;
            Control ctrl = (Control) e.vData;
            if (ctrl == SftMaskAmount) {
                if (!SftMaskAmount.Contents.Valid) {
                    // could validate
                }
                s = SftMaskAmount.TextDisplay;
            } else if (ctrl == SftMaskDateTime) {
                if (!SftMaskDateTime.Contents.Valid) {
                    MessageBox.Show("Please enter a valid date.");
                    e.inputValid = false;
                    return;
                }
                s = SftMaskDateTime.TextDisplay;
            } else
                s = SftBox1.Edit.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
            Control ctrl = (Control) e.vData;
            if (ctrl == SftMaskAmount) {
                SftMaskAmount.Visible = false;
                SftMaskAmount.Enabled = false;
                if (e.saveInput)
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = SftMaskAmount.TextDisplay;
            } else if (ctrl == SftMaskDateTime) {
                SftMaskDateTime.Visible = false;
                SftMaskDateTime.Enabled = false;
                if (e.saveInput) {
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = SftMaskDateTime.TextDisplay;
                    axSftTree1.get_Cell(e.editIndex, e.editCol).DataTag = SftMaskDateTime.Contents.DateTime;
                }
            } else {
                SftBox1.Visible = false;
                SftBox1.Enabled = false;
                if (e.saveInput)
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = SftBox1.Edit.Text;
            }
        }

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

        private void SftMaskAmount_UpDownPress(object sender, AxSftMaskLib60._ISftMaskEvents_UpDownPressEvent e) {
            double amount;
            double increment;
            if (e.counter == 0 || e.counter > 5) {
                amount = Convert.ToDouble(e.field);
                increment = 0.01;
                if (e.counter > 14) increment = 0.1;
                if (e.counter > 23) increment = 1;
                if (e.counter > 52) increment = 10;
                if (e.up)
                    amount = amount + increment;
                else
                    amount = amount - increment;
                if (amount > 99999.99) amount = 99999.99;
                if (amount < -99999.99) amount = -99999.99;
                e.field = amount.ToString("N2");
            }
        }
    }
}