Author Topic: Anatomy of a Gambas 3 project  (Read 7082 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Anatomy of a Gambas 3 project
« on: May 10, 2011, 09:13:15 PM »
I have acquired a wait and see attitude with OOP in Basic. It's interesting to see so many different approaches to the same loose definition of object programming. I like the Gambas approach and here is an example from the distribution.

Code: Text
  1. ' Gambas class file
  2.  
  3. Public Name As String
  4. Public X As Integer
  5. Public Y As Integer
  6. Public ID As Integer
  7.  

Code: Text
  1. ' Gambas class file
  2.  
  3. Public myThing As CThing
  4.  
  5. Static Public Sub Main()
  6.   Dim myForm As Form
  7.   myForm = New FStart
  8.   myForm.Show
  9. End
  10.  
  11. Public Sub btnCreateThing_Click()
  12.   myThing = New CThing
  13.   myThing.Name = "Dummy-Thing"
  14.   mything.X = 11
  15.   mything.Y = 22
  16.   mything.ID = 33
  17.   txtCheckResult.Visible = True
  18. End
  19.  
  20. Public Sub btnCheckThing_Click()
  21.   txtCheckResult.Text = myThing.Name & ", X= " & myThing.X & ", Y= " & myThing.Y & ", ID= " & myThing.ID
  22. Catch
  23.   Message(Error.Text)
  24. End
  25.  
  26. Public Sub btnDestroy_Click()
  27.   myThing = Null
  28.   txtCheckResult.Visible = False
  29. End
  30.  


« Last Edit: May 10, 2011, 09:52:39 PM by ABB »

Aurel

  • Guest
Re: Anatomy of a Gambas 3 project
« Reply #1 on: May 14, 2011, 02:17:02 PM »
John...
This example is great and simple represent real nature of OOP way of programming.
OOP way is very similiar with UDT...

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Anatomy of a Gambas 3 project
« Reply #2 on: July 02, 2011, 10:58:34 PM »
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

Code: [Select]
# 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

Code: [Select]
' 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.


« Last Edit: July 03, 2011, 12:08:09 PM by JRS »