Imports Stax
Imports Stax.UI
Imports Stax.ComponentModel
Imports Stax.AppFramework

Imports System.Threading

Public Class MP4BoxForm
    Inherits ExtendedForm

#Region " Designer "

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Friend WithEvents bOK As System.Windows.Forms.Button
    Friend WithEvents bCancel As System.Windows.Forms.Button
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents gbSubtitles As System.Windows.Forms.GroupBox
    Friend WithEvents bAddSubtitle As System.Windows.Forms.Button
    Friend WithEvents lvSubtitles As System.Windows.Forms.ListView
    Friend WithEvents chLanguage As System.Windows.Forms.ColumnHeader
    Friend WithEvents chName As System.Windows.Forms.ColumnHeader
    Friend WithEvents chPath As System.Windows.Forms.ColumnHeader
    Friend WithEvents bDel As System.Windows.Forms.Button
    Friend WithEvents gbChapters As System.Windows.Forms.GroupBox
    Friend WithEvents bChapters As System.Windows.Forms.Button
    Friend WithEvents tbChapters As System.Windows.Forms.TextBox
    Friend WithEvents TipProvider As Stax.UI.TipProvider
    Friend WithEvents bTools As System.Windows.Forms.Button
    Friend WithEvents cmsTools As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents ShowCommandLineToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents InfoToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents HelpToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ToolStripItemExtender As Stax.UI.Design.ToolStripItemExtender

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.bOK = New System.Windows.Forms.Button
        Me.bCancel = New System.Windows.Forms.Button
        Me.gbSubtitles = New System.Windows.Forms.GroupBox
        Me.bDel = New System.Windows.Forms.Button
        Me.lvSubtitles = New System.Windows.Forms.ListView
        Me.chLanguage = New System.Windows.Forms.ColumnHeader
        Me.chName = New System.Windows.Forms.ColumnHeader
        Me.chPath = New System.Windows.Forms.ColumnHeader
        Me.bAddSubtitle = New System.Windows.Forms.Button
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.gbChapters = New System.Windows.Forms.GroupBox
        Me.tbChapters = New System.Windows.Forms.TextBox
        Me.bChapters = New System.Windows.Forms.Button
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.bTools = New System.Windows.Forms.Button
        Me.cmsTools = New System.Windows.Forms.ContextMenuStrip(Me.components)
        Me.ShowCommandLineToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.InfoToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.HelpToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
        Me.ToolStripItemExtender = New Stax.UI.Design.ToolStripItemExtender
        Me.gbSubtitles.SuspendLayout()
        Me.gbChapters.SuspendLayout()
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.cmsTools.SuspendLayout()
        CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'bOK
        '
        Me.bOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bOK.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.bOK.Location = New System.Drawing.Point(458, 207)
        Me.bOK.Name = "bOK"
        Me.bOK.Size = New System.Drawing.Size(75, 24)
        Me.bOK.TabIndex = 0
        Me.bOK.Text = "OK"
        '
        'bCancel
        '
        Me.bCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bCancel.Location = New System.Drawing.Point(539, 207)
        Me.bCancel.Name = "bCancel"
        Me.bCancel.Size = New System.Drawing.Size(75, 24)
        Me.bCancel.TabIndex = 1
        Me.bCancel.Text = "Cancel"
        '
        'gbSubtitles
        '
        Me.gbSubtitles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.gbSubtitles.Controls.Add(Me.bDel)
        Me.gbSubtitles.Controls.Add(Me.lvSubtitles)
        Me.gbSubtitles.Controls.Add(Me.bAddSubtitle)
        Me.gbSubtitles.Location = New System.Drawing.Point(12, 12)
        Me.gbSubtitles.Name = "gbSubtitles"
        Me.gbSubtitles.Size = New System.Drawing.Size(602, 123)
        Me.gbSubtitles.TabIndex = 2
        Me.gbSubtitles.TabStop = False
        Me.gbSubtitles.Text = "Subtitles"
        '
        'bDel
        '
        Me.bDel.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bDel.Location = New System.Drawing.Point(542, 48)
        Me.bDel.Name = "bDel"
        Me.bDel.Size = New System.Drawing.Size(54, 23)
        Me.bDel.TabIndex = 3
        Me.bDel.Text = "Del"
        Me.bDel.UseVisualStyleBackColor = True
        '
        'lvSubtitles
        '
        Me.lvSubtitles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lvSubtitles.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.chLanguage, Me.chName, Me.chPath})
        Me.lvSubtitles.FullRowSelect = True
        Me.lvSubtitles.Location = New System.Drawing.Point(6, 19)
        Me.lvSubtitles.Name = "lvSubtitles"
        Me.lvSubtitles.Size = New System.Drawing.Size(530, 98)
        Me.lvSubtitles.TabIndex = 2
        Me.lvSubtitles.UseCompatibleStateImageBehavior = False
        Me.lvSubtitles.View = System.Windows.Forms.View.Details
        '
        'chLanguage
        '
        Me.chLanguage.Text = "Language"
        Me.chLanguage.Width = 102
        '
        'chName
        '
        Me.chName.Text = "Name"
        Me.chName.Width = 104
        '
        'chPath
        '
        Me.chPath.Text = "Path"
        Me.chPath.Width = 315
        '
        'bAddSubtitle
        '
        Me.bAddSubtitle.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bAddSubtitle.Location = New System.Drawing.Point(542, 19)
        Me.bAddSubtitle.Name = "bAddSubtitle"
        Me.bAddSubtitle.Size = New System.Drawing.Size(54, 23)
        Me.bAddSubtitle.TabIndex = 1
        Me.bAddSubtitle.Text = "Add"
        Me.bAddSubtitle.UseVisualStyleBackColor = True
        '
        'gbChapters
        '
        Me.gbChapters.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.gbChapters.Controls.Add(Me.tbChapters)
        Me.gbChapters.Controls.Add(Me.bChapters)
        Me.gbChapters.Location = New System.Drawing.Point(12, 141)
        Me.gbChapters.Name = "gbChapters"
        Me.gbChapters.Size = New System.Drawing.Size(602, 60)
        Me.gbChapters.TabIndex = 5
        Me.gbChapters.TabStop = False
        Me.gbChapters.Text = "Chapters"
        '
        'tbChapters
        '
        Me.tbChapters.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbChapters.Location = New System.Drawing.Point(6, 23)
        Me.tbChapters.Name = "tbChapters"
        Me.tbChapters.Size = New System.Drawing.Size(560, 20)
        Me.tbChapters.TabIndex = 1
        Me.TipProvider.SetTipName(Me.tbChapters, "Chapters")
        Me.TipProvider.SetTipText(Me.tbChapters, "A chapter file can be created using DVD Decrypter.")
        '
        'bChapters
        '
        Me.bChapters.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bChapters.Image = Global.StaxRip.My.Resources.Resources.OpenFile16
        Me.bChapters.Location = New System.Drawing.Point(572, 21)
        Me.bChapters.Name = "bChapters"
        Me.bChapters.Size = New System.Drawing.Size(24, 24)
        Me.bChapters.TabIndex = 0
        Me.bChapters.UseVisualStyleBackColor = True
        '
        'bTools
        '
        Me.bTools.Location = New System.Drawing.Point(361, 207)
        Me.bTools.Name = "bTools"
        Me.bTools.Size = New System.Drawing.Size(75, 24)
        Me.bTools.TabIndex = 8
        Me.bTools.Text = "Tools"
        Me.bTools.UseVisualStyleBackColor = True
        '
        'cmsTools
        '
        Me.cmsTools.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ShowCommandLineToolStripMenuItem, Me.InfoToolStripMenuItem, Me.HelpToolStripMenuItem})
        Me.cmsTools.Name = "cmsTools"
        Me.cmsTools.Size = New System.Drawing.Size(196, 92)
        '
        'ShowCommandLineToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.ShowCommandLineToolStripMenuItem, System.Windows.Forms.Keys.F5)
        Me.ShowCommandLineToolStripMenuItem.Name = "ShowCommandLineToolStripMenuItem"
        Me.ShowCommandLineToolStripMenuItem.Size = New System.Drawing.Size(195, 22)
        Me.ShowCommandLineToolStripMenuItem.Text = "Show Command Line..."
        '
        'InfoToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.InfoToolStripMenuItem, System.Windows.Forms.Keys.F2)
        Me.InfoToolStripMenuItem.Name = "InfoToolStripMenuItem"
        Me.InfoToolStripMenuItem.Size = New System.Drawing.Size(195, 22)
        Me.InfoToolStripMenuItem.Text = "Info..."
        '
        'HelpToolStripMenuItem
        '
        Me.ToolStripItemExtender.SetKeyData(Me.HelpToolStripMenuItem, System.Windows.Forms.Keys.F1)
        Me.HelpToolStripMenuItem.Name = "HelpToolStripMenuItem"
        Me.HelpToolStripMenuItem.Size = New System.Drawing.Size(195, 22)
        Me.HelpToolStripMenuItem.Text = "Help"
        '
        'ToolStripItemExtender
        '
        Me.ToolStripItemExtender.Owner = Me
        '
        'MP4BoxForm
        '
        Me.AcceptButton = Me.bOK
        Me.CancelButton = Me.bCancel
        Me.ClientSize = New System.Drawing.Size(626, 243)
        Me.Controls.Add(Me.gbChapters)
        Me.Controls.Add(Me.bTools)
        Me.Controls.Add(Me.gbSubtitles)
        Me.Controls.Add(Me.bCancel)
        Me.Controls.Add(Me.bOK)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.HelpButton = True
        Me.KeyPreview = True
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "MP4BoxForm"
        Me.ShowIcon = False
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "MP4 Container Configuration"
        Me.gbSubtitles.ResumeLayout(False)
        Me.gbChapters.ResumeLayout(False)
        Me.gbChapters.PerformLayout()
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.cmsTools.ResumeLayout(False)
        CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Muxer As MP4BoxMuxer

    Public Sub New(ByVal muxer As MP4BoxMuxer)
        MyBase.New()
        InitializeComponent()
        Me.Muxer = muxer
        AddSubtitles(muxer.SRTSubtitles.ToArray)
        tbChapters.Text = muxer.ChapterFile
    End Sub

    Private Sub AddSubtitles(ByVal ParamArray subtitles As SRTSubtitle())
        For Each i As SRTSubtitle In subtitles
            Dim item As ListViewItem = New ListViewItem(New String() {i.Language.Value.ToString, i.Name.Value, i.Path.Value})
            item.Tag = i
            lvSubtitles.Items.Add(item)
        Next
    End Sub

    Private Sub bAddSubtitle_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bAddSubtitle.Click
        Dim f As New SettingTreeViewForm("Subtitle", "SRT subtitles can be created using [[http://zuggy.wz.cz/dvd.php | SubRip]].")
        f.Height = 200
        Dim ss As New SRTSubtitle(p.Audio0.Language.CultureInfo)

        f.SettingTreeView.AddSetting(New TextBoxTreeNodeSetting("Name", "Subtitle", ss.Name, "Name of the SRT subtitle (optional)."))
        f.SettingTreeView.AddSetting(New FileBrowserTreeNodeSetting("Path", "Subtitle", ss.Path, "Path of the SRT subtitle.", "*.srt|*.srt"))
        f.SettingTreeView.AddSetting(New ComboboxTreeNodeSetting(Of Language)("Language", "Subtitle", ss.Language, "Language of the SRT subtitle.", Language.Languages))

        f.SettingTreeView.ExpandNode(Nothing)

        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If File.Exists(ss.Path.Value) Then
                AddSubtitles(ss)
            End If
        End If

        f.Dispose()
    End Sub

    Private Sub bOK_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bOK.Click
        Muxer.SRTSubtitles.Clear()

        For Each i As ListViewItem In lvSubtitles.Items
            Muxer.SRTSubtitles.Add(DirectCast(i.Tag, SRTSubtitle))
        Next

        Muxer.ChapterFile = tbChapters.Text
    End Sub

    Private Sub bDel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bDel.Click
        If lvSubtitles.SelectedItems.Count > 0 Then
            lvSubtitles.SelectedItems(0).Remove()
        End If
    End Sub

    Private Sub MP4BoxForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        If Not s.StringList.Contains(Me.GetType.FullName) Then
            HelpToolStripMenuItem.PerformClick()
            s.StringList.Add(Me.GetType.FullName)
        End If
    End Sub

    Private Sub bChapters_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bChapters.Click
        Dim d As New OpenFileDialog
        d.Filter = "*.txt|*.txt"

        If File.Exists(tbChapters.Text) Then
            d.FileName = tbChapters.Text
        End If

        If d.ShowDialog = Windows.Forms.DialogResult.OK Then
            tbChapters.Text = d.FileName
        End If
    End Sub

    Private Sub tbChapters_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles tbChapters.DoubleClick
        ProcessWrapper.StartFile(tbChapters.Text)
    End Sub

    Private Sub HelpToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles HelpToolStripMenuItem.Click
        Dim f As New HelpForm()

        f.Document.WriteStart(Text)
        f.Document.WriteParagraph("MP4Box is a [[http://en.wikipedia.org/wiki/Container_format|MP4]] [[glossary.htm#muxing|muxer]].")

        Dim l As New StringPairList

        l.Add("-fps", "Applied for raw streams.")
        l.Add("-chap", "Chapter file")
        l.Add(":lang", "Applied for audio and subtitle streams.")
        l.Add(":delay", "Applied in case the delay wasn't handled by the audio profile.")
        l.Add(":name", "Optionally for subtitle streams, optionally for audio streams (can be customized in the audio settings), 'Video' for video streams.")

        f.Document.WriteTable("Supported command line switches", l)
        f.Document.WriteTips(TipProvider.GetTips)

        f.Show()
    End Sub

    Private Sub InfoToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles InfoToolStripMenuItem.Click
        Muxer.ShowInfoDialog()
    End Sub

    Private Sub ShowCommandLineToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ShowCommandLineToolStripMenuItem.Click
        g.ShowCommandLinePreview(Muxer.GetCommandLine)
    End Sub

    Private Sub bTools_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bTools.Click
        cmsTools.Show(bTools, 0, bTools.Height)
    End Sub

    Private Sub bTools_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles bTools.Paint
        ButtonHelp.DrawDropDownGlyph(bTools, e.Graphics, HorizontalAlignment.Right)
    End Sub
End Class