Programming
the Front End

From a tutorial given at the 1998 Worldwide Mathematica Conference by John M. Novak

The following a specific examples of front end programming that I used to emphasize the general concepts given in my introduction. While there is not a detailed explanatory gloss for the following code, it may still prove useful to someone familiar with general Mathematica programming concepts who wants specific examples of programming with the front end.

These examples were put together for the conference, so the code sometimes skips error checking and boundary case handling in the interests of clarity. For example, the 'Information Buttons' example does not correctly handle all cases, such as those where there is no matching symbol. However, the sense of the code should be reasonably clear.

Information Buttons

The 'Information Buttons' example hooks into a typesetting rule that catches the ?symbol syntax, and assembles a palette in the case where there are multiple matching symbols. It also attaches a button to usage messages that hyperlinks to the help browser.

The code is written to work via a call to 'UsageGrid'; only at the last step is this hooked into a typesetting rule. 'Names' is used to determine the matching symbols.

In the case of multiple matching names, write out a cell containing a grid of buttons that call the function that writes the individual usage messages.

Construct a grid from a list. The factor of '9' is something of an empirically determined magic number specific to the default font family and size of an output cell. The list of names gets padded with a list of empty elements of appropriate size to create a hopefully nice square array.

This writes out the cell with the usage message and an attached button to the help browser if the symbol is in the System` context.

Here is the rule that attaches this function to a typesetting rule.

Here's an example of its use:

?*Factor*

Front End Dialogs

Examples of simple 'dialog box'-like windows.

Kernel Invoked Evaluation

This one is modeled after the kernel function 'Input'; it is invoked by a kernel evaluation, and returns the result to the kernel. Unfortunately, in the current version of Mathematica, there isn't a nice event model, so this consists of a somewhat kludgy busy loop that continually checks the status of the 'dialog' notebook window. This will change in a future version of Mathematica.

Here's the part that opens the window.

FrontEnd Invoked Evaluation

This code creates a window that can stand alone, and be saved and reopened as a functioning dialog. The resulting dialog will invoke a particular kernel evaluation when you click the OK button.

Polyhedron Explorer

Polyhedron Explorer is a demonstration palette that is included with the Mathematica distribution. Embedded within it are some interesting examples of Mathematica programming. To see the palette in action, click here.

Since much of the code is embedded in button functions in the cells, here's a glance at what some of it looks like. Beware that this code won't do much out of the context of the explorer.

Recall that this code was embedded in buttons; it isn't in the form of function definitions as above. Also, this code was first written with an early version of Mathematica V3; today, I would probably save state in the notebook itself, by way of the TaggingRules option, rather than using global variables in the kernel as the following code does.

Create a Shape

Note that much of the following is involved with setting up and retrieving the state of the system. The core is really in the code that updates the graphic cell (in red, below);
though you should also pay attention to the line that precedes it, which locates the graphics cell by way of its cell tag.

Paste the Graphic

This code was chosen as an example of the use of front end tokens. Its purpose was to take the graphic from the explorer and put a copy of it in a working notebook. The Select clause attempts to determine which notebook is the topmost notebook suitable for a paste. Next, the graphic is located in the explorer, copied, and pasted into the working notebook (I've put this part in red). Finally, some attributes are changed in the pasted cell and the graphic is rerendered, for a cleaner presentation.