Compile function NDW_INTERNAL_ETCH_MANAGER_EVENTS(CtrlEntId, Event, @PARAMS)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from Infineon.

    Name        :   NDW_INTERNAL_ETCH_MANAGER_Events

    Description :   This function acts as a commuter module for all events related to this window.

    Notes       :   Commuter Modules are automatically called from the Promoted_Events function which is called by the
                    application-specific promoted event handler. This makes it possible to add QuickEvents that need to
                    execute Basic+ logic without having use the Form Designer to make the association, although this is
                    limited to the events which are currently promoted.

                    If the form needs to call the commuter module directly then the QuickEvent parameters should be
                    formatted like this:

                        '@SELF','@EVENT',['@PARAM1','@PARAMx']

    Parameters  :
        CtrlEntId   [in] -- The fully qualified name of the control calling the promoted event
        Event       [in] -- The event being executed. See the Notes section regarding "PRE" events
        Param1-15   [in] -- Additional event parameter holders
        EventFlow  [out] -- Set to 1 or 0 so the calling event knows whether or not to chain forward. See comments in
                            EVENT_SETUP insert
        Response   [out] -- COMMIT  mode:
                                Response<1> = List of internal GaN Etch IDs selected                          
                            DEFAULT mode:
                                Response<1> EQ False$(0)                  

    History     :   (Date, Initials, Notes)
        04/01/19    djs     Created initial commuter module.
        07/24/19    djs     Updated form and commuter module to support a 'COMMIT' mode where users can select one or
                            more internal GaN Etch runs to be bound to one external GaN Etch run upon the ETCH complete
                            event on the REACT_RUN form.                                 

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_INTERNAL_ETCH_MANAGER

$Insert RLIST_EQUATES
$Insert APP_INSERTS

Equ COL$SELECT to 6

Declare subroutine Database_Services, GaN_Services

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
    Transfer Event to OIEvent
    Transfer Param1 to Event
    Transfer Param2 to Param1
    Transfer Param3 to Param2
    Transfer Param4 to Param3
*    Transfer Param5 to Param4
*    Transfer Param6 to Param5
*    Transfer Param7 to Param6
*    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)

    FormMode       = ''
    ExternalEtchID = ''
    Reactor        = ''
    FormMode = Field(CreateParam, @FM, 1, 1)
    If FormMode _EQC 'COMMIT' then
        ExternalEtchID = Field(CreateParam, @FM, 2, 1)
        Reactor        = Field(CreateParam, @FM, 3, 1)
    end
    Set_Property(@Window, '@FORMMODE', FormMode)
    GoSub Setup_OLE_Controls
    GoSub EnableButtons
    FormSize = ''
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
    
    EventFlow = 1
     
End Event


Event WINDOW.CLOSE(CancelFlag)
    
    End_Dialog(@Window, False$)
    
end event


Event WINDOW.OMNIEVENT(Message, Param1, Param2, Param3, Param4)
    
    If Message _EQC 'Refresh' then
        GoSub RefreshQueue
    end
    
end event


Event PUB_CREATE.CLICK()
    
    ReactorNo = Get_Property(@Window:'.CMB_REACTOR', 'TEXT')
    If ReactorNo NE '' then
        Gan_Services('CreateEtchRun', ReactorNo)
        GoSub RefreshQueue
        GoSub EnableButtons
    end
    
end event


Event CMB_REACTOR.CHANGED(NewData)
    
    If NewData NE '' then
        Set_Property(@Window:'.PUB_CREATE', 'ENABLED', True$)
    end else
        Set_Property(@Window:'.PUB_CREATE', 'ENABLED', False$)
    end
    
end event


Event PUB_EXIT.CLICK()
    
    End_Dialog(@Window, False$)
    
end event


Event OLE_EDT_ETCH_Q.OnContextMenuClick(Item, UserData)
    
    Action   = Item
    Cell     = UserData
    RowIndex = Field(Cell, ';', 2)
    EtchIDs  = Get_Property(@Window, '@ETCHIDS')
    EtchID   = EtchIDs<RowIndex>
    If EtchID NE '' then
        Begin Case
            Case Action EQ 'START'                
                Gan_Services('StartEtchRun', EtchID)
            Case Action EQ 'STOP'
                Gan_Services('StopEtchRun', EtchID)
            Case Action EQ 'DEL'
                Gan_Services('DeleteEtchRun', EtchID)
        End Case
        GoSub RefreshQueue
        GoSub EnableButtons
    end

end event


