Local time: Monday, 24-Nov-2008 03:48:49 EST
Last update at /special/conference/apwww95,Friday, 21-May-2004 09:47:13 EST
![]()
X Programs without X Programming
Jan NewmarchFaculty of Information Science and Engineering
University of Canberra
PO Box 1, Belconnen, ACT 2616
Australia
Email:jan@ise.canberra.edu.au.
URL:http://pandonia.canberra.edu.au
- Abstract
- Programming X in C is complicated, tedious and error prone. Bindings of X to many languages now exist. The simplest of these are to command languages such as the Windowing Korn shell, tclMotif or Tk, and these can reduce the complexity quite substantially. However, much detailed knowledge of X is required even in these systems.
This paper discusses alternative methods of building X applications that do not require such knowledge. Typically the alternatives are much weaker in the range of applications that can be built, but much easier to use. When applicable, these systems can reduce many pages of X code to very small non-X programs.
- Keywords
- X, Motif, Xgen, WWW, Forms, XScript
Introduction
Programming X in C is complicated, tedious and error prone. Bindings of X to many languages now exist. The simplest of these are to command languages such as the Windowing Korn shell, tclMotif or Tk, and these can reduce the complexity quite substantially. However, much detailed knowledge of X is required even in these systems.This paper discusses alternative methods of building X applications that do not require such knowledge. Typically the alternatives are much weaker in the range of applications that can be built, but much easier to use. When applicable, these systems can reduce many pages of X code to very small non-X programs.
Amongst the examples considered are xgen [Buehler92], XScript [Newmarch95a] and the Forms mechanism [Forms] of World Wide Web browsers. For each, a brief description is given and then the advantages and disadvantages of each are discussed, with examples.
xgen allows one to use lists, buttons and labels to construct simple user interfaces. Attached to buttons and lists are pieces of shell code that are executed whenever a selection occurs. This gives a quick way of adding a GUI front-end to the display of information. xgen does not have full language capabilities, however.
XScript is a set of small applications that show lists, text entry boxes, dialogs of various kinds, etc. They are designed to be used in pipelines and general shell scripts to add a GUI interface to existing scripts. In this it differs from the other two systems described by adding to the I/O mechanisms of shell scripts instead of replacing them.
Through the Forms mechanism, Web pages can be built which include lists, buttons of various kinds, and text entry areas. When submitted, these invoke a program on a Web server using the Common Gateway Interface [CGI]. These programs can generate arbitrary output, including more Forms. This gives a more flexible model than xgen, but at the cost of more complexity in setting up. A further advantage is that it allows a platform independent means of displaying programs.
A contrast of these systems with the simpler X programming systems such as Tk and tclMotif [Newmarch94] is given, and conclusions are drawn about the merits of the different approaches.
XGen
Overview
Xgen was designed to be a tool for rapid interface construction to a large collection of non-GUI programs forming the GRASS system [Westervelt91]. It is intended to allow an interface to be built in which the various components either call an external program or collect information for later execution of an external program.It supports a range of widgets from the Motif set [Heller94], allowing them to be built into ``pages'' like a form. The layout can be simple, as in a vertical list, or more complex. The appearance and functional behaviour of the objects can be controlled by using a simple way of setting widget resources.
For some of the widgets, user events will trigger actions that are attached to xgen actions. These actions can cause focus to shift to another form, to set environment variables, to execute an external program or a number of other specific activities. By this means, both the user interface can be controlled and external programs executed.
Xgen is not built with full language capabilities of loops, conditionals, etc. It is only capable of a simple flow of control among a set of programs, but allows that control to be under user control in a graphical environment.
User interface components
The basic components of xgen are the Motif Primitive widgets (some renamed), available as label, separator, button, message, textentry, multiline, list, toggle, slider and pulldown. Each object is specified in a file by its type and then the name by which it will be known. For objects such as label or button this name will also be the default text shown in the widget.Each object specification also includes resource values and actions to be taken on specific events. Objects specifications are separated by semi-colons, as in
label Hello; button "Push Me"; list list1; Component integration
Components are placed on a pane like a form. The simplest organisation is in a vertical list, each succeeding one below the rest. This can be achieved by containing the specification of each object in a menu, as inmenu menu1 ( label Hello; button "Push Me"; ) More complex geometrical arangements can be done using a commandboard, which allows a variety of controls over the layout. Of course, using one of these makes the user interface code more complex, but not substantially so. For example, vertical paning can be set with one resource, horizontal paning by two, the number of columns (or rows) by an additional resource (see later).
Actions
A simplified model of Motif widget actions is used. For example, whereas the Motif PushButton has actions Arm, Activate and Disarm, for xgen this is simplified to just the one action Activate. Similarly, Textentry widgets are activated by pressing Return, and list widgets are activated by selecting items.When a widget is activated, it can run a ``procedural'' resource. The most common of these are ``runforeground'' and ``runbackground'' where the resource value is the command to be executed. For example, to print ``hello world'' when a button is pressed
button "Hello World" runforeground:"echo Hello World"; Values in quotes may be text, widget ``values'' enclosed in [...] or shell commands beginning with `!'. For examples, to set the button label to the current date, thetitlestringresource would be set totitlestring:"!date" In addition, shell environment variables may be set by the ``setVAR'' resource (where VAR is the variable name) as in
setDATE:"!date" Their values can then be used in commands.Environments
Each xgen script consists of a number of environments, which contain a number of shells (menus or commandboards). This allows a hierarchical structure to give more flexibility to the interface.Diary example
As a simple example, consider a diary for one day. It has a list of hours, and when one is selected a one-line entry for that day is displayed. The contents of the entry are taken from a file, by doing a simple grep for that time in the file.environment diary initialshells:today { commandboard today setDATE:"!date" titlestring:"$DATE" pane:on ( list times listtype:single listelement:900 listelement:1000 listelement:1100 listelement:1200 listelement:1300 listelement:1400 listelement:1500 listelement:1600 listelement:1700 ; button "Show Appointment" setTIME:"[times]" setENTRY:"!grep $TIME calendar" updateobject:"today:Entry:$ENTRY" ; textentry Entry ; ) } This is shown in Figure One
![]()
Figure One: A diary using xgenResource control
Xgen uses a subset of the resources available to Motif widgets and applications. For example, the label object can have alignment set, or a bitmap used, as well as resources common to all objects such as width, height, foreground, background, font, etc. For examplelabel hello alignment:center; In addition there are the procedural resources such as runforeground, runbackground, captureoutput, setVAR, inputfrom, sensitive. These allow execution of processes, redirection of I/O and the setting of environment variables.
Some control may be exercised over the window manager decorations (assumed to be mwm) by using the decorations and functions resources.
Summary
Xgen is useful for putting a wrapper around a number of cooperative programs, where information needs to be collected from one, massaged into a useful form and presented to another.
XScript
Overview
XScript is a set of small X applications that are designed for use in pipelines, and to to provide small dialogs that can be used to capture or present information. The intention is that they will provide a means by which a user can interact with a GUI application to act as a Unix filter program. Thus the applications (``applets'') in XScript are intended to be embedded within ordinary shell programs.
![]()
![]()
Figure Two: Some applets from XscriptUser Interface Components
Xscript supplies a set of applets for presenting information as dialogs such as a warning dialog (xwarn), information dialog (xinfo), etc. In addition there are a set of input capture dialogs such as xask to get an answer to a question. These write to standard output. They can be used as the source of a pipeline. Finally, a set of dialogs that present information such as lists that can be used in pipelines are available. For example, xselect reads a list of items from stdin, one per line, presents them as a list, and when one is selected, writes it to stdout. This can be used as a filter in a pipeline.Component Integration
The components are all separate applications, so share no common GUI space.Actions
Actions such as item selection cause the selected item to be written to stdout. This means it can be a source in a pipeline. A command such as xask returns an exit code depending on whether the Yes or No button is pressed. xprompt prompts for text to be entered, and writes this to standard output when the return key is pressed.Example
This is a version of the diary written using XScript applets in a pipeline.echo '900 1000 1100 1200 1300 1400 1500 1600 1700' | xselect -title "`date`" | while read time do xinfo `grep $time calendar` done
![]()
Figure Three: A diary using XScript applets.Summary
Xscript allows shell programs (or indeed, any program that can execute external programs) to be written where terminal I/O can be replaced by simple graphical components.
HTML Forms
Overview
The World Wide Web was originally designed for presentation of static documents distributed across the network, with hyper-links connecting them. A development of this was to allow dynamic creation of documents, based on information collected from the user. This collection is by means of a simple set of GUI elements that are common across X and other windowing environments such as the Macintosh and Microsoft Windows. The information is passed to an HTTPD server using the Common Gateway Interface, which executes a program to generate a document which is returned to the browser.User Interface Components
The components used in Forms are any HTML components such as text highlighted in various ways, numbered and un-numbered lists, hyper-links, images, etc [HTML2], and in addition a number of GUI elements. These are buttons, single and multiple line text entry areas, lists, radio boxes and check boxes. Just like other HTML components, the presentation aspects of these are not specified.In the Motif-based browsers such as Mosaic for X, Netscape for X, etc, these will use Motif components such as PushButton and Text. Other X browsers such as tkWWW may use Tk components, while non-X browsers will use their own GUI presentation components.
For example, a Form containing a single line edit area for name, a multiple line text editing area for address and a submit button may be given by
which appears as
![]()
Component Integration
Components are integrated by the presentation control aspects of HTML such as paragraph breaks, line breaks and separators. The amount of control available is increasing with each version of HTML.In addition, the individual objects allow some control. For example, a TEXTAREA allows the number of rows and columns to be set.
Actions
The individual objects of the GUI generally have no actions associated to them. Text may be freely entered and modified in the single or multiple line editors with no actions occurring. Radio buttons may be checked and unchecked, again with no actions occurring.However, two type of objects may be used which do have actions attached to them. These are input objects defined as type SUBMIT or RESET buttons. A RESET button sets all objects back to their original values. A SUBMIT button collects information from all objects and sends it off as a request.
Each object in a Form may be named, using the name attribute. When a Form is ``submitted'', a string is prepared that is the concatenation of name=value for all objects. To allow for embedded spaces, equals signs, etc, the values are mangled so that spaces are replaced by plus signs and all special characters (such as plus, equals) are replaced by an octal representation. By this means a one line string can be prepared that can be unmangled back into a set of name=value pairs. The actual mechanism by which the string is prepared depends on the ``method'' of the Form. There are a number of programs and libraries that can do this unmangling for you, such as cgiparse (a C program) and cgi-lib.pl (a Perl program).
Client-Server Model
HTML browsers are not specifically intended as simple means to do GUI programming. They are intended to communicate with document servers on the Net, talking the HTTP protocol. When a Form is submitted, the string of name=value pairs is sent to a suitable server for interpretation, resulting in return of a new HTML document for display by the server.To use a Web browser for X programming means that you need to have a server capable of taking the output from a Form and executing a suitable program on the server side, using the Common Gateway Interface. If the program naturally sits in a distributed environment (such as a shared calendar) then this is well worth doing, but as long as your local server can run CGI scripts on your behalf then it can be done for individual programs too.
An attribute of a Form is the server side program to execute. This program runs with environment variables such as USER_INPUT set to the string of name=value mangled pairs. The program needs to unmangle these, perform local execution, and then prepare an HTML document that is echo'd back to the client browser.
Example
We use the diary example of earlier, where the set of times is now presented as a list of Selectable items within a Form#!/bin/sh echo "Content-type: text/html" echo time=`cgiparse -value time` # fixed text cat << EOF EOF
![]()
Figure Four: A diary using an HTML Form.On receipt of this Form, the value of ``time'' 1200 can be obtained by programs such as cgiparse. A diary lookup can then be performed and a suitable reply sent. To avoid having to flip back and forth between screens, the returned reply should also contain the Form so that new entries can be made, where the "grep" in the middle sets the time entry in the Form.
echo "Content-type: text/html echo time=`cgiparse -value time` # fixed text cat << EOF EOF A more complex diary would allow the entry to be set. This would involve replacing each element of the time list with a set of label/entry pairs where the entry box allows the user to set a diary entry. To be fully up-to-date, on entry to the Form they should already be filled in with suitable values. This gets a bit more hairy, as the original document now has to be dynamically determined. This can be done by either making it a higher-level CGI document, or by using server-side includes to set information in the entry boxes.Summary
Using CGI Forms is not as straightforward as using xgen. However, the popularity of the Web means that many people are familiar with creating Web documents, and the additional knowledge required to create Forms is not too bad (although it is still a litle hard to find this knowledge).The resulting programs sit naturally in a distributed environment, caused by the design of the Web. It is also possible to use a heterogeneous system, where a browser running on a PC, say, can cause a CGI script to execute on a Unix system.
The programs are not capable of highly interactive work, since scripts are only submitted when the Submit button is pressed. However, extensions such as HotJava [Java95], or the script support for Forms in HTML 3.0 [HTML3] will improve this.
Comparison to full Programming Systems
There are a number of systems that offer a full programming environment to X, such as Tcl/Tk, tclMotif and Desktop Korn Shell. These are event-driven systems able to exploit all the features of the widget sets. They are roughly comparable in complexity and capability to each other [Newmarch95b].The diary is a simple problem. Doing this in tclMotif gives the program
xtAppInitialize xmRowColumn .form managed xmList .form.times managed \ -itemCount 9 \ -visibleItemCount 9 \ -items {900 1000 1100 1200 1300 1400 1500 1600 1700} \ -selectionPolicy single_select xmText .form.appt managed \ -editable false .form.times singleSelectionCallback { .form.appt setValues \ -value [exec grep %item calendar] } . realizeWidget . mainLoop with appearance
![]()
In this case, there is little difference in length between the tclMotif program and the others. Because the full set of widget methods is available, the solution can be better tuned to the problem in that the GUI space is shared, and selecting an item can result in immediate response (this happens in xgen for this problem, too).
The downside is that there is a far greater amount of detail required here. A greater knowledge of the widgets is required, and of the callback system. What tclMotif gains over C, these other systems gain over tclMotif. (The same could be said for tcl/Tk.)
Conclusion
Three alternative systems were presented which allow X Window programs to be constructed without having to do X Window programming at the event loop level. They provide graphical user elements which can interact with external programs in different ways.Xgen stores intermediate information in shell environment variables, and uses interaction with GUI components to set these. Interaction can also be used to fire up external programs, or to alter the GUI interaction by switching to a new form. Xgen does not have loops or conditionals, though, so is limited in the scope of the programs that can be built.
HTML Forms give a platform-independant GUI. This is strictly as a Form-style interface, with no dialogs. The degree of interaction is strictly limited, and is really just capable of filling out the Form, and selecting various list items, radio buttons, etc. When a Submit button is pressed, the entire Form is shipped across to an HTTPD server for processing. (Multiple Submit buttons can be used in most browsers.) The server is then responsible for creating a new document for return.
XScript adopts the traditional Unix model of small applications communicating via mechanisms such as pipelines. It replaces terminal-based I/O with small X applications that can act as sources and sinks of I/O, or as filters in pipelines. The set is indefinitely extensible.
In comparison with event loop programming, these all offer a higher level of programming. There is less need to know details of widgets or of widget behaviour. The downside is that these systems can accomplish less, and are less able to be tuned to the problem.
Availability
Xgen is available by anonymous ftp from ftp://moon.cecer.army.mil/grass/other/xgen2.1/xgen.tar.Z.
XScript is available by anonymous ftp from ftp://ftp.canberra.edu.au/pub/motif/xscript/xscript.tar.gz
References
- [Buehler92]
- Buehler, K. The xgen Application Generator: Constructing Motif GUIs for TTY Programs, The X Resource, Issue 4, pp 109-136, 1992.
- [Forms]
- Mosaic for X: Fill-Out Form Support, http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html
- [CGI]
- Common Gateway Interface, http://hoohoo.ncsa.uiuc.edu/cgi/
- [Heller94]
- Heller, D. and Ferguson, P. M., The Motif Programming Manual, O'Reilly and Associates, 1994
- [HTML2]
- HTML 2.0 Specification http://info.cern.ch/hypertext/WWW/MarkUp/MarkUp.html
- [HTML3]
- HTML 3.0 Specification, http://www.hpl.hp.co.uk/people/dsr/html/Contents.html
- [Java95]
- The Java HomePage http://java.sun.com
- [Newmarch94]
- Newmarch, J. D., tclMotif: A Binding of tcl to Motif, XHibition, San Jose, 1994, http://pandonia.canberra.edu.au/SW.html.
- [Newmarch95a]
- Newmarch, J. D., XScript: Shell Programming with X, The X Resource, Issue 15, pp 19-34, 1995, http://pandonia.canberra.edu.au/SW.html.
- [Newmarch95b]
- Newmarch, J. D., X Programming using Command Languages, AUUG95 Tutorial session, 1995.
- [Westervelt91]
- Wertervelt, J, et al, GRASS 4.0 User's Reference Manual, USACERL, GRASS Information Center, PO Box 9005, Champaign, Illinois, 1991
Author
Jan Newmarch is a Senior Lecturer in Computing at the University of Canberra. He has written a book on programming in X and Motif and has presented papers about X at AUUG, the Tcl/Tk Workshop, Xhibition and the X Technical Conference. He recently gave a paper at AusWeb95 on extending the Common Client Interface. He has written and released into the public domain the tclMotif system as well as xmfm (a file manager), tclXtSend (a modification of Tk's send for Xt), replayXt (a test system for X) and XScript (a collection of programs in X for use in shell scripts).
[Return to Table of Contents]
COPYRIGHT © 1995 by AUUG95 and APWWW95 Charles Sturt University. ALL RIGHTS RESERVED. ISBN 1 875781 43 9