Compile function NDW_GAN_PARAM_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_GaN_Param_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               

    History     :   (Date, Initials, Notes)
        12/05/19    djs     Created initial commuter module.
        10/27/20    djs     Refactored commuter module to be soft coded instead of hard coded.                  

***********************************************************************************************************************/
#pragma precomp SRP_PreCompiler
#window NDW_GAN_PARAM_MANAGER

$Insert EVENT_SETUP
$Insert APP_INSERTS
$Insert MSG_EQUATES

Declare subroutine End_Window, Database_Services, ErrMsg
Declare function   SQL_Services, SRP_Array, Database_Services, Gan_Services

SubclassInfo    = Form_Services('FindSubclassControl')
Subclass        = SubclassInfo<1>

// The purpose of this line is to prevent the SRP Editor from throwing a warning when compiling.
If Not(Assigned(Param8)) then Param8 = ''

// 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)

    Visible    = Get_Property(@Window, 'VISIBLE')
    If Visible EQ False$ then
        PSNo       = CreateParam
        CurrPartNo = Xlate('PROD_SPEC', PSNo, 'CURR_PART_NO', 'X')
        Set_Property(@Window:'.EDL_PSN', 'TEXT', PSNo)
        Set_Property(@Window:'.EDL_PART_NO', 'TEXT', CurrPartNo)
        
        GoSub Setup_OLE_Controls
        
        FormSize  = '' 
        SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
        Eventflow = 0
    end
    
End Event


Event WINDOW.CLOSE(CancelFlag)
    
    Post_Event('DBW_PROD_SPEC_GAN', 'OMNIEVENT', 'EnableImportButton')
    Set_Property(@Window:'.OLE_TAB', "OLE.FlickerFree", False$)
 
end event


Event OLE_TAB.SelChanged(Index, PrevIndex)
    
    GoSub FillParamCtrl
    
end event


Event PUB_EXIT.CLICK()

    Post_Event(@Window, 'CLOSE')
    
end event


Event PUB_SAVE.CLICK()

    PSN    = Get_Property(@Window:'.EDL_PSN', 'TEXT')
    PartNo = Get_Property(@Window:'.EDL_PART_NO', 'TEXT')
    If ( (PSN NE '') and (PartNo NE '') ) then
        StageList = Get_Property(@Window, '@GAN_PARAM_STAGES')
        ValueList = Get_Property(@Window, '@VALUE_LIST')
        For each Stage in StageList using @VM setting StageIndex
            ParameterList = ValueList<StageIndex>           
            ParamConfigKey = PSN:'*':PartNo:'*':Stage
            Convert @VM to @FM in ParameterList
            Convert @SVM to @VM in ParameterList   
            ParamConfigRec = SRP_Array('Rotate', ParameterList, @FM, @VM)
            NumParams      = DCount(ParamConfigRec<1>, @VM)
            EnabledArray   = ParamConfigRec<3>
            For ParamIndex = 1 to NumParams
                If EnabledArray<0, ParamIndex> EQ '' then EnabledArray<0, ParamIndex> = 0
            Next ParamIndex
            ParamConfigRec<3> = EnabledArray
            Database_Services('WriteDataRow', 'GAN_PARAM_CONFIG', ParamConfigKey, ParamConfigRec, True$, False$, True$)
        Next Stage
        Set_Property(@Window:'.PUB_SAVE', 'ENABLED', False$)
        Send_Event('NDW_GAN_PROD_SPEC', 'OMNIEVENT', 'EnableImportButton')
    end else
        ErrMsg('Error saving GaN parameter configuration due to null PSN or null PartNo.')
    end
    
end event


