4.10. Select and Recreate Any Widget

#!/usr/cogent/bin/phgamma

/*
 * This example is similar to the previous one, in that it pulls
 * specified widgets out of an existing widget, named readtestfile.wgtw.
 * Enter the name for the widget of your choice on the command
 * line.  The possible names are: MyTestFile (brings the whole
 * widget), MainWindowButton, PaneOne, PaneOneButtonA,
 * PaneOneButtonB, PaneTwo, and PaneTwoButtonC.
 */   
   
PtInit(nil);
require_lisp("PhotonWidgets");
require_lisp("PhabTemplate");

/*
 * Read the widget definitions, and access them.
 */
file = string(_os_, "-WidgetFiles/wgt/readtestfile.wgtw");
defs = PhabReadWidgetFile(file);
treewin = car(defs);
 
/*
 * Make a function that walks a widget tree and finds the widget
 * you want. (The same as in the previous example.)
 */
function PhabLookupTree(tree,name)
  {
    local return = nil;
    local trees;
    if (caddr(car(tree)) == name)
      return = tree;
    else
      {
	for (trees = cdr(tree); trees && !return; trees = cdr(trees)){
	   return = PhabLookupTree(car(trees),name);
	 }
      }
    return;
  }

/*
 * Make a new window to hold the widget.
 */
win = new(PtWindow);
win.SetDim(500,450);

exitbut = new(PtButton);
exitbut.SetPos(220, 420);
exitbut.text_string = " Exit ";
PtAttachCallback(exitbut,Pt_CB_ACTIVATE,#exit_program(-1));


if (cadr(argv))
{
  /*
   * Call the PhabLookupTree function on a widget from the command line.
   */
  read_name = symbol(cadr(argv));
  wgtdef = PhabLookupTree(treewin,read_name);
  /*
   * Transform the widget tree into a widget definition.
   */
  wgtdef = cons(wgtdef,nil);
  
  /*
   * Create the widget.
   */
  wgt = PhabCreateWidgets(wgtdef,nil,nil);	  
  
  /*
   * Realize the widget by realizing the window.
   */
  PtRealizeWidget(win);
  PtMainLoop();			
} 

else princ("\nYou must enter one of the following as a command-line argument: \n",
		   "MyTestFile (brings the whole widget), MainWindowButton, PaneOne, \n",
		   "PaneOneButtonA, PaneOneButtonB, PaneTwo, and PaneTwoButtonC\n\n");