Imports System.ComponentModel

Imports Stax
Imports stax.UI
Imports stax.AppFramework

<ToolboxItem(False)> _
Public Class OptionsPreperationControl
    Inherits DialogControl

#Region " Designer "
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents bRename As System.Windows.Forms.Button
    Friend WithEvents bState As System.Windows.Forms.Button

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.lb = New System.Windows.Forms.ListBox
        Me.bAdd = New System.Windows.Forms.Button
        Me.bRemove = New System.Windows.Forms.Button
        Me.bUp = New System.Windows.Forms.Button
        Me.bDown = New System.Windows.Forms.Button
        Me.bEdit = New System.Windows.Forms.Button
        Me.TipProvider = New Stax.UI.TipProvider(Me.components)
        Me.bRename = New System.Windows.Forms.Button
        Me.bState = New System.Windows.Forms.Button
        Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
        Me.bHelp = New System.Windows.Forms.Button
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'lb
        '
        Me.lb.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.lb.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        Me.lb.FormattingEnabled = True
        Me.lb.IntegralHeight = False
        Me.lb.Location = New System.Drawing.Point(12, 32)
        Me.lb.Margin = New System.Windows.Forms.Padding(3, 3, 3, 7)
        Me.lb.Name = "lb"
        Me.lb.Size = New System.Drawing.Size(202, 233)
        Me.lb.TabIndex = 0
        '
        'bNew
        '
        Me.bAdd.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bAdd.Image = Global.StaxRip.My.Resources.Resources.Plus16
        Me.bAdd.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bAdd.Location = New System.Drawing.Point(220, 62)
        Me.bAdd.Name = "bAdd"
        Me.bAdd.Size = New System.Drawing.Size(75, 24)
        Me.bAdd.TabIndex = 3
        Me.bAdd.Text = "Add..."
        Me.TipProvider.SetTipText(Me.bAdd, "Adds a new application.")
        '
        'bRemove
        '
        Me.bRemove.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bRemove.Image = Global.StaxRip.My.Resources.Resources.Minus16
        Me.bRemove.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bRemove.Location = New System.Drawing.Point(220, 92)
        Me.bRemove.Name = "bRemove"
        Me.bRemove.Size = New System.Drawing.Size(75, 24)
        Me.bRemove.TabIndex = 4
        Me.bRemove.Text = "Remove"
        Me.bRemove.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        Me.TipProvider.SetTipText(Me.bRemove, "Removes the selected application.")
        '
        'bUp
        '
        Me.bUp.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bUp.Image = Global.StaxRip.My.Resources.Resources.Up16
        Me.bUp.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bUp.Location = New System.Drawing.Point(220, 122)
        Me.bUp.Name = "bUp"
        Me.bUp.Size = New System.Drawing.Size(75, 24)
        Me.bUp.TabIndex = 2
        Me.bUp.Text = "Up"
        Me.TipProvider.SetTipText(Me.bUp, "Moves the selected application up.")
        '
        'bDown
        '
        Me.bDown.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bDown.Image = Global.StaxRip.My.Resources.Resources.Down16
        Me.bDown.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bDown.Location = New System.Drawing.Point(220, 152)
        Me.bDown.Name = "bDown"
        Me.bDown.Size = New System.Drawing.Size(75, 24)
        Me.bDown.TabIndex = 1
        Me.bDown.Text = "Down"
        Me.TipProvider.SetTipText(Me.bDown, "Moves the selected application down.")
        '
        'bEdit
        '
        Me.bEdit.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bEdit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bEdit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.bEdit.Location = New System.Drawing.Point(220, 31)
        Me.bEdit.Name = "bEdit"
        Me.bEdit.Size = New System.Drawing.Size(75, 24)
        Me.bEdit.TabIndex = 5
        Me.bEdit.Text = "Edit..."
        Me.bEdit.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        Me.TipProvider.SetTipText(Me.bEdit, "Configures the selected application.")
        '
        'bRename
        '
        Me.bRename.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bRename.Location = New System.Drawing.Point(220, 182)
        Me.bRename.Name = "bRename"
        Me.bRename.Size = New System.Drawing.Size(75, 24)
        Me.bRename.TabIndex = 8
        Me.bRename.Text = "Rename..."
        Me.TipProvider.SetTipText(Me.bRename, "Renames the selected application.")
        Me.bRename.UseVisualStyleBackColor = True
        '
        'bState
        '
        Me.bState.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bState.Location = New System.Drawing.Point(220, 212)
        Me.bState.Name = "bState"
        Me.bState.Size = New System.Drawing.Size(75, 24)
        Me.bState.TabIndex = 9
        Me.bState.Text = "State"
        Me.TipProvider.SetTipText(Me.bState, "Enables or disables the selected application.")
        Me.bState.UseVisualStyleBackColor = True
        '
        'bHelp
        '
        Me.bHelp.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bHelp.Location = New System.Drawing.Point(220, 242)
        Me.bHelp.Name = "bHelp"
        Me.bHelp.Size = New System.Drawing.Size(75, 24)
        Me.bHelp.TabIndex = 7
        Me.bHelp.Text = "Help..."
        Me.bHelp.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText
        '
        'OptionsPreperationControl
        '
        Me.Controls.Add(Me.bState)
        Me.Controls.Add(Me.bRename)
        Me.Controls.Add(Me.bHelp)
        Me.Controls.Add(Me.lb)
        Me.Controls.Add(Me.bAdd)
        Me.Controls.Add(Me.bDown)
        Me.Controls.Add(Me.bEdit)
        Me.Controls.Add(Me.bRemove)
        Me.Controls.Add(Me.bUp)
        Me.Name = "OptionsPreperationControl"
        Me.Padding = New System.Windows.Forms.Padding(8, 28, 8, 8)
        Me.Size = New System.Drawing.Size(306, 280)
        Me.Text = "Text"
        Me.UseVisualStyle = True
        CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents lb As System.Windows.Forms.ListBox
    Friend WithEvents bAdd As System.Windows.Forms.Button
    Friend WithEvents bRemove As System.Windows.Forms.Button
    Friend WithEvents bUp As System.Windows.Forms.Button
    Friend WithEvents bDown As System.Windows.Forms.Button
    Friend WithEvents bEdit As System.Windows.Forms.Button
    Friend WithEvents TipProvider As Stax.UI.TipProvider
    Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
    Friend WithEvents bHelp As System.Windows.Forms.Button
