Friday 26 October 2012

Gambas is no shrimp

It may mean "prawn" in Spanish, but in the world of computing, Gambas is a visual programming language for Linux.

 

The Gambas application is an integrated development environment (IDE) which not only allows you to create, edit, run and debug your software, but also create executables (programs) and package them for distribution and deployment to other Linux computers.

 

This post does not cover the Gambas language in any detail, its more of an over-view which takes you through the whole process of creating an application, from writing a very simple program to creating a deployment package.

Gambas is more than just a language, it is also a rapid application development system, making it an ideal starting point for those wishing to learn programming or for programmers taking their first steps with Linux...
...and also (like Visual Basic) its a great choice for anyone wanting to get results quickly. If I may para-phase the great guru Dan Appleman, "I can do clever stuff in C++ but I'm much, much more productive in Gambas."

What's in a name?

The name "Gambas" is a recursive acronym (a kind of programmers joke) for "Gambas Almost Means BAsic", which presumably expands to "Gambas (Gambas Almost Means BAsic) Almost Means BAsic" which expands to "Gambas (Gambas (Gambas Almost Means BAsic) Almost Means BAsic) Almost Means BAsic" and so on.

Oh how we laughed!

Install Gambas

With Debian based Linux distributions you will probably find Gambas in the repository. For example, on Lubuntu you can open Synaptic package manager and select and install Gambas. For the purpose of this post, it does not matter which version you end up with, as we are only going to create a very simple first project.

From your file manager I suggest you create a new directory to hold all your wonderful Gambas projects (e.g. "Gambas").

Create a new project

Run Gambas, and select New Project from the initial screen.
  • For Project Type select Graphical Application.
  • For Project Directory select your newly created "Gambas" directory.
  • For Project Name lets call it: MyFirstApp (this name is used to create a directory for this project).
  • For Project Title let's call it: HelloWorld (this is the title that will appear on your window title bar)

The Gambas IDE should now appear with a left panel containing the project tree-view (if not, just press "F10" to make it visible).



Double click on the FMain node...you should now see a blank "form", which is the design "window" that will be displayed when your project runs.

To the right side of the screen there should be a Properties list for "FMain Form".

Click on the blank cell to the right of the "Background" property. Hopefully this cell now contains "..." which you can click to display the "Select a colour" dialog.
Now select the "Free" tab and select a hideous colour of your own choice, but please make it a light shade.

Once you OK this change, your main form should now be your chosen colour (nice!...it matches your eyes.)



Now also on the right, just below the FMain properties, you should see the Toolbox.

Click to select the giant A, then point to the form and click & drag. This should give you a label (initially called "Label1") which you can re-size and move around the screen using the little grab handles.

Back on the right side, the Properties list should now be called "Label1 Label". (If not, click the label on your form to select it).
Move down the Label1 Property list until your reach "Text", then change the current text (probably "Label1") to "This is Gambas".

You can now re-size the label on the form if it is too small to show all your text.
Now create a second label and position it somewhere on the form below the first one.

Returning to the Toolbox, click the right arrow by the tabs to move through the tool collections until you reach "Special".


Click on the stop-watch icon (the Timer) then draw this on the form, as you did for the labels.
As this timer is not going to be visible, don't worry about size or position.

Now with the timer selected (can you see the grab handles?) go to the Properties on the right and click on the "False" next to the "Enabled" property. Make this "True"
Also click on the Delay value and change this to 5000 (...just to increase the suspense!).



Back on the form, double click on your timer icon.
This should open up a new window (the "scary code window") with the cursor blinking at you, somewhere between the lines:-
 "Public Sub Timer1_Timer()" 
...and...
 "End"

Type the following so that it appears between those lines:-
Label2.Text = "Hello World!!!"

(...Wow! your first line of Gambas code!)

So your Timer1_Timer routine should now look something like this:-
 Public Sub Timer1_Timer()

  Label2.Text = "Hello World!!!"
 
 End

That's it, all done.

Test your program

Click on the play control (>) in the toolbar or just hit "F5".

Your program should run and you should initially see a window like mine:-


After 5 seconds, the text in Label2 should change to: Hello World!!!

If your program does not run like this, go back and check what you have done against these instructions. You'll be doing a lot of this.....its called "debugging"

What have you done?

Well, so far you have written a very simple program which (hopefully) runs. In doing this you have:-
  1. modified the default design form by setting the forms Background property
  2. added a label which just shows text that you set, again using the appropriate property
  3. added a second label which you control "at run time" (in other words your program writes a new message).
  4. used a timer to delay execution of your message by 5000 milliseconds (= 5 seconds).
Basically, when your program runs, the main form loads as a display window and the Timer runs. After the timer delay has expired, the Timer runs your one line of code and the contents of Label2 is updated with your text.

Although the program does not appear to do anything else, it will carry on re-writing your message into Label2 every 5 seconds until you close the program.

The re-work phase

Now is a good time to review your design and make any changes, before you start shipping this killer-app to your customer base.

Things you may care to consider include:-
  1. is 5 seconds longer than your typical customers attention span?
  2. what about removing the default text "Label2" from Label2 "Text" Property, so this is not seen by your users?
  3. was that Background colour really a good idea?

Make an exe

Even if you don't want this program for your own personal use, its a good idea to make an executable file for testing purposes.

This is simple. With your project still loaded in the IDE (but not running) select menu Project > Make > Executable...

Just note where Gambas is going to put it, then OK.

Now use your file manager to navigate to the noted directory, and double click on the file: MyFirstApp.gambas

If it runs as expected, you are ready to create a deployment package.

Make an installation package

Use the file manager to create a new directory for your Gambas Packages. lets call it: Packages

Back in the Gambas IDE, go to menu Project > Make > Installation Package...
On the screens that follow you can:-
  • Add a simple description: The App that shook the world
  • Target Distribution: {hopefully yours is listed, or if your distro is Debian based, just select Debian}
  • Package Group: {select "games"...doesn't really matter}
  • Menu Entry: Games/Toys
  • Desktop Config: {just ignore this for now}
  • Extra Dependencies & Extra Files: {ignore these as well}
  • Destination directory: Packages
  • Now click OK and the process begins.

Once successfully completed, you should have a number of files in the Packages directory. If you created a package for Debian, one of these files will have a name similar to this:-
 myfirstapp_0.0.2-1_all.deb

If you double click on this file (of open with GDebi Package Installer), you can test that your first app installs on your system.

Don't Stop Now!

Maybe coding is not for you after all.

But if you are still having fun, continue fiddling with the Properties for your FMain and the labels.

Maybe add a few more simple controls from the Toolbox and see what you can do.

Further Reading

Checkout the Gambas Documentation from the Help menu.

Here are a few links you may also find useful:-
The Gambas Site:  http://gambas.sourceforge.net/en/main.html
The Beginners Guide: http://beginnersguidetogambas.com/
Gambas Forum, here: http://whiteislandsoftware.com/forum/index.php?page=forumview and here: http://www.linuxbasic.net/index.php

Who is Dan Appleman? http://danappleman.com/

No comments:

Post a Comment