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

Imports LidorSystems.IntegralUI.Lists

Public Class Form1

#Region "Initialization"
    Public Sub New()
        InitializeComponent()

        ' This is required for navigation with keyboard among subitems
        Me.treeListView1.AllowSubItemFocus = True

        CreateLabelContent()

        InitList()
    End Sub

    Private Sub CreateLabelContent()
        Dim content As [String] = "<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>"

        Me.labelInfo.Content = content
    End Sub

    Private Sub labelInfo_ContentObjectClicked(ByVal sender As Object, ByVal e As LidorSystems.IntegralUI.ObjectClickEventArgs) Handles labelInfo.ContentObjectClicked
        If TypeOf e.[Object] Is LinkLabel Then
            System.Diagnostics.Process.Start(TryCast(e.[Object], LinkLabel).Links(0).LinkData.ToString())
        End If
    End Sub

    Private Sub InitList()
        Me.treeListView1.SuspendUpdate()

        InitColumns()
        InitNodes()

        If rbLabel.Checked Then
            ' Allow subitem label editing
            Me.treeListView1.LabelEdit = True

            ' Optional: change the style of label edit control
            Me.treeListView1.LabelEditStyle.BorderCornerRadius = 1
            Me.treeListView1.LabelEditStyle.BorderColor = Color.SteelBlue
        Else
            Me.treeListView1.LabelEdit = False
        End If

        Me.treeListView1.ResumeUpdate()
    End Sub

    Private Sub InitColumns()
        Me.treeListView1.Columns.Clear()

        Dim column As TreeListViewColumn = Nothing

        For j As Integer = 1 To 2
            column = New TreeListViewColumn("Column " & j.ToString(), "Footer " & j.ToString())
            column.Width = 100

            If j = 1 Then
                column.Width = 150
            ElseIf j = 2 AndAlso rbTextControl.Checked Then
                column.ContentType = ColumnContentType.TextControl
            End If

            Me.treeListView1.Columns.Add(column)
        Next
    End Sub

    Private Sub InitNodes()
        Me.treeListView1.Nodes.Clear()

        Dim node As TreeListViewNode = Nothing

        For i As Integer = 1 To 100
            node = New TreeListViewNode("Item " & i.ToString())

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

            Me.treeListView1.Nodes.Add(node)
        Next
    End Sub

    Private Sub InitChildNodes(ByVal parentNode As TreeListViewNode, ByVal level As Integer, ByVal prefix As [String])
        If level = 3 Then
            Return
        End If

        Dim node As TreeListViewNode = Nothing

        For i As Integer = 1 To 3
            node = New TreeListViewNode("Item " & i.ToString())

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

            parentNode.Nodes.Add(node)
        Next
    End Sub

    Private Sub InitSubItems(ByVal node As TreeListViewNode, ByVal i As Integer, ByVal prefix As [String])
        Dim subItem As TreeListViewSubItem = Nothing

        For j As Integer = 1 To Me.treeListView1.Columns.Count
            subItem = New TreeListViewSubItem("Item " & Convert.ToString(prefix) & j.ToString())

            Select Case Me.treeListView1.Columns(j - 1).ContentType
                Case ColumnContentType.TextControl
                    subItem.Value = subItem.Text
                    Exit Select
            End Select

            node.SubItems.Add(subItem)
        Next
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
        InitList()
    End Sub
#End Region

#Region "LabelEdit"
    Private Sub treeListView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As LidorSystems.IntegralUI.ObjectEditEventArgs) Handles treeListView1.BeforeLabelEdit
        If TypeOf e.[Object] Is TreeListViewSubItem Then
            Dim subItem As TreeListViewSubItem = DirectCast(e.[Object], TreeListViewSubItem)

            ' Only allow editing of subitems in second column
            If subItem.Index <> 1 Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub treeListView1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles treeListView1.Click
        If Me.treeListView1.LabelEdit Then
            Dim mousePos As Point = Me.treeListView1.ContentPanel.PointToClient(Control.MousePosition)

            ' Locate the parent node to which this subitems belongs
            Dim node As TreeListViewNode = Me.treeListView1.GetNodeAt(mousePos)

            ' Get the subitem over which mouse click was made
            Dim subItem As TreeListViewSubItem = Me.treeListView1.GetSubItem(node, mousePos)
            If subItem IsNot Nothing Then
                Dim textArea As RectangleF = subItem.TextRect
                textArea.Size = subItem.TextSize

                ' If subitem is clicked outside text area, start manual editing
                ' Useful when subitem text is empty
                If Not textArea.Contains(mousePos) Then
                    subItem.BeginEdit()
                End If
            End If
        End If
    End Sub
#End Region

#Region "TextControl"
    Private Sub checkBorder_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles checkBorder.CheckedChanged
        If Me.treeListView1.Columns.Count = 0 Then
            Return
        End If

        If treeListView1.Columns(1).ContentType = ColumnContentType.TextControl Then
            If TypeOf Me.treeListView1.Columns(1).ContentControl Is LidorSystems.IntegralUI.Controls.TextControl Then
                ' Optional: Hide the border of TextControl for better look
                Dim ctrlText As LidorSystems.IntegralUI.Controls.TextControl = DirectCast(Me.treeListView1.Columns(1).ContentControl, LidorSystems.IntegralUI.Controls.TextControl)
                If checkBorder.Checked Then
                    ctrlText.ColorStyle.BorderColor = Color.Empty
                Else
                    ctrlText.ColorStyle.BorderColor = Color.Transparent
                End If

                Me.treeListView1.Invalidate()
            End If
        End If
    End Sub

    Private Sub rbTextControl_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbTextControl.CheckedChanged
        checkBorder.Enabled = rbTextControl.Checked
    End Sub
#End Region

End Class