#End Region

    Public Sub New()
        InitializeComponent()

        lb.Items.AddRange(CType(ObjectHelp.GetCopy(p.Preparers), List(Of Preparer)).ToArray)

        If lb.Items.Count > 0 Then
            lb.SelectedIndex = 0
        End If

        UpdateControls()
    End Sub

    Private Sub bAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bAdd.Click
        Dim ib As New SelectionBox(Of Preparer)
        ib.Title = "New Preparer"
        ib.Text = "Please select a preparer."

        For Each i As Preparer In Preparer.GetDefaults(True)
            ib.AddItem(i)
        Next

        If ib.Show = DialogResult.OK AndAlso ib.SelecteItem.ShowConfigDialog = DialogResult.OK Then
            lb.Items.Add(ib.SelecteItem)
            lb.SelectedItem = ib.SelecteItem
        End If
    End Sub

    Private Sub OptionsPreperation_FormClosingOK() Handles MyBase.FormClosingOK
        p.Preparers.Clear()

        For Each i As Preparer In lb.Items
            p.Preparers.Add(i)
        Next
    End Sub

    Private Sub UpdateControls()
        Dim p As Preparer = DirectCast(lb.SelectedItem, Preparer)

        bRemove.Enabled = Not lb.SelectedItem Is Nothing
        bUp.Enabled = ListBoxHelp.CanMoveUp(lb)
        bDown.Enabled = ListBoxHelp.CanMoveDown(lb)
        bEdit.Enabled = Not lb.SelectedItem Is Nothing AndAlso p.HasConfigDialog
        bRename.Enabled = Not lb.SelectedItem Is Nothing
        bState.Enabled = Not lb.SelectedItem Is Nothing

        If Not p Is Nothing Then
            If p.Active Then
                bState.Text = "Disable"
            Else
                bState.Text = "Enable"
            End If
        End If
    End Sub

    Private Sub lb_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles lb.DoubleClick
        If lb.SelectedItem IsNot Nothing Then
            bEdit.PerformClick()
        End If
    End Sub

    Private Sub lb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lb.SelectedIndexChanged
        UpdateControls()
    End Sub

    Private Sub bRemove_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bRemove.Click
        If Msg.Show("Please confirm to remove '" + lb.SelectedItem.ToString + "'.", "Remove", _
            MsgIcon.Question, MessageBoxButtons.OKCancel) = DialogResult.OK Then

            ListBoxHelp.RemoveSelection(lb)
        End If
    End Sub

    Private Sub bUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bUp.Click
        ListBoxHelp.MoveSelectionUp(lb)
        UpdateControls()
    End Sub

    Private Sub bDown_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bDown.Click
        ListBoxHelp.MoveSelectionDown(lb)
        UpdateControls()
    End Sub

    Private Sub bConfig_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bEdit.Click
        DirectCast(lb.SelectedItem, Preparer).ShowConfigDialog()
    End Sub

    Private Sub bHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bHelp.Click
        HelpForm.ShowDialog("Preparation", TipProvider.GetTips, "The preparation menu defines a set of applications that are executed to prepare source files. The applications are executed one by one starting from the top so the next application can use the output from the previous application as input. The most commonly task is separating a source file into separate audio and video files.")
    End Sub

    Private Sub bRename_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bRename.Click
        Dim p As Preparer = DirectCast(lb.SelectedItem, Preparer)
        Dim ret As String = InputBox.Show("Please enter the new name.", "Rename Preparer", p.Name)

        If ret <> "" Then
            p.Name = ret
            lb.Items(lb.SelectedIndex) = lb.SelectedItem
        End If
    End Sub

    Private Sub lb_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles lb.DrawItem
        e.DrawBackground()
        Dim p As Preparer = DirectCast(lb.Items(e.Index), Preparer)
        e.Graphics.DrawString(p.Name, e.Font, Brushes.Black, e.Bounds)

        If Not p.Active Then
            e.Graphics.DrawLine(Pens.Black, e.Bounds.Left, e.Bounds.Top + e.Bounds.Height \ 2 + 1, e.Bounds.Left + e.Bounds.Width, e.Bounds.Top + e.Bounds.Height \ 2 + 1)
        End If

        e.DrawFocusRectangle()
    End Sub

    Private Sub bState_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bState.Click
        Dim p As Preparer = DirectCast(lb.SelectedItem, Preparer)
        p.Active = Not p.Active
        lb.Refresh()
        UpdateControls()
    End Sub
End Class