Event OLE_EDT_ETCH_Q.OnClick(Cell, Point, Button, Shift, Ctrl)
    
    GoSub EnableButtons
    If Button EQ "Right" then
        Row             = Field(Cell, ';', 2)
        Reactor         = Get_Property(CtrlEntID, "OLE.CellText[1;":Row:"]")
        StartDTM        = Get_Property(CtrlEntID, "OLE.CellText[2;":Row:"]")
        EndDTM          = Get_Property(CtrlEntID, "OLE.CellText[3;":Row:"]")
        Menu            = ""
        // Initialize all menu options to false until we deem they need to be turned on
        StartStatus     = False$
        StopStatus      = False$
        DelStatus       = True$
        
        Begin Case
            Case StartDTM EQ ''
                // Need to ensure that no other etch has been started for this reactor
                EtchIDs   = ''
                EtchQList = ''
                RowIndex  = 1
                GoSub ClearCursors
                Query  = 'SELECT GAN_ETCH WITH AVAILABLE EQ ':True$:' AND WITH REACTOR EQ ' |
                       : Reactor : ' AND WITH STATUS NE "COMPLETED" AND WITH STATUS NE "READY"' 
                Rlist(Query, Target_ActiveList$, '', '', '')
                EOF = False$
                If @RecCount EQ 0 then StartStatus = True$
            Case StartDTM NE '' and EndDTM EQ ''
                StopStatus = True$
            Case Otherwise$
                Null
        End Case
        Menu<-1> = "START"   :@VM:"Start"                :@VM:StartStatus
        Menu<-1> = "STOP"    :@VM:"Stop"                 :@VM:StopStatus
        Menu<-1> = "DEL"     :@VM:"Delete"               :@VM:DelStatus
        
        Send_Message(CtrlEntID, "OLE.ShowContextMenu", Point, Menu, Cell)
    end
    
end event


Event PUB_START.CLICK()
    
    EtchQCtrl = @Window:'.OLE_EDT_ETCH_Q'
    Cell      = Get_Property(EtchQCtrl, "OLE.SelPos")
    Row       = Cell<2>
    EtchIDs   = Get_Property(@Window, '@ETCHIDS')
    EtchID    = EtchIDs<Row>
    Gan_Services('StartEtchRun', EtchID)
    GoSub RefreshQueue
    GoSub EnableButtons
    
end event


Event PUB_STOP.CLICK()
    
    EtchQCtrl = @Window:'.OLE_EDT_ETCH_Q'
    Cell      = Get_Property(EtchQCtrl, "OLE.SelPos")
    Row       = Cell<2>
    EtchIDs   = Get_Property(@Window, '@ETCHIDS')
    EtchID    = EtchIDs<Row>
    Gan_Services('StopEtchRun', EtchID)
    GoSub RefreshQueue
    GoSub EnableButtons
    
end event


Event PUB_DELETE.CLICK()
    
    EtchQCtrl = @Window:'.OLE_EDT_ETCH_Q'
    Cell      = Get_Property(EtchQCtrl, "OLE.SelPos")
    Row       = Cell<2>
    EtchIDs   = Get_Property(@Window, '@ETCHIDS')
    EtchID    = EtchIDs<Row>
    Gan_Services('DeleteEtchRun', EtchID)
    GoSub RefreshQueue
    GoSub EnableButtons
    
