As Gambas 3 approaches its release, I thought I would introduce what a Gambas project would like from a form and class file aspect. The Gambas IDE/GUI designer is written in Gambas using Qt based GUI components. (Gambas also offers a Gtk option as well.)
TreeViewExample.form# Gambas Form File 3.0
{ Form Form
MoveScaled(25,11.1429,73,54)
Text = ("TreeView Example")
Icon = Picture["treeview.png"]
Resizable = False
{ Help Menu
Text = ("Help")
{ About Menu
Text = ("About")
}
}
{ TreeView1 TreeView
MoveScaled(1,1,24,48)
Editable = True
}
{ Button1 Button
MoveScaled(54,1,17,4)
Text = ("Insert Name")
}
{ Button2 Button
MoveScaled(54,6,17,4)
Text = ("Remove Name")
}
{ TextBox1 TextBox
MoveScaled(27,1,22,4)
}
{ Frame1 Frame
MoveScaled(27,11,44,13)
Text = ("Selected item")
{ TextLabel1 TextLabel
MoveScaled(1,4,38,8)
}
}
{ Label2 Label
MoveScaled(27,25,39,4)
Font = Font["Bold"]
Text = ("Events")
}
{ TextArea1 TextArea
MoveScaled(27,29,44,15)
ReadOnly = True
}
{ Button3 Button
MoveScaled(51,45,20,4)
Text = ("&Clear events")
}
{ RadioButton1 RadioButton
MoveScaled(27,6,12,4)
Text = ("Male")
Value = True
}
{ RadioButton2 RadioButton
MoveScaled(40,6,13,4)
Text = ("Female")
}
}
TreeViewExample.class' Gambas class file
Public intEventNumber As Integer
Public Sub Form_Open()
Dim picMale As Picture
Dim picFemale As Picture
picFemale = Picture["Female.png"]
picMale = Picture["Male.png"]
'This will populate our treeview with our starting entries
'Note: I'll just keep the entries text and its key the same to keep it simple
TreeView1.Add("Bill", "Bill", picMale)
TreeView1.Add("Ted", "Ted", picMale, "Bill")
TreeView1.Add("Sally", "Sally", picFemale, "Bill")
TreeView1.Add("Frank", "Frank", picMale, "Sally")
'TreeView1.MoveCurrent
'TreeView1.Item.Selected = TRUE
'TreeView1.Item.Expanded = TRUE
TreeView1["Bill"].Expanded = True
End
Private Sub RefreshInfo()
'This little check just updates our label so that we know how many
'children an entry has.
Dim sText As String
If Not TreeView1.Current Then
Textlabel1.Text = ""
Return
Endif
With TreeView1.Current
If .Children > 1 Then
sText = .Text & (" has ") & .Children & (" children.")
Else If .Children = 0 Then
sText = .Text & (" has no children.")
Else
sText = .Text & (" has 1 child.")
End If
sText &= ("<br>Item rect is (") & .X & "," & .Y & "," & .W & "," & .H & ")"
End With
TextLabel1.Text = sText
End
Public Sub TreeView1_Click()
'This just updates our event stack
Stack_Event_Log("Click")
End
Public Sub Button1_Click()
Dim sIcon As String
Dim sParent As String
If Textbox1.Text <> Null Then
If RadioButton1.Value Then
sIcon = "Male.png"
Else
sIcon = "Female.png"
End If
'Gets the parent item: the current item, or nothing is the treeview is void
sParent = TreeView1.Key
'Now we will add a new entry with a key and a name of what was in the text box
'We will place it as a child of the currently selected entry
TreeView1.Add(Textbox1.Text, Textbox1.Text, Picture[sIcon], sParent).EnsureVisible
TreeView1.Item.EnsureVisible 'This will make sure that the item we just added to the list is in the visable area of the control. (Scrolling if necessary)
TextBox1.Text = "" 'This empties out textbox
RefreshInfo ' This will update our label and reflect the new number of kids
End If
End
Public Sub Button2_Click()
If Not TreeView1.Key Then Return
'Lets remove the current cursor item
TreeView1.Remove(TreeView1.Key)
'Now move the cursor to the current item (since we are now pointing at a deleted item)
'But first we check the count to make sure we didn't delete the last item in the list
'if we did then we obviously don't run this part.
If TreeView1.Count > 0 Then
'TreeView1.MoveCurrent
'This selects or 'highlights' our current item
'TreeView1.Current.Selected = TRUE
'This will update our label and reflect the new number of kids
RefreshInfo
End If
End
Public Sub TreeView1_Collapse()
'This just updates our event stack
Stack_Event_Log("Collapse")
End
Public Sub TreeView1_DblClick()
'This just updates our event stack
Stack_Event_Log("DblClick")
End
Public Sub TreeView1_Select()
'This just updates our event stack
RefreshInfo
Stack_Event_Log("Select")
End
Public Sub TreeView1_Delete()
'This just updates our event stack
Stack_Event_Log("Delete")
End
Public Sub TreeView1_Expand()
'This just updates our event stack
Stack_Event_Log("Expand")
End
Public Sub Button3_Click()
TextArea1.Text = ""
'IntEventNumber = 0
End
Public Sub About_Click()
Message.Info(("TreeView example written by C. Packard and Fabien Hutrel."))
End
Public Sub TreeView1_Activate()
'This just updates our event stack
Stack_Event_Log("Activate")
End
Public Sub TreeView1_Rename()
'This just updates our event stack
Stack_Event_Log("Rename")
End
Private Sub Stack_Event_Log(anevent As String)
'This updates our event stack, this sub is used by all events... it display the current node key.
Dim sKey As String
Try sKey = TreeView1.Item.Key
TextArea1.Text = "Event(" & intEventNumber & "): " & anevent & " Item: '" & sKey & "'\n" & TextArea1.Text
intEventNumber = intEventNumber + 1
End
Public Sub TreeView1_Cancel()
Stack_Event_Log("Cancel")
End
Application can be run/debugged from the IDE.