Functions of MyExplorer

#Func_MyExplorer.ps1

. .\Func_Form.ps1
. .\Func_FormParts.ps1
. .\Func_FormDialog.ps1
. .\Func_Files.ps1
. .\Func_Kaitei.ps1
. .\Func_FormConfig.ps1

$NmFileConfig = "Config_MyExplorer.txt"

function GetFormMyFolder{
    param(
        $FormOwner
        
        )
    # Table 定義
    $Table1=GetNewTable -nCol 5 -nRow 20 -SideMargin 10   

    # Controls 定義
    $Lab1= GetNewLabel "My Folder"  | SetToTable $Table1 -nRow 0 -spanCol 2

    $TxbPath = GetNewTextBox -Wrap -ScrollVertical -LabelMode | 
                        SetToTable $Table1 -nRow 1 -spanCol 4 -spanRow 2

    $Lab2 = GetNewLabel "Folder Contents" | SetToTable $Table1 -nRow 3 -spanCol 2
    $Liv1 = GetNewListView | SetToTable $Table1 -nRow 4 -spanCol 4 -spanRow 15


    # $Liv1
    [void]$Liv1.Columns.Add("Name",145)
    [void]$Liv1.Columns.Add("Date",70)

    # Event

    # Form 定義
    $xy = ($FormOwner.Location.X + $FormOwner.Width), $FormOwner.Location.Y
    $Form2 = GetNewForm -text "My Folder" -FormOwner $FormOwner -xy $xy

    # Form Event

    # Form 表示
    $Form2.Controls.Add($Table1)

    return $Form2,$TxbPath,$Liv1
}

function GetFormRightMenu{
    param(
        $FormOwner        
        )
    # Table 定義
    $Table1=GetNewTable -nCol 2 -nRow 4 -SideMargin 10

    # Controls 定義
    $Btns = @()
    $Btns += GetNewButton "Kaitei to MyFolder" | SetToTable $Table1 -nRow 0 -spanCol 2
    $Btns += GetNewButton "Copy to MyFolder" | SetToTable $Table1 -nRow 1 -spanCol 2
    $Btns += GetNewButton "Link to MyFolder" | SetToTable $Table1 -nRow 2 -spanCol 2
    $Btns += GetNewButton "Close" | SetToTable $Table1 -nRow 3 -spanCol 2

    # Event

    # Form 定義
    $FormR = GetNewForm -text "Right Menu" -FormOwner $FormOwner
    
    # Form Event

    # Form 表示
    $FormR.Controls.Add($Table1)
    return $FormR,$Btns
}