end event

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Setup_OLE_Controls:
    
    // Qualify OLE events that we want to intercept
    Qualifier    = ''
    Qualifier<1> = 1
    Qualifier<4> = 1 ; * process synchronously (i.e. immediately)
    EtchQCtrl = @Window:'.OLE_EDT_ETCH_Q'
    Send_Message(EtchQCtrl, 'QUALIFY_EVENT', 'OLE.PosChanged', Qualifier)
    Send_Message(EtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnContextMenuClick', Qualifier)
    Send_Message(EtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnClick', Qualifier)
    Set_Property(EtchQCtrl, "OLE.CellProtection[All; All]", 'SEL')
    Set_Property(EtchQCtrl, "OLE.ResetSelPos", False$)

    GoSub RefreshQueue
    
    HeaderFontArray  = 'Segoe UI':@SVM:8:@SVM:700    
    HeaderColArray = 40:@FM:False$:@FM:False$:@FM:False$    
    Set_Property(EtchQCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)    
    Set_Property(EtchQCtrl, "OLE.CellFont[1;All]", HeaderFontArray)
    Set_Property(EtchQCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(EtchQCtrl, "OLE.HeaderFont[1; All]", HeaderFontArray)
    Set_Property(EtchQCtrl, "OLE.HeaderColumn[1]", HeaderColArray)
    EtchHeaderTitles = "Reactor":@VM:"Start":@VM:"End":@VM:"Hours":@VM:"User"
    Set_Property(EtchQCtrl, "OLE.TitleList", EtchHeaderTitles)
    Set_Property(EtchQCtrl, "OLE.HeaderAlignment[All; 1]", 'C':@FM:'C':@FM:'C')
    Set_Property(EtchQCtrl, "OLE.CellAlignment[All; All]", 'C':@FM:'C':@FM:'C')
    Set_Property(EtchQCtrl, "OLE.AllowDeletions", False$)
    Set_Property(EtchQCtrl, "OLE.AllowInserts", False$)
    
    // Disable resizing of the header row and header column
    // Resize header column to fit contents
    HeaderRowArray = Get_Property(EtchQCtrl, "OLE.HeaderRow[1]")
    HeaderRowArray<3> = False$
    Set_Property(EtchQCtrl, "OLE.HeaderRow[1]", HeaderRowArray)
    // Disable resizing of columns as there is no need for this on this form
    StageColSize = Get_Property(EtchQCtrl, "OLE.DataColumn[1]")
    StageColSize<3> = False$
    Set_Property(EtchQCtrl, "OLE.DataColumn[All]", StageColSize)   
    StageColSize<1> = 50
    Set_Property(EtchQCtrl, "OLE.DataColumn[1]", StageColSize)
    StageColSize<1> = 120
    Set_Property(EtchQCtrl, "OLE.DataColumn[2-3]", StageColSize)
    StageColSize<1> = 50
    Set_Property(EtchQCtrl, "OLE.DataColumn[4]", StageColSize)
    StageColSize<1> = 140
    Set_Property(EtchQCtrl, "OLE.DataColumn[5]", StageColSize)
    // Disable resizing of rows as there is no need for this on this form
    RowSizeProps = Get_Property(EtchQCtrl, "OLE.DataRow[1]")
    RowSizeProps<3> = False$
    Set_Property(EtchQCtrl, "OLE.DataRow[All]", RowSizeProps)
    Set_Property(EtchQCtrl, "OLE.ScrollBarsVisible", 'Always':@FM:'Never')
    
    SelPos = '-1':@FM:'-1'
    Set_Property(EtchQCtrl, "OLE.SelPos", SelPos)
    
return


ClearCursors:
    
    For counter = 0 to 8
        ClearSelect counter
    Next counter
    
return


RefreshQueue:
    
    EtchIDs   = ''
    EtchQList = ''
    RowIndex  = 1
    GoSub ClearCursors
    Query  = 'SELECT GAN_ETCH WITH AVAILABLE EQ ':True$:' BY REACTOR BY START_DTM'
    Rlist(Query, Target_ActiveList$, '', '', '')
    EOF = False$
    If @RecCount then
        Loop
        ReadNext GaNEtchID then            
            Database_Services('ActivateRecord', 'GAN_ETCH', GaNEtchID)                      
            EtchQList<RowIndex, 1>  = {REACTOR}
            EtchQList<RowIndex, 2>  = OConv({START_DTM}, 'DT')
            EtchQList<RowIndex, 3>  = OConv({END_DTM}, 'DT')
            EtchQList<RowIndex, 5>  = OConv({USER}, '[XLATE_CONV,LSL_USERS*FIRST_LAST]')
            EtchQList<RowIndex, 4>  = {ELAP_HOURS}
            EtchIDs<RowIndex>       = GaNEtchID   
            RowIndex               += 1           
        end else
            EOF = True$
        end
        Until EOF EQ True$
        Repeat
    end                 
    NumEtchCols = 5
    NumEtchRows = @RecCount
    EtchDimArray  = NumEtchCols :@FM: NumEtchRows
    Set_Property(EtchQCtrl, "OLE.Dimension", EtchDimArray)
    Set_Property(@Window, '@ETCHIDS', EtchIDs)
    Set_Property(@Window:'.OLE_EDT_ETCH_Q', 'OLE.LIST', EtchQList)
          
return


EnableButtons:
    
    StartStatus = False$
    StopStatus  = False$
    DelStatus   = False$
    EtchQCtrl   = @Window:'.OLE_EDT_ETCH_Q'
    Cell        = Get_Property(EtchQCtrl, "OLE.SelPos")
    Convert @FM to ';' in Cell
    Row         = Field(Cell, ';', 2)
    If Row NE '-1' then
        
        EtchIDs     = Get_Property(@Window, '@ETCHIDS')
        EtchID      = EtchIDs<Row>
        If EtchID NE '' then DelStatus = True$
        Reactor     = Get_Property(EtchQCtrl, "OLE.CellText[1;":Row:"]")
        StartDTM    = Get_Property(EtchQCtrl, "OLE.CellText[2;":Row:"]")
        EndDTM      = Get_Property(EtchQCtrl, "OLE.CellText[3;":Row:"]")

        Begin Case
            Case StartDTM EQ ''
                // Need to ensure that no other etch has been started for this reactor
                EtchIDs   = ''
                EtchQList = ''
                RowIndex  = 1
                GoSub ClearCursors
                Query  = 'SELECT GAN_ETCH WITH AVAILABLE EQ ':True$:' AND WITH REACTOR EQ ' |
                       : Reactor : ' AND WITH STATUS NE "COMPLETED" AND WITH STATUS NE "READY"' 
                Rlist(Query, Target_ActiveList$, '', '', '')
                EOF = False$
                If ( (@RecCount EQ 0) and (EtchID NE '') ) then StartStatus = True$
            Case StartDTM NE '' and EndDTM EQ ''
                StopStatus = True$
            Case Otherwise$
                Null
        End Case
    end
    
    Set_Property(@Window:'.PUB_START',  'ENABLED', StartStatus)
    Set_Property(@Window:'.PUB_STOP',   'ENABLED', StopStatus)
    Set_Property(@Window:'.PUB_DELETE', 'ENABLED', DelStatus)
        
return