Event OLE_PARAM_EDT.OnCheckChanged(Cell, OldValue, NewValue)
    
    CellCol                      = Field(Cell, ';', 1)
    CellRow                      = Field(Cell, ';', 2)
    ParameterList                = ''
    ParamCtrl                    = @Window:'.OLE_PARAM_EDT'
    TabCtrl                      = @Window:'.OLE_TAB'
    NewValue                     = Get_Property(ParamCtrl, 'OLE.CellCheck[':CellCol:'; ':CellRow:']')
    TabIndex                     = Get_Property(TabCtrl, 'OLE.SelectedTab')
    NumCols                      = 3
    ValueList                    = Get_Property(@Window, '@VALUE_LIST')
    ParameterList                = ValueList<TabIndex>
    ParameterList<0, CellRow, 3> = NewValue
    ValueList<TabIndex>          = ParameterList
    Set_Property(@Window, '@VALUE_LIST', ValueList) 

    GoSub EnableSaveButton
    
end event


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


Setup_OLE_Controls:

    GoSub SetupTabCtrl
    GoSub SetupParamCtrl
    GoSub ReadCurrParams
    GoSub FillParamCtrl
    
return


SetupTabCtrl:
    
    TabCtrl = @Window:'.OLE_TAB'   
    CaptionList = Xlate('APP_INFO', 'GAN_PARAM_STAGES', '', 'X')
    NumTabs     = DCount(CaptionList, @VM)
    Set_Property(TabCtrl, "OLE.TabCount", NumTabs)
    Set_Property(@Window, '@GAN_PARAM_STAGES', CaptionList)
    Convert @VM to ',' in CaptionList
    Set_Property(TabCtrl, "OLE.CaptionList", CaptionList)
    FontArray   = 'Segoe UI':@SVM:9:@SVM:700
    Set_Property(TabCtrl, "OLE.Font", FontArray)
    Set_Property(TabCtrl, "OLE.SelectFont", FontArray)
    
    // Qualify OLE events that we want to intercept
    Qualifier = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(TabCtrl, 'QUALIFY_EVENT', 'OLE.SelChanged', Qualifier)   
    Set_Property(TabCtrl, "OLE.FlickerFree", True$)
    
return


SetupParamCtrl:
    
    ParamCtrl     = @Window:'.OLE_PARAM_EDT'
    HeaderRowTitles   = 'Metrology Name':@VM:'Parameter Name':@VM:'Critical'
    DimArray = 3:@FM:1
    Set_Property(ParamCtrl, "OLE.Dimension", DimArray)
    Set_Property(ParamCtrl, "OLE.TitleList", HeaderRowTitles)
    
    HeaderFontArray   = 'Segoe UI':@SVM:8:@SVM:700
    Set_Property(ParamCtrl, "OLE.HeaderFont[All; All]", HeaderFontArray)
    HeaderColArray = 0:@FM:False$:@FM:False$:@FM:False$
    Set_Property(ParamCtrl, "OLE.HeaderColumn[1]", HeaderColArray)
    
    // Disable resizing of columns as there is no need for this on this form
    StageColSize    = Get_Property(ParamCtrl, "OLE.DataColumn[1]")
    StageColSize<1> = 358
    StageColSize<3> = False$
    Set_Property(ParamCtrl, "OLE.DataColumn[1-2]", StageColSize)
    StageColSize<1> = 80
    Set_Property(ParamCtrl, "OLE.DataColumn[3]", StageColSize)
    
    CheckBoxArray      = 'Check Box'
    CheckBoxArray<2,1> = True$
    Set_Property(ParamCtrl, "OLE.CellType[3; All]", CheckBoxArray)
    Set_Property(ParamCtrl, "OLE.CellAlignment[1-2; All]", 'C':@FM:'L':@FM:'C')
    Set_Property(ParamCtrl, "OLE.CellAlignment[3; All]", 'C':@FM:'C':@FM:'C')
    Set_Property(ParamCtrl, "OLE.HeaderAlignment[All; 1]", 'C':@FM:'C':@FM:'C')
    Set_Property(ParamCtrl, "OLE.CellProtection[1-2; All]", 'Read Only')
    
    // Qualify OLE events that we want to intercept
    Qualifier = ''
    Qualifier<1> = 1
    Qualifier<4> = 0 ; * process synchronously (i.e. immediately)
    Send_Message(ParamCtrl, 'QUALIFY_EVENT', 'OLE.OnCheckChanged', Qualifier)   
       
