[comp.windows.x] suggestions regarding Args[] usage in the X Toolkit

sundar@WHEATIES.AI.MIT.EDU (Sundar Narasimhan) (07/12/88)

The styles of XtSetArg suggested in the ToolKit documentation are all
fine. But for those of us that have <varargs.h>, and have used
SunView, and like less cluttered code, the following should make it a
little easier to create and add widgets (it is also less error prone,
believe me): (I'd like to suggest that
something like the following be added to the toolkit)

Example of usage:
    wid_status_form = (Widget) 
      xt_add_widget("statusform", wid_frame, formWidgetClass,
		    XtNfromVert, wid_status,
		    XtNresizable, TRUE,
		    NULL);

    wid_commands = (Widget) 
      xt_add_widget("commands", wid_frame, formWidgetClass,
		    XtNfromVert, wid_status_form,
		    XtNresizable, FALSE,
		    NULL);

I leave similar interfaces for the XtSetValues and XtGetValues
functions, and ways of implementing this without using a fixed-size
array on the stack as exercises for the reader.  

-Sundar
-----------------
#include <X11/Intrinsic.h>
#include <varargs.h>
#include <stdio.h>

#define  MAXARGS       20

/* Varargs passed one down */
xt_set_listargs(args, i, maxval, list)
Arg args[];
int *i;
int maxval;
va_list *list;
{
    va_list  var = *list;
    int      argno;
    String argstr;
    XtArgVal argval;
    
    argstr = va_arg(var, char *);

    if(i != NULL) argno = *i;
    else argno = 0;

    while(argstr) {
	argval = va_arg(var, XtArgVal);
	if((argno + 1) > maxval) {
	    fprintf(stderr, "xt_set_args: Arg array overflowed!\n");
	    va_end(var);
	    return -1;
	}
	XtSetArg(args[argno], argstr, argval);
	if(i != NULL)
	  *i = ++argno;
	argstr = va_arg(var, char *);
    }
    return 0;
}

/*VARARGS*/
Widget
xt_add_widget(name, parent, class, va_alist)
char *name;
Widget parent;
WidgetClass class;
va_dcl
{
    va_list var;
    Arg args[MAXARGS];

    int i = 0;

    /* First take care of any additional arguments */
    va_start(var);
    xt_set_listargs(args, &i, MAXARGS, &var);
    va_end(var);

    return(XtCreateManagedWidget(name, class, parent, args, i));
}