Compile function NDW_EXTERNAL_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_EXTERNAL_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] -- Response<1> = List of internal GaN Etch IDs selected
                            Response<2> = List of external GaN Etch IDs selected                                        

    History     :   (Date, Initials, Notes)
        08/29/19    djs     Created initial commuter module.                                 

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_EXTERNAL_ETCH_MANAGER

$Insert RLIST_EQUATES
$Insert APP_INSERTS

Equ INT_COL$REACTOR  to 1
Equ INT_COL$START    to 2
Equ INT_COL$END      to 3
Equ INT_COL$HOURS    to 4
Equ INT_COL$USER     to 5
Equ INT_COL$SELECT   to 6

Equ EXT_COL$ETCH_IDS to 1
Equ EXT_COL$SELECT   to 2

Declare subroutine Database_Services, GaN_Services
Declare function   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)

    Reactor = CreateParam
    Set_Property(@Window, '@REACTOR', Reactor)
    GoSub Setup_OLE_Controls
    FormSize = ''
    SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
     
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 RefreshInternalQueue
        GoSub RefreshExternalQueue
    end
    
end event


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


Event OLE_EDT_INT_ETCH_Q.OnCheckChanged(Cell, OldValue, NewValue)
    
    GoSub EnableCommitButton
    
end event


Event OLE_EDT_EXT_ETCH_Q.OnCheckChanged(Cell, OldValue, NewValue)
    
    GoSub EnableCommitButton 
    
end event


Event PUB_COMMIT.CLICK()
    
    SelEtchIDs      = ''
    IntEtchIDList   = Get_Property(@Window, '@INTETCHIDS')
    SelIntEtchIDs   = ''
    IntEtchQArray   = Get_Property(@Window:'.OLE_EDT_INT_ETCH_Q', 'OLE.ARRAY')
    IntRowsSelected = IntEtchQArray<INT_COL$SELECT>
    For each Checkmark in IntRowsSelected using @VM setting vPos
        If Checkmark EQ True$ then
            SelIntEtchIDs<0, -1> = IntEtchIDList<vPos>
        end
    Next Checkmark
    
    SelExtEtchIDs   = ''
    CheckStatus  = Get_Property(@Window:'.CHK_EXT_ETCH', 'CHECK')
    If CheckStatus EQ True$ then
        SelExtEtchIDs = 'No Etch Required'
    end else
        ExtEtchQArray   = Get_Property(@Window:'.OLE_EDT_EXT_ETCH_Q', 'OLE.ARRAY')
        ExtEtchIDList   = ExtEtchQArray<EXT_COL$ETCH_IDS>
        ExtRowsSelected = ExtEtchQArray<EXT_COL$SELECT>
        For each Checkmark in ExtRowsSelected using @VM setting vPos
            If Checkmark EQ True$ then
                SelExtEtchIDs<0, -1> = ExtEtchIDList<0, vPos>
            end
        Next Checkmark
    end
 
    SelEtchIDs<1> = SelIntEtchIDs
    SelEtchIDs<2> = SelExtEtchIDs
    End_Dialog(@Window, SelEtchIDs)
    
end event


