This chapter contain small examples of growing complexity for illustrating viola features and ideas. It's time to get some hands on experience.
First assuming you have the viola binary installed on your system. If not, read the preface for FTP information. And, make sure you've set up the color resources correctly...
Let's build a hello world application such as this (figure 1):
Describing the attributes that make up each objects is the main task in building an Viola application. One approach is to directly edit the Viola object file, and have Viola interpret the file in one batch. Another approach is to use certain assistance applications that help the developer in creating and edit objects. Both approaches are useful in different situation, and complement each other.
There are a few ways to construct this application. One could build it by interactively issuing commands to the viola interpreter. One could use an interface builder (an application that issues the low level commands for you). Or, as we'll do here, specify a description of the application in a file, and let viola interpret the application description file.
Since the interface builder is not yet fully developed, let's just use the conventional write-the-app-file approach.
Here is such a file:
The function of the various parts of the listing should be fairly obvious. This application describes three "object" entities. Namely, they are "hello", "hello.greet", and "hello.bye". Viola will instantiate the object "hello" from the class "vpane"; "hello.greet" from "txtDisp"; and "hello.bye" from "txtButton".
This is a diagram of the objects parent/child relationship:
And this the visual representation, as nested windows:
And, how the windows are formatted, taking into account the behaviour of the classes and the speficied geometry constraints:
Vpane is a class of objects that tiles its children objects vertically; txtDisp is a text display class; and txtButton is a text labeled button.
To run the application, enter that listing into a file named "hello.v", and let viola run on it, thusly (note that -path should point to the directory containing hello.v):
The option "-o hello" specifies that viola should, on start up, load and
instantiate the object "hello" (and along with all the other objects
in the same file named "hello.v"). To make sure that viola will find
the file,
If all went well, voila, a GUI will be displayed resembling figure 1.
And, when you press the button "Bye", the viola process will terminate as specified in that button's script. This will happen because when you use the mouse to click on that button, the mouse-button-click events/messages are sent to that object. In this case, the script keeps a look out for "buttonRelease" message. So, as you can imagine, we can stick in more interesting script than exit() to respond to messages.
This hello-world application gives a glimpse of the basic utility of viola: providing a simple way to create GUI applications by describing the type and attributes of GUI objects, the relationships among the objects, and an extension scripting language for programming specific actions.
To see the application description realized (assuming that the app is aved in usr/work/viola/tests/objs/demoTF.v), type:
Now you should have a window with a narrow scrollbar on the left edge, and a text field displaying the content of the /etc/motd file.
When the object demo.tf is first created it'll get an "init" message. Every object gets that message upon creation. In this case it gives the object a chance to load the file "/etc/motd" and puts it in the objects' "content" slot.
Since we didn't bother to include an exit button, the easiest way to terminate it is by hiting control-C on the shell window to kill the process.
For more application examples, see Chapter 8.