2.3. Entering Data

This tutorial illustrates three ways to enter data from a window: in a text entry field, using radio buttons, or with a track bar. All of the widgets are linked to the DataSim:UpdateFrequency point. They each change the value of the point. The text entry and track bar widgets also change to indicate the value of the point whenever the point changes.

Output

Code

The complete code for this tutorial is shown below, and is included in your DataHub distribution, in the scripts subdirectory, such as C:\Program Files\Cogent\DataHub\scripts\myscript.g. Please refer to Accessing Scripts in the DataHub Scripting manual for details on how to load and run a script.

require ("Application");
require ("WindowsSupport");

class MyDataEntry Application
{
    window;
}

/* Sets a DataHub point to the position of a scrolling widget. */
method MyDataEntry.Scrolled (tb)
{
    $DataSim:UpdateFrequency = tb.GetPos();
}

/* Formats a label for convenience. */
method MyDataEntry.format_label (win, letterh, letterw, font, color)
{
    win.SetForeground (0, 0, color);
    win.SetFontEx (letterh, letterw, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, font);
}

/* Sets a DataHub point to the value of a widget.*/
method MyDataEntry.SubmitEval (txt)
{
    local x;
    if (!undefined_p(x = eval_string (txt.GetWindowText())))
    {
        if (number_p(x) && (x >= 1) && (x <= 20))
            $DataSim:UpdateFrequency = x;
        else
            txt.SetWindowText("Try again");
    }
    else
        txt.SetWindowText("Try again");
    nil;
}


/* The 'main line' of the program. */
method MyDataEntry.constructor ()
{
    local    rect = CreateRect (0, 0, 300, 300), txt, btn;
    local    x = 20, y = 10, w = 300, h = 20;

    /* Create the window, set its size, position, and colors. */    
    .window = new GWindow();
    .window.Create (0, rect, "Data Entry", WS_OVERLAPPEDWINDOW, 0);
    .window.MoveWindow(20, 200, 350, 285);
    .window.SetBackground(0, GetSysColor(COLOR_3DFACE), 0);
    .window.SetChildBackground(0, GetSysColor(COLOR_3DFACE), 0);
    
    /* Allow for the possibility that DataSim isn't running
     * or that UpdateFrequency isn't defined. */
    $DataSim:UpdateFrequency := 5;
    datahub_command("(cset DataSim:UpdateFrequency 5)");
 
    /* Create labels. */
    txt = .window.CreateControl (GStatic, x, 12, w, h, 
                                 "           Widgets for data entry");
    .format_label (txt, 18, 9, "Arial Bold", 0xee0000);
    txt = .window.CreateControl (GStatic, x, 43, w, h, 
                                 "GEdit widget");
    .format_label (txt, 14, 6, "Arial Bold", 0xee0000);
    txt = .window.CreateControl (GStatic, x, 85, w, h, 
                                 "Enter an update frequency between 1 and 20.");
    txt = .window.CreateControl (GStatic, x, 111, w, h, 
                                 "GGroupBox and GRadioButton widgets");
    .format_label (txt, 14, 6, "Arial Bold", 0xee0000);
    txt = .window.CreateControl (GStatic, x, 188, w, h, 
                                 "GTrackBarCtrl widget");
    .format_label (txt, 14, 6, "Arial Bold", 0xee0000);

    /* Create a data entry field for Update Frequency. */
    txt = .window.CreateControl (GEdit, x, 63, 60, 20, 
                                 string($DataSim:UpdateFrequency), WS_BORDER);
    txt.SetBackground (0, GetSysColor (COLOR_WINDOW), 0);
    .window.onChange (#$DataSim:UpdateFrequency,
                   `(@txt).SetWindowText (string(value)));
    
    btn = .window.CreateControl (GButton, x + 60 + 10, 63, 60, 20, "Submit");
    .window.CommandHandler(BN_CLICKED, btn.GetDlgCtrlID(),
                           `(@self).SubmitEval(@txt));
    
    /* Create a control group and two radio buttons for Update Frequency. */    
    b0 = .window.CreateControl (GGroupBox, x, 130, w, 40,
                                "Change Update Frequency");
    b = b0.CreateControl (GRadioButton, 10, 10, 140, 25,
                          "UpdateFrequency = 1");
    b2 = b0.CreateControl (GRadioButton, 150, 10, 140, 25,
                           "UpdateFrequency = 10");    
    b0.CommandHandler (BN_CLICKED, b.GetDlgCtrlID(),
                       `((@b0).IsDlgButtonChecked((@b).GetDlgCtrlID()) == 0 ? nil :
                         $DataSim:UpdateFrequency = 1));
    b0.CommandHandler (BN_CLICKED, b2.GetDlgCtrlID(),
                       `((@b0).IsDlgButtonChecked((@b2).GetDlgCtrlID()) == 0 ? nil :
                          $DataSim:UpdateFrequency = 10));
        .window.onChange (#$DataSim:UpdateFrequency,
                          `(@b).SetCheck (value == 1 ? 1 : 0));
        .window.onChange (#$DataSim:UpdateFrequency,
                          `(@b2).SetCheck (value == 10 ? 1 : 0));
    
    /* Create a track bar control for Update Frequency. */
    tb = .window.CreateControl (GTrackBarCtrl, x, 205, w, h, "");
    tb.SetRange (1, 20, 1);
    tb.ModifyStyle (0, TBS_AUTOTICKS, 0);
    tb.SetTicFreq (2);
    .window.MessageHandler (WM_HSCROLL, `(@self).Scrolled(@tb));
    .window.onChange (#$DataSim:UpdateFrequency,
                      `(@tb).SetPos (value));
    if (!undefined_p($DataSim:UpdateFrequency))
        tb.SetPos ($DataSim:UpdateFrequency);

    .window.MessageHandler (WM_CLOSE, `destroy(@self));
    .window.ShowWindow (SW_SHOW);
}

method MyDataEntry.destructor ()
{
    if (instance_p(.window))
        destroy(.window);
}

ApplicationSingleton (MyDataEntry);