2. Simple Widgets

Label

Command

Toggle


  /**************************************************************
   *
   *                      rbox.c
   *
   *  Simple radio button utility.  The radio_box procedure creates
   *  a widget hierarchy that contains a box widget plus an
   *  arbitrary number of toggle widgets that are organized as
   *  a radio group.  The radio box hierarchy is used to maintain
   *  the value of a variable, this variable can have one of
   *  a small set of values, with each value represented by one
   *  of the radio buttons.  When a radio button is pressed the
   *  value of the variable changes.  The parameters passed to
   *  the radio_box procedure is a list of names for the buttons,
   *  the variable value corresponding to each button, and a
   *  pointer to the variable to be changed.  The value returned
   *  is the widget id of the box widget
   *
   *****************************************************************/

  #include  < X11/StringDefs.h > 
  #include  < X11/Intrinsic.h > 
  #include  < X11/Xaw/Box.h > 
  #include  < X11/Xaw/Toggle.h > 

  struct radio_struct {
       int  *variable;
       int  value;
  };


  static void rgback(w,rs,call)
  Widget w;
  struct radio_struct *rs;
	      
  XtPointer call; {

       *(rs->variable) = rs->value;

  }


  Widget radio_box(parent,names,values,variable,initial)
  Widget    parent;   /* parent of the radio box */
  char *names[];
  int  *values;
  int  *variable;
  int  initial; {
       struct    radio_struct *rs;
       Widget    togs[100];
       int  ntogs;
       char *init_name;
       Widget    box;
       Widget  group;
       int  len;
       Arg  wargs[10];



       int  index;
       int  i;

       box = XtCreateManagedWidget("box",boxWidgetClass,parent,
		 NULL, 0);

       ntogs = 0;
       while(names[ntogs] != 0) {
	    togs[ntogs] = XtCreateWidget(names[ntogs],
		      toggleWidgetClass,box,NULL,0);

	    if(ntogs == 0)
		 group = togs[0];

	    XawToggleChangeRadioGroup(togs[ntogs],group);

	    rs = (struct radio_struct *) malloc(sizeof *rs);
	    rs->variable = variable;
	    rs->value = values[ntogs];

	    XtAddCallback(togs[ntogs],XtNcallback,rgback,rs);

	    ntogs++;
       }

       XtManageChildren(togs,ntogs);

       index = -1;
       for(i=0; i < ntogs; i++)
	    if(values[i] == initial)
		 index = i;

       if(index  <  0)
	    index = 0;

       *variable = values[index];

       len = strlen(names[index]) + 1;
       init_name = (char *)malloc(len);
       strcpy(init_name,names[index]);

       XtSetArg(wargs[0],XtNradioData,init_name);
       XtSetValues(togs[index],wargs,1);

       XawToggleSetCurrent(group,init_name);
				     

       return(box);
  }









  /**********************************************************
   *
   *                     rtest.c
   *
   *  Test program for the radio box
   *
   *********************************************************/

  #include  < X11//StringDefs.h > 
  #include  < X11/Intrinsic.h > 
  #include  < X11/Xaw/Box.h > 
  #include  < X11/Xaw/Command.h > 
  #include "lib.h"

  char *names[] = {
       "value1",
       "value2",
       "value3",
       "value4",
       "value5",
       0
  };

  int values[] = {
       1,
       2,
       3,
       4,
       5
  };

  int var;

  void var_print(w,client,call)
  Widget         w;
  XtPointer client;
  XtPointer call; {

       printf("var: %d\n",var);

  }

  main(argc, argv)
  int argc;
  char **argv; {
       Widget toplevel;
       Widget box;
       Widget buttons;
       Widget quit;
       Widget print;

       toplevel = XtInitialize(argv[0],"rtest",NULL, 0,
		      &argc,argv);

       box = XtCreateManagedWidget("main",boxWidgetClass,
		 toplevel, NULL, 0);

       print = XtCreateManagedWidget("print", commandWidgetClass,
		 box, NULL, 0);

       XtAddCallback(print, XtNcallback, var_print, NULL);
					    

       quit = quit_button(box);
		  

       buttons = radio_box(box,names,values,&var,1);
		      

       XtRealizeWidget(toplevel);

       XtMainLoop();

  }


List

Scrollbar


  Go to Xt and the Athena Widgets        Go to Index        Go to Composite Widgets