return


ReadCurrParams:
    
    PartNo         = Get_Property(@Window:'.EDL_PART_NO', 'TEXT')
    PSNo           = Get_Property(@Window:'.EDL_PSN', 'TEXT')
    ParamCtrl      = @Window:'.OLE_PARAM_EDT'
    TabCtrl        = @Window:'.OLE_TAB'    
    ParameterList  = Gan_Services('GetCurrPartParams', PartNo)
    GaNParamStages = Get_Property(@Window, '@GAN_PARAM_STAGES')

    For each Stage in GaNParamStages using @VM setting TabIndex            
        ParamConfigKey = PSNo:'*':PartNo:'*':Stage
        EnabledArray   = Xlate('GAN_PARAM_CONFIG', ParamConfigKey, 'ENABLED', 'X')
        Convert @VM to @SVM in EnabledArray
        StageList      = ParameterList<TabIndex>
        NumParams      = DCount(StageList, @VM)
        For ParamPos = 1 to NumParams
            StageList<0, ParamPos, 3> = EnabledArray<0, 0, ParamPos>    
        Next ParamPos
        ParameterList<TabIndex> = StageList          
    Next Stage
    Set_Property(@Window, '@VALUE_LIST', ParameterList)
    Set_Property(@Window, '@ORIG_VALUE_LIST', ParameterList)  
    
return


FillParamCtrl:
    
    ParameterList = ''
    ParamCtrl     = @Window:'.OLE_PARAM_EDT'
    TabCtrl       = @Window:'.OLE_TAB'
    TabIndex      = Get_Property(TabCtrl, 'OLE.SelectedTab')
    NumCols       = 3
    ValueList     = Get_Property(@Window, '@VALUE_LIST')
    ParameterList = ValueList<TabIndex>
    Convert @VM to @FM in ParameterList
    Convert @SVM to @VM in ParameterList             
    NumRows = DCount(ParameterList, @FM)
    If NumRows EQ 0 then NumRows = 1
    DimArray = 3:@FM:NumRows
    ParamCtrl = @Window:'.OLE_PARAM_EDT'
    Set_Property(ParamCtrl, "OLE.Dimension", DimArray)
    Set_Property(ParamCtrl, "OLE.CellAlignment[1-2; All]", 'C':@FM:'L':@FM:'L')
    Set_Property(ParamCtrl, 'OLE.List', ParameterList)
    
*    If TabIndex EQ 3 then
*        // 'CAN'
*        If TabIndex EQ 3 then
*            // 'CAN'
*            ParameterList = SRP_Array('Rotate', ParameterList, @FM, @VM)
*            ParamNames    = ParameterList<1>
*            Locate 'Candela Total Defects' in ParamNames using @VM setting vPos then
*                Set_Property(ParamCtrl, "OLE.CellCheckEnabled[3; ":vPos:"]", False$)
*                Set_Property(ParamCtrl, "OLE.CellCheckEnabled[3; ":vPos:"]", False$)
*            end
*        end
*    end
    
    GoSub EnableSaveButton
    
return


EnableSaveButton:

    ChangeDetected = False$   
    ValueList     = Get_Property(@Window, '@VALUE_LIST')
    OrigValueList = Get_Property(@Window, '@ORIG_VALUE_LIST')
    If ValueList NE OrigValueList then ChangeDetected = True$          
    Set_Property(@Window:'.PUB_SAVE', 'ENABLED', ChangeDetected)
    
return


MergeList:
    
    ParamConfigKey = PSNo:'*':PartNo:'*':Stage
    EnabledArray   = Xlate('GAN_PARAM_CONFIG', ParamConfigKey, 'ENABLED', 'X')
    List = ParameterList<TabIndex>
    Convert @VM to @FM in List
    Convert @SVM to @VM in List
    List = SRP_Array('Rotate', List, @FM, @VM)
    List<3> = EnabledArray
    List = SRP_Array('Rotate', List, @FM, @VM)
    
return

