Compile function RDS_LAYER_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        :   RDS_LAYER_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

    History     :   (Date, Initials, Notes)
        10/04/18    djs     Created initial commuter module.
        10/12/18    djs     Added a check to see if PSN and Reactor are set before attempting to set current run
                            parameters. If a Reactor has not yet been assigned to this RDS, then the code fetches
                            the scheduled reactor for this work order.

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window RDS_LAYER

$Insert EVENT_SETUP
$Insert RDS_EQUATES
$Insert APP_INSERTS
$Insert RLIST_EQUATES
$Insert SECURITY_RIGHTS_EQU
$Insert DICT_EQUATES

EQU COL$MET_NO		TO 1
EQU COL$ZONE		TO 2
EQU COL$THICK_AVG	TO 3
EQU COL$THICK_STDV	TO 4
EQU COL$RES_AVG		TO 5
EQU COL$RES_STDV	TO 6
EQU COL$RES_UNITS	TO 7
EQU COL$OUT_OF_SPEC	TO 8

EQU HIDDEN$	TO 32

Declare subroutine Comm_RDS_Layer, Tool_Parms_Services, Set_Status, RList, Errmsg, Database_Services, Msg
Declare subroutine PlaceDialog, obj_RDS_Layer, Security_Err_Msg
Declare function   Tool_Parms_Services, Get_Property, Database_Services, Security_Check, MemberOf, Get_EventStatus

GoToEvent Event for CtrlEntId else
    // Event not implemented
end

Return EventFlow or 1

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


Event WINDOW.CREATE(CreateParam)
    
    RDSLayerID = CreateParam
    RDSNo      = Field(RDSLayerID, '*', 1, 1)
    LayerID    = Field(RDSLayerID, '*', 2, 1)
    Set_Property(@Window:'.RDS_NO', 'TEXT', RDSNo)
    Set_Property(@Window:'.LS_ID', 'TEXT', LayerID)
    // Center window in middle of user's screen
    PlaceDialog( -2, -2 )
    Send_Event(@Window, 'READ')
    GoSub Refresh   
    
end event


Event WINDOW.READ()
    
    CurrRecord = Get_Property(@WINDOW,'RECORD')
    Set_Property(@WINDOW,'@ORG_RECORD',CurrRecord)

end event


Event WINDOW.WRITE()
        
    CurrRecord = Get_Property(@WINDOW,'RECORD')
    OrgRecord  = Get_Property(@WINDOW,'@ORG_RECORD')

    IF CurrRecord NE OrgRecord THEN
        CurrDTM = OCONV(Date(),'D4/'):' ':OCONV(Time(),'MTS')
        NewLine = @USER4:@VM:CurrDTM
        Send_Message(@WINDOW:'.MODIFY_USER', 'INSERT', 1, NewLine)
    END
    
    IOOptions = Get_Property(@Window, 'IOOPTIONS')
    IOOptions<6> = 1
    Set_Property(@Window, 'IOOPTIONS', IOOptions)
    Set_Status(0)
    Forward_Event()
    errCode = ''
    
    If Get_EventStatus(errCode) then
        EventFlow = EVENT_STOP$
    end else
        Post_Event(@WINDOW,'CLOSE')
    end
 
end event


Event PUB_CONFIRM_PARMS.CLICK()
    
    RDSNo  = Get_Property(@Window:'.RDS_NO', 'TEXT')
    RDSRec = Database_Services('ReadDataRow', 'RDS', RDSNo)
    RDSRec<RDS_RDS_LAYER_ACK$> = True$
    Database_Services('WriteDataRow', 'RDS', RDSNo, RDSRec, True$, False$, True$)
    Set_Property(@Window:'.PUB_CONFIRM_PARMS', 'ENABLED', False$)
    Set_Property(@Window:'.PUB_CONFIRM_PARMS', 'HELPTEXT', 'Confirmation not required.')
    Post_Event(@Window, 'CLOSE')
    
end event


