n recent years, the move towards web-based applications has accelerated for a variety of reasons, including the need for simple and reliable installations. Despite the many reasons for developing web applications, an old-fashioned, double-clickable native application is still better at some things. The user experience, for example, is generally richer and it matches what the average user expects from commercial desktop software.
For many vertical market applications, it is not practical?nor desirable?to make a web application because end users don’t want browser-based applications and are comfortable with feature-rich desktop applications. Slow or limited web access also creates less-than-ideal environments for web applications. In addition, some businesses still question the safety and security of data transferred over the web.
REALbasic from Real Software Inc. is a useful development environment that enables you to create a wide variety of native applications and utilities quickly and easily. The Personal Edition provides the basic features to create these applications, while the Professional Edition adds the ability to compile native executables for Windows, Mac OS X, and a wide variety of Linux systems just by setting a checkbox or two regardless of your development platform. You can also connect to a wide variety of database servers using the Professional Edition, and you can develop and debug on one platform while running in another as with VMware or Parallels (see Sidebar 1. REALbasic Pricing and Terms).
This article introduces REALbasic. It demonstrates the easy-to-use form layout and coding environments and shows how to use control events in applications.
Homage to Visual Basic
The REALbasic integrated development environment (IDE) is quite similar to Microsoft Visual Basic; it also combines into one environment the REALbasic language with all aspects of interface building and code editing. The IDE generally prevents you from doing silly things by providing specialized editors that don’t require you to know the exact syntax of the language, which makes its learning curve less steep than those of other languages. For example, you add a method via a method editor with specific edit fields for the method name, parameter list (with auto complete), return type (with auto complete), and scope declaration.
In other languages, you would have to learn the rules for declaring methods before writing any code. For example, declaring a user-defined function in Visual Basic requires you to know the syntax for creating one. REALbasic has no such requirement because the method editor allows you only one way to declare a user-defined function?you can’t type it incorrectly.
The REALbasic programming language may be easy to use and learn, but don’t be fooled by the “basic” in its name. It is a modern, cross-platform, and object-oriented language that is highly extensible with operating system declares and plugins written in C++. Nearly all controls and classes within REALbasic can be subclassed (the notable exceptions to the rule are database and recordset objects).
The IDE helps manage some of the more mundane aspects of your code. The code editor features auto-indent and code folding, which allow you to easily see and collapse your code. By keeping your code properly indented, your REALbasic code stays readable and consistent across development teams.
REALbasic in Action
|Figure 1. New Project Screen: When you first launch REALbasic, it defaults to a new blank project.
To demonstrate how REALbasic works, let’s dig into a simple project that adds user text to a listbox. You can download a demo version of REALbasic for your platform of choice and follow along. Because the REALbasic IDE is itself written in REALbasic, it is a good example of a cross-platform application using REALbasic.
Start REALbasic and it will default to a new blank project (see Figure 1). Along the top of the IDE is a toolbar with customizable controls for common tasks such as save, run, and build. In the same area, you can also create bookmarks for places in your code and search the code without having to go to another dialog. Just below the main toolbar is a tab control. By default, the only tab is the project tab, which contains a listing of all the objects in the project, a properties list, and another mini toolbar for creating new objects.
|Figure 2. Form Editor: Selecting a control from the list on the left and dragging it onto the window creates an instance of the control on the window.
Double click on the Window1 object and REALbasic creates a new tab called Window1. This is the form editor, and on its left you’ll see a list of all the controls you can drag onto a window (see Figure 2). A convenient popup menu above the controls list lets you see All, Native RB, Plugin-In, Project, and Favorite controls. On its right is a properties list.
Selecting a control from the list and dragging it onto the window creates an instance of the control on the window. For this project, drag an editfield, pushbutton, and listbox onto the window. Notice that the properties list changes as you drop each control onto the window.
Click on PushButton1 and change the caption property to “Add To List.” Set the default property to true. The caption is a little big for the button so grab the handles on the button to make it wider. When you change the width by hand, you’ll notice that the width property in the position section changes. You can also change the width in the properties list manually and it will change on the form editor.
|Figure 3. Using the Code Editor: The code editor offers multiple autocomplete items.
So far you’ve only played with control properties; you haven’t touched any code. Double click on Pushbutton1 and another tab populated with the code editor will appear. Along the left of the page is a list of controls and event handlers for the current object (Window1). All events available for each control are listed and most are self-explanatory. Because you double-clicked on Pushbutton1, the editor automatically selected the Action event. Most of the time, the editor will open to the event used most often with a particular control, which for PushButtons is Action (i.e., they are pushed).
Customizable buttons along the top of the code editor (see Figure 3) let you add new items such as menu handlers, methods, and properties, and perform tasks such as comment lines of code. Depending upon the object, you can add your own event definitions, structures, and enums.
Coding in “Autopilot”
For the Action event of Pushbutton1, you will add the text from EditField1 to Listbox1. For bigger projects, I recommend better naming conventions! When you start typing in the code editor, a grey ellipsis (…) appears after the text you’ve typed to indicate there are multiple items it can autocomplete. When the ellipsis appears, pressing tab will pop up a selectable list of all possible solutions. As you keep typing, the list narrows to fit what you type (see Figure 4).
|Figure 4. Autocomplete Heaven: REALbasic’s autocomplete features make it easy to learn for novices and easy to use for professionals.
Select Listbox1 and press period. The ellipsis appears; so press tab and a list of all available properties and methods will appear in a popup menu.
Another helpful feature is the autohelp, which appears at the bottom of the window. Autohelp shows the function call as well as the inputs and outputs of the function. Between autocomplete and autohelp you’ll spend less time documenting and more time creating usable software.
|Figure 5. Debugging Code: REALbasic’s debug application starts automatically after error-free code compiles.
Back to Work
You’ll use the following code in your Pushbutton1.Action event:
listbox1.AddRow = editfield1.text
This adds a row of text to the listbox equal to the text that’s in the editfield. Now click on the Run button at the top of the window to start debugging your application. The compiler will give you an error list. Depending upon a preference setting, you can see multiple errors or just one.
|Figure 6. Testing Code: Adding special characters such as “!” test REALbasic’s Unicode handling.
In this case, you have only one error. Double-clicking on the error message takes you to the offending line of code. This error is a really simple one: AutoHelp at the bottom of the window tells you it’s a method call. The code should be:
Change the code and click Run again. This time, it compiles cleanly and the debug application starts up (note the .debug in the application name). See Figure 5.
Type some text into the editfield and either press Enter/Return or click the Add to List button. You can get tricky and add special characters into the field to test REALbasic’s Unicode handling (see Figure 6).
Testing Your App
|Figure 7. Entering a Breakpoint: Breakpoints can be added in the integrated debugger.
Let’s explore the integrated debugger. Quit the debug application and go back to the code editor for Pushbutton1.Action. The column to the left of the code has some small horizontal lines that indicate places where you can place breakpoints (see Figure 7). Use the mouse to click on your line of code and place a breakpoint.
Run the debug application, enter some text, and hit the Add To List button. The IDE comes to the front, a new Run tab appears, and the line of code where you put the breakpoint is highlighted. Instead of the properties list on the right side, you now see a variables list (see Figure 8).
|Figure 8. Code Variables: The debug application lets you test your code’s variables.
Click on Window1.Window1, which will display all the properties for Window1. Then click on Contents, which shows you all the objects on this instance of Window1 (see Figure 9). From here, you can view any of the properties of any of the controls on this window.
Take some time to explore all the controls. In particular, click on the EditField1.EditField to see its properties. Scroll down to the Text property and you’ll see that its text is the same as what you typed into the editfield. You can change the text by clicking the magnifying glass. Click the Resume button at the top of the window to continue.
While this isn’t a practical example, it demonstrates how REALbasic works and how easy it is to get started. From this point, you could use the standard REALbasic database or XML classes to create your own document format, read/write the data, and make a simple “to do” application.
|Figure 9. Table of Contents: The Contents box reveals all the objects of any variable in your code.
Platform Neutrality—with a Couple ‘Gotchas’
The professional version of REALbasic allows you to write on the platform of your choice and then easily run and debug in another environment. For example, I wrote most of the example project for this article on Macintosh OS X, but I ran and debugged it in Windows and Ubuntu while running VMware.
Don’t fret if you’re a Windows or Linux user—you can work the opposite way as well. The test environment need only run a simple application called the Remote Debugger Stub (appropriately written in REALbasic). However, be aware that controls on Linux are a little bigger than they are on Macintosh and Windows. One way to get around this is to add code in the Open events of the pushbutton and editfield controls and target Linux builds specifically to change their heights:
#if TargetLinux Then me.Height = 26#endif
This isn’t a very efficient solution though, nor does it make good use of REALbasic’s ability to subclass controls. In a real application, I would create editfield and pushbutton subclasses that changed their heights automatically at runtime. I might even use a global constant, so I would have to change the height value in only one location.
It’s also important to remember that nearly all REALbasic controls are a compromise. There are controls available for Windows that have many features not found on the Macintosh and Linux platforms (and visa versa), so don’t expect to get your favorite ActiveX multi-column, drop-down combo box to be native (i.e., cross-platform) in REALbasic. Most of these things you can certainly build, but will be a lot of work. And while REALbasic for Windows does support some ActiveX, it’s not fully supported.
There is also an active third-party market for REALbasic controls, classes, and modules if you need features that REALbasic doesn’t have out of the box. I recommend looking at the Monkeybread Software plugins, which extend REALbasic with around 20,000 additional functions for Mac OS X, Windows, and Linux. For additional cross-platform controls, I recommend the Einhugur controls. To find additional REALbasic controls, classes, modules, source code, consultants, and tutorials, search RBGarage.