Attribute VB_Name = "CHROPROC1"

Function ChroCombine(strChro As String) As Integer
    Const MAX_SPECS = 6

    Dim nPeak As Integer
    Dim nPeaks As Integer
    Dim n1stPeak As Integer
    Dim nLstPeak As Integer
    Dim nSpecs As Integer
    Dim sIntParams As INTEGRATE_CHRO_PARAMS
    Dim asPeak() As PKD_FULL_PEAK
    Dim hDm As Long
    Dim sRawFile As VGRAWFILE
    Dim wbPrintMode As Integer
    Dim strIniFile As String
    Dim strReplaceSpectra As String
    
    strIniFile = App.Path + "\" + App.EXEName + ".INI"
    wbPrintMode = GetPrivateProfileInteger("SpecPrint", "PrintMode", strIniFile)
        
    If GetSampleListFileVB(sRawFile) = False Then
        ChroCombine = False
        Exit Function
    End If
    
    
    ' Finding the size of the file (no. of scans in function)
    sRawFile.lpVGFile.dwSize = DmRawScansInFunction(sRawFile)
    
    ' SCR 534 12/09/96
    ' Call changed from IntegrateChromatogram
    ' David Varley
    
    nPeaks = IntegrateChromatogramProcess(Chr$(0), sRawFile, 1, strChro, 0, 0, 0, asPeak(), sIntParams, True, hDm)

    If nPeaks > 0 Then
        n1stPeak = LBound(asPeak)
        nLstPeak = UBound(asPeak)
        nSpecs = 0
        SpecClose
        ChroClose
        
        'Set the spectrum window to add new spectra
        strReplaceSpectra = GetPrivateProfileStr("SpeDisplay", "Replace", "C:\MassLynx\MassLynx.ini")
        Call WritePrivateProfileStr("SpeDisplay", "Replace", "FALSE", "C:\MassLynx\MassLynx.ini")

        For nPeak = n1stPeak To nLstPeak
            frmChroProc.lblStatus.Caption = "Processing: " + CStr(nPeak - n1stPeak + 1) + "/" + CStr(nPeaks)

            If CombineTop(CInt(asPeak(nPeak).fTop), sRawFile) = 0 Then
                ChroCombine = nPeak
                Exit Function
            End If

            nSpecs = nSpecs + 1

            If nSpecs >= MAX_SPECS Or nPeak = nLstPeak Then
                Call SpecPrint(wbPrintMode)
                SpecClose
                nSpecs = 0
            End If
    
        Next nPeak
        
        ChroClose
    
        'Reset the spectrum window to add/replace new spectra as before
        Call WritePrivateProfileStr("SpeDisplay", "Replace", strReplaceSpectra, "C:\MassLynx\MassLynx.ini")
    
    End If
    
    DmRawClose
    ChroCombine = nPeaks
End Function

Function CombineTop(nTopScan As Integer, sRawFile As VGRAWFILE) As Integer
    Const OFFSET = 1

    Dim nStartScan As Integer
    Dim nEndScan As Integer
    Dim nLstScan As Integer
    Dim strFile As String

    nLstScan = CInt(sRawFile.lpVGFile.dwSize)

    If nTopScan < 1 Or nTopScan > nLstScan Then
        Exit Function
    End If

    nStartScan = nTopScan - OFFSET
    nEndScan = nTopScan + OFFSET

    If nStartScan < 1 Then
        nStartScan = 1
    End If

    If nEndScan > nLstScan Then
        nEndScan = nLstScan
    End If

    Call ChroCombineScans(CLng(nStartScan), CLng(nEndScan), 0, 0, 0, 0, 1, 1)
    CombineTop = nTopScan
    
End Function

Sub Main()
    Dim strChroDesc As String
    Dim strIniFile As String
    Dim sRect As RECT

    strIniFile = App.Path + "\" + App.EXEName + ".INI"
    strChroDesc = GetPrivateProfileStr("Combine", "ChroDesc", strIniFile)
    Call FormPosRead("Combine", "Combiner", strIniFile, sRect)
    Call FormPosSet(frmChroProc, sRect)
    frmChroProc.Show
    Call SetWindowPos(CLng(frmChroProc.hWnd), -1, 0, 0, 0, 0, 3)
    frmChroProc.lblStatus.Caption = "Initializing"
    frmChroProc.lblStatus.Caption = ChroCombine(strChroDesc)
    Call FormPosGet(frmChroProc, sRect)
    Call FormPosWrite("Combine", "Combiner", strIniFile, sRect)
    End
End Sub