Viewer for plain ASCII

The viewer is an applet corresponding to W3A/V. It works by opening an XmText widget and displaying the text in it.

instance is an array that maps IDs to XmText widgets. ninstances is the number of assigned IDs, except that there may be lower numbered IDs that have been freed again. When closePlain is called, the corresponding widget element of instance is set to NULL, so that the ID can be reused.

openPlain finds the first unused element in instance. The index will be the ID that is passed to the caller. It then asks for a work area and creates an XmText widget under it.

writePlain adds text to the XmText widget.

closePlain destroys the XmText widget and marks the element in instance as free, by setting it to NULL.

<<*>> =
#include <Xm/Xm.h> #include <Xm/Text.h> #include <w3a.h> #define MAXINSTANCE 30 static struct {Widget w; int nchars;} instance[MAXINSTANCE]; static int ninstances = 0; Bool initPlain() { /* Nothing to initialize */ } long openPlain(const W3ADocumentInfo info, W3AWindow work_area) { Widget w; Arg args[3]; int i, nargs = 0; for (i = 0; i < ninstances; i++) if (! instance[i].w) break; XtSetArg(args[nargs], XmNeditMode, XmMULTI_LINE_EDIT); nargs++; XtSetArg(args[nargs], XmNeditable, FALSE); nargs++; w = XmCreateScrolledText(work_area, "plain", args, nargs); XtManageChild(w); XtRealizeWidget(w); instance[i].w = w; instance[i].nchars = 0; if (i == ninstances) ninstances++; return i; } int writePlain(long id, const char *buf, size_t nbytes) { char *s, *t; s = XtMalloc(nbytes + 1); *s = '\0'; strncat(s, buf, nbytes); for (t = s; *t; t++) if (*t == '\015') *t = ' '; XmTextInsert(instance[id].w, instance[id].nchars, s); XtFree(s); instance[id].nchars += nbytes; return nbytes; } Bool closePlain(long id) { int i; XtDestroyWidget(instance[id].w); instance[id].w = NULL; }