Write GUI apps in Pascal with Lazarus
David Bolton explains how to get started with Lazarus and Free Pascal to develop GUI applications running on Linux.
LAZARUS
Credit: www.lazarus-ide.org
OUR EXPERT
David Bolton has 30 years of Pascal programming experience under his belt, so knows a thing or two about developing GUI programming applications on Linux and other platforms.
The source code for this example is zipped up in the file Lazarusexample.zip at https://github.com/David-H-Bolton/Projects. Just open the link in Firefox, click on the file, then the three dots on the right, and download it.
Creating GUI apps in Linux is more complex than making simple apps that run in a terminal. C There are several ways that are mostly GTK-based with C or C++, such as GTK+/C, Qt/C++, FLTK/C++ and WxWidgets/C++. You can also use the Python library PyGObject.
There is another, easier way if you don’t mind learning Pascal. Install Lazarus (see boxout opposite), an IDE that also installs the cross-platform opensource programming language Free Pascal. This lets you create GUI apps visually by dropping icons on a form, setting their properties, then compiling and running the program.
After installing it, you can start Lazarus with the command startlazarus in a terminal. That opens the Lazarus editor. You should see five different windows:
1. The top one is the main window, with the menu and all components and their icons.
2. To the left is the Object Inspector. This shows the properties for the selected component on the Visual form (see 4).
3.
The source editor is where you edit the source code of your Pascal application.
4.
The gridded window with the title Form1 is the Visual form.
5. A
Messages window at the bottom. This shows the compile results.
GUI applications in Free Pascal are made up of several file types. There’s the main program file, which has an LPR extension (short for Lazarus PRoject). It’s not visible initially but click the Project menu and then click View Project Source (at the bottom of the menu that appears), and it opens in the source editor.
Anything with a $ inside comment braces {$ } is a compiler directive. The first directive {$mode objfpc} tells it to compile in objfpc mode (see the online documentation). The {$H+} tells it to use long strings.
The uses block is a list of units that are needed to provide functions and procedures. In Pascal, a unit is a module that is linked in to your application at compile time. It’s a handy way of splitting your application into smaller parts. Free Pascal comes with many standard units, such as forms, classes, sysutils and interfaces. You can also create your own units.