Event PUB_UPDATE_LIMITS.CLICK()

    IF Security_Check( 'Recipe', Write$ ) or MemberOf(@User4, 'ENGINEERING_LEAD') or MemberOf(@User4, 'ENGINEERING') THEN

        RDSNo = Get_Property(@WINDOW:'.RDS_NO','DEFPROP')
        LSId  = Get_Property(@WINDOW:'.LS_ID','DEFPROP')
        
        IF RDSNo EQ '' OR LSId EQ '' THEN RETURN
        IOOptions = Get_Property(@Window, 'IOOPTIONS')
        IOOptions<6> = 1
        Set_Property(@Window, 'IOOPTIONS', IOOptions)
            
        Send_Event(@WINDOW,'WRITE')       
        obj_RDS_Layer('UpdateLimits',RDSNo:@RM:LSId)       
        IF Get_Status(errCode) THEN ErrMsg(errCode)       
        Send_Event(@Window, 'READ')
        
    END ELSE   
        Security_Err_Msg( 'Recipe', Write$ )        
    END
    
end event


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


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


Refresh:
    
    LSId = Get_Property(@WINDOW:'.LS_ID','TEXT')

    BEGIN CASE
        CASE LSId = 'L1'
            Set_Property(@WINDOW:'.LS_ID','BACKCOLOR',RCV_BLUE$)
            
        CASE LSId = 'L2'
            Set_Property(@WINDOW:'.LS_ID','BACKCOLOR',PRE_BLUE$)
            
        CASE LSId = '2'
            Set_Property(@WINDOW:'.LS_ID','BACKCOLOR',INP_BLUE$)
            
        CASE 1
            Set_Property(@WINDOW:'.LS_ID','BACKCOLOR',KEY_YELLOW$)
            
    END CASE

    Set_Property(@WINDOW:'.OVERRIDE_LIMITS_GROUP','ENABLED',0)

    RDSNo	= Get_Property( @WINDOW:'.RDS_NO', 'DEFPROP' )
    RDSRec	= XLATE('RDS',RDSNo,'','X')

    LayerSetID	= Get_Property(@WINDOW:'.LS_ID','DEFPROP')

    PSNId		= RDSRec< RDS_PROD_SPEC_ID$ >
    RecipeNo	= Get_Property(@WINDOW:'.RECIPE_NO','DEFPROP')

    RecipeParms = Get_Property(@WINDOW:'.RECIPE_PARMS','ARRAY')						;* Entered by user
    RecipeLimits = Get_Property(@WINDOW:'.RECIPE_LIMITS','LIST')<1>

    CONVERT @VM TO @FM IN RecipeLimits

    ColCnt = COUNT(RecipeLimits,@FM) + (RecipeLimits NE '')							;* This is 16 columns as of the development of this window

    RecipeParms = Get_Property(@WINDOW:'.RECIPE_PARMS','ARRAY')

    ParameterStyles	= Send_Message( @WINDOW:'.RECIPE_PARMS', "COLSTYLE", 0, '' )
    LimitStyles		= Send_Message( @WINDOW:'.RECIPE_LIMITS', "COLSTYLE", 0, '' )

    CONVERT @VM TO '' IN RecipeParms

    ColNames	= Get_Property( @WINDOW:'.RECIPE_PARMS', 'COLUMN' )

    OutOfSpecCols = ''
    FOR Col = 1 TO ColCnt
        IF RecipeLimits<Col> = '' AND RecipeParms<Col> = '' THEN
            ParameterStyles<Col>	= BitOr( ParameterStyles<Col>, HIDDEN$ )				;* Column not used -> Hide
            LimitStyles<Col>		= BitOr( LimitStyles<Col>, HIDDEN$ )
        END ELSE
            ParameterStyles<Col>	= BitAnd( ParameterStyles<Col>, BitNot(HIDDEN$) )		;* Column used -> Show
            LimitStyles<Col>		= BitAnd( LimitStyles<Col>, BitNot(HIDDEN$) )
            
            LimitData	= RecipeLimits<Col>
            ParamData	= RecipeParms<Col>
            
            ColName		= ColNames<1,1,Col>
            Conversion	= XLATE('DICT.RDS_LAYER',ColName,DICT_CONV$,'X')
            
            SWAP ' ~ ' WITH @VM IN LimitData
            
            IF Conversion NE '' THEN
                ParamData = ICONV(ParamData,Conversion)
                LimitData = ICONV(LimitData,Conversion)
            END
            
            Minimum = LimitData<1,1>
            Maximum = LimitData<1,2>

            IF ParamData NE '' THEN
                IF ParamData < Minimum OR ParamData > Maximum THEN
                    Send_Message(@WINDOW:'.RECIPE_PARMS','COLOR_BY_POS',Col,1,RED$)
                    OutOfSpecCols = INSERT(OutOfSpecCols,Col,0,0,1)
                END ELSE
                    Send_Message(@WINDOW:'.RECIPE_PARMS','COLOR_BY_POS',Col,1,WHITE$)
                END
            END ELSE
                Send_Message(@WINDOW:'.RECIPE_PARMS','COLOR_BY_POS',Col,1,WHITE$)
            END
            
        END

    NEXT Col

    LOCATE '1' IN OutOfSpecCols USING @FM SETTING Pos THEN
        Set_Property(@WINDOW:'.OUT_OF_SPEC','CHECK',1)
    END ELSE
        Set_Property(@WINDOW:'.OUT_OF_SPEC','CHECK',0)
    END

    Parameter_Styles	= Send_Message( @WINDOW:'.RECIPE_PARMS', "COLSTYLE", 0, ParameterStyles  )
    LimitStyles			= Send_Message( @WINDOW:'.RECIPE_LIMITS', "COLSTYLE", 0, LimitStyles  )	   

    * QBF buttons

    Ctrls  = @WINDOW:'.QBF_FIRST_FIX':@RM		; Props  = 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_PREV_FIX':@RM		; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_ABS_FIX':@RM			; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_NEXT_FIX':@RM		; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_LAST_FIX':@RM		; Props := 'ENABLED':@RM
    Ctrls := @WINDOW:'.QBF_STOP_FIX'			; Props := 'ENABLED'

    IF Get_Property(@WINDOW,'QBFLIST') = '' THEN
        Vals = 0:@RM:0:@RM:0:@RM:0:@RM:0:@RM:0
    END ELSE
        Vals = 1:@RM:1:@RM:1:@RM:1:@RM:1:@RM:1
    END

    Set_Property(Ctrls,Props,Vals)

    * Turn edit table symbolic column backgrounds to green

    ETSymbolics = Get_Property(@WINDOW,'@ET_SYMBOLICS')		;* Loaded during 'Create' in obj_Appwindow

    ETCtrls	= ETSymbolics<1>
    ETCols	= ETSymbolics<2>

    FOR I = 1 TO COUNT(ETCtrls,@VM) + (ETCtrls NE '')
        ETCtrl	= ETCtrls<1,I>
        ETList = Get_Property(ETCtrl,'LIST')
        FOR Line = 1 TO COUNT(ETList,@FM) + (ETList NE '')
            IF ETList<Line,1> NE '' THEN
                FOR N = 1 TO COUNT(ETCols<1,I>,@SVM) + (ETCols<1,I> NE '')
                    stat = Send_Message(ETCtrl,'COLOR_BY_POS',ETCols<1,I,N>,Line,GREEN$)
                NEXT N
            END
        NEXT I
    NEXT I

    TestList = Get_Property(@WINDOW:'.RDS_TEST_KEYS','LIST')

    FOR I = 1 TO COUNT(TestList,@FM) + (TestList NE '')
        IF ICONV(TestList<I,COL$OUT_OF_SPEC>,'B') = 1 THEN
            Send_Message(@WINDOW:'.RDS_TEST_KEYS','COLOR_BY_POS',0,I,RED$)
        END ELSE
            Send_Message(@WINDOW:'.RDS_TEST_KEYS','COLOR_BY_POS',0,I,WHITE$)
        END
    NEXT I

    IsTestWafer = Xlate('RDS', RDSNo, 'ON_TW_FREQ', 'X')
    RDSLayerAck = RDSRec<RDS_RDS_LAYER_ACK$>
    If ( (IsTestWafer EQ True$) and ( (RDSLayerAck EQ False$) or (RDSLayerAck EQ '') ) ) then
        Set_Property(@Window:'.PUB_CONFIRM_PARMS', 'ENABLED', True$)
    end else
        Set_Property(@Window:'.PUB_CONFIRM_PARMS', 'ENABLED', False$)
        Set_Property(@Window:'.PUB_CONFIRM_PARMS', 'HELPTEXT', 'Confirmation not required.') 
    end

RETURN

