8

Lazarus: Risen From The Grave? | Late Developer

 2 years ago
source link: https://latedev.wordpress.com/2013/03/15/lazarus-risen-from-the-grave/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Lazarus: Risen From The Grave?

March 15, 2013

Introduction

Like many programmers "of a certain age" (such as the divine Ms. Stob), I used to do a lot of work with Borland’s Delphi product. When this came out back in 1995, it trounced its main rival, Visual Basic, both in terms of ease-of-use and of performance. Unfortunately, criminal incompetence on the part of its various owners, and the departure of its chief architect to Microsoft (where he designed C#, among other products) has seen its market share dwindle to almost nothing in recent years, and its pricing structure hasn’t helped, in an era of free software.

However, Delphi has always had its fanatical adherents, and some of them have created an Open Source version of the product called Lazarus. Available for Windows, Linux and the Mac, Lazarus provides a complete IDE for developing GUI applications (and non-GUI ones, if the fancy takes you) using the Object Pascal programming language. In this article, I kick the tyres of Lazarus by writing a very small "hello world" application.

Hello World

I am renowned for my exquisite taste in GUI design, and here is the final result of my labours using Lazarus. The user enters their name, clicks on a button, and the app greets them. After 5 seconds, both the input and the greeting fields are cleared automatically.

laz-done

I’ll now show you how I created this magnificent application!

Launching Lazarus

When you start Lazarus (which is a very fast operation compared with the likes of Visual Studio), you will be presented with a default application project containing a single empty form:

The GUI should be familiar to old Delphi users, and won’t hold many surprises for others. At the top, you have the menu and toolbar, together with a tabbed palette containing the numerous GUI and non-GU controls that Lazarus provides. To the left is the object inspector, which allows the properties of the controls to be changed. In the centre are the two windows for editing the code and the components of the form you are working on, and at the bottom is a window for compiler messages and the like. Lazarus is resolutely multi-window – there is no option to tile the IDE.

Changing Properties

The first thing I want to do is to change some of the properties of the form. I’m going to change its title, and give it a non-sizable border. To do that, I clicked on the form in the IDE and then started changing things in the Object Inspector. First the form’s title; notice that as the form is also a control it uses the name "Caption" for this property:

Changing the border style uses a dropdown menu provided by the Object Inspector:

laz-border

I want a non-sizeable, dialog-style border, so I chose bsDialog.

Testing The Changes

One of the main advantages of an IDE is that it encourages the "make a small change and see if it worked" style of programming, which discourages bugs from creeping into the code. I now want to run the program to see if the changes I’ve made look good. To do that, I simply hit the Run button on the toolbar. It’s here that difference from Delphi becomes obvious – the compile and link times for the program are nowhere near as good as they would be for Delphi. It’s not exactly slow (certainly its faster than a C++ compile and link would be), but there is a definite pause. Anyway, the resulting form looks fine:

laz-form1

Adding Controls

Time now to add some controls to the form. You do this by dragging them from the palette below the IDE’s menu:

As well as the "standard" controls, such as buttons and text edits, Lazarus comes with a wide variety of other controls (about 200 in all) to provide facilities like data aware grids, standard dialogs etc. And it’s easy to create your own controls from scratch, or to modify existing ones using inheritance. I added a couple of labels, a text edit and a button to the form, and used the Object Inspector to give them more meaningful names than the defaults provided by Lazarus, and to change their captions where appropriate:

laz-controls

Lazarus provides numerous options for aligning controls, which I used to make the "Who are you?" prompt and the associated text edit line up vertically.

Adding An Event

Like all modern GUI builders, Lazarus is event driven. Nothing much happens until the user clicks on a control, or performs some other action, at which point one or more event will fire. Events are associated with particular controls, I want to add an "OnClick" event to the "Greet" button which will read the text the user entered and set the "Greeting" label accordingly. Events are added from the Object Inspector:

laz-onclick 

I double-clicked on OnClick, and Lazarus generated the following event handler for me:

laz-event1

The event handler, like all Lazarus code, is written in Object Pascal. This will never be my favourite language, but it’s pretty easy to understand, and easy to pick up if you have a background in languages such as C++, Java or C#. The code I added to the handler looks like this:

laz-event2

The editor has code completion, but only for looking up method names – it won’t complete half-completed names like "Greet" in this case.

With this even handler in place, I can click on the button and have the app say "Hello" to me. I now want to reset the user input and the greeting to be empty after 5 seconds have elapsed. To do this, I need to add a timer to the form.

Adding a Timer

Timers are  controls which you add to the form in the same way as buttone stc. but which have no visual representation at run-time. The timer control can be found in the "System" palette:

laz-syspal

I placed a timer on the form, and then changed its Enabled property to False, and its interval to 5000 milliseconds. Then I modified the button event handler:

lazevent3

So when the button is clicked, the timer is enabled and the countdown commences. I now need to provide an event handler on the timer to clear the label and text edit:

laz-event4

And with that, the application is pretty much complete. I made a couple of cosmetic changes, such as the greeting’s font, but that’s about it.

Other IDE Features

Lazarus is a reasonably full featured IDE; as well as the code and form editing, you get some refactoring support and an integrated debugger. I was quite interested to see how the debugger performed, as I’ve always found the one in Delphi itself rather poor. So I set a breakpoint in the timer event handler and ran the app again. It quickly becomes clear that the debugger is none other than gdb, which could be a good thing or a bad thing, depending on your feelings about gdb. Lazarus does quite a nice job of putting a windowed interface on top of it, but I suspect that gdb’s problems when been driven from a GUI will show through if I ever needed to do anything complex in the way of debugging.

Conclusion

Lazarus  makes a very good stab at being a free, open-source Delphi clone. There are some rough edges, and the compile/link times could be a bit better, but on the whole I was pretty impressed with it. People looking for a GUI builder that works cross-platform on Windows, Linux and the Mac should consider giving it a try.

Advertisements
Report this ad

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK