Chapter 2. Tutorials

Table of Contents

2.1. Making a Window
2.2. Displaying Data
2.3. Entering Data
2.4. A complete demo program: wtltest.g

The tutorials given here show how to create a window where you can display and interact with live data. These tutorials build on the tutorials in the DataHub Scripting manual and assume you have some knowledge of Gamma.

2.1. Making a Window

[Note]

This first tutorial is repeated from the DataHub Scripting manual.

  1. Open the Properties window, select the Scripting option, and click the New button to create a new script.
  2. In the New Script File dialog, name the main class 'MyWindows' and select the Windows option. More details.
  3. Add the file to your list of files, and load it now. Here's how. A new window will open:
  4. Click the close icon in the top right corner. The window will close, and you will see this dialog box:

The Code

The code that gets written to the MyApp.g file is as follows:

require ("Application");

/* Get the Gamma library functions and methods for ODBC and/or
 * Windows programming.  Uncomment either or both. */

require ("WindowsSupport");
//require ("ODBCSupport");

/* Applications share the execution thread and the global name
 * space, so we create a class that contains all of the functions
 * and variables for the application.  This does two things:
 *   1) creates a private name space for the application, and
 *   2) allows you to re-load the application to create either
 *      a new unique instance or multiple instances without
 *      damaging an existing running instance.
 */
class MyApp Application
{
    window;
}

/* Use methods to create functions outside the 'main line'. */
method MyApp.samplemethod ()
{
}

/* Write the 'main line' of the program here. */
method MyApp.constructor ()
{
    local    rect = CreateRect (0, 0, 300, 300), txt;
    .window = new GWindow();
    
    .window.Create (0, rect, "Hello", WS_OVERLAPPEDWINDOW, 0);
    .window.CenterWindow();
    txt = .window.CreateControl (GStatic, 0, 0, 280, 22, "Hello world", SS_CENTER);
    txt.CenterWindow();
    .window.MessageHandler (WM_DESTROY, `destroy(@self));
    .window.ShowWindow (SW_SHOW);
}

/* Any code to be run when the program gets shut down. */
method MyApp.destructor ()
{
    // The WM_DESTROY message could come before or after this destructor depending
    // on whether the application instance is destroyed or the window is closed
    // first.  We protect against the case where the window is closed first.
    if (instance_p(.window) && .window.GetHwnd() != 0)
        .window.PostMessage (WM_CLOSE, 0, 0);
    MessageBox(0, string ("Application: ", class_name(self), " completed."), "Done", 0);
}

/* Start the program by instantiating the class.  If your
 * constructor code does not create a persistent reference to
 * the instance (self), then it will be destroyed by the
 * garbage collector soon after creation.  If you do not want
 * this to happen, assign the instance to a global variable, or
 * create a static data member in your class to which you assign
 * 'self' during the construction process.  ApplicationSingleton()
 * does this for you automatically. */
ApplicationSingleton (MyApp);