v02i005: a different interface to widget creation, Part01/01

Mike Wexler mikew at wyse.wyse.com
Fri Oct 21 04:33:06 AEST 1988


Submitted-by: Dan Heller <island!maui!argv at Sun.COM>
Posting-number: Volume 2, Issue 5
Archive-name: widgetwrap/part01



Mike -- I think the net will find this useful ... Please ACK this note.

: This is a shell archive file. Remove everything above this line
: to unbundle. chmod +x "thisfile", then run it: e.g. % thisfile
: SHAR archive format.  Archive created Sat Oct 15 19:55:54 PDT 1988
: file contains: 
:	README
:	WidgetWrap.man
:	WidgetWrap.c
:	WidgetWrap.h
:	Patchlevel.h
echo x - README
sed 's/^X//' > README <<'+END+OF+README'
X
XThis software was written by Dan Heller <island!argv at sun.com> or
X<dheller at ucbcory.berkeley.edu>.  Mail to me if you have comments,
Xsuggestions, bugs, or whatever.
X
XThis is the WidgetWrap library.  It contains four files:
X    WidgetWrap.c
X    WidgetWrap.h
X    WidgetWrap.man
X    README
X
XWidgetWrap.c -- variable argument style programmer interfaces to widgets:
X    WidgetCreate(name, class, parent, varargs...);
X    WidgetSet(name, varargs);
X    WidgetGet(name, varargs);
X    GenericWidgetName(buf);
X
XThe purpose of this library is to allow the programmer to create
Xwidgets and set/get widget attributes via a variable argument list
Xstyle of function call.  This eliminates the need for many local
Xvariables and bothersome XtSetArg() calls and so forth.  An example
Xof usage:
X
X    Widget foo;
X
X    foo = WidgetCreate("foo", labelWidgetClass, toplevel,
X        XtNlabel,	"Widget",
X	XtNforeground,	WhitePixelOfScreen(XtScreen(toplevel)),
X	XtNbackground,	BlackPixelOfScreen(XtScreen(toplevel)),
X	XtNborderWidth,	1,
X	NULL);
X
XSee the C code and man page for more extensive documentation and notes.
X
XTo compile, just cc -c WidgetWrap.c and append the object file to any
Xlocal libraries you may have.  Or, just use the .o to link with other
Xprograms you may be writing.  Install the WidgetWrap.h file somewhere
Xlike /usr/include/X11 so you don't have to add annoying -I options to
Xyour compile command line or makefiles.
+END+OF+README
echo '-rw-rw-r--  1 argv         1414 Oct 15 19:53 README    (as sent)'
chmod u=rw,g=rw,o=r README
ls -l README
echo x - WidgetWrap.man
sed 's/^X//' > WidgetWrap.man <<'+END+OF+WidgetWrap.man'
X.\" This man page was written by Dan Heller <island!argv at sun.com>
X.\" or <dheller at ucbcory.berkeley.edu>
X.TH WidgetWrap 3X "" "1 March 1988" "X Version 11" 
X.SH NAME
XWidgetCreate, WidgetSet, WidgetGet \- programmer's convenience routines
X.SH SYNTAX
XWidgetCreate\^(\fIname\fP\^, \fIclass\fP\^, \fIparent\fP\^, \fIvarargs\fP)
X.br
X      char *\fIname\fP\^;
X.br
X      WidgetClass \fIclass\fP\^;
X.br
X      Widget \fIparent\fP\^;
X.LP
XWidgetSet\^(\^\fIwidget\fP, \fIvarargs\fP\^)
X.br
X      Widget *\fIwidget\fP\^;
X.LP
XWidgetGet\^(\^\fIwidget\fP, \fIvarargs\fP\^)
X.br
X      Widget *\fIwidget\fP\^;
X.LP
Xchar *
XGenericWidgetName\^(\^\fIbuf\fP)
X.br
X      char *\fIbuf\fP\^;
X.SH ARGUMENTS
X.IP \fIbuf\fP 1i
XBuffer to place new widget name.
X.IP \fIclass\fP 1i
XThe class of the widget to be created.
X.IP \fIname\fP 1i
XThe name of the widget when created.
X.IP \fIparent\fP 1i
XThe parent of the widget to be created.
X.IP \fIwidget\fP 1i
XThe widget to set or get attributes.
X.SH DESCRIPTION
X.ds WC .PN WidgetCreate
XThe purpose of these functions is to allow the programmer to create
Xwidgets and set and get widget attributes via a variable argument list
Xstyle of function call.  This eliminates the need for many local
Xvariables and bothersome XtSetArg() calls and so forth.  An example
Xof usage:
X.sp
X.in +2
X.nf
XWidget foo;
X.sp
Xfoo = WidgetCreate("foo", labelWidgetClass, toplevel,
X.in +2
XXtNlabel,           "Widget",
XXtNforeground,      WhitePixelOfScreen(XtScreen(toplevel)),
XXtNbackground,      BlackPixelOfScreen(XtScreen(toplevel)),
XXtNborderWidth,     1,
XNULL);
X.sp
X.fi
X.in -4
XAs you can see, the list must be NULL terminated.  You may pass up to
Xto MAXARGS argument pairs (which is defined in WidgetWrap.h).  There are
Xspecial args available to the Create/Get/Set functions that are available:
X.sp
X.in +2
XXtNmanaged\ \ \ \ pass "False" to create a non-managed widget.
XXtNargList\ \ \ \ takes \fItwo\fP parameters.
X.in -2
X.sp
XThe XtNargList makes it possible to pass attributes to the Create/Get/Set
Xcalls that are probably common to many widgets to be created or reset.
X.sp
X.in +2
X.nf
Xstatic Arg args[] = {
X.in +2
XXtNforeground,   black,
XXtNbackground,   white,
XXtNwidth,        20,
XXtNheight,       10,
X.ti -2
X};
X.sp
X.ti -2
Xfoo = WidgetCreate(NULL, widgetClass, toplevel,
XXtNargList,      args, XtNumber(args),
XNULL);
X.in -4
X.fi
X.sp
XMost large applications will create huge numbers of widgets which the
Xprogrammer has to think up unique names for all of them.  What's more,
Xtypically, as noted by the examples above, the names are constant strings
Xwhich takes up memory, disk spaces, etc...  So, if WidgetCreate() gets
XNULL as the name of the widget, then a widget name will be created
Xautomatically by calling GenericWidgetName() since most of the time,
Xuser's don't care what the name of a widget is, this capability is available.
X.SH DIAGNOSTICS
XWidgetCreate() will return NULL if a widget cannot be created.  It uses
Xthe routines (and is a front end for) XtCreateWidget() and
XXtCreateManagedWidget().
X.SH "SEE ALSO"
XXtCreateWidget(3X),
XXtCreateManagedWidget(3X),
X.br
X\fIXlib \- C Language X Interface\fP
+END+OF+WidgetWrap.man
echo '-rw-rw-r--  1 argv         3098 Oct 15 19:48 WidgetWrap.man    (as sent)'
chmod u=rw,g=rw,o=r WidgetWrap.man
ls -l WidgetWrap.man
echo x - WidgetWrap.c
sed 's/^X//' > WidgetWrap.c <<'+END+OF+WidgetWrap.c'
X/*
X * WidgetWrap.c -- variable argument style programmer interfaces to widgets:
X *    WidgetCreate(name, class, parent, varargs...);
X *    WidgetSet(name, varargs);
X *    WidgetGet(name, varargs);
X *    GenericWidgetName(buf);
X *
X * This module was written by Dan Heller <island!argv at sun.com> or
X * <dheller at ucbcory.berkeley.edu>.
X *
X * The purpose of this module is to allow the programmer to Create
X * widgets and set/get widget attributes via a variable argument list
X * style of function call.  This eliminates the need for many local
X * variables and bothersome XtSetArg() calls and so forth.  An example
X * of usage:
X *
X *    Widget foo;
X *
X *    foo = WidgetCreate("foo", labelWidgetClass, toplevel,
X *        XtNlabel,		"Widget",
X *	  XtNforeground,	WhitePixelOfScreen(XtScreen(toplevel)),
X *	  XtNbackground,	BlackPixelOfScreen(XtScreen(toplevel)),
X *        XtNborderWidth,	1,
X *        NULL);
X *
X * As you can see, the list must be NULL terminated.  You may pass up to
X * to MAXARGS argument pairs.  Increase this number in WidgetWrap.h if
X * necessary.  There are special args availabel to the create/get/set
X * functions that are available:
X *
X *    XtNmanaged		pass "False" to create a non-managed widget.
X *    XtNargList		takes _two_ parameters.
X *
X * The XtNargList makes it possible to pass attributes to the create/get/set
X * calls that are probably common to many widgets to be created or reset.
X *
X * static Arg args[] = {
X *     XtNforeground,	black,
X *     XtNbackground,   white,
X *     XtNwidth,	20,
X *     XtNheight,	10,
X * };
X * foo = WidgetCreate("bar", widgetClass, toplevel,
X *     XtNargList,	args, XtNumber(args),
X *     NULL);
X *
X * Most large applciations will create huge numbers of widgets which the
X * programmer has to think up unique names for all of them.  What's more,
X * typically, as noted by the examples above, the names are constant strings
X * which takes up memory, disk spaces, etc...  So, if WidgetCreate() gets
X * NULL as the name of the widget, then a widget name will be created
X * automatically.  Since most of the time, user's don't care what the name
X * of a widget is, this capability is available.
X *
X * Finally, a note about varargs.  Note that there are many different
X * implementations of varargs.  To maintain portability, it is important
X * to never return from a function that uses varargs without calling va_end().
X * va_start() and va_end() should always exist in the same block of {}'s.
X * There can be blocks between them, but va_end() shouldn't be in a block
X * inside of the va_start() stuff.  This is to allow support for weird
X * implementations which define va_start() to something like:  ".... { "
X * (pyramid computers for one).
X * Also, if you use varargs, never declare "known" arguments; extract them
X * from the vararg list later.
X */
X#include <stdio.h>
X#include <X11/Intrinsic.h>
X#include <varargs.h>
X#include "X11/WidgetWrap.h"
X
Xchar *
XGenericWidgetName(buf)
Xchar *buf;
X{
X    static int widget_count;
X
X    (void) sprintf(buf, "_widget.%6d", widget_count++);
X    return buf;
X}
X
X/*
X * WidgetCreate()
X *	Create a widget passing it's instance attributes and other parameters
X * as variable arguments.  This removes the need to build your own Arg[]
X * lists, etc...  Terminate argument list pairs with a NULL argument.
X */
X/* VARARGS */
XWidget
XWidgetCreate(va_alist)
Xva_dcl
X{
X    va_list	var;
X    Arg		args[MAXARGS];
X    int		err = 0, nargs, i = 0;
X    int		managed = True;
X    String	argstr;
X    XtArgVal	argval;
X    char	*name, buf[32];
X    WidgetClass class;
X    Widget	parent;
X
X    va_start(var);
X
X    if (!(name = va_arg(var, char *)))
X	name = GenericWidgetName(buf);
X    class = va_arg(var, WidgetClass);
X    parent = va_arg(var, Widget);
X
X    while (argstr = va_arg(var, char *)) {
X	if (i == MAXARGS) {
X	    fprintf(stderr, "WidgetCreate: too many arguments: %d\n", i);
X	    err++;
X	    break;
X	}
X	if (!strcmp(argstr, XtNargList)) {
X	    ArgList list = va_arg(var, ArgList);
X	    int numargs = va_arg(var, int);
X	    for (numargs--; i < MAXARGS && numargs >= 0; i++, numargs--)
X		XtSetArg(args[i], list[numargs].name, list[numargs].value);
X	    if (i == MAXARGS) {
X		fprintf(stderr, "WidgetCreate: too many arguments: %d\n", i);
X		err++;
X		break;
X	    }
X	} else if (!strcmp(argstr, XtNmanaged))
X	    /* managed is _really_ boolean, but varargs only works with ints */
X	    managed = va_arg(var, int);
X	else {
X	    argval = va_arg(var, XtArgVal);
X	    XtSetArg(args[i], argstr, argval);
X	    ++i;
X	}
X    }
X    va_end(var);
X
X    if (err)
X	return NULL;
X
X    if (managed)
X	return XtCreateManagedWidget(name, class, parent, args, i);
X    else
X	return XtCreateWidget(name, class, parent, args, i);
X}
X
X/*
X * WidgetSet()
X *	Once a widget has been created, you may use this routine to
X * add or change a varaible number of attributes on it.
X */
X/*VARARGS*/
Xvoid
XWidgetSet(va_alist)
Xva_dcl
X{
X    String   argstr;
X    Arg      args[MAXARGS];
X    XtArgVal argval;
X    int      i = 0;
X    va_list  var;
X    Widget   w;
X
X    va_start(var);
X
X    w = va_arg(var, Widget);
X
X    while (argstr = va_arg(var, char *)) {
X	if (i == MAXARGS) {
X	    fprintf(stderr, "Warning: increase MAXARGS! (%d)\n", i);
X	    XtSetValues(w, args, i);
X	    i = 0;
X	}
X	if (!strcmp(argstr, XtNargList)) {
X	    ArgList list = va_arg(var, ArgList);
X	    XtArgVal numargs = va_arg(var, Cardinal);
X	    XtSetValues(w, list, numargs);
X	} else {
X	    argval = va_arg(var, XtArgVal);
X	    XtSetArg(args[i], argstr, argval);
X	    ++i;
X	}
X    }
X    va_end(var);
X    if (i > 0)
X	XtSetValues(w, args, i);
X}
X
X/*
X * WidgetGet()
X *     Get the values of a widget via an interface identical to WidgetSet
X */
X/*VARARGS*/
Xvoid
XWidgetGet(va_alist)
Xva_dcl
X{
X    String   argstr;
X    Arg      args[MAXARGS];
X    XtArgVal argval;
X    int      i = 0;
X    va_list  var;
X    Widget   w;
X
X    va_start(var);
X
X    w = va_arg(var, Widget);
X
X    while (argstr = va_arg(var, char *)) {
X	argval = va_arg(var, XtArgVal);
X	if (i == MAXARGS) {
X	    fprintf(stderr, "Warning: increase MAXARGS! (%d)\n", i);
X	    XtGetValues(w, args, i);
X	    i = 0;
X	}
X	if (!strcmp(argstr, XtNargList)) {
X	    ArgList list = va_arg(var, ArgList);
X	    XtArgVal numargs = va_arg(var, Cardinal);
X	    XtGetValues(w, list, numargs);
X	} else
X	    XtSetArg(args[i], argstr, argval);
X        ++i;
X    }
X    va_end(var);
X    if (i > 0)
X	XtGetValues(w, args, i);
X}
+END+OF+WidgetWrap.c
echo '-rw-r--r--  1 argv         6348 Oct 15 19:45 WidgetWrap.c    (as sent)'
chmod u=rw,g=r,o=r WidgetWrap.c
ls -l WidgetWrap.c
echo x - WidgetWrap.h
sed 's/^X//' > WidgetWrap.h <<'+END+OF+WidgetWrap.h'
X/*
X * WidgetWrap.h -- header file for the WidgetWrap library.
X *
X * This module was written by Dan Heller <island!argv at sun.com> or
X * <dheller at ucbcory.berkeley.edu>.
X */
X
X#define MAXARGS		50
X#define XtNargList	"Arglist"
X#define XtNmanaged	"Managed"
X
Xextern void WidgetSet(), WidgetGet();
Xextern Widget WidgetCreate();
Xextern char *GenericWidgetName();
+END+OF+WidgetWrap.h
echo '-rw-rw-r--  1 argv          352 Oct 15 19:45 WidgetWrap.h    (as sent)'
chmod u=rw,g=rw,o=r WidgetWrap.h
ls -l WidgetWrap.h
echo x - Patchlevel.h
sed 's/^X//' > Patchlevel.h <<'+END+OF+Patchlevel.h'
X#define PATCHLEVEL 0
+END+OF+Patchlevel.h
echo '-rw-rw-r--  1 argv           21 Oct 15 19:47 Patchlevel.h    (as sent)'
chmod u=rw,g=rw,o=r Patchlevel.h
ls -l Patchlevel.h
exit 0
-- 
Mike Wexler(wyse!mikew)    Phone: (408)433-1000 x1330
Moderator of comp.sources.x



More information about the Comp.sources.x mailing list