4.8. Read and Print a Widget File

#!/usr/cogent/bin/phgamma

/*
 * This example reads and prints a widget file locating each widget and
 * printing its level, type, and name on one line, followed by its resources.
 * The program reads a file name from the command line, using the argument
 * (cadr(argv)). Our test file is a window that contains one button and
 * two panes, each of which also contain buttons. Enter its name on the
 * command line as: QNX4-WidgetFiles/wgt/readtestfile.wgtw for QNX 4, or
 * QNX6-WidgetFiles/wgt/readtestfile.wgtw for QNX 6.
 */

PtInit(nil);

/*
 * Define some PhabGet- functions. (These are scheduled to be added as
 * library functions in the near future).
 */
PhabGetTop = car;
PhabGetRoot = car;
PhabGetChildren = cdr;
PhabGetLevel = car;
PhabGetType = cadr;
PhabGetName = caddr;
function PhabGetResources (wgt)
     {
       caddr(cdr(wgt));
     }

require_lisp("PhotonWidgets");
require_lisp("PhabTemplate");

/* Check to see if there is a command-line argument. */
if (cadr(argv))
{	
  /*
   * Read and print the complete widget file definition.
   */
  defs = PhabReadWidgetFile(cadr(argv)); 
  pretty_princ("The widget definition is:\n", defs, "\n\n");
  
  tree = PhabGetTop(defs);
  
  /*
   * Make a function that deconstructs the widget file definition,
   * extracting each widget definition in turn, and identifying
   * each part of each definition.
   */
  function print_tree(tree)
	{
	  local root = PhabGetRoot(tree);
	  local level = PhabGetLevel(root);
	  local type = PhabGetType(root);
	  local name = PhabGetName(root);
	  local resources = PhabGetResources(root);
	  
	  princ("The ", name, " widget is type ", type,
			" at level ", level, ". \nIts resources are: \n");
	  pretty_princ(resources, "\n");
	  
	  local children = PhabGetChildren(tree);
	  if (children)
	  {
		pretty_princ("Its children are:\n", children, "\n\n");
	  }
	  else princ("It has no children.\n\n");
	  
	  with child in children do {
		print_tree(child);
	  }
	}
  
  /*
   * Call the function, then create and realize the widgets as an illustration.
   */
  print_tree(tree);
  
  NewWindow = car(car(PhabCreateWidgets(defs, nil, nil)));
  NewWindow.SetPos(250,50);
  
  PtRealizeWidget(NewWindow);
  
  PtMainLoop();
}

else princ("Please enter a command-line argument, such as: \n",
		   "QNX4-WidgetFiles/wgt/readtestfile.wgtw in QNX 4, or\n",
		   "QNX6-WidgetFiles/wgt/readtestfile.wgtw in QNX 6.\n");