Functions for FormConfig

# Func_FormConfig.ps1

. .\Func_Form.ps1
. .\Func_FormParts.ps1
. .\Func_FormDialog.ps1
. .\Func_Files.ps1

$kugiriCo = "`t"

function GetConfigContent{
    param(
        $NmFileConfig,
        [int]$NumPath =-1,
        [int]$NumWord =-1,
        [switch]$AryPaths,
        [switch]$AryWords      
        )
    CdMyData

    if(-not(MyTestPath -name $NmFileConfig)){
        "CONFIGURATION FilesSerch" | Out-File -FilePath $NmFileConfig
    }

    $Contents = Get-Content $NmFileConfig

    if($NumPath -ge 0){
        foreach($el in $Contents){
            if( ($el -split $kugiriCo )[0] -eq "Path $NumPath"){
                return ($el -split $kugiriCo )[3] 
            }
        }
    }
    if($NumWord -ge 0){
        foreach($el in $Contents){
            if( ($el -split $kugiriCo )[0] -eq "Word$NumWord"){
                return ($el -split $kugiriCo )[1] 
            }
        }
    }
    if($AryPaths){
        $aryOutPaths = @()
        foreach($el in $Contents){
            if( ($el -split $kugiriCo )[0] -like "Path*"){
                $aryOutPaths += ,@( ($el -split $kugiriCo )[1] ,($el -split $kugiriCo )[3] )                 
            }
        }
        return $aryOutPaths
    }
    if($AryWords){
        $aryOutFilters = @()
        foreach($el in $Contents){
            if( ($el -split $kugiriCo )[0] -like "Word*"){
                $aryOutFilters += ($el -split $kugiriCo )[1]
            }
        }
        return $aryOutFilters
    }
 }

function AddItemConfig{
    #[CmdletBinding()]
    param(
        [string]$ItemType,
        [string]$ItemName,
        [string]$Description,
        [Parameter(ValueFromPipeline=$True)][psobject[]]$ItemsConfig = $null
    )

    begin{
        [int]$cnt=-1
        $ret=@()
    }
    process{
        foreach($el in $ItemsConfig){
            $cnt++
            $ret += $el 
        }
    }
    end{
        $p = @{index = ($cnt+1) ; type=$ItemType; name = $ItemName; description = $Description}
        $ObjItem = New-Object PSObject -Property $p
        $ret = $ret + $ObjItem    
        return $ret
    }
}