function MyExplorer{
    param(
        $pathIn,
        $nameIn,$nameInWild,
        $FilterIn=""
        )
    # Table 定義
    $Table1=GetNewTable -nCol 5 -nRow 21 -SideMargin 10   

    # Controls 定義
    $Lab1= GetNewLabel "Folder Path"  | SetToTable $Table1 -nRow 0 -spanCol 2

    $TxbPath = GetNewTextBox -Wrap -ScrollVertical | SetToTable $Table1 -nRow 1 -spanCol 4 -spanRow 2
    $btnOk =GetNewButton "OK" | SetToTable $Table1 -nRow 1 -nCol 4
    $btnSelect =GetNewButton "Select" | SetToTable $Table1 -nRow 2 -nCol 4

    $Lab2 = GetNewLabel "Folder Contents" | SetToTable $Table1 -nRow 3 -spanCol 2
    $Lab3 = GetNewLabel "Fol:  File:" -MojiSize 9 | SetToTable $Table1 -nCol 2 -nRow 3 -spanCol 2
    $Liv1 = GetNewListView | SetToTable $Table1 -nRow 4 -spanCol 4 -spanRow 15
    $BtnMySort = GetNewButton "MySort" -Mojisize 8  | SetToTable $Table1 -nRow 4 -nCol 4
    $btnFilter = GetNewButton "Filter" | SetToTable $Table1 -nRow 5 -nCol 4
    $LabFilter = GetNewLabel "" -MojiSize 9 -MiddleCenter | SetToTable $Table1 -nRow 6 -nCol 4
    $BtnFiFol = GetNewButton "Fi/Fol" | SetToTable $Table1 -nRow 7 -nCol 4
    $LabFiFol = GetNewLabel "" -MojiSize 9 -MiddleCenter | SetToTable $Table1 -nRow 8 -nCol 4
    $BtnMyFolder = GetNewButton "MyFolder" | SetToTable $Table1 -nRow 9 -nCol 4
    $BtnConfig = GetNewButton "Config" | SetToTable $Table1 -nRow 11 -nCol 4
    $LabFileName = GetNewLabel "file name" | SetToTable $Table1 -nRow 19 -spanCol 5 -spanRow 2

    # $Liv1
    [void]$Liv1.Columns.Add("Name",145)
    [void]$Liv1.Columns.Add("Date",70)

    # Event
    [string[]]$script:StrsFilt = $FilterIn,""
    [int]$script:FiFolVal = 0
    $script:FormMy = $null
    $script:Liv1SelItem =$null
    $Color1= $Liv1.backcolor

    $ClickOK={
        # path get
        $pathIn = $TxbPath.Text
        if( (MyTestPath $pathIn) -eq $false ){ $Liv1.Items.Clear() ; return }
        # objList make&sort
        $CI = Get-ChildItem $pathIn | select Name,LastWriteTime | sort Name  
        $cntFol = (Get-ChildItem $pathIn -Directory).count
        $cntFile = (Get-ChildItem $pathIn -File).count
        $Lab3.text="Fol:$cntFol  File:$cntFile"

        foreach($el in $script:StrsFilt){
            if([bool]$el){                
                if($el -like "{*}"){
                    $seiki=$el.Substring(1,$el.Length-2)
                    $CI = $CI | where{$_.Name -match $seiki}
                }else{
                    $CI = $CI | where{$_.Name -like $el}
                }
            
            }
        }
        if($script:FiFolVal -eq 0){ $LabFiFol.text="" }
        if($script:FiFolVal -eq 1){ $CI = $CI | where{$_.Name -like "*.*" } ;$LabFiFol.text="Files" }
        if($script:FiFolVal -eq 2){ $CI = $CI | where{$_.Name -notlike "*.*" } ;$LabFiFol.text="Folders"}

        #Liv1 output
        $Liv1.Items.Clear()
        foreach($it in $CI){        
            [void] $Liv1.Items.Add( $it.Name )
            [void] $Liv1.Items[-1].subitems.add( ($it.LastWriteTime).ToString( "yy/MM/dd" ) )
        }
            
    }
    $btnOk.Add_Click($ClickOK)
    
    $btnSelect.Add_Click({
        $xy= ( ($Form1.Location.X + $form1.Width),($Form1.Location.Y + $btnSelect.Location.Y))
        $aryPaths = GetConfigContent $NmFileConfig -AryPaths
        $ret = GetByFormInputCmb "Select Folder" -FormOwner $Form1 -CmbItems $aryPaths -xy $xy
        foreach($ary in $aryPaths){
            if( $ary[0] -eq $ret ){
                $TxbPath.text = $ary[1]
                $btnOK.select()
                & $ClickOK
                break            
            }
        } 
    })
  
    $BtnMySort.Add_Click({
        $objBuf1 = $Liv1.Items | where {$_.subitems[0].Text.length -ge 7} |
            sort {$_.subitems[0].Text.Substring(0,3) + $_.subitems[0].Text.Substring(4,3) + `
                $_.subitems[0].Text.Substring(3,1) + (MySubstring -strIn $_.subitems[0].Text -nSta 7)}
        $objBuf2 = $Liv1.Items | where {$_.subitems[0].Text.length -lt 7} | sort 
        $objBuf=$objBuf1 + $objBuf2

        $Liv1.Items.Clear()
        foreach($it in $objBuf){        
            [void] $Liv1.Items.Add( $it.subitems[0].Text )
            [void] $Liv1.Items[-1].subitems.add( $it.subitems[1].Text )
        }   
        $objBuf = $objBuf1 = $objBuf2 = $null
    })

    $btnFilter.Add_Click({
        $xy= ( ($Form1.Location.X + $form1.Width),($Form1.Location.Y + $btnFilter.Location.Y) )
        $aryFilter = @("") + @(GetConfigContent $NmFileConfig -AryWords)

        $ret = GetByFormInputCmb "Filter Set" -FormOwner $Form1 -CmbItems $aryFilter `
                -xy $xy -MinLength 0 -CntCmb 2 -InitText $script:StrsFilt
        if($ret -eq $null){
            # 閉じるボタン
        }else{
            $script:StrsFilt = $ret
            & $ClickOK 
            $flag=$false
            foreach($el in $ret){ if($el.length -ge 1){ $flag=$true } }
            if($flag){ $LabFilter.Text = "Filt ON" }else{ $LabFilter.Text = "" }
        }
    })
    $BtnFiFol.Add_Click({
        $script:FiFolVal = ($script:FiFolVal+1)%3
        & $ClickOK 
    })
    $BtnMyFolder.Add_Click({ 
        $pathMyFolder= (GetConfigContent $NmFileConfig -AryPaths)[-1][1]
        $TxbMy.text=$pathMyFolder

        $CI = Get-ChildItem $pathMyFolder | select Name,LastWriteTime | sort Name  
        $LivMy.Items.Clear()
        foreach($it in $CI){        
            [void] $LivMy.Items.Add( $it.Name )
            [void] $LivMy.Items[-1].subitems.add( ($it.LastWriteTime).ToString( "yy/MM/dd" ) )
        }

        $FormMy.Location = [string]($Form1.Location.X + $Form1.Width) +','+ [string]$Form1.Location.Y
        $FormMy.Visible = -not($FormMy.Visible)
        $Form1.Activate()
    })
    $BtnConfig.Add_Click({        
        $ItemsConfig= AddItemConfig -ItemType "Path" -ItemName "a" -Description "" |
                AddItemConfig -ItemType "Path" -ItemName "b" -Description "" |
                AddItemConfig -ItemType "Path" -ItemName "c" -Description "" |
                AddItemConfig -ItemType "Word" -Description "Filter0" |
                AddItemConfig -ItemType "Word" -Description "Filter1" |
                AddItemConfig -ItemType "Word" -Description "Filter2" |
                AddItemConfig -ItemType "Word" -Description "Filter3" |
                AddItemConfig -ItemType "Word" -Description "Filter4" |
                AddItemConfig -ItemType "Path" -ItemName "MyFolder" -Description "MyFolder" 

        ShowFormConfig -FormOwner $Form1 -NmFileConfig $NmFileConfig -ItemsConfig $ItemsConfig    
    })

    $Liv1.Add_DoubleClick({
        $nm = $Liv1.SelectedItems[0].Subitems[0].text
        cd $TxbPath.text
        Invoke-Item $nm    
    })
    $Liv1.Add_MouseUp({ # RClickLiv1
        if( $_.Button -eq "Right" ){
            if($script:Liv1SelItem -eq $null){ return }
            $script:Liv1SelItem.backColor ="#CCCCCC"
            $x = [System.Windows.Forms.Cursor]::Position.X +10
            $y = [System.Windows.Forms.Cursor]::Position.Y +10
            $FormRight.Location = "$x,$y" 
            $FormRight.Visible = $true
            $FormRight.Location = "$x,$y"     
        }
    })
    $Liv1.Add_ItemSelectionChanged({
        if($Liv1.SelectedItems[0] -eq $null){ return }
        if($script:Liv1SelItem.BackColor -eq "#CCCCCC"){ $script:Liv1SelItem.BackColor =$Color1}
        $script:Liv1SelItem = $Liv1.SelectedItems[0]
        $LabFileName.text = $script:Liv1SelItem.Subitems[0].text
    })
    $Liv1.Add_ColumnClick({
        $numSort = $_.Column
        $objBuf = $Liv1.Items | sort{$_.subitems[0].Text} | sort{$_.subitems[$numSort].Text}

        $Liv1.Items.Clear()
        foreach($it in $objBuf){        
            [void] $Liv1.Items.Add( $it.subitems[0].Text )
            [void] $Liv1.Items[-1].subitems.add( $it.subitems[1].Text )
        }   
        $objBuf = $null
    })

    # Form 定義
    $Form1 = GetNewForm -text "MyExplorer"

    # Form Event
    $FormShown = {
        $Form1.Activate()
        if([bool]$pathIn){
            $TxbPath.text=$pathIn
            & $ClickOK
        }
        foreach($el in $Liv1.Items){
            if($el.Subitems[0].text -eq $nameIn){$Liv1.Select();$el.selected=$true}
            if($el.Subitems[0].text -like $nameInWild){$Liv1.Select();$el.selected=$true}
        }
    }
    $FormCloseing = { }    
    $ActivatedForm = {
        $FormRight.visible=$false
    }
    $Form1.Add_Shown($FormShown)
    $form1.Add_Closing($CloseForm)
    $Form1.Add_Activated($ActivatedForm)

    # Right-Form
    $FormBtn = GetFormRightMenu -FormOwner $Form1
    $FormRight = $FormBtn[0]
    $BtnsRight = $FormBtn[1]

    $ClickBtnsInRight={
        $val = $FormRight.ActiveControl.Text
        $nm = $Liv1.SelectedItems[0].Subitems[0].text
        $pathMyFolder = (GetConfigContent $NmFileConfig -AryPaths)[-1][1]

        switch($val){
            {$_ -like "Kaitei*"} {
                cd $TxbPath.text
                $nmNext = KaiteiName $nm
                if(MyTestPath -name $nmNext){
                    $res= ResByMsgbox -Information -Text "Error`r`n$nmNext already exists"
                    break
                }
                $res = ResByMsgbox -ConfirmForExec -text "Kaitei OK?"
                if($res -eq $resNo){ return }                
                
                MyCopyInCurDir -nmCopy $nm -nmPaste $nmNext
                MakeShortcut -nmTarget $nmNext -pathPaste $pathMyFolder
                $FormRight.Close()
                & $ClickOK
                foreach($el in $Liv1.Items){
                    if($el.Subitems[0].text -eq "$nmNext"){
                        $Liv1.Select();$el.selected=$true
                        $el.BackColor = "#CCCCCC"
                    }
                }
                & $ClickMyFolder
                $FormMy.visible=$true; $FormMy.Activate()
                foreach($el in $LivMy.Items){
                    if($el.Subitems[0].text -like "$nmNext*"){$LivMy.Select();$el.selected=$true}
                }
            }     
            {$_ -like "Copy*"} {
                $res = ResByMsgbox -ConfirmForExec -text "Copy OK?"
                if($res -eq $resNo){ return }
                cd $TxbPath.text
                MyCopy -nmCopy $nm -pathPaste $pathMyFolder
                $FormRight.Close()
                & $ClickMyFolder
                $FormMy.visible=$true; $FormMy.Activate()
                foreach($el in $LivMy.Items){
                    if($el.Subitems[0].text -eq $nm){$LivMy.Select();$el.selected=$true}
                }
            }
            {$_ -like "Link*"} {
                $res = ResByMsgbox -ConfirmForExec -text "Link OK?"
                if($res -eq $resNo){ return }
                cd $TxbPath.text
                MakeShortcut -nmTarget $nm -pathPaste $pathMyFolder
                $FormRight.Close()
                & $ClickMyFolder
                $FormMy.visible=$true; $FormMy.Activate()
                foreach($el in $LivMy.Items){
                    if($el.Subitems[0].text -like "$nm*"){$LivMy.Select();$el.selected=$true}
                }         
            }
            {$_ -like "Close*"} { $FormRight.Close() }
            default {"Not matched."}
        }
    }
    foreach($el in $BtnsRight){ $el.Add_Click($ClickBtnsInRight) }
    $CloseFormRight={
        $_.Cancel = $True
        $FormRight.Visible = $false
        $Form1.Activate()
    }
    $FormRight.Add_Closing($CloseFormRight)

    # FormMy
    $CloseFormMy={
        $_.Cancel = $True
        $FormMy.Visible = $false
    }
    $ret = GetFormMyFolder -FormOwner $Form1 
    $FormMy=$ret[0]; $TxbMy=$ret[1]; $LivMy=$ret[2]
    $FormMy.Visible=$false
    $FormMy.Add_Closing($CloseFormMy)    

    # Form 表示
    $Form1.Controls.Add($Table1)
    $res = $Form1.ShowDialog() 
    if($res -eq $resOK){
        return $Txb1.text 
    }
}