Graphical Applications Using MetaCard
Backtracking a little, before you can write a script, you need to create an object to attach the script to. MetaCard applications are composed of one or more stacks, each of which has one or more cards. The metaphor (inherited from HyperCard) is of a stack of index cards, but you can also think of it as being pages in a book, slides in a presentation or the frames in a movie or video. The stacks are stored as binary files similar to the resource files used on the Mac and Windows. But in addition to object descriptions, data such as the state of buttons and the text in fields is also stored in these files.
The objects are created, sized and positioned using MetaCard's IDE (Integrated Development Environment). To start building a new application window, choose “New Stack” from the File menu (the first card is created automatically with the stack). Then choose tools from the tool bar and draw the fields and buttons you need. See Figure 1 for a screen snapshot of the MetaCard top application.
After creating the stack and the controls in it, you can add scripts to the stack, cards and/or controls. Normally each object has its own script, but I've put all of the handlers for the top program in the card script. This means that the mouseUp handler (see Listing 3) is a little unusual, since it gets called through the message-passing hierarchy: messages not handled by an object can be handled by objects higher up in the hierarchy. In this case, a handler in the card script handles messages sent to any of the controls on the card.
Writing one big handler instead of a bunch of small ones usually means you have to figure out which object the message was originally sent to. The target function supplies this information. The target function returns an object that you can get the properties of or send messages to. This mouseUp handler also shows off the MetaCard switch control structure:
The “Kill Process” case is executed when the user clicks on the “Kill Process” button. This section gets the PID of the process from the selected line in field and uses the MetaTalk kill command to kill it.
The “Set Update Interval...” section prompts the user for the new updateinterval value using the ask dialog, verifies that the value is a number and tells the user that it must be if it isn't. The ask and answer dialogs are built into MetaCard and are quick and easy ways to get simple responses from the user. If the new data checks out OK, the handler cancels the current timer and then calls the updatelist handler to restart it.
The “toplist” case will be executed when the user clicks on a line in the main field. This case enables the “Kill Process” button and suppresses updates for 5 seconds, allowing the user time to kill the process before the selection is cleared when the field is next updated.
Listing 4 shows the handlers for the stack-oriented messages, including those sent when the stack opens and closes and is iconified and uniconfied. Remember the goal of having the application go to sleep when it is iconified.
MetaCard doesn't have a constraint-based geometry system, so you must write scripts to handle resizing and repositioning controls when a stack window is resized. The resizeStack message handler that does this geometry management is shown in Listing 2. Using the IDE, I set the stack properties such that the stack is not resizable in width, only in height (since you need to be able to see all fields). So this handler only has to resize the main field vertically and reposition the buttons along the bottom edge of the display. This simple four-statement handler reliably handles the task without triggering the time-consuming trial-and-error phase of development required to get a constraint-based system working correctly.
Running the MetaCard version of top takes about twice as much CPU time as the character-based version (6% vs. 3% on a Pentium 90 running Linux 1.2.13). This is a typical result, since a well-written MetaCard script generally runs 2 to 4 times slower than a comparable C program. Of course, the MetaCard version only took a fraction of time to develop. And because it is so much smaller, it will take far less time and effort to maintain and customize. The character-based top program is written in C and is about 10 times as long. Memory usage for the MetaCard version of top is considerably less than the total of the character-based top program added to the memory needed for the xterm and the extra bash process required to run it.
|Huge Package Overhaul for Debian and Ubuntu||Jul 23, 2015|
|diff -u: What's New in Kernel Development||Jul 22, 2015|
|Shashlik - a Tasty New Android Simulator||Jul 21, 2015|
|Embed Linux in Monitoring and Control Systems||Jul 20, 2015|
|The Controversy Behind Canonical's Intellectual Property Policy||Jul 17, 2015|
|Non-Linux FOSS: Portable Apps, in the Cloud!||Jul 15, 2015|
- Huge Package Overhaul for Debian and Ubuntu
- Shashlik - a Tasty New Android Simulator
- diff -u: What's New in Kernel Development
- The Controversy Behind Canonical's Intellectual Property Policy
- Home Automation with Raspberry Pi
- Embed Linux in Monitoring and Control Systems
- Purism Librem 13 Review
- One Port to Rule Them All!
- Privacy Is Personal
- General Relativity in Python