Imports System.Runtime.InteropServices
Imports System.Text
Imports System.ComponentModel
Imports System.Threading

Namespace UI
    Public Class TreeViewHelp
        Public Shared Function AddNode(ByVal path As String, ByVal tv As TreeView) As TreeNode
            Dim pathElements As String() = path.Split("/"c)
            Dim currentNodeList As TreeNodeCollection = tv.Nodes
            Dim currentPath As String = ""
            Dim ret As TreeNode = Nothing

            For Each iNodeName As String In pathElements
                If currentPath <> "" Then
                    currentPath += "/"
                End If

                currentPath += iNodeName
                Dim found As Boolean = False

                For Each iNode As TreeNode In currentNodeList
                    If iNode.Text = iNodeName Then
                        ret = iNode
                        currentNodeList = iNode.Nodes
                        found = True
                    End If
                Next

                If Not found Then
                    ret = New TreeNode
                    ret.Text = iNodeName
                    currentNodeList.Add(ret)
                    currentNodeList = ret.Nodes
                End If
            Next

            Return ret
        End Function

        Public Shared Function GetNodes(ByVal tnc As TreeNodeCollection) As List(Of TreeNode)
            Dim ret As New List(Of TreeNode)
            AddNodesRecursive(tnc, ret)
            Return ret
        End Function

        Private Shared Sub AddNodesRecursive(ByVal searchList As TreeNodeCollection, ByVal returnList As List(Of TreeNode))
            For Each i As TreeNode In searchList
                returnList.Add(i)
                AddNodesRecursive(i.Nodes, returnList)
            Next
        End Sub
    End Class


    Public Class ButtonHelp
        Public Shared Sub DrawDropDownGlyph(ByVal b As ButtonBase, ByVal g As Graphics, ByVal alignment As HorizontalAlignment)
            DrawDropDownGlyph(b, g, alignment, 10)
        End Sub

        Public Shared Sub DrawDropDownGlyph(ByVal b As ButtonBase, ByVal g As Graphics, ByVal alignment As HorizontalAlignment, ByVal fontsize As Integer)
            Dim s As String = "6"
            Dim font As Font = New Font("Marlett", fontsize)
            Dim size As SizeF = g.MeasureString(s, font)
            Dim x As Integer

            If alignment = HorizontalAlignment.Right Then
                x = CInt(b.Width - 5 - size.Width)
            ElseIf alignment = HorizontalAlignment.Center Then
                x = CInt((b.Width - size.Width) / 2) + 1
            Else
                x = 5
            End If

            Dim y As Integer = CInt((b.Height - size.Height) / 2) + 1

            If b.Enabled Then
                g.DrawString(s, font, Brushes.Black, x, y)
            Else
                Dim brush As New SolidBrush(SystemColors.GrayText)
                g.DrawString(s, font, brush, x, y)
                brush.Dispose()
            End If
        End Sub

        Public Shared Sub ShowBold(ByVal b As Button)
            b.Font = New Font(b.Font, FontStyle.Bold)

            For i As Integer = 0 To 20
                Application.DoEvents()
                Thread.Sleep(10)
            Next

            If b.IsHandleCreated Then
                b.Font = New Font(b.Font, FontStyle.Regular)
            End If
        End Sub
    End Class

    Public Class KeysHelp
        Private Shared Converter As TypeConverter = TypeDescriptor.GetConverter(GetType(Keys))
        Private Shared KeysTexts As Dictionary(Of Keys, String)

        Shared Sub New()
            KeysTexts = New Dictionary(Of Keys, String)
            KeysTexts(Keys.Add) = "+ (Numpad)"
            KeysTexts(Keys.Back) = "Back"
            KeysTexts(Keys.Decimal) = "Decimal"
            KeysTexts(Keys.Delete) = "Delete"
            KeysTexts(Keys.Divide) = "Divide"
            KeysTexts(Keys.Down) = "Down"
            KeysTexts(Keys.End) = "End"
            KeysTexts(Keys.Enter) = "Enter"
            KeysTexts(Keys.Escape) = "Escape"
            KeysTexts(Keys.Home) = "Home"
            KeysTexts(Keys.Insert) = "Insert"
            KeysTexts(Keys.Left) = "Left"
            KeysTexts(Keys.Multiply) = "Multiply"
            KeysTexts(Keys.Next) = "Page Down"
            KeysTexts(Keys.Prior) = "Page Up"
            KeysTexts(Keys.Right) = "Right"
            KeysTexts(Keys.Space) = "Space"
            KeysTexts(Keys.Subtract) = "- (Numpad)"
            KeysTexts(Keys.Up) = "Up"
            KeysTexts(Keys.Control) = "Control"
            KeysTexts(Keys.Alt) = "Alt"
            KeysTexts(Keys.Shift) = "Shift"

            KeysTexts(Keys.D0) = "0"
            KeysTexts(Keys.D1) = "1"
            KeysTexts(Keys.D2) = "2"
            KeysTexts(Keys.D3) = "3"
            KeysTexts(Keys.D4) = "4"
            KeysTexts(Keys.D5) = "5"
            KeysTexts(Keys.D6) = "6"
            KeysTexts(Keys.D7) = "7"
            KeysTexts(Keys.D8) = "8"
            KeysTexts(Keys.D9) = "9"

            KeysTexts(Keys.NumPad0) = "0 (Numpad)"
            KeysTexts(Keys.NumPad1) = "1 (Numpad)"
            KeysTexts(Keys.NumPad2) = "2 (Numpad)"
            KeysTexts(Keys.NumPad3) = "3 (Numpad)"
            KeysTexts(Keys.NumPad4) = "4 (Numpad)"
            KeysTexts(Keys.NumPad5) = "5 (Numpad)"
            KeysTexts(Keys.NumPad6) = "6 (Numpad)"
            KeysTexts(Keys.NumPad7) = "7 (Numpad)"
            KeysTexts(Keys.NumPad8) = "8 (Numpad)"
            KeysTexts(Keys.NumPad9) = "9 (Numpad)"
        End Sub

        Public Shared Function GetKeyString(ByVal k As Keys) As String
            If k = Keys.None Then
                Return ""
            End If

            Dim s As String = ""

            If (k And Keys.Control) = Keys.Control Then
                k = k Xor Keys.Control
                s += "Ctrl+"
            End If

            If (k And Keys.Alt) = Keys.Alt Then
                k = k Xor Keys.Alt
                s += "Alt+"
            End If

            If (k And Keys.Shift) = Keys.Shift Then
                k = k Xor Keys.Shift
                s += "Shift+"
            End If

            If KeysTexts.ContainsKey(k) Then
                s += KeysTexts(k)
            Else
                Dim value As Integer = MapVirtualKey(CInt(k), 2)

                If value = 0 OrElse (value And 1 << 31) = 1 << 31 Then
                    s += k.ToString
                Else
                    s += ChrW(value)
                End If
            End If

            Return s
        End Function

        <DllImport("user32.dll")> _
        Private Shared Function MapVirtualKey( _
            ByVal wCode As Integer, _
            ByVal wMapType As Integer) As Integer
        End Function
    End Class

    Public Class CheckedListBoxHelp
        Public Shared Sub MoveSelectedItemUp(ByVal clb As CheckedListBox)
            Dim i As Integer = clb.SelectedIndex

            If i > 0 Then
                Dim state As Boolean = clb.GetItemChecked(i)

                clb.Items.Insert(i - 1, clb.SelectedItem)
                clb.Items.RemoveAt(i + 1)
                clb.SelectedIndex = i - 1
                clb.SetItemChecked(clb.SelectedIndex, state)
            End If
        End Sub

        Public Shared Sub MoveSelectedItemDown(ByVal clb As CheckedListBox)
            Dim i As Integer = clb.SelectedIndex

            If i < clb.Items.Count - 1 Then
                Dim state As Boolean = clb.GetItemChecked(i)

                clb.Items.Insert(i + 2, clb.SelectedItem)
                clb.Items.RemoveAt(i)
                clb.SelectedIndex = i + 1
                clb.SetItemChecked(clb.SelectedIndex, state)
            End If
        End Sub

        Public Shared Sub DeleteSelectedItem(ByVal clb As CheckedListBox)
            Dim i As Integer = clb.SelectedIndex

            clb.Items.Remove(clb.SelectedItem)

            If clb.Items.Count > i Then
                clb.SelectedIndex = i
            Else
                If clb.Items.Count > 0 Then
                    clb.SelectedIndex = clb.Items.Count - 1
                End If
            End If
        End Sub
    End Class
End Namespace