Gambas 3 TreeView and TabStrip Example Code

I sometimes play around with Xojo and PureBasic for quick and simple GUI apps since the interface designers are convenient and the language makes for easy coding. I was checking out Gambas 3 and found it to be fairly good but lacking in a lot of examples. I was messing around with TreeView and TabStrip working together. It took a bit and I am sure there are better ways to approach this, but if you need examples of either of these controls this is working code.

I have a form with an HSplit. If the left side I have a TreeView and the right I have a TabStrip. I left names default and made the object expand to the window. I still haven’t figured out how to resize either side of the HStrip. This just loads some books off the file system to populate the treeview and creates a tab when clicked. I also added tab delete buttons. I have a module for the base path, but you can set it in form open. Screen shot here.

At this point I am thinking I will need to subclass and make my own form objects as this was a pain in the ass. Maybe this will help someone.

Public Sub TabStrip1_Close(idx As Integer)
  Try TabStrip1[idx].Text = Null
    TabStrip1[idx].Delete()
  Catch
    TabStrip1[idx].Text = Null
    TabStrip1.Closable = False
End

Public Sub TabStrip1_Click()
  Dim k As String
  If Len(TabStrip1[TabStrip1.Index].Text) > 0 Then
    Message(TabStrip1[TabStrip1.Index].Text)
  Endif
End

Public Sub TreeView1_Click()

  Dim counter As Integer

  If TreeView1.Item.Key Begins "root_" Or TreeView1.Item.Key Begins "parent_" Then
    'Do nothing For parent items or capture parent events.
    Return
  Endif
  
  If TreeView1.Item.Children = 0 Then
    
    If TabStrip1.Index = 0 And Len(TabStrip1[TabStrip1.Index].Text) = 0 Then
      If Len(TreeView1.Item.Text) > 0 Then
        If Len(TreeView1.Item.Text) > 0 Then
          TabStrip1[TabStrip1.Index].Text = TreeView1.Item.Text
          TabStrip1.Closable = True
          TabStrip1.Count = 1
        Endif
      Endif
    Endif
    
    For counter = 0 To TabStrip1.Count - 1
      If TreeView1.Item.Text = TabStrip1[counter].Text Then
        'Focus tree item on corresponding tab select.
        TabStrip1.Index = counter
      Endif
    Next

    If TreeView1.Item.Text = TabStrip1[TabStrip1.Index].Text Then
      Return
    Else
      'Add new tab if not in tabs.
      TabStrip1.Count += 1
      TabStrip1[TabStrip1.Index].Text = TreeView1.Item.Text
    Endif
  Endif
End


Public Sub Form_Open()

  Dim File As String
  Dim path_parts As String[]
  Dim file_parts As String[]
  Dim cat As String
  Dim cat_key As String
  Dim title As String
  Dim treeIndex As Integer
  
  'Dim img_book As New Picture(16, 16, False)
  'Dim img_cat As New Picture(16, 16, False)
  'Dim img_root As New Picture(16, 16, False)
  'img_book.Load("book.png")
  'img_cat.Load("cat.png")
  'img_root.Load("root.png")
  
  'Globals.basePath = "~/ebooks"
  If Not Exist(globals.basePath) Then
    Mkdir globals.basePath
  Else
    'TreeView1.Add("root_EBooks", "EBooks", img_root) 'With icon.
    TreeView1.Add("root_EBooks", "EBooks")
    For Each File In RDir(globals.basePath, "*.epub").Sort()
      
      path_parts = Split(File, "/")
      cat = path_parts[0]
      cat_key = "parent_" & cat
      file_parts = Split(path_parts[1], ".")
      title = file_parts[0]
      
      If Not TreeView1.Exist(cat_key) Then
        TreeView1.Add(cat_key, cat,, "root_EBooks")
      Endif
      
      treeIndex += 1
      TreeView1.Add(title, title,, cat_key)
    Next
  Endif

End