Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports gmRTL.Core

Namespace MSOutl
        Public Class Outline : Inherits TreeView

        Private items As List(Of TreeNode) = New List(Of TreeNode)()

        Private indents As List(Of Integer) = New List(Of Integer)()

        Private ind As String = "   "

        Public Property Style() As StyleConstants
            Get
                Return StyleConstants.outTextOnly
            End Get
            Set(value As StyleConstants)
            End Set
        End Property

        Public Property PicturePlus() As Image
            Get
                Return Nothing
            End Get
            Set(value As Image)
            End Set
        End Property

        Public Property PictureMinus() As Image
            Get
                Return Nothing
            End Get
            Set(value As Image)
            End Set
        End Property

        Public Property PictureLeaf() As Image
            Get
                Return Nothing
            End Get
            Set(value As Image)
            End Set
        End Property

        Public Property PictureOpen() As Image
            Get
                Return Nothing
            End Get
            Set(value As Image)
            End Set
        End Property

        Public Property PictureClosed() As Image
            Get
                Return Nothing
            End Get
            Set(value As Image)
            End Set
        End Property

        Public Function IndexOf(nod As TreeNode) As Integer
            Return Me.items.IndexOf(nod)
        End Function

        Private _listIndex As Integer
        Protected Overrides Sub OnNodeMouseClick(e As TreeNodeMouseClickEventArgs)
            MyBase.OnNodeMouseClick(e)
            _listIndex = IndexOf(e.Node)
        End Sub

        Public Property ListIndex() As Integer
            Get
                Return Me._listIndex
            End Get
            Set(value As Integer)
                _listIndex = value
                MyBase.SelectedNode = Me.GetNode(value)
            End Set
        End Property

        Public Property ListCount() As Integer
            Get
                Return Me.items.Count
            End Get
            Set(value As Integer)
            End Set
        End Property


        Private Function GetNode(Index As Integer) As TreeNode
            Dim result As TreeNode
            If Index < 0 OrElse Index > Me.items.Count Then
                Debug.WriteLine("Invalid Node Index:" & Index)
                result = Nothing
            Else
                result = Me.items(Index)
            End If
            Return result
        End Function

        Public Sub Clear()
            Me.items.Clear()
            Me.Nodes.Clear()
        End Sub

        Public Sub AddItem(Item As String, Optional Index As Integer = 0)
            Dim nod As TreeNode = New TreeNode()
            nod.Text = Item
            nod.Name = CStr(Index)
            Me.items.Add(nod)
            Me.indents.Add(1)
            Me.Nodes.Add(nod)
        End Sub

        Public Property List(Index As Integer) As String
        Get
            Return Me.items(Index).Text
        End Get
        Set(value As String)
            Me.items(Index).Text = value
            GetNode(Index).Tag = value
        End Set
        End Property

        Public Overloads Property Indent(Index As Integer) As Integer
          Get
                Return Me.indents(Index)
          End Get
          Set(value As Integer)

                Me.indents(Index) = value
                Dim nod As TreeNode = Me.GetNode(Index)
                For i As Integer = Index - 1 To 0 Step -1
                    If Me.indents(i) < value Then
                        Dim parent As TreeNode = Me.GetNode(i)
                        nod.Remove()
                        parent.Nodes.Add(nod)
                        Exit For
                    End If
                Next
          End Set
        End Property

        Public Property Expand(Index As Integer) As Boolean
        Get
            Return Me.GetNode(Index).IsExpanded
        End Get
        Set(value As Boolean)
            If value Then
                Me.GetNode(Index).Expand()
            Else
                Me.GetNode(Index).Collapse()
            End If
        End Set
        End Property

        Public Property ItemData(Index As Integer) As Integer
        Get
            Return CInt(Me.GetNode(Index).Tag)
        End Get
        Set(value As Integer)
            Me.GetNode(Index).Tag = CStr(value)
        End Set
        End Property
        Public ReadOnly Property FullPath(ByVal Index As Integer) As String
        Get
            Return Me.GetNode(Index).FullPath
        End Get
        End Property

        Public ReadOnly Property HasSubItems(ByVal Index As Integer) As Boolean
        Get
            Return Me.GetNode(Index).GetNodeCount(False) > 0
        End Get
        End Property

    End Class
    Public Enum StyleConstants
        outTextOnly
        outPictureText
        outPlusMinusText
        outPlusPictureText
        outTreelinesText
        outTreelinesPictureText
    End Enum
End Namespace
