14.10. Photon: the History Window - HistoryWindow class, .constructor, create_history

This way of creating a window in Photon is different from how we created the Monitor and Log windows. Here we create a HistoryWindow class, and then use its instance variables to hold the window widgets and other necessary data. The advantage of doing this is that the class and all its widgets are available throughout the program as a global variables.

   /*--------------------------------------------------------------------
    * Class:       HistoryWindow
    * Description: Creates the History window using PhabAttachWidgets.
    *------------------------------------------------------------------*/
   class HistoryWindow
   {
     window;
     int_set;
   }
   
   PhabAttachWidgets (HistoryWindow, anyos_assign("history_widgetfile"));
   

The HistoryWindow class is associated with the Phab widget file, using the Gamma PhabAttachWidgets function. (We use the anyos_assign function to get the correct file name.) This gives us full access to all the widgets in the widget file, along with their resources, as instance variables. The int_set instance variable gives us the same kind of access to an instance of the InterpolatorSettings class and its resources.

The window gets instantiated by the .constructor method, which applies the Gamma PhabRoot function to the .PhabInstantiate method as explained in the PhabAttachWidgets reference of the Gamma/Photon manual.

   /*--------------------------------------------------------------------
    * Method:      HistoryWindow.constructor
    * Returns:     the root widget of a widget hierarchy
    * Description: Instantiates a HistoryWindow.
    *------------------------------------------------------------------*/
   method HistoryWindow.constructor ()
   {
     .window = PhabRoot(.PhabInstantiate (t));
   }
   

The Photon widgets and Gamma functions used here have been discussed in previous sections, but the window and the widgets are accessed in Gamma as explained above.

   /*--------------------------------------------------------------------
    * Function:    create_history
    * Returns:     doesn't return
    * Description: Creates the History window.
    *------------------------------------------------------------------*/
   function create_history ()
   {
     local buttons, version_string;
     local        hw = new (HistoryWindow);
     
     hw.GraphPane.anchor_flags = Pt_LEFT_ANCHORED_LEFT |
       Pt_RIGHT_ANCHORED_RIGHT | Pt_BOTTOM_ANCHORED_BOTTOM |
         Pt_TOP_ANCHORED_TOP;  

Assigning values to the widgets from the InterpolatorSettings instance becomes trivial.

     hw.int_set = new(InterpolatorSettings);
     hw.change_int(hw.NoIntBut, "NoInterpolator");
     hw.SecsEnt.numeric_value = RECORD_TIME;
     hw.YHistEnt.text_string = hw.int_set.y_history;
     hw.StartEnt.text_string = hw.int_set.start;
     hw.DurEnt.text_string = hw.int_set.duration;
     hw.XHistEnt.text_string = hw.int_set.x_history;
     hw.IntEnt.text_string = hw.int_set.interval;
     hw.GapEnt.text_string = hw.int_set.max_gap;
     hw.YHistEnt.items = array ("MV_001", "PV_001");
     hw.XHistEnt.items = array ("PV_001", "MV_001");
   
     /*Attach message callbacks to buttons and entry widgets.*/
     attach_msg(hw.PtHist, "6", "1");
     attach_msg(hw.RecBut, "6.2", "6");
     attach_msg(hw.DisplayBut, "6.3", "6");
     attach_msg(hw.NoIntBut, "6.31", "6");
     attach_msg(hw.PeriodBut, "6.32", "6");
     attach_msg(hw.RelBut, "6.33", "6");
     attach_msg(hw.FixRelBut, "6.34", "6");
     attach_msg(hw.YHistEnt, "6.35", "6");
     attach_msg(hw.StartEnt, "6.36", "6");
     attach_msg(hw.DurEnt, "6.37", "6");
     attach_msg(hw.XHistEnt, "6.35", "6");
     attach_msg(hw.IntEnt, "6.38", "6");
     attach_msg(hw.GapEnt, "6.39", "6");
     attach_msg(hw.SetupBut, "6.5", "6");
     attach_msg(hw.DBNoneBut, "6.51", "6");
     attach_msg(hw.DBBut, "6.52", "6");
     attach_msg(hw.DBPBut, "6.53", "6");
   
     /* Display version info and start-up message.*/
     display_hs_info(hw.Text);
   
     /* Callback for the PtComboBox entry, number of seconds to record.*/
     PtAttachCallback(hw.SecsEnt, Pt_CB_NUMERIC_CHANGED,
                      `(RECORD_TIME = (@hw).SecsEnt.numeric_value));
     
     /* Callbacks for non-deadband buttons to send a query.*/
     PtAttachCallback(hw.NoIntBut, Pt_CB_ACTIVATE,
                      `query_refresh(@hw.NoIntBut, "NoInterpolator"));
     PtAttachCallback(hw.PeriodBut, Pt_CB_ACTIVATE,
                      `query_refresh(@hw.PeriodBut, "Periodic"));
     PtAttachCallback(hw.RelBut, Pt_CB_ACTIVATE,
                      `query_refresh(@hw.RelBut, "Relative"));
     PtAttachCallback(hw.FixRelBut, Pt_CB_ACTIVATE,
                      `query_refresh(@hw.FixRelBut, "FixedRelative"));
     PtAttachCallback(hw.DisplayBut, Pt_CB_ACTIVATE,
                      `query_refresh(@hw.DisplayBut, nil));

The query_refresh function sets up and sends a new query. The record_data function (used below) starts a new recording session. The db_prep_query function removes traces from the graph when a Deadband options button is untoggled.

   
     /* A callback to record data, which includes sending a query. */
     PtAttachCallback(hw.RecBut, Pt_CB_ACTIVATE,
                      `(record_data((@hw).RecBut, (@hw).DisplayBut,
                                    (@hw).int_set, (@hw).Text,
                                    (@hw).PtHist, (@hw).YHistEnt,
                                    (@hw).StartEnt, (@hw).DurEnt,
                                    (@hw).XHistEnt, (@hw).IntEnt,
                                    (@hw).GapEnt)));
     
     /* Callbacks to set the deadband flag.*/
     PtAttachCallback(hw.DBNoneBut, Pt_CB_ACTIVATE, `((@hw).int_set.dbflag = "NONE"));
     PtAttachCallback(hw.DBBut, Pt_CB_ACTIVATE, `((@hw).int_set.dbflag = "DB"));
     PtAttachCallback(hw.DBPBut, Pt_CB_ACTIVATE, `((@hw).int_set.dbflag = "DBP"));
     
     /* A callback for deadband buttons to prepare for and send a query.*/
     buttons = list(hw.DBNoneBut, hw.DBBut, hw.DBPBut);
     with b in buttons do
       {
         PtAttachCallback(b, Pt_CB_ACTIVATE, `db_prep_query(@b));
       }
     
     /* Deadband setup.*/
     dbmv_set = new(DeadbandSettings);
     dbmv_set.history = "MV_001";
     
     dbpv_set = new(DeadbandSettings);
     dbpv_set.history = "PV_001";
     
     db_list = list(dbmv_set, dbpv_set);
     
     PtAttachCallback(hw.SetupBut, Pt_CB_ACTIVATE,
                      `create_dbtype_win(@db_list, (@hw).SetupBut));
     
     PtAttachCallback(hw.ExitBut, Pt_CB_ACTIVATE, #exit_program(-1));
     
     hw.PtHist.SetPos(320, 250);
     PtRealizeWidget (hw.window);
     send_message("nsnames");
     hw;
     
     /* Loop forever handling events. */
     while(t) next_event(); 
     
   }