4.10. Encapsulate Loading and Creating Widgets

#!/usr/cogent/bin/phgamma

/*
 * This example is the same as the previous one, except that it
 * demonstrates one way of doing object-oriented encapsulation in Gamma.
 * It uses the progam PhabOO.g which defines classes that encapsulate
 * loading and creating widgets.
 *
 * 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);
require_lisp("PhotonWidgets.lsp");
require_lisp("PhabTemplate.lsp");
require_lisp("/usr/cogent/lib/const/Filesys.lsp");

/*
 * Require the PhabOO.g file that defines the encapsulation classes.
 */
require("lib/PhabOO.g");
require("lib/utility.g");

/*
 * Create a function that walks a widget tree and finds
 * the widget you specify by name.
 */
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, read in the widget definitions,
 * access them, and display them in the window.
 */
win1 = new(PhabWindow);
file = string("WidgetFiles/wgt/", cadr(argv),".wgtw");
load = win1.Load(file);
win1.Open();

/*
 * Make another window to hold a button.
 */
win2 = new(PtWindow);
win2.SetArea(350,50,120,120);

/*
 * Load the definitions for the widget tree.
 */
pdefs = new(PhabWgtwDefs);
defs = car(pdefs.Load(file));

/*
 * Find the widget (a button) you have chosen, as specified
 * by name on the command line.
 */
choice = symbol(caddr(argv));
buttree = PhabLookupTree(defs,choice);
butdef = cons(buttree,nil);

/*
 * Create the widget, position it, and realize it by
 * realizing the window.
 */
newbut = caar(PhabCreateWidgets(butdef,nil,nil));
newbut.SetPos(20,40);
PtRealizeWidget(win2);

PtMainLoop(); 
		

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.