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 BookTable
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.CheckBox uglyCheckBox;
        private System.Windows.Forms.CheckBox plusMinusCheckBox;
        private System.Windows.Forms.ImageList itemImageList;
        private System.Windows.Forms.ImageList buttonImageList;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.ImageList plusMinusImageList;
        private System.Windows.Forms.Label label1;
        public System.Windows.Forms.Label Label5;
        private System.Windows.Forms.MenuItem menuItem1;
        private AxSftTreeLib70.AxSftTree axSftTree1;
        private System.Windows.Forms.ContextMenu ItemContextMenu;
        private System.Windows.Forms.MenuItem menuItem2;
        private System.Windows.Forms.MenuItem menuItem3;
        private System.Windows.Forms.MenuItem menuItem4;
        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.uglyCheckBox = new System.Windows.Forms.CheckBox();
            this.plusMinusCheckBox = new System.Windows.Forms.CheckBox();
            this.itemImageList = new System.Windows.Forms.ImageList(this.components);
            this.buttonImageList = new System.Windows.Forms.ImageList(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.plusMinusImageList = new System.Windows.Forms.ImageList(this.components);
            this.label1 = new System.Windows.Forms.Label();
            this.Label5 = new System.Windows.Forms.Label();
            this.ItemContextMenu = new System.Windows.Forms.ContextMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.axSftTree1 = new AxSftTreeLib70.AxSftTree();
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.axSftTree1)).BeginInit();
            this.SuspendLayout();
            // 
            // uglyCheckBox
            // 
            this.uglyCheckBox.Location = new System.Drawing.Point(216, 424);
            this.uglyCheckBox.Name = "uglyCheckBox";
            this.uglyCheckBox.Size = new System.Drawing.Size(189, 23);
            this.uglyCheckBox.TabIndex = 10;
            this.uglyCheckBox.Text = "Use \"Plain\" Buttons";
            this.uglyCheckBox.CheckedChanged += new System.EventHandler(this.uglyCheckBox_CheckedChanged);
            // 
            // plusMinusCheckBox
            // 
            this.plusMinusCheckBox.Location = new System.Drawing.Point(8, 424);
            this.plusMinusCheckBox.Name = "plusMinusCheckBox";
            this.plusMinusCheckBox.Size = new System.Drawing.Size(190, 23);
            this.plusMinusCheckBox.TabIndex = 9;
            this.plusMinusCheckBox.Text = "Show Plus/Minus Graphic";
            this.plusMinusCheckBox.CheckedChanged += new System.EventHandler(this.plusMinusCheckBox_CheckedChanged);
            // 
            // itemImageList
            // 
            this.itemImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.itemImageList.ImageSize = new System.Drawing.Size(16, 16);
            this.itemImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("itemImageList.ImageStream")));
            this.itemImageList.TransparentColor = System.Drawing.Color.Yellow;
            // 
            // buttonImageList
            // 
            this.buttonImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.buttonImageList.ImageSize = new System.Drawing.Size(40, 9);
            this.buttonImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("buttonImageList.ImageStream")));
            this.buttonImageList.TransparentColor = System.Drawing.Color.Yellow;
            // 
            // button1
            // 
            this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.button1.Location = new System.Drawing.Point(376, 8);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(81, 30);
            this.button1.TabIndex = 8;
            this.button1.Text = "Close";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // plusMinusImageList
            // 
            this.plusMinusImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.plusMinusImageList.ImageSize = new System.Drawing.Size(14, 14);
            this.plusMinusImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("plusMinusImageList.ImageStream")));
            this.plusMinusImageList.TransparentColor = System.Drawing.Color.White;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(366, 37);
            this.label1.TabIndex = 7;
            this.label1.Text = "Click on the header buttons to sort books or reorder the second and third column " +
                "using column drag && drop.";
            // 
            // 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(376, 176);
            this.Label5.Name = "Label5";
            this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.Label5.Size = new System.Drawing.Size(80, 201);
            this.Label5.TabIndex = 14;
            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;
            // 
            // ItemContextMenu
            // 
            this.ItemContextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                            this.menuItem1,
                                                                                            this.menuItem2,
                                                                                            this.menuItem3,
                                                                                            this.menuItem4});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.Text = "Edit";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 1;
            this.menuItem2.Text = "Insert";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 2;
            this.menuItem3.Text = "Append";
            this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 3;
            this.menuItem4.Text = "Delete";
            this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
            // 
            // axSftTree1
            // 
            this.axSftTree1.Location = new System.Drawing.Point(8, 48);
            this.axSftTree1.Name = "axSftTree1";
            this.axSftTree1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftTree1.OcxState")));
            this.axSftTree1.Size = new System.Drawing.Size(358, 372);
            this.axSftTree1.TabIndex = 15;
            this.axSftTree1.ItemDblClick += new AxSftTreeLib70._DSftTreeEvents_ItemDblClickEventHandler(this.axSftTree1_ItemDblClick);
            this.axSftTree1.ContextMenuEvent += new AxSftTreeLib70._DSftTreeEvents_ContextMenuEventHandler(this.axSftTree1_ContextMenuEvent);
            this.axSftTree1.ItemClick += new AxSftTreeLib70._DSftTreeEvents_ItemClickEventHandler(this.axSftTree1_ItemClick);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.button1;
            this.ClientSize = new System.Drawing.Size(464, 446);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.axSftTree1,
                                                                          this.Label5,
                                                                          this.uglyCheckBox,
                                                                          this.plusMinusCheckBox,
                                                                          this.button1,
                                                                          this.label1});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - BookTable 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 button1_Click(object sender, System.EventArgs e) {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e) {

            axSftTree1.BulkUpdate = true;

            // set default item graphic.  This can also be done at design time
            axSftTree1.Items.ItemImageExpanded.SetImageListH((int) itemImageList.Handle, 0);
            axSftTree1.Items.ItemImageExpandable.SetImageListH((int) itemImageList.Handle, 1);
            axSftTree1.Items.ItemImageLeaf.SetImageListH((int) itemImageList.Handle, 2);

            // set column header pictures
            axSftTree1.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortAsc;
            axSftTree1.get_Header(1).Image.Clear();
            axSftTree1.get_Header(2).Image.Clear();

            Random random = new Random();

            // set the cell font for books
            Font fnt = new Font(axSftTree1.Font, FontStyle.Bold);
            stdole.IFontDisp pFont = OLECvt.ToIFontDisp(fnt);

            for (int bk = 1 ; bk <= 4 ; ++bk) {
                // add a book
                int bookIndex = axSftTree1.Items.Add("Book " + bk.ToString());
            
                axSftTree1.get_Cell(bookIndex, 1).Text = "Description for book " + bk.ToString();
                int size = random.Next(1, bk == 1 ? 99 : 999);
                axSftTree1.get_Cell(bookIndex, 2).Text = size.ToString();
                axSftTree1.get_Item(bookIndex).Data = size;
                // add chapters
                for (int ch = 1 ; ch <= 2 ; ++ch) {
                    int index = axSftTree1.Items.Add("Chapter " + ch.ToString());
                    axSftTree1.get_Item(index).Level = 1;
                    // add sections
                    for (int sect = 1 ; sect <= 2 ; ++sect) {
                        index = axSftTree1.Items.Add("Section " + sect.ToString());
                        axSftTree1.get_Item(index).Level = 2;
                    }
                }
                // after adding the book and all dependent items, we
                // collapse the item, so it's up to the user to expand it
                axSftTree1.get_Item(bookIndex).Collapse(false);
                // set font
                axSftTree1.get_Cell(bookIndex, 0).Font = pFont;
            }
            // End of Mass-Update
            axSftTree1.BulkUpdate = false;
            // Make columns optimal
            axSftTree1.ColumnsObj.MakeOptimal();
            // allow horizontal scrolling
            axSftTree1.Items.RecalcHorizontalExtent();
        }

        private void plusMinusCheckBox_CheckedChanged(object sender, System.EventArgs e) {
            if (plusMinusCheckBox.Checked) {
                axSftTree1.Items.PlusMinusImageExpandable.NETImageObject = plusMinusImageList.Images[0];
                axSftTree1.Items.PlusMinusImageExpanded.NETImageObject = plusMinusImageList.Images[1];
            } else {
                axSftTree1.Items.PlusMinusImageExpandable.Clear();
                axSftTree1.Items.PlusMinusImageExpanded.Clear();
            }
        }

        private void uglyCheckBox_CheckedChanged(object sender, System.EventArgs e) {
            if (uglyCheckBox.Checked)
                axSftTree1.ButtonPicture = buttonImageList.Images[0];
            else
                axSftTree1.ButtonPicture = null;
        }

        private void SortHeader(short ColNum) {
            if (axSftTree1.get_Header(ColNum).Image.Appearance == SftPictureImageConstants.sftImageSortAsc) {
                // Sort the data. Note that column 2 is sorted by Item.Data, which is
                // an integer value (book size in pages)
                if (ColNum == 2)
                    axSftTree1.Items.SortDependents(-1, ColNum,  SftTreeSortTypeConstants.sortSftTreeDscItemData);
                else
                    axSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeDescending);
                axSftTree1.get_Header(ColNum).Image.Appearance = SftPictureImageConstants.sftImageSortDesc;
            } else {
                axSftTree1.get_Header(0).Image.Clear();
                axSftTree1.get_Header(1).Image.Clear();
                axSftTree1.get_Header(2).Image.Clear();
                // Sort the data. Note that column 2 is sorted by Item.Data, which is
                // an integer value (book size in pages)
                if (ColNum == 2)
                    axSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeAscItemData);
                else
                    axSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeAscending);
                axSftTree1.get_Header(ColNum).Image.Appearance = SftPictureImageConstants.sftImageSortAsc;
            }
        }

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemClickEvent e) {
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            SftTreeButtonConstants button = (SftTreeButtonConstants) e.button;
            // respond to click in column header with left mouse button
            if (area == SftTreeAreaTypeConstants.constSftTreeColumnHeader && button == SftTreeButtonConstants.constSftTreeLeftButton)
                SortHeader(e.colIndex);
            else if (area == SftTreeAreaTypeConstants.constSftTreeExpandAll)
                axSftTree1.get_Item(e.itemIndex).Expand(false, true);
        }

        private void axSftTree1_ItemDblClick(object sender, AxSftTreeLib70._DSftTreeEvents_ItemDblClickEvent e) {
            // Respond to click in column header with left mouse button
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            SftTreeButtonConstants button = (SftTreeButtonConstants) e.button;
            if (area == SftTreeAreaTypeConstants.constSftTreeColumnRes && button == SftTreeButtonConstants.constSftTreeLeftButton) {
                axSftTree1.get_Column(e.colIndex).MakeOptimal();
                axSftTree1.Items.RecalcHorizontalExtent();
            } else if (area == SftTreeAreaTypeConstants.constSftTreeColumnHeader && button == SftTreeButtonConstants.constSftTreeLeftButton) 
                SortHeader(e.colIndex);
        }

        private void menuItem1_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void menuItem2_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void menuItem3_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void menuItem4_Click(object sender, System.EventArgs e) {
            MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.");
        }

        private void axSftTree1_ContextMenuEvent(object sender, AxSftTreeLib70._DSftTreeEvents_ContextMenuEvent e) 
        {
            Point point = axSftTree1.PointToScreen(new Point(e.x, e.y));

            // Determine click context menu for header or item
            int l, t, h, w;
            axSftTree1.Headers.GetPositionPix(out l, out t, out w, out h);
            if (e.x >= l && e.x < l + w && e.y >= t && e.y <= t + h) {
                HeaderMenu(PointToClient(point));
                return;
            }
            
            // determine item right-clicked
            int ItemIndex = axSftTree1.Items.HitTestPix(e.x, e.y);
            if (ItemIndex >= 0 && ItemIndex < axSftTree1.Items.Count) {
                axSftTree1.Items.Current = ItemIndex;
                axSftTree1.get_Item(ItemIndex).Selected = true;
                axSftTree1.CancelMode();
                ItemContextMenu.Show(this, PointToClient(point));
            }
        }

        private void menuHeader_Click(object sender, System.EventArgs e) {
            MenuItem mItem = (MenuItem) sender;
            short colIndex = (short) mItem.Index;
            if (mItem.Checked)
                axSftTree1.get_Column(colIndex).WidthPix = 0;
            else
                axSftTree1.get_Column(colIndex).MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
            mItem.Checked = !mItem.Checked;
        }

        private void menuHeaderShowAll_Click(object sender, System.EventArgs e) {
            axSftTree1.ColumnsObj.MakeOptimal();
        }

        private void HeaderMenu(Point cursor) {

            System.Windows.Forms.ContextMenu menu = new ContextMenu();
            System.Windows.Forms.MenuItem mItem;

            int count = 0;
            for (short c = 0 ; c < axSftTree1.ColumnsObj.Count ; ++c) {
                mItem = new System.Windows.Forms.MenuItem();
                mItem.Text = axSftTree1.get_Header(c).Text;
                if (axSftTree1.get_Column(c).WidthPix > 0) {
                    mItem.Checked = true;
                    ++count;
                } else
                    mItem.Checked = false;
                mItem.Click += new System.EventHandler(menuHeader_Click);
                menu.MenuItems.Add(mItem);
            }

            if (count <= 1) {
                for (short c = 0 ; c < axSftTree1.ColumnsObj.Count ; ++c) {
                    if (menu.MenuItems[c].Checked)
                        menu.MenuItems[c].Enabled = false;
                    else
                        menu.MenuItems[c].Enabled = true;
                }
            }

            mItem = new System.Windows.Forms.MenuItem("-");
            menu.MenuItems.Add(mItem);
            
            mItem = new System.Windows.Forms.MenuItem("&Show All");
            mItem.Enabled = (count < axSftTree1.ColumnsObj.Count);
            mItem.Click += new System.EventHandler(menuHeaderShowAll_Click);
            menu.MenuItems.Add(mItem);
            
            axSftTree1.CancelMode();
            menu.Show(this, cursor);
        }
    }
}