Creating Simple Motif GUIs Using Korn Shell Scripts

ยท

3 min read

Writing Motif GUI applications usually requires some C knowledge and a fair amount of time. But there's another (faster) way to prototype and create simple ones. The Common Desktop Environment comes with a special edition of KornShell with bindings for Motif calls, so you can easily create or prototype working GUIs. KornShell is based on the Bourne Shell (not to be confused with BASH), so syntax isn't extremely different. It's not anything modern, but if you're just looking to create a simple GUI for a script on a system that ships with CDE, it's a good alternative to using tcl.

To start, you only need the following:

  • the Motif toolkit
  • the Common Desktop Environment or at very least its Korn shell (dtksh).

Once you've installed both, open a text editor of your choice and write these two lines:

#!/usr/dt/bin/dtksh
. /usr/dt/lib/dtksh/DtFuncs.dtsh

The first like is the "she-bang" which tells the system to use CDE's special version of the Korn shell. The second line imports a file with some useful definitions. Make sure that the paths match those of your CDE installation.

Now let's create the main window:

XtInitialize TOPLEVEL helloWorld Dtksh $0
XtSetValues $TOPLEVEL title:"Hello World"

XtCreateManagedWidget BBOARD bboard XmBulletinBoard $TOPLEVEL \
  resizePolicy:RESIZE_NONE \
  height:150 width:250 \
  background:SkyBlue
XtCreateManagedWidget BUTTON pushbutton XmPushButton $BBOARD \
  background:LigthBlue \
  foreground:Black \
  labelString:"Click me!" \
  height:30 width:100 x:75 y:60 shadowThickness:3

This is quite a lot of code and it might take a few moments to figure it out, but all it does is create a top-level window ($TOPLEVEL), the pane where all controls will be layed out ($BBOARD) and a button ($BUTTON).

Adding more controls is not hard and you can leave out most of the parameters if you don't need them.

If you try to run it now, nothing noticable will happen. This is because we've only defined the UI, but haven't actually told the shell to display it. The following two lines take care of this:

XtRealizeWidget $TOPLEVEL
XtMainLoop

This will display the window ($TOPLEVEL) and loop until it's closed or otherwise terminated. You can already run the script at this point, just don't forget to make it executable (chmod +x script.sh).

A Hello World window

We now have a window with a button, but we're still missing the click handler code. For this, we need to bind a function as a parameter to the XtAddCallback function. Let's write our function first:

displayMessage() {
    DtkshDisplayInformationDialog "Hello!" "Hello World!"
}

...and now we just have to connect it to our button. Add the following line above XtRealizeWidget:

XtAddCallback $BUTTON activateCallback displayMessage

The entire script should now look like this:

#!/usr/dt/bin/dtksh
. /usr/dt/lib/dtksh/DtFuncs.dtsh

# this function is called when the button is clicked
displayMessage() {
    DtkshDisplayInformationDialog "Hello!" "Hello World!"
}

# define the UI
XtInitialize TOPLEVEL helloWorld Dtksh $0
XtSetValues $TOPLEVEL title:"Hello World"

XtCreateManagedWidget BBOARD bboard XmBulletinBoard $TOPLEVEL \
  resizePolicy:RESIZE_NONE \
  height:150 width:250 \
  background:SkyBlue
XtCreateManagedWidget BUTTON pushbutton XmPushButton $BBOARD \
  background:LigthBlue \
  foreground:Black \
  labelString:"Click me!" \
  height:30 width:100 x:75 y:60 shadowThickness:3


# add the callback to the button
XtAddCallback $BUTTON activateCallback displayMessage

# display the window
XtRealizeWidget $TOPLEVEL
XtMainLoop

If you run it now and click the button, a message shows up:

A Motif Hello World program

You can experiment with the script a bit (change the colors, labels, add another button to the window and so on). Most function names are easy to guess. For example, if you'd like a warning popup instead of the plain one, you can replace DtkshDisplayInformationDialog with DtkshDisplayWarningDialog.

Most function names match those used in C and if you have a newer version of the CDE, there's also a bundled program called dtinfo which contains a moderately detailed writeup about dtksh programming.

While CDE is not used a lot anymore, it's still present on certain older systems and this makes scripts feel less out of place compared to the rest of the system.