//                                                                  
// Copyright © 2013 Lidor Systems.
// All Rights Reserved.
//
// This SOFTWARE is provided "AS IS", WITHOUT WARRANTY OF ANY KIND.
// either express or implied.                        
//

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

using LidorSystems.IntegralUI.Lists;

namespace SubItem_Text_Editing
{
    public partial class Form1 : Form
    {
        #region Initialization
        public Form1()
        {
            InitializeComponent();

            // This is required for navigation with keyboard among subitems
            this.treeListView1.AllowSubItemFocus = true;

            CreateLabelContent();

            InitList();
        }

        private void CreateLabelContent()
        {
            String content = "<div style=\"font:Verdana,9,r\"><p>   This sample shows how edit the subitem text using LabelEdit and in place editor as TextControl.</p><br></br>";
            content += "<p>   When <b>LabeEdit</b> is active a text box will popup whenever a subitem is clicked. You can edit the text from this text box and when pressing the ENTER key, the change will be applied to the subitem text.</p><br></br>";
            content += "<p>   When in place editor is active, a <b>TextControl</b> will popup whenever a subitem is clicked. The editing process is the same, and all changes are accepted when ENTER key is pressed.</p><br></br>";
            content += "<p>   To cancel the edit process, either click outside current subitem or press ESCAPE key. For demonstration purpose, only subitems in second column are allowed to edit its text.</p><br></br>";
            content += "<p><table width=\"100%\" cellpadding=\"5\"><tr><td width=\"100%\" style=\"align:middleright;textcolor:DarkRed;hovertextcolor:Red\"><a href=\"www.lidorsystems.com/support/articles/winforms/treelistview/\">More Info ...</a></td></tr></table></p>";
            content += "</div>";

            this.labelInfo.Content = content;
        }

        private void labelInfo_ContentObjectClicked(object sender, LidorSystems.IntegralUI.ObjectClickEventArgs e)
        {
            if (e.Object is LinkLabel)
                System.Diagnostics.Process.Start((e.Object as LinkLabel).Links[0].LinkData.ToString());
        }

        private void InitList()
        {
            this.treeListView1.SuspendUpdate();

            InitColumns();
            InitNodes();

            if (rbLabel.Checked)
            {
                // Allow subitem label editing
                this.treeListView1.LabelEdit = true;

                // Optional: change the style of label edit control
                this.treeListView1.LabelEditStyle.BorderCornerRadius = 1;
                this.treeListView1.LabelEditStyle.BorderColor = Color.SteelBlue;
            }
            else
                this.treeListView1.LabelEdit = false;

            this.treeListView1.ResumeUpdate();
        }

        private void InitColumns()
        {
            this.treeListView1.Columns.Clear();

            TreeListViewColumn column = null;

            for (int j = 1; j <= 2; j++)
            {
                column = new TreeListViewColumn("Column " + j.ToString(), "Footer " + j.ToString());
                column.Width = 100;

                if (j == 1)
                    column.Width = 150;
                else if (j == 2 && rbTextControl.Checked)
                    column.ContentType = ColumnContentType.TextControl;

                this.treeListView1.Columns.Add(column);
            }
        }

        private void InitNodes()
        {
            this.treeListView1.Nodes.Clear();

            TreeListViewNode node = null;

            for (int i = 1; i <= 100; i++)
            {
                node = new TreeListViewNode("Item " + i.ToString());

                InitSubItems(node, i, i.ToString());
                InitChildNodes(node, 0, i.ToString());

                this.treeListView1.Nodes.Add(node);
            }
        }

        private void InitChildNodes(TreeListViewNode parentNode, int level, String prefix)
        {
            if (level == 3)
                return;

            TreeListViewNode node = null;

            for (int i = 1; i <= 3; i++)
            {
                node = new TreeListViewNode("Item " + i.ToString());

                InitSubItems(node, i, prefix + i.ToString());
                InitChildNodes(node, level + 1, prefix + i.ToString());

                parentNode.Nodes.Add(node);
            }
        }

        private void InitSubItems(TreeListViewNode node, int i, String prefix)
        {
            TreeListViewSubItem subItem = null;

            for (int j = 1; j <= this.treeListView1.Columns.Count; j++)
            {
                subItem = new TreeListViewSubItem("Item " + prefix + j.ToString());

                switch (this.treeListView1.Columns[j-1].ContentType)
                {
                    case ColumnContentType.TextControl:
                        subItem.Value = subItem.Text;
                        break;
                }

                node.SubItems.Add(subItem);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            InitList();
        }
        #endregion

        #region LabelEdit
        private void treeListView1_BeforeLabelEdit(object sender, LidorSystems.IntegralUI.ObjectEditEventArgs e)
        {
            if (e.Object is TreeListViewSubItem)
            {
                TreeListViewSubItem subItem = (TreeListViewSubItem)e.Object;

                // Only allow editing of subitems in second column
                if (subItem.Index != 1)
                    e.Cancel = true;
            }
        }

        private void treeListView1_Click(object sender, EventArgs e)
        {
            if (this.treeListView1.LabelEdit)
            {
                Point mousePos = this.treeListView1.ContentPanel.PointToClient(Control.MousePosition);

                // Locate the parent node to which this subitems belongs
                TreeListViewNode node = this.treeListView1.GetNodeAt(mousePos);

                // Get the subitem over which mouse click was made
                TreeListViewSubItem subItem = this.treeListView1.GetSubItem(node, mousePos);
                if (subItem != null)
                {
                    RectangleF textArea = subItem.TextRect;
                    textArea.Size = subItem.TextSize;

                    // If subitem is clicked outside text area, start manual editing
                    // Useful when subitem text is empty
                    if (!textArea.Contains(mousePos))
                        subItem.BeginEdit();
                }
            }
        }
        #endregion

        #region TextControl
        private void checkBorder_CheckedChanged(object sender, EventArgs e)
        {
            if (this.treeListView1.Columns[1].ContentType == ColumnContentType.TextControl)
            {
                if (this.treeListView1.Columns[1].ContentControl is LidorSystems.IntegralUI.Controls.TextControl)
                {
                    // Optional: Hide the border of TextControl for better look
                    LidorSystems.IntegralUI.Controls.TextControl ctrlText = (LidorSystems.IntegralUI.Controls.TextControl)this.treeListView1.Columns[1].ContentControl;
                    if (checkBorder.Checked)
                        ctrlText.ColorStyle.BorderColor = Color.Empty;
                    else
                        ctrlText.ColorStyle.BorderColor = Color.Transparent;

                    this.treeListView1.Invalidate();
                }
            }
        }

        private void rbTextControl_CheckedChanged(object sender, EventArgs e)
        {
            checkBorder.Enabled = rbTextControl.Checked;
        }
        #endregion
    }
}