Author Topic: Gambas  (Read 23226 times)

JRS

  • Guest
Gambas
« on: January 14, 2011, 03:03:47 PM »
It doesn't look like there is much interest in my VB6 under Linux presentation so I thought I would chat about Gambas instead. If you like VB classic, you're going to love Gambas. (sorry Windows users) Here is a chart building example to introduce some of the language concepts and use.



Gambas is a Basic development environment supporting the Basic programming language with object extensions. It includes an IDE, a BASIC compiler, an interpreter, an archiver and a graphical user interface component. Gambas is an interpreted language, with no "just-in-time" or other compilation to native code at all. Although not intended to be a Visual Basic clone, it has a visual rapid application development interface like VB. With Gambas, you can quickly design your program GUI with QT or GTK+, access MySQL, PostgreSQL, Firebird, ODBC and SQLite databases, pilot KDE applications with DCOP, translate your program into any language, create network applications easily, make 3D OpenGL applications, make CGI web applications, and so on...

What Is Gambas?  (VB on X)

Online Documentation

Gambas vs.Visual Basic (Differences between the language implementations)

Supported operating systems include Linux and FreeBSD, OpenBSD.



Entry Form Class
Code: [Select]
' Gambas class file

PUBLIC SUB btnClose_Click()
  ME.Close
END

PUBLIC SUB btnDraw_Click()
  DIM total AS Integer
  total = 10
  WITH FormChart
    .value = NEW Float[]
    .total = total
    .value.Resize(total + 1)
    TRY .value[1] = Val(textbox1.Text)
    TRY .value[2] = Val(textbox2.Text)
    TRY .value[3] = Val(textbox3.Text)
    TRY .value[4] = Val(textbox4.Text)
    TRY .value[5] = Val(textbox5.Text)
    TRY .value[6] = Val(textbox6.Text)
    TRY .value[7] = Val(textbox7.Text)
    TRY .value[8] = Val(textbox8.Text)
    TRY .value[9] = Val(textbox9.Text)
    TRY .value[10] = Val(textbox10.Text)
    .Show
  END WITH
END

PUBLIC SUB Form_Open()
END

PUBLIC SUB TextBox6_KeyPress()
END

FormData.form
Code: [Select]
# Gambas Form File 2.0

{ Form Form
  MoveScaled(29.75,16.375,50.375,43.25)
  Text = ("Chart example")
  Border = Window.Fixed
  { TextLabel1 TextLabel
    MoveScaled(3,2,6.75,3.25)
    Text = ("Data 1")
  }
  { TextLabel2 TextLabel
    MoveScaled(3,6,6.75,3.25)
    Text = ("Data 2")
  }
  { TextLabel3 TextLabel
    MoveScaled(3,10,6.75,3.25)
    Text = ("Data 3")
  }
  { TextLabel4 TextLabel
    MoveScaled(3,14,6.75,3.25)
    Text = ("Data 4")
  }
  { TextLabel5 TextLabel
    MoveScaled(3,18,6.75,3.25)
    Text = ("Data 5")
  }
  { TextLabel6 TextLabel
    MoveScaled(3,22,6.75,3.25)
    Text = ("Data 6")
  }
  { TextLabel7 TextLabel
    MoveScaled(3,26,6.75,3.25)
    Text = ("Data 7")
  }
  { TextLabel8 TextLabel
    MoveScaled(3,30,6.75,3.25)
    Text = ("Data 8")
  }
  { TextLabel9 TextLabel
    MoveScaled(3,34,6.75,3.25)
    Text = ("Data 9")
  }
  { TextLabel10 TextLabel
    MoveScaled(3,38,6.75,3.25)
    Text = ("Data 10")
  }
  { TextBox1 TextBox
    MoveScaled(11,2,14,3)
    Text = ("")
  }
  { TextBox2 TextBox
    MoveScaled(11,6,14,3)
    Text = ("")
  }
  { TextBox3 TextBox
    MoveScaled(11,10,14,3)
    Text = ("")
  }
  { TextBox4 TextBox
    MoveScaled(11,14,14,3)
    Text = ("")
  }
  { TextBox5 TextBox
    MoveScaled(11,18,14,3)
    Text = ("")
  }
  { TextBox6 TextBox
    MoveScaled(11,22,14,3)
    Text = ("")
  }
  { TextBox7 TextBox
    MoveScaled(11,26,14,3)
    Text = ("")
  }
  { TextBox8 TextBox
    MoveScaled(11,30,14,3)
    Text = ("")
  }
  { TextBox9 TextBox
    MoveScaled(11,34,14,3)
    Text = ("")
  }
  { TextBox10 TextBox
    MoveScaled(11,38,14,3)
    Text = ("")
  }
  { btnDraw Button
    MoveScaled(30,2,19,4)
    Text = ("&Draw it")
  }
  { btnClose Button
    MoveScaled(30,7,19,4)
    Text = ("&Close")
  }
}

   

