Chapter 2

Getting Started with Examples

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...

2.1 Hello World

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:

\class {vpane} \name {hello} \children {hello.greet hello.bye} \width {200} \height {60} \ \class {txtLabel} \name {hello.greet} \parent {hello} \label {Hello there!!} \ \class {txtButton} \name {hello.bye} \parent {hello} \label {Bye!} \gapH {4} \gapV {2} \script { switch (arg[0]) { case "buttonRelease": exit(0); break; } usual(); } \

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):

% viola -o hello -path "."

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, -path "." tells viola to look for the file in the current directory, where we've saved the listing "hello.v".

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.

2.2 Text Field with Scrollbar

\name {demoTF} \class {hpane} \width {550} \height {200} \children {} \ \name {} \class {slider} \parent {demoTF} \maxWidth {20} \shownNotify {} \ \name {} \class {txtDisp} \parent {demoTF} \script { switch (arg[0]) { case "init": /* initialize this object as usual, then, load some text * into the text field. */ usual(); set("content", loadFile("/etc/hosts")); /* return to escape, so that we won't fall into the * usual() method again (the one below). */ return; break; } usual(); } \shownNotify {} \

To see the application description realized (assuming that the app is aved in usr/work/viola/tests/objs/demoTF.v), type:

viola -o demoTF -path /usr/work/viola/tests/objs

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 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.

GOTO Preface, Previous Chapter (1), Next Chapter (3)