14.18. Photon: the Deadband Type Window - create_dbtype_win, db_options

The Deadband Type window displays text entry widgets that let the user modify the deadband types on either of the two histories (MV_001, or PV_001). The OK button applies entered values to the appropriate deadband and closes the window. The entered values are maintained and displayed the next time the window is opened. The Cancel button cancels all changes made since the window was opened, and closes the window. Each time a new deadband type is applied the system prints the return value from the Cascade Historian in STDOUT.

The create_dbtype_win function uses the ! argument syntax to prevent evaluation of its first argument (point_list). This is because the function requires a list of DeadbandSettings instances. When you pass a list that is not protected from evaluation by the ! operator, it gets evaluated by the Lisp engine, which expects every list to begin with either a symbol, a class, or a function. But an instance of the DeadbandSettings class is none of these, so passing an unprotected list will generate error messages.

   /*--------------------------------------------------------------------
    * Function:    create_dbtype_win
    * Returns:     a Deadband Type window, or a destroyed instance
    * Description: Creates the Deadband Type window.
    *------------------------------------------------------------------*/
   function create_dbtype_win (!point_list, button)
   {
     local db1, db2, pa1, pa2;
     if ((button.flags & Pt_SET) != 0)
       {
         if (undefined_p(db) || db.window == nil)
           {
             db = new (DeadbandType);
             db.DeadBandWindow.SetPos(150, 300);
             attach_msg(db.DeadBandWindow, "6.54", "6");
             
             db1 = car(point_list);
             db2 = cadr(point_list);
             

At this point we need to address a problem of maintaining a copy of the existing entries to support the Cancel and OK buttons' functionality. The solution is to store entered data in an array, and assign it (using the .set_parms method) only when the OK button is pressed. Otherwise, the values don't change. This array is one of the arguments needed by the db_options function to create the entry widgets.

             pa1 = array(db1.absolute, db1.percent, db1.timelimit, db1.countlimit);
             pa2 = array(db2.absolute, db2.percent, db2.timelimit, db2.countlimit);
             
             db_options(pa1, 0, 0, 0);
             db_options(pa1, 1, 0, 1);
             db_options(pa1, 2, 0, 2);
             db_options(pa1, 3, 0, 3);
             
             db_options(pa2, 0, 1, 0);
             db_options(pa2, 1, 1, 1);
             db_options(pa2, 2, 1, 2);
             db_options(pa2, 3, 1, 3);
             
             attach_msg(db.CancelBut, "6.55", "6.5");
             attach_msg(db.OKBut, "6.56", "6.5");
             
             PtAttachCallback(db.CancelBut, Pt_CB_ACTIVATE,
                              `((@button).flags = cons(Pt_SET, nil)));
             PtAttachCallback(db.CancelBut, Pt_CB_ACTIVATE,
                              `PtDestroyWidget((@db).window));                        
             
             PtAttachCallback(db.OKBut, Pt_CB_ACTIVATE,
                              `(@db1).set_parms((@db).OKBut, @pa1));
             PtAttachCallback(db.OKBut, Pt_CB_ACTIVATE,
                              `(@db2).set_parms((@db).OKBut, @pa2));
             PtAttachCallback(db.OKBut, Pt_CB_ACTIVATE,
                              `((@button).flags = cons(Pt_SET, nil)));
             PtAttachCallback(db.OKBut, Pt_CB_ACTIVATE,
                              `PtDestroyWidget((@db).window));
             
             PtAttachCallback(db.window, Pt_CB_DESTROYED,
                              `((@button).flags = cons(Pt_SET, nil)));
             PtAttachCallback(db.window, Pt_CB_DESTROYED,
                              `((@db).window = nil));
           }
         if(db)
           PtRealizeWidget (db.window);
         db;
       }        
     else
       PtDestroyWidget (db.window);
   }
   

The db_options function adds the PtText widgets.

   /*--------------------------------------------------------------------
    * Function:    db_options
    * Returns:     a PtText widget
    * Description: Creates a PtText widget, gives it a position, and gives
    *              it a callback to update the corresponding value in the
    *              parameter array.
    *------------------------------------------------------------------*/
   function db_options (p_array, index, col, row)
   {
     local option;
     local cols = [86, 148];
     local rows = [55, 86, 127, 157];
     
     option = new(PtText);
     option.SetDim(40, 20);
     option.fill_color = 0xffffee;
     option.border_width = 1;
     option.text_string = (string(p_array[index]));
     option.SetPos(cols[col], rows[row]);
     PtAttachCallback(option, Pt_CB_TEXT_CHANGED,
                      `((@p_array)[@index] = number((@option).text_string)));
     option;
   }