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

using AxSftBoxLib45;
using SftBoxLib45;
using Softelvdm.OLEConvert;

namespace FontDlg
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button closeButton;
        private System.Windows.Forms.Label label1;
        private AxSftBoxLib45.AxSftBox fontNames;
        private AxSftBoxLib45.AxSftBox fontStyles;
        private AxSftBoxLib45.AxSftBox fontSizes;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label sampleText;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.CheckBox condHeaders;

        private SftBoxSortConstants fontNamesDirection = SftBoxSortConstants.sortSftBoxAscending;
        private SftBoxSortConstants fontStylesDirection = SftBoxSortConstants.sortSftBoxAscending;
        private SftBoxSortConstants fontSizesDirection = SftBoxSortConstants.sortSftBoxAscending;
        private System.Windows.Forms.Label label5;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.closeButton = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.fontNames = new AxSftBoxLib45.AxSftBox();
            this.fontStyles = new AxSftBoxLib45.AxSftBox();
            this.fontSizes = new AxSftBoxLib45.AxSftBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.sampleText = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.condHeaders = new System.Windows.Forms.CheckBox();
            this.label5 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.fontNames)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fontStyles)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fontSizes)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // closeButton
            // 
            this.closeButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.closeButton.Location = new System.Drawing.Point(395, 8);
            this.closeButton.Name = "closeButton";
            this.closeButton.Size = new System.Drawing.Size(87, 30);
            this.closeButton.TabIndex = 0;
            this.closeButton.Text = "Close";
            this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(380, 37);
            this.label1.TabIndex = 1;
            this.label1.Text = "Easily add font selection to your applications (even on tool bars) using SftBox/O" +
                "CX\'s font capabilities.";
            // 
            // fontNames
            // 
            this.fontNames.Location = new System.Drawing.Point(7, 59);
            this.fontNames.Name = "fontNames";
            this.fontNames.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("fontNames.OcxState")));
            this.fontNames.Size = new System.Drawing.Size(263, 38);
            this.fontNames.TabIndex = 2;
            this.fontNames.ItemDblClk += new AxSftBoxLib45._ISftBoxEvents_ItemDblClkEventHandler(this.fontNames_ItemDblClk);
            this.fontNames.ItemClick += new AxSftBoxLib45._ISftBoxEvents_ItemClickEventHandler(this.fontNames_ItemClick);
            this.fontNames.SelectionChange += new System.EventHandler(this.fontNames_SelectionChange);
            // 
            // fontStyles
            // 
            this.fontStyles.Location = new System.Drawing.Point(278, 57);
            this.fontStyles.Name = "fontStyles";
            this.fontStyles.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("fontStyles.OcxState")));
            this.fontStyles.Size = new System.Drawing.Size(117, 40);
            this.fontStyles.TabIndex = 3;
            this.fontStyles.ItemDblClk += new AxSftBoxLib45._ISftBoxEvents_ItemDblClkEventHandler(this.fontStyles_ItemDblClk);
            this.fontStyles.ItemClick += new AxSftBoxLib45._ISftBoxEvents_ItemClickEventHandler(this.fontStyles_ItemClick);
            this.fontStyles.SelectionChange += new System.EventHandler(this.fontStyles_SelectionChange);
            // 
            // fontSizes
            // 
            this.fontSizes.Location = new System.Drawing.Point(402, 57);
            this.fontSizes.Name = "fontSizes";
            this.fontSizes.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("fontSizes.OcxState")));
            this.fontSizes.Size = new System.Drawing.Size(80, 40);
            this.fontSizes.TabIndex = 4;
            this.fontSizes.ItemDblClk += new AxSftBoxLib45._ISftBoxEvents_ItemDblClkEventHandler(this.fontSizes_ItemDblClk);
            this.fontSizes.ItemClick += new AxSftBoxLib45._ISftBoxEvents_ItemClickEventHandler(this.fontSizes_ItemClick);
            this.fontSizes.SelectionChange += new System.EventHandler(this.fontSizes_SelectionChange);
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.sampleText);
            this.panel1.Location = new System.Drawing.Point(146, 106);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(190, 91);
            this.panel1.TabIndex = 5;
            // 
            // sampleText
            // 
            this.sampleText.Location = new System.Drawing.Point(0, 23);
            this.sampleText.Name = "sampleText";
            this.sampleText.Size = new System.Drawing.Size(190, 68);
            this.sampleText.TabIndex = 0;
            this.sampleText.Text = "AaBbCcYyZz";
            this.sampleText.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(7, 204);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(475, 38);
            this.label3.TabIndex = 6;
            this.label3.Text = "Click on the header buttons in the combo box to sort entries.  Make sure the comb" +
                "o box is dropped down so you can see the sorting take place.";
            // 
            // condHeaders
            // 
            this.condHeaders.Checked = true;
            this.condHeaders.CheckState = System.Windows.Forms.CheckState.Checked;
            this.condHeaders.Location = new System.Drawing.Point(7, 250);
            this.condHeaders.Name = "condHeaders";
            this.condHeaders.Size = new System.Drawing.Size(212, 23);
            this.condHeaders.TabIndex = 7;
            this.condHeaders.Text = "Always show headers.";
            this.condHeaders.CheckedChanged += new System.EventHandler(this.condHeaders_CheckedChanged);
            // 
            // label5
            // 
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(176, 248);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(304, 32);
            this.label5.TabIndex = 10;
            this.label5.Text = "Click on the SftBox/OCX control above and hit F1 to access help information for S" +
                "ftBox/OCX 4.5";
            this.label5.Visible = false;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(489, 287);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.condHeaders);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.fontSizes);
            this.Controls.Add(this.fontStyles);
            this.Controls.Add(this.fontNames);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.closeButton);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - FontDlg Sample";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.fontNames)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fontStyles)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fontSizes)).EndInit();
            this.panel1.ResumeLayout(false);
            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 closeButton_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            // add all font information
            UpdateFontNames();
        }

        private void UpdateFontNames()
        {
            // add all font names
            fontNames.BulkUpdate = true;
            fontNames.Items.Clear();
            fontNames.Items.AddFontNames(-1, SftBoxFontConstants.fontSftBoxAll, true, 0);
            fontNames.Items.Selection = 0;
            PerformSort(fontNames, SftBoxSortConstants.sortSftBoxAscending);
            fontNames.BulkUpdate = false;
            fontNames.Columns.MakeOptimal(0);
            fontNames.Items.RecalcHorizontalExtent(0);
        }

        private void UpdateFontStyles()
        {
            // add all styles for the current font name
            if (fontNames.Items.Selection >= 0) {
                string OldText = fontStyles.Edit.Text;
                fontStyles.Items.AddFontStyles(fontNames.get_Cell(fontNames.Items.Selection,0).Text, 0, "", "");
                int index = fontStyles.Items.Find(OldText, 0, 0, false, true, true);
                if (index < 0) index = 0;
                fontStyles.Items.Selection = index;
            } else
                fontStyles.Items.Clear();
            PerformSort(fontStyles, SftBoxSortConstants.sortSftBoxAscending);
            fontStyles.Columns.MakeOptimal(0);
            fontStyles.Items.RecalcHorizontalExtent(0);
        }

        private void UpdateFontSizes()
        {
            // add all sizes for the current font name
            if (fontNames.Items.Selection >= 0) {
                string OldText = fontSizes.Edit.Text;
                fontSizes.Items.AddFontSizes(fontNames.get_Cell(fontNames.Items.Selection,0).Text, 0);
                int index = fontSizes.Items.Find(OldText, 0, 0, false, true, true);
                if (index < 0) index = 0;
                fontSizes.Items.Selection = index;
            } else
                fontSizes.Items.Clear();
            PerformSort(fontSizes, SftBoxSortConstants.sortSftBoxAscending); 
            fontSizes.Columns.MakeOptimal(0);
            fontSizes.Items.RecalcHorizontalExtent(0);
            UpdateSample();
        }

        private void fontNames_SelectionChange(object sender, System.EventArgs e)
        {
            UpdateFontStyles();
        }

        private void fontStyles_SelectionChange(object sender, System.EventArgs e)
        {
            UpdateFontSizes();
        }

        private void fontSizes_SelectionChange(object sender, System.EventArgs e)
        {
            UpdateSample();
        }

        private void UpdateSample()
        {
            int SelItem = fontStyles.Items.Selection;
            if (SelItem >= 0 && fontSizes.Edit.Text != "") {
                sampleText.Text = "AaBbCcYyZz";
                FontStyle style = 0;
                if ((fontStyles.get_Cell(SelItem, 0).Data & 0x80000000) != 0)
                    style |= FontStyle.Italic;
                int weight = (fontStyles.get_Cell(SelItem, 0).Data & 0x7fffffff);
                if (weight > 400)
                    style |= FontStyle.Bold;
                sampleText.Font = new Font(fontNames.Edit.Text, Convert.ToSingle(fontSizes.Edit.Text), style);
            } else
                sampleText.Text = "";
        }

        private void condHeaders_CheckedChanged(object sender, System.EventArgs e)
        {
            if (condHeaders.Checked) {
                fontNames.Headers.Main = true;
                fontNames.Headers.DropDown = false;
                fontStyles.Headers.Main = true;
                fontStyles.Headers.DropDown = false;
                fontSizes.Headers.Main = true;
                fontSizes.Headers.DropDown = false;
            } else {
                fontNames.Headers.Main = false;
                fontNames.Headers.DropDown = true;
                fontStyles.Headers.Main = false;
                fontStyles.Headers.DropDown = true;
                fontSizes.Headers.Main = false;
                fontSizes.Headers.DropDown = true;
            }
        }

        private void PerformSort(AxSftBox box, SftBoxSortConstants currDirection)
        {
            if (currDirection == SftBoxSortConstants.sortSftBoxAscending)
                box.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortAsc;
            else
                box.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortDesc;
            // Font styles and font names are sorted by Cell.Text property
            // Font sizes are sorted according to numeric Cell.Data property
            // Outside of this function, only sortSftBoxAscending and sortSftBoxDescending
            // are used, so we need to translate these
            if (box == fontSizes) {
                if (currDirection == SftBoxSortConstants.sortSftBoxAscending)
                    currDirection = SftBoxSortConstants.sortSftBoxCellValueAscending;
                else
                    currDirection = SftBoxSortConstants.sortSftBoxCellValueDescending;
            }
            box.Items.Sort(-1, 0, currDirection);
        }

        private void ReverseSortOrder(AxSftBox box, ref SftBoxSortConstants currDirection)
        {
            if (currDirection == SftBoxSortConstants.sortSftBoxAscending)
                currDirection = SftBoxSortConstants.sortSftBoxDescending;
            else
                currDirection = SftBoxSortConstants.sortSftBoxAscending;
        }

        private void fontNames_ItemClick(object sender, AxSftBoxLib45._ISftBoxEvents_ItemClickEvent e)
        {
            if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
                ReverseSortOrder(fontNames, ref fontNamesDirection);
                PerformSort(fontNames, fontNamesDirection);
            }
        }

        private void fontNames_ItemDblClk(object sender, AxSftBoxLib45._ISftBoxEvents_ItemDblClkEvent e)
        {
            if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
                ReverseSortOrder(fontNames, ref fontNamesDirection);
                PerformSort(fontNames, fontNamesDirection);
            }
        }

        private void fontStyles_ItemClick(object sender, AxSftBoxLib45._ISftBoxEvents_ItemClickEvent e)
        {
            if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
                ReverseSortOrder(fontStyles, ref fontStylesDirection);
                PerformSort(fontStyles, fontStylesDirection);
            }
        }

        private void fontStyles_ItemDblClk(object sender, AxSftBoxLib45._ISftBoxEvents_ItemDblClkEvent e)
        {
            if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
                ReverseSortOrder(fontStyles, ref fontStylesDirection);
                PerformSort(fontStyles, fontStylesDirection);
            }
        }

        private void fontSizes_ItemClick(object sender, AxSftBoxLib45._ISftBoxEvents_ItemClickEvent e)
        {
            if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
                ReverseSortOrder(fontSizes, ref fontSizesDirection);
                PerformSort(fontSizes, fontSizesDirection);
            }
        }

        private void fontSizes_ItemDblClk(object sender, AxSftBoxLib45._ISftBoxEvents_ItemDblClkEvent e)
        {
            if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
                ReverseSortOrder(fontSizes, ref fontSizesDirection);
                PerformSort(fontSizes, fontSizesDirection);
            }
        }
    }
}