Chapter 4. Building Applications - a Tutorial

Table of Contents

4.1. Create a Window
4.2. Add an Exit Button
4.3. Add Functionality with Callbacks
4.4. Load a Window Created in PhAB
4.5. Read a Widget File and Create its Widgets
4.6. Read a Widget File and Assign its Widgets to a Class
4.7. Set, Test, and Clear Widget Flags
4.8. Read and Print a Widget File
4.9. Extract Certain Widgets
4.10. Select and Recreate Any Widget
4.11. Print a Widget Tree Summary

Here is a graduated tutorial that introduces some of the key features of Gamma, such as creating widgets, attaching callbacks, setting flags, and manipulating widget files for reuse. The programs and any associated widget files are in the distribution. The tutorial programs are numbered t_01 through t_11. You can run them from the command line by using the command phgamma followed by the name of the tutorial. For example, type phgamma t_04 to run Tutorial 4: Load a Window Created in PhAB.

4.1. Create a Window

#!/usr/cogent/bin/phgamma

/*
 * This example demonstrates how to create a Photon application in Gamma.
 * Since the Photon widget set is mapped as OOP classes in Gamma, any
 * Photon widget can be created using the 'new' function and then
 * manipulated by simply modifying its instance variables.
 * 
 * This file is best viewed with a tab width of 4.
 *
 * First, we load some Photon Widget convenience functions to make our
 * job easier.  In this case, we are loading support for the SetDim,
 * SetPos and other functions which we use in this and later examples.
 */
require_lisp("PhotonWidgets");

/*
 * Initiate the graphics session with the Photon window manager.  This
 * must always be done before any Photon call can be made.
 */
PtInit(nil);

/*
 * Create a new instance of a PtWindow object.  You can look up PtWindow
 * in the Reference Manual, which is also linked to the online help for
 * Photon.  Gamma uses the same resources and constants as Photon widgets,
 * except that resource names are in lower case, and have the Pt_ARG_
 * prefix removed.  All constants are named identically to the ones named
 * in the Photon documentation.
 */
window = new(PtWindow);

/*
 * Set the width and height of the window.
 */
window.SetDim(300,250);

/*
 * Set the fill color of the window.
 */
window.fill_color = PgRGB(240,220,220);

/*
 * Realize the window widget on the screen.  A widget is not visible
 * until it has been explicitly realized, or until its container is
 * realized.
 */
PtRealizeWidget(window);

/*
 * Start an infinite event loop to handle Photon events. This keeps the
 * window open until the program exits (such as when Alt_F4 is pressed).
 */
PtMainLoop();