4.9. Extract Certain Widgets

#!/usr/cogent/bin/phgamma

/*
This example reads a widget file that contains 8 buttons with .gif
images on them.  It creates the widgets and puts them into two
windows.  The first window contains all of the widgets, while the
second window contains a widget of your choice, which you can enter as
the third argument on the command line. To run this example, you must
be in a directory that can access the .wgt file. Type the 2 arguments
buttons and [button name] at the shell prompt, where [button name]
can be one of: Go, Stop, Pause, Done, Increase, Decrease, Left or Right.
*/

PtInit(nil);

/*
 * Create some convenience functions.
 */
PhabGetTop = car;
PhabGetRoot = car;
PhabGetChildren = cdr;
PhabGetLevel = car;
PhabGetType = cadr;
PhabGetName = caddr;

require_lisp("PhotonWidgets.lsp");
require_lisp("PhabTemplate.lsp");

/*
 * Read the widget definitions, and access them.
 */
file = string(_os_, "-WidgetFiles/wgt/buttons.wgtw");
defs = PhabReadWidgetFile(file);
tree = PhabGetTop(defs);
children = PhabGetChildren(tree);

/*
 * Make a function to index the buttons by number.
 */
function index_buttons(buttons)
{
	local index = 1;
	indexlist = list();

	with button in buttons do
	{
		local name = PhabGetName(car(button));
		indexlist = cons(list(name, index), indexlist);
		index ++;
	}
	indexlist;
}

/*
 * Make a function to look up a requested button.
 */
function lookup (name, list)
{
	car(assoc(name, list));
}

/*
 * Make a new window, create all the buttons, and display them
 * in the window.
 */
win = new(PtWindow);
win.SetArea(100,50,120,500);
for (i=0;i<8;i++)
{
	eachbut = car(car(PhabCreateWidgets(list(car(nth_cdr(children,i)))
		,nil,nil)));
	if (eachbut.name == #Done)
	  PtAttachCallback(eachbut, Pt_CB_ACTIVATE,#exit_program(-1));
	eachbut.SetPos(25, (i * 60));
	PtRealizeWidget(eachbut);
	PtExtentWidget(win);
}

/*
 * Find the requested button.
 */
buttonlist = index_buttons(children);
if (cadr(argv))
  argument = cadr(argv);
else argument = "Go";
found = lookup(symbol(argument), buttonlist);
index = cadr(found) - 1;

/*
 * Make a new window, create the requested button, and display it.
 */
win2 = new(PtWindow);
win2.SetArea(250,50,120,120);
anybut = car(car(PhabCreateWidgets(list(car(nth_cdr(children,index)))
	,nil,nil)));
anybut.SetPos(20,40);

princ("Choose another button by using one of:\n",
	  "Go, Stop, Pause, Done, Increase, Decrease, Left or Right\n",
	  "as a command-line argument.\n");

PtRealizeWidget(win);
PtMainLoop();