Ceci est une ancienne révision du document !
In this series of articles, I will be building a text-based application with Free Pascal, using its text-based interface for user interaction. This will be combined with other, more modern, technologies such as database access using SQL and Web access with HTTP. The final aim of the project is to demonstrate how Pascal can be used to build a modern application, while avoiding the overhead associated with a graphical interface that uses a widget set such as GTK or Qt. In the first part of this series we installed the required compiler software and developer’s interface. We compiled a simple console-based program and a first Free Vision application. In this second part we will be tuning our Free Vision interface and making it respond to commands.
Fleshing up the menu Let us now create a bespoke menu and make it responsive. To do so, we will overwrite two separate procedures of the standard TApplication object. The first is procedure InitMenuBar which, as its name suggests, populates the menu bar at the top of the screen. The other is the procedure that handles window events, in which we will be defining how our application needs to respond to button presses and suchlike. So our application object derives from the standard TApplication, with the addition of these two procedures. They are declared as “virtual” (top right)since they overwrite existing methods. We will be putting in a very basic menu bar with two submenus: a “File” menu with some standard options, and a “Help menu” with an option to open up the traditional “About” dialog (see box below). As readers accustomed to Turbo Pascal will have noticed, this is exactly the same syntax. In fact, I would not be surprised to learn that one of my projects from back then would just compile under Free Pascal. That is, if I manage to dig out the 3½” diskettes and a disk drive that still works. Just to refresh memories, let us note that “~” is used to indicate key accelerators, hcNoContext is the null Help Context, kbF2 is the code for keyboard key F2, and cmOpen is a 16-bit unsigned integer (word) code for standard command “Open”. The rest should be quite easy to figure out.
Responding to events The menu prepared above will actually respond to some buttons, but not to others. The difference is that TApplication’s default HandleEvent method knows what to do when the cmQuit command is issued (it simply quits). But it has not been programmed to respond to other events, such as the cmHelp command we put into the Help > About menu item. This is why we will be replacing the existing HandleEvent with our own. For example: It is especially important to include the original HandleEvent with the “inherited” command, since it handles the cmQuit command and we still want that to work. To respond to the cmHelp command, we are using a simple if structure to set up a MessageBox. This is a fast way of creating a message dialog box, that can be of several different types (Information, Alert, Error…). We do not get to personalize it much, since the final parameter can only hold flags to set up the dialog title, and the choice of the “OK” and/or “Cancel” buttons. To create a more interesting dialog window, we would need to write our own class (inheriting from TWindow or TDialog), which, though feasible, would be slightly more work. See the third article in the series for more details.
Setting up a file input box There are also other, predefined, dialog types available. To take a classic example, in our programs we sometimes need to prompt the user to enter a file through the typical File Open dialog. Free Vision does things in the very same way as Turbo Vision. We will modify our HandleEvent to respond to cmOpen. To do so, we will need several variables: var FileName: String; result : integer; pOpen : PFileDialog; FileName is a string to save the user-inputted filename, result will contain the command code of the button chosen (cmOpen or cmCancel), while pOpen is a pointer to the new dialog to be created. Now, within HandleEvent, let us reply to cmOpen in the same way we handled cmHelp (see box top right).
We have created a new FileDialog object, specifying that we wish to filter files with the ’*.txt’ pattern. When executed, the command chosen by the user is stored in variable result, and can then be examined to control subsequent actions. The dialog produced has nothing special to it as it is standard in Turbo and Free Vision. It just works and is very responsive. However, there are a couple of points to be noted since the last time I stared at one of these, back in the days of MS-DOS. The first is that it seems to handle well a POSIX file system, with the “/” file separator instead of MS-DOS’s idiosyncratic “C:” and “\”. The user can easily navigate through a standard Ubuntu home directory. On the other hand, we can also see that Unicode characters in filenames that do not appear in the common ASCII codepage (codes up to 127) have been altered, specifically the accents in “Música” and “Públic”. This may be a minor problem for some, or major for others if the intended end user’s environment is mostly in a non-latin script.
By this point, we have been using several of Free Vision’s library units, which must be included at the beginning of our program file: uses App, Objects, Menus, Drivers, Views, Dialogs, MsgBox, StdDlg; The complete code for this example is available at http://pastebin.com/ZTbN7Eft, in which the reader can see the points covered in this part of the series: personalizing the Free Vision menu bar, responding to commands, and using default message boxes and dialogs. In the next part, we will see how to connect our application to a modern database such as Sqlite3.