function ShowFormConfig{
    param(
        $FormOwner,
        $NmFileConfig,
        [psobject[]]$ItemsConfig
    )
    
    if([bool]$NmFileConfig -eq $false){
         Write-Warning 'NmFileConfig error. ShowFormConfig exited'
         return
    }
    $nmConfig = MySplitName $NmFileConfig

    # cd
    CdMyData

    # Const
    $ItemsDrive = @("Top") + @(GetDrivePaths)

    # ItemsConfig
    $cntIdx1 = 0
    foreach($el in $ItemsConfig){
        if($el.type -eq "Path"){ $cntIdx1++ }
    }

    # TableA
    $TableA=GetNewTable -nCol 8 -nRow 2 -SideMargin 10
    $LabTitle= GetNewLabel "CONFIGURATION" -MojiSize 11 | SetToTable $TableA -nRow 0 -spanCol 3
    $TableA.RowStyles[1].Height =10
    $y = $TableA.PreferredSize.Height

    # TableB
    $TableBs = @()
    $LabsPath = @();$NamesPath = @(); $CmbsDrive=@(); $TxbsPath = @(); $BtnsRef = @(); $DescriptPath = @();
    $LabsWord = @(); $TxbsWord =@(); $DescriptWord = @()
    $cntB=-1 ; $cntB1 = -1 ; $cntB2 = -1

    foreach($el in $ItemsConfig){
        $cntB++
        if($el.type -eq "Path"){ # TableB1 定義
            $cntB1++
            if([bool]$el.name){ $nmBuf = $el.name }else{$nmBuf = "(Name$cntB1)" }
            if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Description$cntB1" }

            $TableBs += GetNewTable -nCol 8 -nRow 3 -SideMargin 10 -xy (0,$y)
            $LabsPath += GetNewLabel "Path $cntB1" -MiddleLeft | SetToTable $TableBs[-1] -nRow 0
            $NamesPath += GetNewTextBox $nmBuf -LabelMode -MojiSize 9 -Wrap | SetToTable $TableBs[-1] -nRow 1
            $CmbsDrive += GetNewCombo -CmbItems $ItemsDrive | SetToTable $TableBs[-1] -nRow 0 -nCol 1
            $BtnsRef += GetNewButton "Ref" | SetToTable $TableBs[-1] -nRow 1 -nCol 1
            $TxbsPath += GetNewTextBox -Wrap -ScrollVertical |
                            SetToTable $TableBs[-1] -nRow 0 -nCol 2 -spanCol 4 -spanRow 2 
            $DescriptPath += GetNewTextBox $deBuf -MojiSize 9 -Wrap -LabelMode | 
                            SetToTable $TableBs[-1] -nRow 0 -nCol 6 -spanCol 2 -spanRow 2
            $TableBs[-1].RowStyles[2].Height =10
            $y = $y + $TableBs[-1].PreferredSize.Height            
        }
        if($el.type -eq "Word"){ # TableB2 定義
            $cntB2++
            if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Word$cntB1" }

            $TableBs += GetNewTable -nCol 8 -nRow 2 -SideMargin 10 -xy (0,$y)
            $LabsWord += GetNewLabel "Word$cntB2" -MiddleLeft | SetToTable $TableBs[-1] -nRow 0
            $TxbsWord += GetNewTextBox | SetToTable $TableBs[-1] -nRow 0 -nCol 1 -spanCol 2
            $DescriptWord += GetNewTextBox -text $deBuf -MojiSize 9 -LabelMode | 
                                        SetToTable $TableBs[-1] -nRow 0 -nCol 2 -spanCol 5 
            $TableBs[-1].RowStyles[1].Height =10
            $y = $y + $TableBs[-1].PreferredSize.Height
        }
    }


    # TableC 定義
    $TableC=GetNewTable -nCol 8 -nRow 3 -SideMargin 10 -xy (0,$y)

    $TableC.RowStyles[0].Height =10
    $BtnApply =GetNewButton "Apply" | SetToTable $TableC -nRow 1 -nCol 3
    $BtnClose =GetNewButton "Close" | SetToTable $TableC -nRow 1 -nCol 4
    $BtnInit =GetNewButton "Initialize" | SetToTable $TableC -nRow 1 -nCol 6
    $TableC.RowStyles[2].Height =10

    # Read txt
    if(-not(MyTestPath -name $NmFileConfig)){
        "CONFIGURATION $nmConfig" | Out-File -FilePath $NmFileConfig
    }
    $Contents = Get-Content $NmFileConfig
    for($idx=0;$idx -lt $LabsPath.count;$idx++){
        foreach($el in $Contents){
            if( ($el -split $kugiriCo )[0] -eq $LabsPath[$idx].text){
                $NamesPath[$idx].text = ($el -split $kugiriCo )[1]
                if($NamesPath[$idx].text -eq ""){$NamesPath[$idx][$idx].text="(Name$idx)"}
                $CmbsDrive[$idx].text = ($el -split $kugiriCo )[2]
                $TxbsPath[$idx].text = ($el -split $kugiriCo )[3] 
                $DescriptPath[$idx].text = ($el -split $kugiriCo )[4] 
                if($DescriptPath[$idx].text -eq ""){$DescriptPath[$idx].text="Description $idx"}
            }
        }
    }
    for($idx=0;$idx -lt $LabsWord.count;$idx++){
        foreach($el in $Contents){
            if( ($el -split $kugiriCo )[0] -eq $LabsWord[$idx].text){
                $TxbsWord[$idx].text = ($el -split $kugiriCo )[1] 

                $DescriptWord[$idx].text = ($el -split $kugiriCo )[2] 
                if($DescriptWord[$idx].text -eq ""){$DescriptWord[$idx].text="Description $idx"}
            }
        }
    }
    
    # Event
    $ClickRef={        
        $idx = [Array]::IndexOf($BtnsRef,$Form1.ActiveControl)

        $pathRootRef = $CmbsDrive[$idx].text
        if($pathRootRef -eq "Top"){$pathRootRef=$pathTop} 

        $ret = GetFolderPathByDialog -PathRoot $pathRootRef
        if([bool]$ret){ $TxbsPath[$idx].text=$ret }
    }
    $ClickApply={
        $res = ResByMsgbox -ConfirmForExec -Text "Update Config OK ?"
        if($res -eq $resNo){ return }
        "CONFIGURATION $nmConfig" | Out-File -FilePath $NmFileConfig
        for($idx=0;$idx -lt $LabsPath.count;$idx++){
            if($TxbsPath[$idx].text -like "$pathTop*"){
                $CmbsDrive[$idx].text="Top"
            }else{
                $CmbsDrive[$idx].text= MySplitPathDrive $TxbsPath[$idx].text
            }
            ( $LabsPath[$idx].text + $kugiriCo + $NamesPath[$idx].text + $kugiriCo `
                + $CmbsDrive[$idx].text + $kugiriCo + $TxbsPath[$idx].text + $kugiriCo `
                + $DescriptPath[$idx].text  ) | 
                                        Out-File -FilePath $NmFileConfig -Append
        }

        for($idx=0;$idx -lt $LabsWord.count;$idx++){
            ( $LabsWord[$idx].text + $kugiriCo + $TxbsWord[$idx].text  + $kugiriCo `
                 + $DescriptWord[$idx].text ) | 
                                        Out-File -FilePath $NmFileConfig -Append
        }

        [String](Get-Date) | Out-File -FilePath $NmFileConfig -Append
    }
    $ClickClose={
        $Form1.close()
    }
    $DClickTxblabel={
        $ctlMe=$Form1.ActiveControl
        $xy = (($Form1.Location.X + $Form1.Width), ($Form1.Location.Y + $ctlMe.Location.Y -25) )
        $ret = GetByFormInputTxb -TxbSpanRow 2 -xy $xy -InitText $ctlMe.text
        if([bool]$ret){ $ctlMe.text=$ret }
    }
    $ClickInit={       
        $cntB1 = -1; $cntB2 = -1;
        foreach($el in $ItemsConfig){
            if($el.type -eq "Path"){
                $cntB1++
                if([bool]$el.name){ $nmBuf = $el.name }else{$nmBuf = "(Name$cntB1)" }
                $NamesPath[$cntB1].text = $nmBuf 
                if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Description$cntB1" }
                $DescriptPath[$cntB1].text = $deBuf
                $CmbsDrive[$cntB1].text = ""
                $TxbsPath[$cntB1].text = ""
            }
            if($el.type -eq "Word"){
                $cntB2++
                if([bool]$el.description){ $deBuf = $el.description }else{$deBuf= "Word$cntB1" }
                $DescriptWord[$cntB2].text = $deBuf
                $TxbsWord[$cntB2].text = ""
            }
        }
    }

    foreach($el in $BtnsRef){ $el.Add_Click($ClickRef) }
    foreach($el in $NamesPath){ $el.Add_DoubleClick($DClickTxblabel) }
    foreach($el in $DescriptPath){ $el.Add_DoubleClick($DClickTxblabel) }
    foreach($el in $DescriptWord){ $el.Add_DoubleClick($DClickTxblabel) }
    $BtnApply.Add_Click($ClickApply)
    $BtnClose.Add_Click($ClickClose)
    $BtnInit.Add_Click($ClickInit)

    # Form 定義
    $Form1 = GetNewForm -text "Config Form" -FormOwner $FormOwner
    
    # Form Event
    $FormShown = { 
        $Form1.Activate() 
        foreach($el in  $CmbsDrive){$el.select()}
        $btnClose.select()}
    $FormClosing = { }    
    $Form1.Add_Shown($FormShown)
    $form1.Add_Closing($FormClosing)

    # Form 表示
    $Form1.Controls.Add($TableA)
    foreach($el in $TableBs){
        $Form1.Controls.Add($el)
    }
    $Form1.Controls.Add($TableC)
    $res = $Form1.ShowDialog() 
    if($res -eq $resOK){
         
    }
}