Chart Class
Code: [Select]
' Gambas class file

' ======================================
' This example is to make a bar chart
' using DrawingArea
' may be it can help you to make a chart
' if you have any question you can send to
'   yudi@kecoak.or.id
' Thank You

PUBLIC total AS Integer
PUBLIC value AS Float[]

PUBLIC SUB btnClose_Click()
  ME.Close
END

PUBLIC SUB _new()
  ME.Center
END

PUBLIC SUB Form_Open()
  Draw_Chart
END

PUBLIC SUB Draw_Chart()
  DIM i AS Integer
  DIM skala_1 AS Integer
  DIM skala_2 AS Integer
  DIM distance_x AS Float
  DIM distance_y AS Float
  DIM width_draw AS Integer
  DIM tot AS Integer
  DIM colors AS Integer[]
  DIM bottom AS Integer
  DIM sumdata AS Integer

  colors = NEW Integer[]
  colors.Resize(total + 1)

  FOR i = 1 TO total
    sumdata = sumdata + value[i]
  NEXT

  IF sumdata = 0 THEN sumdata = 1

  FOR i = 1 TO total
    value[i] = (value[i] / sumdata) * 10
  NEXT

  drwchart.Clear

  draw.Begin(drwchart)

  skala_1 = drwchart.ClientH / 11
  distance_y = drwchart.ClientH - skala_1
  bottom = distance_y + 8

  FOR i = 0 TO 100 STEP 10
    draw.ForeColor = color.black
    draw.Text(i, 0, distance_y)
    draw.Line(25, distance_y + 8, drwchart.clientw, distance_y + 8)
    distance_y = distance_y - skala_1
  NEXT

  draw.Line(30, 0, 30, drwchart.ClientH)

  skala_2 = (drwchart.ClientW - 30) \ 10
  distance_x = skala_2 + 30
  width_draw = skala_2 / 2

  FOR i = 1 TO Total
       draw.LineWidth = 1
       draw.ForeColor = color.Black
       draw.Text(i, distance_x - (width_draw / 2) - 4, drwchart.ClientH - 20)
       draw.Line(distance_x - (width_draw / 2), 0, distance_x - (width_draw / 2), drwchart.ClientH - skala_1 + 8)
       draw.LineWidth = width_draw
       draw.ForeColor = color.RGB(i * 100, i * 10, i * 50)
       colors[i] = draw.ForeColor
       tot = skala_1 * value[i] + skala_1 - 8
       draw.Line(distance_x - (width_draw / 2), bottom, distance_x - (width_draw / 2), drwchart.ClientH - tot)
       distance_x = distance_x + skala_2
  NEXT

  DRAW.End
END

PUBLIC SUB btnAbout_Click()
  DIM i AS String
  i = "<h2>Example to make bar chart</h2>\n"
  i = i & "This example has made by : " & Chr(10)
  i = i & "   Yudi Astira" & Chr(10)
  i = i & "   yudi@kecoak.or.id" & Chr(10)
  i = i & "   necrose #hdteam on Dal.Net" & Chr(10)
  i = i & "Thank You"
  message.Info(i, "&Close")
END

PUBLIC SUB drwChart_Draw()
END

FormChart.form
Code: [Select]
# Gambas Form File 2.0

{ Form Form
  MoveScaled(0,0,72,50)
  Background = &HFFEFBF&
  Text = ("Example to make a Bar Chart")
  Border = Window.Fixed
  { bg PictureBox
    MoveScaled(1,1,70.25,43.375)
    Background = &HFFFFFF&
    Border = Border.Plain
  }
  { drwChart DrawingArea
    MoveScaled(2,2,69,41)
    Background = &HFFFFFF&
    Cached = True
  }
  { btnClose Button
    MoveScaled(56.25,45,15,4)
    Text = ("&Close")
  }
  { btnAbout Button
    MoveScaled(39.875,45,15,4)
    Text = ("&About")
    Picture = Picture["graph.png"]
  }
  { TextLabel1 TextLabel
    MoveScaled(2,2,4,2.625)
    Background = &HFFFFFF&
    Text = ("(%)")
  }
}