Event CHK_EXT_ETCH.CLICK()
    
    ExtEtchQCtrl = @Window:'.OLE_EDT_EXT_ETCH_Q'
    CheckStatus  = Get_Property(CtrlEntID, 'CHECK')
    If CheckStatus EQ True$ then
        // Disable External Etch edit table
        Set_Property(ExtEtchQCtrl, "OLE.CellProtection[All; All]", 'Full')
        Set_Property(ExtEtchQCtrl, "OLE.CellCheckEnabled[2; All]", False$)
        DiabledColorArray = @FM:'OD'
        Set_Property(ExtEtchQCtrl,"OLE.CellColors[All; All]", DiabledColorArray)
        Set_Property(ExtEtchQCtrl,"OLE.HeaderColors[All; 1]", DiabledColorArray)
    end else
        // Enable External Etch edit table
        Set_Property(ExtEtchQCtrl, "OLE.CellProtection[All; All]", 'None')
        Set_Property(ExtEtchQCtrl, "OLE.CellCheckEnabled[2; All]", True$)
        EnabledColorArray = @FM:'None'
        Set_Property(ExtEtchQCtrl,"OLE.CellColors[All; All]", EnabledColorArray)
        Set_Property(ExtEtchQCtrl,"OLE.HeaderColors[All; 1]", EnabledColorArray)
    end
    GoSub EnableCommitButton
    
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)
    
    ////////////////////////////// Setup Internal Etch Edit Table //////////////////////////////////
    
    IntEtchQCtrl = @Window:'.OLE_EDT_INT_ETCH_Q'
    Send_Message(IntEtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnClick', Qualifier)
    Send_Message(IntEtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)
    
    GoSub RefreshInternalQueue
    
    HeaderFontArray  = 'Segoe UI':@SVM:8:@SVM:700    
    HeaderColArray = 40:@FM:False$:@FM:False$:@FM:False$    
    Set_Property(IntEtchQCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)    
    Set_Property(IntEtchQCtrl, "OLE.CellFont[1;All]", HeaderFontArray)
    Set_Property(IntEtchQCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(IntEtchQCtrl, "OLE.HeaderFont[1; All]", HeaderFontArray)
    Set_Property(IntEtchQCtrl, "OLE.HeaderColumn[1]", HeaderColArray)
    EtchHeaderTitles = "Reactor":@VM:"Start":@VM:"End":@VM:"Hours":@VM:"User":@VM:"Select"
    Set_Property(IntEtchQCtrl, "OLE.TitleList", EtchHeaderTitles)
    Set_Property(IntEtchQCtrl, "OLE.HeaderAlignment[All; 1]", 'C':@FM:'C':@FM:'C')
    Set_Property(IntEtchQCtrl, "OLE.CellAlignment[All; All]", 'C':@FM:'C':@FM:'C')
    Set_Property(IntEtchQCtrl, "OLE.AllowDeletions", False$)
    Set_Property(IntEtchQCtrl, "OLE.AllowInserts", False$)
    
    CheckBoxArray      = 'Check Box'
    CheckBoxArray<2,1> = True$
    Set_Property(IntEtchQCtrl, "OLE.CellType[":INT_COL$SELECT:"; All]", CheckBoxArray)
    NumIntEtchIDs = DCount(Get_Property(@Window, '@INTETCHIDS'), @FM)
    CheckEnabled = (NumIntEtchIDs NE 0)
    Set_Property(IntEtchQCtrl, "OLE.CellCheckEnabled[6; All]", CheckEnabled)
    
    // Disable resizing of the header row and header column
    // Resize header column to fit contents
    HeaderRowArray = Get_Property(IntEtchQCtrl, "OLE.HeaderRow[1]")
    HeaderRowArray<3> = False$
    Set_Property(IntEtchQCtrl, "OLE.HeaderRow[1]", HeaderRowArray)
    // Disable resizing of columns as there is no need for this on this form
    StageColSize = Get_Property(IntEtchQCtrl, "OLE.DataColumn[1]")
    StageColSize<3> = False$
    Set_Property(IntEtchQCtrl, "OLE.DataColumn[All]", StageColSize)   
    StageColSize<1> = 50
    Set_Property(IntEtchQCtrl, "OLE.DataColumn[1]", StageColSize)
    StageColSize<1> = 120
    Set_Property(IntEtchQCtrl, "OLE.DataColumn[2-3]", StageColSize)
    StageColSize<1> = 50
    Set_Property(IntEtchQCtrl, "OLE.DataColumn[4]", StageColSize)
    StageColSize<1> = 143
    Set_Property(IntEtchQCtrl, "OLE.DataColumn[5]", StageColSize)
    StageColSize<1> = 50
    Set_Property(IntEtchQCtrl, "OLE.DataColumn[6]", StageColSize)
    // Disable resizing of rows as there is no need for this on this form
    RowSizeProps = Get_Property(IntEtchQCtrl, "OLE.DataRow[1]")
    RowSizeProps<3> = False$
    Set_Property(IntEtchQCtrl, "OLE.DataRow[All]", RowSizeProps)
    Set_Property(IntEtchQCtrl, "OLE.ScrollBarsVisible", 'Always':@FM:'Never')
    
    
    ////////////////////////////// Setup External Etch Edit Table //////////////////////////////////
    ExtEtchQCtrl = @Window:'.OLE_EDT_EXT_ETCH_Q'
    Send_Message(ExtEtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnClick', Qualifier)
    Send_Message(ExtEtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)
    
    GoSub RefreshExternalQueue
    
    HeaderFontArray  = 'Segoe UI':@SVM:8:@SVM:700    
    HeaderColArray = 40:@FM:False$:@FM:False$:@FM:False$    
    Set_Property(ExtEtchQCtrl, "OLE.CellFont[All; All]", 'Segoe UI':@SVM:8)    
    Set_Property(ExtEtchQCtrl, "OLE.CellFont[1;All]", HeaderFontArray)
    Set_Property(ExtEtchQCtrl, "OLE.HeaderFont[All; 1]", HeaderFontArray)
    Set_Property(ExtEtchQCtrl, "OLE.HeaderFont[1; All]", HeaderFontArray)
    Set_Property(ExtEtchQCtrl, "OLE.HeaderColumn[1]", HeaderColArray)
    EtchHeaderTitles = "Etch IDs":@VM:"Select"
    Set_Property(ExtEtchQCtrl, "OLE.TitleList", EtchHeaderTitles)
    Set_Property(ExtEtchQCtrl, "OLE.HeaderAlignment[All; 1]", 'C':@FM:'C':@FM:'C')
    Set_Property(ExtEtchQCtrl, "OLE.CellAlignment[All; All]", 'C':@FM:'C':@FM:'C')
    Set_Property(ExtEtchQCtrl, "OLE.AllowDeletions", False$)
    Set_Property(ExtEtchQCtrl, "OLE.AllowInserts", False$)
    
    CheckBoxArray      = 'Check Box'
    CheckBoxArray<2,1> = True$
    Set_Property(ExtEtchQCtrl, "OLE.CellType[":EXT_COL$SELECT:"; All]", CheckBoxArray)
    NumExtEtchIDs = DCount(Get_Property(@Window, '@EXTETCHIDS'), @FM)
    CheckEnabled = (NumExtEtchIDs NE 0)
    Set_Property(ExtEtchQCtrl, "OLE.CellCheckEnabled[2; All]", CheckEnabled)    
    
    // Disable resizing of the header row and header column
    // Resize header column to fit contents
    HeaderRowArray = Get_Property(ExtEtchQCtrl, "OLE.HeaderRow[1]")
    HeaderRowArray<3> = False$
    Set_Property(ExtEtchQCtrl, "OLE.HeaderRow[1]", HeaderRowArray)
    // Disable resizing of columns as there is no need for this on this form
    EtchIDColSize = Get_Property(ExtEtchQCtrl, "OLE.DataColumn[1]")
    EtchIDColSize<3> = False$
    Set_Property(ExtEtchQCtrl, "OLE.DataColumn[All]", EtchIDColSize)   
    EtchIDColSize<1> = 120
    Set_Property(ExtEtchQCtrl, "OLE.DataColumn[1]", EtchIDColSize)
    SelectColSize = EtchIDColSize
    SelectColSize<1> = 50
    Set_Property(ExtEtchQCtrl, "OLE.DataColumn[2]", SelectColSize)
    // Disable resizing of rows as there is no need for this on this form
    RowSizeProps = Get_Property(ExtEtchQCtrl, "OLE.DataRow[1]")
    RowSizeProps<3> = False$
    Set_Property(ExtEtchQCtrl, "OLE.DataRow[All]", RowSizeProps)
    Set_Property(ExtEtchQCtrl, "OLE.ScrollBarsVisible", 'Always':@FM:'Never')
        
return


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


RefreshInternalQueue:
    
    IntEtchQCtrl = @Window:'.OLE_EDT_INT_ETCH_Q'
    IntEtchIDs   = ''
    IntEtchQList = ''
    RowIndex     = 1
    GoSub ClearCursors
    Query  = "SELECT GAN_ETCH WITH AVAILABLE EQ ":True$:" AND WITH END_DTM NE '' AND WITH REACTOR EQ '":Reactor:"' BY START_DTM"
    Rlist(Query, Target_ActiveList$, '', '', '')
    EOF = False$
    If @RecCount then
        Loop
        ReadNext IntEtchID then

            Database_Services('ActivateRecord', 'GAN_ETCH', IntEtchID)                      
            IntEtchQList<RowIndex, 1> = {REACTOR}
            IntEtchQList<RowIndex, 2> = OConv({START_DTM}, 'DT')
            IntEtchQList<RowIndex, 3> = OConv({END_DTM}, 'DT')
            IntEtchQList<RowIndex, 5> = OConv({USER}, '[XLATE_CONV,LSL_USERS*FIRST_LAST]')
            IntEtchQList<RowIndex, 4> = {ELAP_HOURS}
            IntEtchIDs<RowIndex>      = IntEtchID   
            RowIndex += 1
            
        end else
            EOF = True$
        end
        Until EOF EQ True$
        Repeat
    end
                  
    NumIntEtchCols  = 6
    NumIntEtchRows  = @RecCount
    IntEtchDimArray = NumIntEtchCols :@FM: NumIntEtchRows
    Set_Property(IntEtchQCtrl, "OLE.Dimension", IntEtchDimArray)
    Set_Property(@Window, '@INTETCHIDS', IntEtchIDs)
    Set_Property(IntEtchQCtrl, 'OLE.LIST', IntEtchQList)  
    
return


RefreshExternalQueue:
    
    ExtEtchQCtrl    = @Window:'.OLE_EDT_EXT_ETCH_Q'
    Reactor         = Get_Property(@Window, '@REACTOR')
    ExtEtchQList    = Gan_Services('GetAvailableEtchIDs', Reactor)
    NumExtEtchCols  = 2
    NumExtEtchRows  = 0
    If ExtEtchQList NE '' then NumExtEtchRows  = DCount(ExtEtchQList, @FM)
    ExtEtchDimArray = NumExtEtchCols :@FM: NumExtEtchRows
    Set_Property(ExtEtchQCtrl, "OLE.Dimension", ExtEtchDimArray)
    Set_Property(@Window, '@EXTETCHIDS', ExtEtchQList)
    Set_Property(ExtEtchQCtrl, 'OLE.LIST', ExtEtchQList)
    
return


EnableCommitButton:
    
    IntEtchQArray   = Get_Property(@Window:'.OLE_EDT_INT_ETCH_Q', 'OLE.ARRAY')
    IntRowsSelected = Sum(IntEtchQArray<INT_COL$SELECT>)
    ExtEtchQArray   = Get_Property(@Window:'.OLE_EDT_EXT_ETCH_Q', 'OLE.ARRAY')
    ExtRowsSelected = Sum(ExtEtchQArray<EXT_COL$SELECT>)
    NoExtEtchReq    = Get_Property(@Window:'.CHK_EXT_ETCH', 'CHECK')
    If ( (IntRowsSelected GT 0) and (ExtRowsSelected GT 0) ) or ( (IntRowsSelected GT 0) and (NoExtEtchReq EQ True$) ) then
        Set_Property(@Window:'.PUB_COMMIT', 'ENABLED', True$)
    end else
        Set_Property(@Window:'.PUB_COMMIT', 'ENABLED', False$)
    end
    
return
