Compile function NDW_GAN_ETCH_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_GAN_ETCH_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_GAN_ETCH

$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
    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
    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 EDT_ETCH_QUEUE.POSCHANGED(NextColumn, NextRow)
    
    
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
    end

end event


Event OLE_EDT_ETCH_Q.OnClick(Cell, Point, Button, Shift, Ctrl)
    
    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 OLE_EDT_ETCH_Q.OnCheckChanged(Cell, OldValue, NewValue)
    
    EtchQArray = Get_Property(@Window:'.OLE_EDT_ETCH_Q', 'OLE.ARRAY')
    RowsSelected = Sum(EtchQArray<COL$SELECT>)
    If RowsSelected GT 0 then
        Set_Property(@Window:'.PUB_COMMIT', 'ENABLED', True$)
    end else
        Set_Property(@Window:'.PUB_COMMIT', 'ENABLED', False$)
    end 
    
end event


Event PUB_COMMIT.CLICK()
    
    EtchIDList = Get_Property(@Window, '@ETCHIDS')
    SelEtchIDs = ''
    EtchQArray = Get_Property(@Window:'.OLE_EDT_ETCH_Q', 'OLE.ARRAY')
    RowsSelected = EtchQArray<COL$SELECT>
    For each Checkmark in RowsSelected using @VM setting vPos
        If Checkmark EQ True$ then
            SelEtchIDs<0, -1> = EtchIDList<vPos>
        end
    Next Checkmark
    End_Dialog(@Window, SelEtchIDs)
    
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)
    
    If FormMode _EQC 'COMMIT' then

        WindowTitle = Get_Property(@Window, 'TEXT')
        NewWindowTitle = 'Commit Etch - ':ExternalEtchID
        Set_Property(@Window, 'TEXT', NewWindowTitle)
        Send_Message(EtchQCtrl, 'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)
        Set_Property(@Window:'.GROUP_ETCH_CREATE', 'VISIBLE', False$)
        Set_Property(@Window:'.PUB_EXIT', 'VISIBLE', False$)
        Set_Property(@Window:'.PUB_COMMIT', 'VISIBLE', True$)
        
        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":@VM:"Select"
        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$)
        
        CheckBoxArray      = 'Check Box'
        CheckBoxArray<2,1> = True$
        Set_Property(EtchQCtrl, "OLE.CellType[":COL$SELECT:"; All]", CheckBoxArray)
        
        CommitCtrl      = @Window:'.PUB_COMMIT'
        FormSize        = Get_Property(@Window, 'SIZE')
        EtchQCtrlSize   = Get_Property(EtchQCtrl, 'SIZE')
        CommitCtrlSize  = Get_Property(CommitCtrl, 'SIZE')
        FormSize<3>     = FormSize<3> + 50
        FormSize<4>     = FormSize<4> - 20
        Set_Property(@Window, 'SIZE', FormSize)
        EtchQCtrlSize<3> = EtchQCtrlSize<3> + 50
        Set_Property(EtchQCtrl, 'SIZE', EtchQCtrlSize)
        CommitCtrlSize<1> = CommitCtrlSize<1> + 50
        CommitCtrlSize<2> = CommitCtrlSize<2> - 10
        Set_Property(CommitCtrl, 'SIZE', CommitCtrlSize)        
        
        // 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> = 143
        Set_Property(EtchQCtrl, "OLE.DataColumn[5]", StageColSize)
        StageColSize<1> = 50
        Set_Property(EtchQCtrl, "OLE.DataColumn[6]", 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')
        
    end else
    
        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')
    end
    
return

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


RefreshQueue:
    
    FormMode = Get_Property(@Window, '@FORMMODE')
    
    If FormMode _EQC 'COMMIT' then
        
        EtchIDs   = ''
        EtchQList = ''
        RowIndex  = 1
        GoSub ClearCursors
        Query  = 'SELECT GAN_ETCH WITH AVAILABLE EQ ':True$:' AND WITH REACTOR EQ ':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 = 6
        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)
        
    end else
        
        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)
        
    end  
    
return


