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. You can copy this code into a new file and run it as a script. Please refer to Copying a complete tutorial in the DataHub Scripting manual for details on how to do this.

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

class MyDataEntry Application
{
    window;
}

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

/* Formats a label for convenience. */
function 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.*/
function SubmitEval (txt)
{
    if (wParam == VK_RETURN)
    {
        local x;
        if (!undefined_p(x = eval_string (txt.GetWindowText())))
        {
            if (number_p(x) && (x >= 1) && (x <= 20))
                $DataSim: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;
    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:DataSim.UpdateFrequency := 5;
    datahub_command("(cset DataSim: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, 83, w, h, 
                                    "Enter an update frequency between 1 and 20.");
    txt = .window.CreateControl (GStatic, x, 113, 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, 60, 60, 20, 
                                    string($DataSim:DataSim.UpdateFrequency), WS_BORDER);
    txt.MessageHandler (WM_CHAR, `SubmitEval ((@txt)));
    txt.SetBackground (0, GetSysColor (COLOR_WINDOW), 0);
    .window.onChange (#$DataSim:DataSim.UpdateFrequency,
                   `(@txt).SetWindowText (string(value)));
    
    /* 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:DataSim.UpdateFrequency = 1));
    b0.CommandHandler (BN_CLICKED, b2.GetDlgCtrlID(),
        `((@b0).IsDlgButtonChecked((@b2).GetDlgCtrlID()) == 0 ? nil :
          $DataSim:DataSim.UpdateFrequency = 10));
    
    /* 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:DataSim.UpdateFrequency,
                   `(@tb).SetPos (value));
    if (!undefined_p($DataSim:DataSim.UpdateFrequency))
        tb.SetPos ($DataSim:DataSim.UpdateFrequency);

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

ApplicationSingleton (MyDataEntry);