Hint: - Compiling from the command line.
  • Navigate to your project folder ( the folder where all your .form, .class & .module files are located )
  • Type the command gbc2 -a {full folder path}
  • Type the command gba2 -o {full folder path}/myprojectname.gambas
  • You will then have the myprogramname.gambas executable in the specified -o folder.
« Last Edit: January 15, 2011, 01:26:29 AM by JRS »

JRS

  • Guest
Re: Gambas
« Reply #1 on: January 15, 2011, 03:01:16 AM »
* There is no global variables in Gambas!

As a workaround, put them in your main module and declare them as PUBLIC. If you do not have a main module in your project, but a main form, then declare them as STATIC PUBLIC. To access these variables, you must use the name of the main module or form: MyMainModule.MyGlobalVariable or MyMainForm.MyGlobalVariable.
 

* To know if a string is empty, it is not necessary to use the Len() function. You can directly test it, as an empty string is FALSE and a non-empty string is TRUE.
Code: [Select]
For example, instead of doing :

IF Len(MyString) > 0 THEN ...
IF Len(MyString) = 0 THEN ...

You should do :

IF MyString THEN ...
IF NOT MyString THEN ...

* The LAST keyword returns the last control that has received an event. This is very useful when you want to write an event handler that is independent of any control name. For example, let's suppose you want to write a calculator program. You have defined ten buttons, one for each digit, each one in the same group "Digit". The Tag of each control is set to the digit drawn in the button. Your event handler may look like that :
Code: [Select]
PUBLIC SUB Digit_Click()
  Display = Display & LAST.Tag
  RefreshDisplay
END

* String functions Left$, Right$ and Mid$ have useful behaviors in Gambas. The second parameter of Left$ and Right$ is optional, and is one by default.
Code: [Select]
Left$("Gambas") returns "G"
Right$("Gambas") returns "s"

This second parameter can be negative, and then gives the number of characters not to extract.
Left$("Gambas", -2) returns "Gamb"
Right$("Gambas", -2) returns "mbas"

Likewise, the third argument of Mid$ can be negative, and then gives the number of characters from the end of the string not to extract.
Mid$("Gambas", 2, -2) returns "amb"

* You can use the DEBUG instruction to print debugging messages to the console (namely the standard error output). It behaves exactly like the PRINT instruction. These messages are prefixed with the class name, method name and line number of the DEBUG instruction. The debugging messages are automatically removed when creating an executable without debugging information.

* Error management in Gambas is done with the following instructions:

TRY tries to execute a statement without raising an error. The ERROR instruction is used just after to know if the statement was executed correctly.
Code: [Select]
TRY MyFile = OPEN "/etc/password" FOR WRITE
IF ERROR THEN PRINT "I cannot do what I want!"

* Error management in Gambas:

CATCH indicates the beginning of the error management part of a function or procedure. It is put at the end of the function code. The catch part is executed when an error is raised between the beginning of the function execution and its end. If an error is raised during the execution of the catch part, it is normally propagated.
Code: [Select]
SUB ProcessFile(FileName AS STRING)
  ...
  OPEN FileName FOR READ AS #hFile
  ...
  CLOSE #hFile
CATCH ' Executed only if there is an error
  PRINT "Cannot process file "; FileName
END

FINALLY introduces the code executed at the end of the function, even if an error was raised during its execution. The finally part is not mandatory. If there is a catch part in the function, the finally part must precede it. If an error is raised during the execution of the finally part, it is normally propagated.
Code: [Select]
SUB ProcessFile(FileName AS STRING)
  ...
  OPEN FileName FOR READ AS #hFile
  ...
FINALLY ' Always executed, even if a error is raised
  CLOSE #hFile
CATCH ' Executed only if there is an error
  PRINT "Cannot print file "; FileName
END

* - Current Directory
Quote
> If you have to start using things like 'EXEC [ "pwd" ] TO sPwd' to get
> the current dir it doesn't make sense to use a Gambas script.
>

There is no current directory in Gambas!

--
BenoƮt Minisini
« Last Edit: January 16, 2011, 11:08:15 PM by JRS »

