Developing Basic Programs with Gambas

Back to Basics


Visual Basic still has a strong following among the Windows faithful, but Linux users have fewer options for programming in the Basic language. Now the Linux world can turn to Gambas, a free IDE for Linux based on Basic.

By Frank Wieduwilt

Despite its long history, the Basic programming language has always been controversial. Microsoft's Visual Basic has at least allowed a dialect of Basic to establish itself with Windows users. The Open Source community was once limited to only a few marginal Basic projects, such as XBasic [1], but the Gambas project [2] has gained popularity recently as a powerful and versatile Basic development environment for Linux. With an interface that is reminiscent of Visual Basic, Gambas [2] supports efficient programming with a short learning curve. In fact, many believe that Gambas is a far more efficient Basic dialect than VB.

The Gambas IDE includes a debugger for bug hunting, a graphical form editor, and a menu editor. Its modular design means that developers are not restricted to a specific toolkit; Gambas programs can be designed with a Qt or Gtk GUI at the press of a button, although so far the Gtk module is only available with the unstable version.

On your Disk

You'll find the source code for Gambas at the Gambas website [2], along with information on how to obtain binary packages for Debian, Fedora, Gentoo, Slackware, SuSE, and other popular Linux distributions.

To build from the source code, you need the Qt developer packages, version 3.2 or newer, qt3-devel or libqt3-dev. If you intend to use Gambas to develop KDE-specific applications, you will additionally need the KDE library developer packages, kdelibs3-devel or kde-devel.

After installing the supporting packages, unpack the Gambas source code by entering tar -xjf gambas-1.0.3.tar.bz2, change to the directory created by the last step, and type ./configure to set up the makefiles. Then type make to launch into the build. After completing the build, become root and type make install to install the program. You can then launch the developer environment by typing gambas.

In the welcome window, select New Project, and a wizard will take you through a few introductory steps. First of all, you need to decide whether your project will be GUI or text based. You'll also need to specify whether you want Gambas should copy code from an existing project. Then type the name of the project directory where you will store the source code files. If you are unfamiliar to Gambas programming, you might like to check out the sample files to help you get started; these files are also accessible through the start up dialog.

Besides the form you are currently working on, Gambas shows you a project window, a toolbox, a properties window, and the source code editor (Figure 1). The tree view in the project window gives you a useful overview of the files that make up your project. The toolbox allows easy access to the controls available for the GUI, and the properties window allows you to modify the appearance and behavior of the program interface. Finally, the source code editor allows you to edit your Basic code directly.

Figure 1: Gambas has a source code editor, a toolbox, a properties window, and other useful components.

Two windows appear whenever you open a form, so the screen can quickly become cluttered. The KDE developer environment, KDevelop [7], solves this problem by opening elements within a main window; developers would have an easier time with managing the screen space if Gambas used a similar approach.

Forms

Every GUI-based program needs at least one window or a form to accept user input and display output. To create a new form, right click on the tree view in the project window and select New | Form in the drop-down. Type a name for the form in the dialog box and, to specify that you want the window to open automatically when the program is launched, select Startup class. To populate the empty form, add some controls from the Toolbox window (Figure 2).

Figure 2: Gambas has a big collection of controls.

To add a widget to a form, first select the widget in the toolbox and then use your mouse to draw a box of the required size within the form. You can then use the Properties window (Figure 1) to configure the control, for example, by assigning the name that the program will use to reference the control. The X, Y, Width, and Height parameters allow you to define where the control will appear in the window. You can also specify the title, font, and font size, along with the foreground and background colors.

You can add functionality to a Gambas program simply by adding components. Gambas comes with KDE widgets and a basic set of modules for database access. To use these features, select Project | Properties in the project window and then click on the required item in the Components tab.

Using these tools, a developer can quickly put together a tiny HTML viewer just by pointing and clicking. In the toolbox, click on the KDE tab and select the Konqueror icon. Drag the Konqueror element to the required size within the form, and in the properties window, enter the URL of the HTML file the browser will load on launching as the Path.

You can actually use Gambas' own source code editor as an element of your own programming by selecting the qb.qt.editor component. There are also a few experimental modules, such as the XML and Qt extension set. Work is in progress on more components, such as the Gtk extension referred to earlier.

And... Action!

To give the controls you have added something to do, double click an element to open the source code editor with the cursor at the appropriate position in the source code. You can now enter the commands you want to run: for a button, this will typically mean saying what happens when a user clicks the button.

As the name suggests, the menu editor allows you to set up menus. To launch the editor, right click in a form and select the Menu editor item. The menu editor supports a four-level menu structure and allows you to assign keyboard shortcuts and icons to the individual items.

Basic Flavors in Linux

Besides Gambas, Linux also has two other free Basic variants: HBasic [3], a full-fledged developer environment, and the wxBasic [4] interpreter. Additionally, the developers of the commercial KBasic [5] have been promising a fully VB-compatible IDE for quite a while, although a pre-release version is nowhere in sight. PowerBasic [6] and RealBasic are two more commercial, multi-platform dialects - although they do not support development on Linux.

Database Access

One of Visual Basic's biggest strengths is its easy access to databases. Gambas makes a very successful attempt at emulating this useful functionality. At this time of writing. Gambas supports MySQL, PostgreSQL, and Sqlite databases. The Database manager is located in the project window menu below Tools | Database manager.... You must also have the qp.db component in order for a program to access a database.

To add a new table to an existing database, again invoke the context menu and select Create table.... Define the fields for the table on the right side of the dialog. The Data tab takes you to a table view where you can enter data directly. Finally, the Create Gambas code item in the drop down menu creates the source code required to initialize the database for your program.

Help All Round

Gambas has an extensive help system with descriptions for keywords, a short introduction to programming, and an explanation on the differences between Gambas and Visual Basic. The help for individual keywords also refers you to differences between Gambas and VB. The Gambas homepage has a Wiki [8] with more tips on programming and converting existing VB projects.

Converting from Visual Basic

The Gambas and Visual Basic GUIs are similar, so VB users should have no trouble with navigation. Unfortunately, there are major differences between the syntax of Gambas and Visual Basic, so programmers will not be able to migrate VB programs without some attention. This said, the effort involved in converting simple projects without ActiveX controls will typically be acceptable.

INFO
[1] XBasic: http://www.maxreason.com/software/xbasic/xbasic.html
[2] Gambas: http://gambas.sourceforge.net
[3] HBasic: http://hbasic.sourceforge.net
[4] WxBasic: http://wxbasic.sourceforge.net
[5] KBasic: http://www.kbasic.de
[6] PowerBasic: http://www.powerbasic.com
[7] KDevelop: http://www.kdevelop.org
[8] Gambas Wiki: http://www.binara.com/gambas-wiki/bin/view/Gambas10/WebHome