using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

using Softelvdm.SftTreeNET;
using Softelvdm.Controls;

namespace WindowsApplication1 {
    public partial class PartsSample1 : Form {
        public PartsSample1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            sftTree1.Initializing = true;
            sftTree1.Columns.Count = 4;
            // This sample demonstrates how the various parts of a tree control are used in cells.
            // To prepare for this sample, create a new project with a blank form and add
            // a SftTree/NET control named sftTree1.
            // In addition, adjust the following FromFile method to use a (small) bitmap
            // that is located on your system.
            Image img = Bitmap.FromFile("..\\..\\test.bmp");

            // Add an item
            ItemClass item = sftTree1.ItemCollection.Add();

            // Add a text part to the first cell (could use CellBaseClass.Text instead)
            CellClass cell = item.Cells[0];
            TextPartClass tp = new TextPartClass("The first cell", LineStyleEnum.Wordwrap, HAlignmentOptionalEnum.Left,
                         VAlignmentOptionalEnum.Top, Color.Blue, Color.Green, Color.Red, Color.Blue, new Font("Arial", 12), StringFormatFlags.LineLimit| StringFormatFlags.MeasureTrailingSpaces); 
            cell.Parts.Add(tp);
            // Followed by a check box (still in the first cell)
            CheckBoxPartClass cb = new CheckBoxPartClass( CheckBoxStateEnum.Unchecked, HAlignmentOptionalEnum.Left, VAlignmentOptionalEnum.Top, true);
            cb.Action += new GenericPartClass.ActionEventHandler(cb_Action);
            cell.Parts.Add(cb);
            // Followed by an image (still in the first cell)
            ImagePartClass ip = new ImagePartClass(img);
            cell.Parts.Add(ip);
            
            // Add contents to the second cell
            item.Cells[1].Text = "The\neasy\nway";

            // Add an image to the third cell
            cell = item.Cells[2];
            cell.Image = img;
            ip = (ImagePartClass) cell.Parts[0];
            ip.Action += new GenericPartClass.ActionEventHandler(ip_Action);

            // Add another item
            item = sftTree1.ItemCollection.Add();

            // Add a progressbar to the first cell
            ProgressBarPartClass pbp = new ProgressBarPartClass(0, 50, 25, 100, 12, GadgetAppearanceEnum.ThemedSystem,
                            Color.Red, Color.White, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
            pbp.PartAlignment = PartAlignmentEnum.EntireArea;
            pbp.Appearance = GadgetAppearanceEnum.System;
            item.Cells[0].Parts.Add(pbp);

            // Add a button to the second cell
            ButtonPartClass bp = new ButtonPartClass(false, HAlignmentOptionalEnum.Default, VAlignmentOptionalEnum.Default,
                        true, false, GadgetAppearanceEnum.ThemedSystem, null, "Click Me", null, Color.Red);
            bp.Action += new GenericPartClass.ActionEventHandler(bp_Action);
            item.Cells[1].Parts.Add(bp);

            // Add text to the third cell
            cell = item.Cells[2];
            cell.Text = "Sample Text";
            // Add a dropdown button (still in the third cell)
            DropDownButtonPartClass ddp = new DropDownButtonPartClass();
            ddp.Action += new GenericPartClass.ActionEventHandler(ddp_Action);
            ddp.PartAlignment = PartAlignmentEnum.FlushEnd;
            cell.Parts.Add(ddp);

            // Add another item
            item = sftTree1.ItemCollection.Add();

            // Add three parts with a vertical orientation to the first cell
            cell = item.Cells[0];
            cell.Orientation = OrientationOptionalEnum.Vertical;
            tp = new TextPartClass("First part");
            tp.HAlign = HAlignmentOptionalEnum.Left;
            cell.Parts.Add(tp);
            ip = new ImagePartClass(img);
            ip.HAlign = HAlignmentOptionalEnum.Center;
            cell.Parts.Add(ip);
            tp = new TextPartClass("Third part");
            tp.HAlign = HAlignmentOptionalEnum.Right;
            cell.Parts.Add(tp);

            // Add three parts with a vertical orientation to the second cell
            cell = item.Cells[1];
            cell.Orientation = OrientationOptionalEnum.Vertical;
            tp = new TextPartClass("First part");
            tp.HAlign = HAlignmentOptionalEnum.Center;
            cell.Parts.Add(tp);
            ip = new ImagePartClass(img);
            ip.HAlign = HAlignmentOptionalEnum.Center;
            cell.Parts.Add(ip);
            tp = new TextPartClass("Third part");
            tp.HAlign = HAlignmentOptionalEnum.Center;
            cell.Parts.Add(tp);

            // Add three parts with a vertical orientation to the third cell
            cell = item.Cells[2];
            cell.Orientation = OrientationOptionalEnum.Vertical;
            tp = new TextPartClass("First part");
            tp.HAlign = HAlignmentOptionalEnum.Right;
            cell.Parts.Add(tp);
            ip = new ImagePartClass(img);
            ip.HAlign = HAlignmentOptionalEnum.Center;
            cell.Parts.Add(ip);
            tp = new TextPartClass("Third part");
            tp.HAlign = HAlignmentOptionalEnum.Left;
            cell.Parts.Add(tp);

            // Set a sorting indicator in the first column header
            cell = sftTree1.Headers[0,0];
            cell.SortIndicatorPosition = SortIndicatorPositionEnum.Side;
            cell.SortStatus = SortStatusEnum.Ascending;

            sftTree1.Columns.MakeOptimal(0, false);
            sftTree1.RecalcHorizontalExtent();
            sftTree1.Initializing = false;
        }

        // Dropdown button Action event
        void ddp_Action(object sender, ActionEventArgs e) {
            DropDownButtonPartClass ddp = (DropDownButtonPartClass)e.Part;
            MessageBox.Show("Dropdown button clicked.");
        }

        // Button part Action event
        void bp_Action(object sender, ActionEventArgs e) {
            // this is our button that was just clicked
            ButtonPartClass bp = (ButtonPartClass)e.Part;
            // this is the cell containing the button
            CellClass cell = (CellClass)bp.PartOwner;
            // get the previous cell
            cell = cell.Previous; 
            // this cell's first (and only) part is a progressbar (in this example)
            ProgressBarPartClass pbp = (ProgressBarPartClass) cell.Parts[0];
            // increment the progressbar's Value property
            int val;
            if (pbp.Value == pbp.Max)
                val = 0;
            else
                val = pbp.Value + 10;
            val = Math.Min(val, pbp.Max);
            pbp.Value = val;
        }

        // Checkbox part Action event
        void cb_Action(object sender, ActionEventArgs e) {
            MessageBox.Show("Clicked the checkbox.");
        }

        // Image part Action event
        void ip_Action(object sender, ActionEventArgs e) {
            MessageBox.Show("Clicked the image.");
        }

        // ItemClick event
        private void sftTree1_ItemClick(object sender, ItemClickEventArgs e) {
            Debug.Write("** ItemClick");
            DumpValues(e);
        }

        // This is a small helper routine to show all properties and fields of an object
        private void DumpValues(object o) {
            PropertyInfo[] api = o.GetType().GetProperties();
            foreach (PropertyInfo pi in api)
                Debug.Write(" " + pi.Name + " " + pi.GetValue(o, new object[] { }));
            FieldInfo[] afi = o.GetType().GetFields();
            foreach (FieldInfo fi in afi)
                Debug.Write(" " + fi.Name + " " + fi.GetValue(o));
            Debug.WriteLine("");
        }
    }
}