JRS

  • Guest
Re: Gambas
« Reply #2 on: January 15, 2011, 03:20:15 PM »
Quote
Gambas components are shared libraries written in C or C++ that add new functions to the Gambas interpreter.

If you're a Linux (Ubuntu) user this is painless to install via synaptics installer (gnome default) . Building it from scratch is a challenge. (dependencies, time to compile this monster, ...)

The IDE/GUI designer is written in Gambas so you have a professional class example as a reference. It comes with sample programs to help you get started. (see attachments in the previous post for a few from the distribution)
« Last Edit: January 16, 2011, 04:24:01 AM by JRS »

JRS

  • Guest
Re: Gambas
« Reply #3 on: January 15, 2011, 09:01:43 PM »
Gambas can create GUI applications using either the Qt or Gtk toolkits. Here is a quick Gtk example and the project files it generated.



FMain.class
Code: [Select]
' Gambas class file

PUBLIC SUB _new()

END

PUBLIC SUB Form_Open()

END

FMain.form
Code: [Select]
# Gambas Form File 2.0

{ Form Form
  MoveScaled(0,0,42,22)
  Text = ("Gambas Gtk Window")
  { Label1 Label
    MoveScaled(1,3,12,2)
    Text = ("My Label")
  }
  { TextBox1 TextBox
    MoveScaled(14,3,19,3)
    Text = ("")
  }
  { Button1 Button
    MoveScaled(14,10,14,5)
    Text = ("&Quit")
  }
}
« Last Edit: January 16, 2011, 04:27:54 AM by JRS »

JRS

  • Guest
Re: Gambas
« Reply #4 on: January 15, 2011, 10:47:35 PM »
The Gambas interpreter can be used as a console mode scripting language using the gbs2 program. (this isn't included by default and you need to install the Gambas Scripter)

scriptest.gambas
Code: [Select]
DIM x AS Integer

FOR x = 1 TO 10
  PRINT x
NEXT

jrs@Laptop:~/gambas/test$ gbs2 scriptest.gambas
1
2
3
4
5
6
7
8
9
10
jrs@Laptop:~/gambas/test$
« Last Edit: January 17, 2011, 09:45:30 PM by JRS »

JRS

  • Guest
Re: Gambas
« Reply #5 on: January 16, 2011, 12:04:20 AM »
Ubuntu offers the Gambas 2.21 release via the Synaptic installer. A 2.22 release is available with a bunch of Gtk fixes. Another being a better compiling experience under Ubuntu 10.10 - what the author is now developing under.

Here is a good way to get what you need. (packages already installed and current will be skipped using the below apt install command line)

Code: [Select]
sudo apt-get install build-essential autoconf libbz2-dev libfbclient2 libmysqlclient-dev unixodbc-dev libpq-dev libsqlite0-dev libsqlite3-dev libgtk2.0-dev libldap2-dev libcurl4-gnutls-dev libgtkglext1-dev libpcre3-dev libsdl-sound1.2-dev libsdl-mixer1.2-dev libsdl-image1.2-dev libsage-dev libxml2-dev libxslt1-dev libbonobo2-dev libcos4-dev libomniorb4-dev librsvg2-dev libpoppler-dev libpoppler-glib-dev libasound2-dev libesd0-dev libdirectfb-dev libaa1-dev libxtst-dev libffi-dev kdelibs4-dev firebird2.1-dev libqt4-dev libglew1.5-dev libimlib2-dev libv4l-dev libsdl-ttf2.0-dev libgnome-keyring-dev libgdk-pixbuf2.0-dev

Success!



I now have a current version of Gambas with all it's features enabled. This is a first for me as I have tried on CentOS many times before. (never a full build)

      Click image for animated version.

Take some time to look Gambas over and I think you will be pleasantly surprised.
« Last Edit: January 19, 2011, 03:18:24 AM by JRS »

JRS

  • Guest
Re: Gambas
« Reply #6 on: January 16, 2011, 11:15:31 PM »
I only can say that is good becose there is no windows version.

Gambas works best on Linux because it's community supported and is a conglomeration of technologies. Windows promotes proprietary concepts, unfair competition, patients and greed.  


Quote
I never wanna use such a program on windows.

You will want to stay away from Visual Basic as well. That is what Gambas is modeled after.

« Last Edit: January 16, 2011, 11:20:55 PM by JRS »