OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmMain.cs
index 70b8c48..bf00e22 100644 (file)
@@ -1,5 +1,6 @@
 using System;\r
 using System.Collections.Generic;\r
+using System.Collections.Specialized;\r
 using System.ComponentModel;\r
 using System.Data;\r
 using System.Drawing;\r
@@ -7,68 +8,189 @@ using System.Text;
 using System.Windows.Forms;\r
 using System.Net;\r
 using System.IO;\r
+using System.Diagnostics;\r
+using System.Threading;\r
+using System.Runtime.InteropServices;\r
+using System.Globalization;\r
+using System.Text.RegularExpressions;\r
+\r
 \r
 namespace Handbrake\r
 {\r
     public partial class frmMain : Form\r
     {\r
+        // -------------------------------------------------------------- \r
+        // Applicaiton Startup Stuff\r
+        // --------------------------------------------------------------\r
+\r
+        #region Application Startup\r
+\r
+        // Some stuff that needs to be Initialized. \r
+        private Process hbProc;\r
+        private Parsing.DVD thisDVD;\r
+        private frmQueue queueWindow = new frmQueue();\r
+\r
+        // The main window beings here...\r
         public frmMain()\r
         {\r
+            // Load the splash screen in this thread\r
+            Form splash = new frmSplashScreen();\r
+            splash.Show();\r
+\r
+            //Create a label that can be updated from the parent thread.\r
+            Label lblStatus = new Label();\r
+\r
+            //Size the label\r
+            lblStatus.Size = new Size(250, 20);\r
+\r
+            //Position the label\r
+            lblStatus.Location = new Point(10, 280);\r
+\r
+            //Put the label on the splash form\r
+            splash.Controls.Add(lblStatus);\r
+\r
+            //Fire a thread to wait for 2 seconds.  The splash screen will exit when the time expires\r
+            Thread timer = new Thread(splashTimer);\r
+            timer.Start();\r
+\r
             InitializeComponent();\r
-        }\r
 \r
-        // --------------------------------------------------------------\r
-        // onLoad - setup the program ready for use.\r
-        // --------------------------------------------------------------\r
-        private void frmMain_Load(object sender, EventArgs e)\r
-        {\r
+            // show the form, but leave disabled until preloading is complete\r
+            this.Enabled = false;\r
+\r
+            // Show the main form\r
+            this.Show();\r
+\r
+            // Forces frmMain to draw\r
+            Application.DoEvents();\r
+\r
             // Set the Version number lable to the corect version.\r
-            Version.Text = "Version " + Properties.Settings.Default.GuiVersion;\r
+            Version.Text = "Version " + Properties.Settings.Default.CliVersion;\r
 \r
+            // update the status\r
+            lblStatus.Text = "Checking for updates ...";\r
+            // redraw the splash screen\r
+            Application.DoEvents();\r
             // Run the update checker.\r
             updateCheck();\r
-\r
-            // Now load the users default if required.\r
+            Thread.Sleep(200);\r
+\r
+            // Update the presets\r
+            lblStatus.Text = "Updaing Presets ...";\r
+            Application.DoEvents();\r
+            updatePresets();\r
+            Thread.Sleep(200);\r
+\r
+            // Now load the users default if required. (Will overide the above setting)\r
+            lblStatus.Text = "Loading User Default Settings...";\r
+            Application.DoEvents();\r
+            loadNormalPreset();\r
             loadUserDefaults();\r
-            \r
+            Thread.Sleep(100);\r
+\r
+            // Enable or disable tooltips\r
+            lblStatus.Text = "Loading Tooltips ...";\r
+            Application.DoEvents();\r
+            tooltip();\r
+            Thread.Sleep(100);\r
+\r
+            // Hide the preset bar if required.\r
+            hidePresetBar();\r
+\r
+            //Finished Loading\r
+            lblStatus.Text = "Loading Complete!";\r
+            Application.DoEvents();\r
+            Thread.Sleep(200);\r
+\r
+            // Wait until splash screen is done\r
+            while (timer.IsAlive)\r
+            { Thread.Sleep(100); }\r
+\r
+            //Close the splash screen\r
+            splash.Close();\r
+            splash.Dispose();\r
+\r
+            // Turn the interface back to the user\r
+            this.Enabled = true;\r
+        }\r
+\r
+        private void splashTimer(object sender)\r
+        {\r
+            Thread.Sleep(2000);  //sit for 2 seconds then exit\r
+        }\r
+\r
+        private void showSplash(object sender)\r
+        {\r
+            // Display splash screen for 1.5 Seconds\r
+            Form splash = new frmSplashScreen();\r
+            splash.Show();\r
+            Thread.Sleep(1500);\r
+            splash.Close(); // Then close.\r
         }\r
 \r
-        public void loadUserDefaults()\r
-        { \r
+        private void loadNormalPreset()\r
+        {\r
+            ListViewItem item = listview_presets.FindItemWithText("Normal");\r
+\r
+            if (item != null)\r
+            {\r
+                listview_presets.SelectedItems.Clear();\r
+                item.Selected = true;\r
+            }\r
+        }\r
+\r
+        private void loadUserDefaults()\r
+        {\r
             try\r
             {\r
+                // Load the users default settings or if the user has not got this option enabled, load the normal preset.\r
                 if (Properties.Settings.Default.defaultSettings == "Checked")\r
                 {\r
-                    //Source\r
+                    // Source\r
                     text_source.Text = Properties.Settings.Default.DVDSource;\r
                     drp_dvdtitle.Text = Properties.Settings.Default.DVDTitle;\r
                     drop_chapterStart.Text = Properties.Settings.Default.ChapterStart;\r
                     drop_chapterFinish.Text = Properties.Settings.Default.ChapterFinish;\r
-                    //Destination\r
+\r
+                    // Destination\r
                     text_destination.Text = Properties.Settings.Default.VideoDest;\r
                     drp_videoEncoder.Text = Properties.Settings.Default.VideoEncoder;\r
                     drp_audioCodec.Text = Properties.Settings.Default.AudioEncoder;\r
                     text_width.Text = Properties.Settings.Default.Width;\r
                     text_height.Text = Properties.Settings.Default.Height;\r
-                    //Picture Settings Tab\r
+\r
+                    // Picture Settings Tab\r
                     drp_crop.Text = Properties.Settings.Default.CroppingOption;\r
                     text_top.Text = Properties.Settings.Default.CropTop;\r
                     text_bottom.Text = Properties.Settings.Default.CropBottom;\r
                     text_left.Text = Properties.Settings.Default.CropLeft;\r
                     text_right.Text = Properties.Settings.Default.CropRight;\r
                     drp_subtitle.Text = Properties.Settings.Default.Subtitles;\r
-                    //Video Settings Tab\r
+\r
+                    // Video Settings Tab\r
                     text_bitrate.Text = Properties.Settings.Default.VideoBitrate;\r
                     text_filesize.Text = Properties.Settings.Default.VideoFilesize;\r
                     slider_videoQuality.Value = Properties.Settings.Default.VideoQuality;\r
+\r
                     if (Properties.Settings.Default.TwoPass == "Checked")\r
                     {\r
                         check_2PassEncode.CheckState = CheckState.Checked;\r
                     }\r
-                    if (Properties.Settings.Default.DeInterlace == "Checked")\r
+\r
+                    drp_deInterlace_option.Text = Properties.Settings.Default.DeInterlace;\r
+                    drp_deNoise.Text = Properties.Settings.Default.denoise;\r
+\r
+                    if (Properties.Settings.Default.detelecine == "Checked")\r
+                    {\r
+                        check_detelecine.CheckState = CheckState.Checked;\r
+                    }\r
+\r
+                    if (Properties.Settings.Default.detelecine == "Checked")\r
                     {\r
-                        check_DeInterlace.CheckState = CheckState.Checked;\r
+                        check_deblock.CheckState = CheckState.Checked;\r
                     }\r
+\r
+\r
                     if (Properties.Settings.Default.Grayscale == "Checked")\r
                     {\r
                         check_grayscale.CheckState = CheckState.Checked;\r
@@ -88,16 +210,25 @@ namespace Handbrake
                     {\r
                         check_largeFile.CheckState = CheckState.Checked;\r
                     }\r
-                    //Audio Settings Tab\r
+\r
+                    if (Properties.Settings.Default.chapterMarker == "Checked")\r
+                    {\r
+                        Check_ChapterMarkers.CheckState = CheckState.Checked;\r
+                    }\r
+\r
+                    // Audio Settings Tab\r
                     drp_audioBitrate.Text = Properties.Settings.Default.AudioBitrate;\r
                     drp_audioSampleRate.Text = Properties.Settings.Default.AudioSampleRate;\r
                     drp_audioChannels.Text = Properties.Settings.Default.AudioChannels;\r
-                    //H264 Tab\r
+\r
+                    // H264 Tab\r
                     if (Properties.Settings.Default.CRF == "Checked")\r
                     {\r
                         CheckCRF.CheckState = CheckState.Checked;\r
                     }\r
                     rtf_h264advanced.Text = Properties.Settings.Default.H264;\r
+\r
+                    groupBox_output.Text = "Output Settings (Preset: " + Properties.Settings.Default.selectedPreset + ")";\r
                 }\r
             }\r
             catch (Exception)\r
@@ -106,183 +237,104 @@ namespace Handbrake
             }\r
         }\r
 \r
-        public void updateCheck()\r
+        private Boolean updateCheck()\r
         {\r
-            if (Properties.Settings.Default.updateStatus == "Checked")\r
+            try\r
             {\r
-\r
-                try\r
+                if (Properties.Settings.Default.updateStatus == "Checked")\r
                 {\r
                     String updateFile = Properties.Settings.Default.updateFile;\r
                     WebClient client = new WebClient();\r
                     String data = client.DownloadString(updateFile);\r
                     String[] versionData = data.Split('\n');\r
 \r
-                    if ((versionData[0] != Properties.Settings.Default.GuiVersion) || (versionData[1] != Properties.Settings.Default.CliVersion))\r
-                    {\r
-                        lbl_update.Visible = true;\r
-                    }\r
+                    int verdata = int.Parse(versionData[0].Replace(".", ""));\r
+                    int vergui = int.Parse(Properties.Settings.Default.GuiVersion.Replace(".", ""));\r
+                    int verd1 = int.Parse(versionData[1].Replace(".", ""));\r
+                    int cliversion = int.Parse(Properties.Settings.Default.CliVersion.Replace(".", ""));\r
+\r
+                    Boolean update = ((verdata > vergui) || (verd1 > cliversion));\r
+\r
+                    lbl_update.Visible = update;\r
+\r
+                    return update;\r
                 }\r
-                //else fail displaying an error message.\r
-                catch (Exception)\r
+                else\r
                 {\r
-                    //Silently ignore the error\r
+                    return false;\r
                 }\r
             }\r
+            catch (Exception)\r
+            {\r
+                // Silently ignore the error\r
+                return false;\r
+            }\r
         }\r
 \r
+        private void tooltip()\r
+        {\r
+            if (Properties.Settings.Default.tooltipEnable == "Checked")\r
+            {\r
+                ToolTip.Active = true;\r
+            }\r
+        }\r
 \r
+        private void hidePresetBar()\r
+        {\r
+            if (Properties.Settings.Default.hidePresets == "Checked")\r
+            {\r
+                btn_presets.Visible = true;\r
+                this.Width = 591;\r
+            }\r
 \r
-        // --------------------------------------------------------------\r
-        // The Menu Bar\r
-        // --------------------------------------------------------------\r
+        }\r
 \r
-        // FILE MENU --------------------------------------------------------------\r
+        #endregion\r
+\r
+        // -------------------------------------------------------------- \r
+        // The main Menu bar.\r
+        // -------------------------------------------------------------- \r
+\r
+        #region File Menu\r
         private void mnu_open_Click(object sender, EventArgs e)\r
         {\r
             string filename;\r
             File_Open.ShowDialog();\r
             filename = File_Open.FileName;\r
+\r
             if (filename != "")\r
             {\r
                 try\r
                 {\r
-                    // Create StreamReader & open file\r
-                    StreamReader line = new StreamReader(filename);\r
-                    string temporyLine; // Used for reading the line into a varible before processing on the checkState items below.\r
-                    \r
-                    // Read in the data and set the correct GUI component with the setting.\r
-                    text_source.Text = line.ReadLine();\r
-                    drp_dvdtitle.Text = line.ReadLine();\r
-                    drop_chapterStart.Text = line.ReadLine();\r
-                    drop_chapterFinish.Text = line.ReadLine();\r
-                    text_destination.Text = line.ReadLine();\r
-                    drp_videoEncoder.Text = line.ReadLine();\r
-                    drp_audioCodec.Text = line.ReadLine();\r
-                    text_width.Text = line.ReadLine();\r
-                    text_height.Text = line.ReadLine();\r
-                    text_top.Text = line.ReadLine();\r
-                    text_bottom.Text = line.ReadLine();\r
-                    text_left.Text = line.ReadLine();\r
-                    text_right.Text = line.ReadLine();\r
-                    drp_subtitle.Text = line.ReadLine();\r
-                    text_bitrate.Text = line.ReadLine();\r
-                    text_filesize.Text = line.ReadLine();\r
-                    slider_videoQuality.Value = int.Parse(line.ReadLine());\r
-\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_2PassEncode.CheckState = CheckState.Checked;\r
-                    }\r
-\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_DeInterlace.CheckState = CheckState.Checked;\r
-                    }\r
-\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_grayscale.CheckState = CheckState.Checked;\r
-                    }\r
+                    // Some things that need to be done to reset some gui components:\r
+                    CheckPixelRatio.CheckState = CheckState.Unchecked;\r
 \r
-                    drp_videoFramerate.Text = line.ReadLine();\r
-\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        Check_ChapterMarkers.CheckState = CheckState.Checked;\r
-                    }\r
+                    //---------------------------\r
+                    // Get the Preset\r
+                    // ---------------------------\r
 \r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        CheckPixelRatio.CheckState = CheckState.Checked;\r
-                    }\r
+                    // Create StreamReader & open file\r
+                    StreamReader line = new StreamReader(filename);\r
 \r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_turbo.CheckState = CheckState.Checked;\r
-                    }\r
+                    // Send the query from the file to the Query Parser class\r
+                    Functions.QueryParser presetQuery = Functions.QueryParser.Parse(line.ReadLine());\r
 \r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_largeFile.CheckState = CheckState.Checked;\r
-                    }\r
-   \r
-                    drp_audioBitrate.Text = line.ReadLine();\r
-                    drp_audioSampleRate.Text = line.ReadLine();\r
-                    drp_audioChannels.Text = line.ReadLine();\r
-                    drp_audioMixDown.Text = line.ReadLine();\r
-                    \r
-                    // Advanced H264 Options\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        CheckCRF.CheckState = CheckState.Checked;\r
-                    }\r
-                    rtf_h264advanced.Text = line.ReadLine();\r
+                    // Now load the preset\r
+                    presetLoader(presetQuery, filename);\r
 \r
                     // Close the stream\r
                     line.Close();\r
-\r
-\r
-                    // Fix for SliderValue not appearing when Opening saved file\r
-                    SliderValue.Text = slider_videoQuality.Value + "%";\r
-\r
-                } catch (Exception){\r
+                }\r
+                catch (Exception exc)\r
+                {\r
                     MessageBox.Show("Unable to load profile.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                    MessageBox.Show(exc.ToString());\r
                 }\r
             }\r
         }\r
 \r
         private void mnu_save_Click(object sender, EventArgs e)\r
         {\r
-            // Note : All these declarations are not really needed and should be removed at some point.\r
-            // The values can simply be added directly to the WriteLine statments.\r
-                \r
-            //Source\r
-            string source = text_source.Text;\r
-            string dvdTitle = drp_dvdtitle.Text;\r
-            string ChapterStart = drop_chapterStart.Text;\r
-            string ChapterFinish = drop_chapterFinish.Text;\r
-            //Destination\r
-            string destination = text_destination.Text;\r
-            string videoEncoder = drp_videoEncoder.Text;\r
-            string audioEncoder = drp_audioCodec.Text;\r
-            string width = text_width.Text;\r
-            string height = text_height.Text;\r
-            //Picture Settings Tab\r
-            string cropTop = text_top.Text;\r
-            string cropBottom = text_bottom.Text;\r
-            string cropLeft = text_left.Text;\r
-            string cropRight = text_right.Text;\r
-            string subtitles = drp_subtitle.Text;\r
-            //Video Settings Tab\r
-            string videoBitrate = text_bitrate.Text;\r
-            string videoFilesize = text_filesize.Text;\r
-            string videoQuality = slider_videoQuality.Value.ToString();\r
-            string twoPassEncoding = check_2PassEncode.CheckState.ToString();\r
-            string deinterlace = check_DeInterlace.CheckState.ToString();\r
-            string grayscale = check_grayscale.CheckState.ToString();\r
-            string videoFramerate = drp_videoFramerate.Text;\r
-            string pixelRation = CheckPixelRatio.CheckState.ToString();\r
-            string ChapterMarkers = Check_ChapterMarkers.CheckState.ToString();\r
-            string turboH264 = check_turbo.CheckState.ToString();\r
-            string largeFile = check_largeFile.CheckState.ToString();\r
-            //Audio Settings Tab\r
-            string audioBitrate = drp_audioBitrate.Text;\r
-            string audioSampleRate = drp_audioSampleRate.Text;\r
-            string audioChannels = drp_audioChannels.Text;\r
-            string AudioMixDown = drp_audioMixDown.Text;\r
-            //H264 Tab\r
-            string CRF = CheckCRF.CheckState.ToString();\r
-            string advH264 = rtf_h264advanced.Text;\r
-\r
             string filename;\r
             File_Save.ShowDialog();\r
             filename = File_Save.FileName;\r
@@ -293,241 +345,145 @@ namespace Handbrake
                     // Create a StreamWriter and open the file\r
                     StreamWriter line = new StreamWriter(filename);\r
 \r
-                   line.WriteLine(source);\r
-                   line.WriteLine(dvdTitle);\r
-                   line.WriteLine(ChapterStart);\r
-                   line.WriteLine(ChapterFinish);\r
-                   line.WriteLine(destination);\r
-                   line.WriteLine(videoEncoder);\r
-                   line.WriteLine(audioEncoder);\r
-                   line.WriteLine(width);\r
-                   line.WriteLine(height);\r
-                   line.WriteLine(cropTop);\r
-                   line.WriteLine(cropBottom);\r
-                   line.WriteLine(cropLeft);\r
-                   line.WriteLine(cropRight);\r
-                   line.WriteLine(subtitles);\r
-                   line.WriteLine(videoBitrate);\r
-                   line.WriteLine(videoFilesize);\r
-                   line.WriteLine(videoQuality);\r
-                   line.WriteLine(twoPassEncoding);\r
-                   line.WriteLine(deinterlace);\r
-                   line.WriteLine(grayscale);\r
-                   line.WriteLine(videoFramerate);\r
-                   line.WriteLine(ChapterMarkers);\r
-                   line.WriteLine(pixelRation);\r
-                   line.WriteLine(turboH264);\r
-                   line.WriteLine(largeFile);\r
-                   line.WriteLine(audioBitrate);\r
-                   line.WriteLine(audioSampleRate);\r
-                   line.WriteLine(audioChannels);\r
-                   line.WriteLine(AudioMixDown);\r
-                   line.WriteLine(CRF);\r
-                   line.WriteLine(advH264);\r
-                   // close the stream\r
-                   line.Close();\r
-                   MessageBox.Show("Your profile has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
+                    // Generate and write the query string to the file\r
+                    String query = GenerateTheQuery();\r
+                    line.WriteLine(query);\r
+\r
+                    // close the stream\r
+                    line.Close();\r
+                    MessageBox.Show("Your profile has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
                 }\r
-                catch(Exception)\r
+                catch (Exception)\r
                 {\r
                     MessageBox.Show("Unable to write to the file. Please make sure the location has the correct permissions for file writing.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
                 }\r
-                \r
-            }\r
-        }\r
 \r
-        private void mnu_update_Click(object sender, EventArgs e)\r
-        {\r
-            Form Update = new frmUpdate();\r
-            Update.Show();\r
+            }\r
         }\r
 \r
         private void mnu_exit_Click(object sender, EventArgs e)\r
         {\r
-            this.Close();\r
+            Application.Exit();\r
         }\r
 \r
-        // TOOLS MENU --------------------------------------------------------------\r
+        #endregion\r
+\r
+        #region Tools Menu\r
+\r
         private void mnu_encode_Click(object sender, EventArgs e)\r
         {\r
-            Form Queue = new frmQueue();\r
-            Queue.Show();\r
+            showQueue();\r
         }\r
 \r
         private void mnu_viewDVDdata_Click(object sender, EventArgs e)\r
         {\r
-            Form DVDData = new frmDVDData();\r
-            DVDData.Show();\r
+            frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
+            dvdInfoWindow.Show();\r
         }\r
 \r
         private void mnu_options_Click(object sender, EventArgs e)\r
         {\r
             Form Options = new frmOptions();\r
-            Options.Show();\r
+            Options.ShowDialog();\r
         }\r
 \r
-        // PRESETS MENU --------------------------------------------------------------\r
-        private void mnu_preset_ipod133_Click(object sender, EventArgs e)\r
-        {\r
-            CheckPixelRatio.CheckState = CheckState.Unchecked;\r
-            text_width.Text = "640";\r
-            text_height.Text = "480";\r
-            drp_videoEncoder.Text = "H.264 (iPod)";\r
-            text_bitrate.Text = "1000";\r
-            text_filesize.Text = "";\r
-            slider_videoQuality.Value = 0;\r
-            SliderValue.Text = "0%";\r
-            drp_audioBitrate.Text = "160";\r
-            rtf_h264advanced.Text = "";\r
-            drp_crop.Text = "No Crop";\r
-        }\r
+        #endregion\r
 \r
-        private void mnu_preset_ipod178_Click(object sender, EventArgs e)\r
-        {\r
-            CheckPixelRatio.CheckState = CheckState.Unchecked;\r
-            text_width.Text = "640";\r
-            text_height.Text = "352";\r
-            drp_videoEncoder.Text = "H.264 (iPod)";\r
-            text_bitrate.Text = "1000";\r
-            text_filesize.Text = "";\r
-            slider_videoQuality.Value = 0;\r
-            SliderValue.Text = "0%";\r
-            drp_audioBitrate.Text = "160";\r
-            rtf_h264advanced.Text = "";\r
-            drp_crop.Text = "No Crop";\r
-        }\r
+        #region Presets Menu\r
 \r
-        private void mnu_preset_ipod235_Click(object sender, EventArgs e)\r
+        private void mnu_presetReset_Click(object sender, EventArgs e)\r
         {\r
-            CheckPixelRatio.CheckState = CheckState.Unchecked;\r
-            text_width.Text = "640";\r
-            text_height.Text = "272";\r
-            drp_videoEncoder.Text = "H.264 (iPod)";\r
-            text_bitrate.Text = "1000";\r
-            text_filesize.Text = "";\r
-            slider_videoQuality.Value = 0;\r
-            SliderValue.Text = "0%";\r
-            drp_audioBitrate.Text = "160";\r
-            rtf_h264advanced.Text = "";\r
-            drp_crop.Text = "No Crop";\r
+            listview_presets.Items.Clear();\r
+            updatePresets();\r
+            MessageBox.Show("Presets have been updated", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
         }\r
 \r
-        private void mnu_appleTv_Click(object sender, EventArgs e)\r
+        private void mnu_SelectDefault_Click(object sender, EventArgs e)\r
         {\r
-            text_width.Text = "";\r
-            text_height.Text = "";\r
-            drp_videoEncoder.Text = "H.264";\r
-            text_bitrate.Text = "3000";\r
-            text_filesize.Text = "";\r
-            slider_videoQuality.Value = 0;\r
-            SliderValue.Text = "0%";\r
-            drp_audioBitrate.Text = "160";\r
-            CheckPixelRatio.CheckState = CheckState.Checked;\r
-            drp_audioSampleRate.Text = "48";\r
-            rtf_h264advanced.Text = "bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:no-dct-decimate=1:trellis=2";\r
-            drp_crop.Text = "No Crop";\r
-            \r
-        }\r
+            ListViewItem item = listview_presets.FindItemWithText("Normal");\r
 \r
-        private void mnu_presetPS3_Click(object sender, EventArgs e)\r
-        {\r
-            CheckPixelRatio.CheckState = CheckState.Unchecked;\r
-            text_width.Text = "";\r
-            text_height.Text = "";\r
-            drp_videoEncoder.Text = "H.264";\r
-            text_bitrate.Text = "3000";\r
-            text_filesize.Text = "";\r
-            slider_videoQuality.Value = 0;\r
-            SliderValue.Text = "0%";\r
-            drp_audioBitrate.Text = "160";\r
-            CheckPixelRatio.CheckState = CheckState.Checked;\r
-            drp_audioSampleRate.Text = "48";\r
-            rtf_h264advanced.Text = "level=41";\r
-            drp_crop.Text = "No Crop";\r
+            if (item != null)\r
+            {\r
+                listview_presets.SelectedItems.Clear();\r
+                item.Selected = true;\r
+            }\r
+\r
+            if (presetStatus == false)\r
+            {\r
+                this.Width = 881;\r
+                presetStatus = true;\r
+                btn_presets.Text = "Hide Presets";\r
+            }\r
         }\r
 \r
-        private void mnu_ProgramDefaultOptions_Click(object sender, EventArgs e)\r
+        #endregion\r
+\r
+        #region Help Menu\r
+\r
+\r
+        private void mnu_quickStart_Click(object sender, EventArgs e)\r
         {\r
-            //Source\r
-            Properties.Settings.Default.DVDSource = text_source.Text;\r
-            Properties.Settings.Default.DVDTitle = drp_dvdtitle.Text;\r
-            Properties.Settings.Default.ChapterStart = drop_chapterStart.Text;\r
-            Properties.Settings.Default.ChapterFinish = drop_chapterFinish.Text;\r
-            //Destination\r
-            Properties.Settings.Default.VideoDest = text_destination.Text;\r
-            Properties.Settings.Default.VideoEncoder = drp_videoEncoder.Text;\r
-            Properties.Settings.Default.AudioEncoder = drp_audioCodec.Text;\r
-            Properties.Settings.Default.Width = text_width.Text;\r
-            Properties.Settings.Default.Height = text_height.Text;\r
-            //Picture Settings Tab\r
-            Properties.Settings.Default.CroppingOption = drp_crop.Text;\r
-            Properties.Settings.Default.CropTop = text_top.Text;\r
-            Properties.Settings.Default.CropBottom = text_bottom.Text;\r
-            Properties.Settings.Default.CropLeft = text_left.Text;\r
-            Properties.Settings.Default.CropRight = text_right.Text;\r
-            Properties.Settings.Default.Subtitles = drp_subtitle.Text;\r
-            //Video Settings Tab\r
-            Properties.Settings.Default.VideoBitrate = text_bitrate.Text;\r
-            Properties.Settings.Default.VideoFilesize = text_filesize.Text;\r
-            Properties.Settings.Default.VideoQuality = slider_videoQuality.Value;\r
-            Properties.Settings.Default.TwoPass = check_2PassEncode.CheckState.ToString();\r
-            Properties.Settings.Default.DeInterlace = check_DeInterlace.CheckState.ToString();\r
-            Properties.Settings.Default.Grayscale = check_grayscale.CheckState.ToString();\r
-            Properties.Settings.Default.Framerate = drp_videoFramerate.Text;\r
-            Properties.Settings.Default.PixelRatio = CheckPixelRatio.CheckState.ToString();\r
-            Properties.Settings.Default.turboFirstPass = check_turbo.CheckState.ToString();\r
-            Properties.Settings.Default.largeFile = check_largeFile.CheckState.ToString();\r
-            //Audio Settings Tab\r
-            Properties.Settings.Default.AudioBitrate = drp_audioBitrate.Text;\r
-            Properties.Settings.Default.AudioSampleRate = drp_audioSampleRate.Text;\r
-            Properties.Settings.Default.AudioChannels = drp_audioChannels.Text;\r
-            //H264 Tab\r
-            Properties.Settings.Default.CRF = CheckCRF.CheckState.ToString();\r
-            Properties.Settings.Default.H264 = rtf_h264advanced.Text;\r
-            Properties.Settings.Default.Save();\r
+            Form QuickStart = new frmQuickStart();\r
+            QuickStart.ShowDialog();\r
         }\r
 \r
-        // Help Menu --------------------------------------------------------------\r
         private void mnu_wiki_Click(object sender, EventArgs e)\r
         {\r
-            System.Diagnostics.Process.Start("http://handbrake.m0k.org/trac");\r
+            Process.Start("http://handbrake.m0k.org/trac");\r
         }\r
 \r
-        private void mnu_onlineDocs_Click(object sender, EventArgs e)\r
+        private void mnu_faq_Click(object sender, EventArgs e)\r
         {\r
-            System.Diagnostics.Process.Start("http://handbrake.m0k.org/?page_id=11");\r
+            Process.Start("http://handbrake.m0k.org/trac/wiki/WindowsGuiFaq");\r
         }\r
 \r
-        private void mnu_faq_Click(object sender, EventArgs e)\r
+        private void mnu_onlineDocs_Click(object sender, EventArgs e)\r
         {\r
-            System.Diagnostics.Process.Start("http://handbrake.m0k.org/trac/wiki/WindowsGuiFaq");\r
+            Process.Start("http://handbrake.m0k.org/?page_id=11");\r
         }\r
 \r
         private void mnu_homepage_Click(object sender, EventArgs e)\r
         {\r
-            System.Diagnostics.Process.Start("http://handbrake.m0k.org");\r
+            Process.Start("http://handbrake.m0k.org");\r
         }\r
 \r
         private void mnu_forum_Click(object sender, EventArgs e)\r
         {\r
-            System.Diagnostics.Process.Start("http://handbrake.m0k.org/forum");\r
+            Process.Start("http://handbrake.m0k.org/forum");\r
         }\r
 \r
-        private void mnu_about_Click(object sender, EventArgs e)\r
+        private void mnu_UpdateCheck_Click(object sender, EventArgs e)\r
         {\r
-                       Form About = new frmAbout();\r
-            About.Show();\r
+            Boolean update = updateCheck();\r
+            if (update == true)\r
+            {\r
+                MessageBox.Show("There is a new update available. Please visit http://handbrake.m0k.org for details!", "Update Check", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
+            }\r
+            else\r
+            {\r
+                MessageBox.Show("There are no new updates at this time.", "Update Check", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
+            }\r
         }\r
 \r
+        private void mnu_about_Click(object sender, EventArgs e)\r
+        {\r
+            Form About = new frmAbout();\r
+            About.ShowDialog();\r
+        }\r
 \r
+        #endregion\r
 \r
         // -------------------------------------------------------------- \r
         // Buttons on the main Window\r
         // --------------------------------------------------------------\r
+\r
+        #region Buttons\r
+\r
         private void btn_Browse_Click(object sender, EventArgs e)\r
         {\r
-            String filename ="";\r
+            String filename = "";\r
             text_source.Text = "";\r
+            frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
 \r
             if (RadioDVD.Checked)\r
             {\r
@@ -535,11 +491,10 @@ namespace Handbrake
                 filename = DVD_Open.SelectedPath;\r
                 if (filename != "")\r
                 {\r
+                    Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow);\r
                     text_source.Text = filename;\r
-                    Form frmReadDVD = new frmReadDVD(filename);\r
-                    frmReadDVD.Show();\r
+                    frmRD.ShowDialog();\r
                 }\r
-\r
             }\r
             else\r
             {\r
@@ -547,24 +502,38 @@ namespace Handbrake
                 filename = ISO_Open.FileName;\r
                 if (filename != "")\r
                 {\r
+                    Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow);\r
                     text_source.Text = filename;\r
-                    Form frmReadDVD = new frmReadDVD(filename);\r
-                    frmReadDVD.Show();\r
+                    frmRD.ShowDialog();\r
                 }\r
+            }\r
 \r
+            // Check if there was titles in the dvd title dropdown \r
+            if (filename == "")\r
+            {\r
+                text_source.Text = "Click 'Browse' to continue";\r
             }\r
 \r
-                \r
+            // If there are no titles in the dropdown menu then the scan has obviously failed. Display an error message explaining to the user.\r
+            if (drp_dvdtitle.Items.Count == 0)\r
+            {\r
+                MessageBox.Show("No Title(s) found. Please make sure you have selected a valid, non-copy protected source. Please refer to the FAQ (see Help Menu).", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+            }\r
         }\r
 \r
         private void btn_destBrowse_Click(object sender, EventArgs e)\r
         {\r
-            // TODO: Need to write some code to check if there is a reasonable amount of disk space left.\r
+            // This removes the file extension from the filename box on the save file dialog.\r
+            // It's daft but some users don't realise that typing an extension overrides the dropdown extension selected.\r
+            // Should be a nicer way to do this.\r
+            DVD_Save.FileName = DVD_Save.FileName.Replace(".mp4", "").Replace(".m4v", "").Replace(".mkv", "").Replace(".ogm", "").Replace(".avi", "");\r
 \r
+            // Show the dialog and set the main form file path\r
             DVD_Save.ShowDialog();\r
             text_destination.Text = DVD_Save.FileName;\r
 \r
-            if (Check_ChapterMarkers.CheckState.ToString() == "Checked")\r
+            // Quicktime requires .m4v file for chapter markers to work. If checked, change the extension to .m4v (mp4 and m4v are the same thing)\r
+            if (Check_ChapterMarkers.Checked)\r
             {\r
                 string destination = text_destination.Text;\r
                 destination = destination.Replace(".mp4", ".m4v");\r
@@ -590,84 +559,406 @@ namespace Handbrake
 \r
         private void btn_queue_Click(object sender, EventArgs e)\r
         {\r
-            Form Queue = new frmQueue();\r
-            Queue.Show();\r
-        }\r
 \r
-        private void btn_encode_Click(object sender, EventArgs e)\r
-        {\r
-            String query = "";\r
\r
-            if (QueryEditorText.Text == "")\r
-            {\r
-                query = GenerateTheQuery();\r
-                MessageBox.Show(query);\r
-            }\r
+            if (text_source.Text == "" || text_source.Text == "Click 'Browse' to continue" || text_destination.Text == "")\r
+                MessageBox.Show("No source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
             else\r
             {\r
-                query = QueryEditorText.Text;\r
-            }\r
-\r
-            System.Diagnostics.Process hbProc = new System.Diagnostics.Process();\r
-            hbProc.StartInfo.FileName = "hbcli.exe";\r
-            hbProc.StartInfo.Arguments = query;\r
-            hbProc.StartInfo.UseShellExecute = false;\r
-            hbProc.Start();\r
-            \r
+                string query;\r
+                if (QueryEditorText.Text == "")\r
+                {\r
+                    query = GenerateTheQuery();\r
+                }\r
+                else\r
+                {\r
+                    query = QueryEditorText.Text;\r
+                }\r
+                queueWindow.list_queue.Items.Add(query);\r
+                queueWindow.Show();\r
+            }\r
+        }\r
 \r
-            MessageBox.Show("The encode process has now started.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
-       \r
-            // TODO: Need to write a bit of code here to do process monitoring.\r
-            // Note: hbProc.waitForExit will freeze the app, meaning one cannot add additional items to the queue during an encode.\r
+        private void btn_copy_Click(object sender, EventArgs e)\r
+        {\r
+            if (QueryEditorText.Text != "")\r
+                Clipboard.SetText(QueryEditorText.Text, TextDataFormat.Text);\r
         }\r
 \r
+        private void showQueue()\r
+        {\r
+            queueWindow.Show();\r
+        }\r
+\r
+        #endregion\r
+\r
         // -------------------------------------------------------------- \r
-        // Items that require actions on frmMain\r
+        // Main Window Preset System\r
         // --------------------------------------------------------------\r
 \r
+        #region Preset System\r
+\r
+        // Import Current Presets\r
+        private void updatePresets()\r
+        {\r
+            string[] presets = new string[17];\r
+            presets[0] = "Animation";\r
+            presets[1] = "AppleTV";\r
+            presets[2] = "Bedlam";\r
+            presets[3] = "Blind";\r
+            presets[4] = "Broke";\r
+            presets[5] = "Classic";\r
+            presets[6] = "Constant Quality Rate";\r
+            presets[7] = "Deux Six Quatre";\r
+            presets[8] = "Film";\r
+            presets[9] = "iPhone / iPod Touch";\r
+            presets[10] = "iPod High-Rez";\r
+            presets[11] = "iPod Low-Rez";\r
+            presets[12] = "Normal";\r
+            presets[13] = "PS3";\r
+            presets[14] = "PSP";\r
+            presets[15] = "QuickTime";\r
+            presets[16] = "Television";\r
+\r
+            ListViewItem preset_listview = new ListViewItem();\r
+            string[] presetList = new string[1];\r
+\r
+            foreach (string preset in presets)\r
+            {\r
+                presetList[0] = preset;\r
+                preset_listview = new ListViewItem(presetList);\r
+\r
+                // Now Fill Out List View with Items\r
+                listview_presets.Items.Add(preset_listview);\r
+            }\r
+\r
+            string appPath = Application.StartupPath.ToString() + "\\";\r
+            string strCmdLine = "cmd /c " + '"' + '"' + appPath + "HandBrakeCLI.exe" + '"' + " --preset-list >" + '"' + appPath + "presets.dat" + '"' + " 2>&1" + '"';\r
+            Process hbproc = Process.Start("CMD.exe", strCmdLine);\r
+            hbproc.WaitForExit();\r
+            hbproc.Dispose();\r
+            hbproc.Close();\r
+\r
+        }\r
+\r
+        // Varibles\r
+        private Boolean presetStatus = false;\r
+\r
+        // Buttons\r
+        private void btn_presets_Click(object sender, EventArgs e)\r
+        {\r
+            if (presetStatus == false)\r
+            {\r
+                this.Width = 881;\r
+                presetStatus = true;\r
+                btn_presets.Text = "Hide Presets";\r
+            }\r
+            else\r
+            {\r
+                this.Width = 590;\r
+                presetStatus = false;\r
+                btn_presets.Text = "Show Presets";\r
+            }\r
+\r
+        }\r
+\r
+        private void btn_setDefault_Click(object sender, EventArgs e)\r
+        {\r
+            //Source\r
+            Properties.Settings.Default.DVDSource = text_source.Text;\r
+            Properties.Settings.Default.DVDTitle = drp_dvdtitle.Text;\r
+            Properties.Settings.Default.ChapterStart = drop_chapterStart.Text;\r
+            Properties.Settings.Default.ChapterFinish = drop_chapterFinish.Text;\r
+            //Destination\r
+            Properties.Settings.Default.VideoDest = text_destination.Text;\r
+            Properties.Settings.Default.VideoEncoder = drp_videoEncoder.Text;\r
+            Properties.Settings.Default.AudioEncoder = drp_audioCodec.Text;\r
+            Properties.Settings.Default.Width = text_width.Text;\r
+            Properties.Settings.Default.Height = text_height.Text;\r
+            //Picture Settings Tab\r
+            Properties.Settings.Default.CroppingOption = drp_crop.Text;\r
+            Properties.Settings.Default.CropTop = text_top.Text;\r
+            Properties.Settings.Default.CropBottom = text_bottom.Text;\r
+            Properties.Settings.Default.CropLeft = text_left.Text;\r
+            Properties.Settings.Default.CropRight = text_right.Text;\r
+            Properties.Settings.Default.Subtitles = drp_subtitle.Text;\r
+            //Video Settings Tab\r
+            Properties.Settings.Default.VideoBitrate = text_bitrate.Text;\r
+            Properties.Settings.Default.VideoFilesize = text_filesize.Text;\r
+            Properties.Settings.Default.VideoQuality = slider_videoQuality.Value;\r
+            Properties.Settings.Default.TwoPass = check_2PassEncode.CheckState.ToString();\r
+            Properties.Settings.Default.DeInterlace = drp_deInterlace_option.Text;\r
+            Properties.Settings.Default.Grayscale = check_grayscale.CheckState.ToString();\r
+            Properties.Settings.Default.Framerate = drp_videoFramerate.Text;\r
+            Properties.Settings.Default.PixelRatio = CheckPixelRatio.CheckState.ToString();\r
+            Properties.Settings.Default.turboFirstPass = check_turbo.CheckState.ToString();\r
+            Properties.Settings.Default.largeFile = check_largeFile.CheckState.ToString();\r
+            Properties.Settings.Default.detelecine = check_detelecine.CheckState.ToString();\r
+            Properties.Settings.Default.denoise = drp_deNoise.Text;\r
+            Properties.Settings.Default.deblock = check_deblock.CheckState.ToString();\r
+            Properties.Settings.Default.chapterMarker = Check_ChapterMarkers.CheckState.ToString();\r
+            //Audio Settings Tab\r
+            Properties.Settings.Default.AudioBitrate = drp_audioBitrate.Text;\r
+            Properties.Settings.Default.AudioSampleRate = drp_audioSampleRate.Text;\r
+            Properties.Settings.Default.AudioChannels = drp_audioChannels.Text;\r
+            //H264 Tab\r
+            Properties.Settings.Default.CRF = CheckCRF.CheckState.ToString();\r
+            Properties.Settings.Default.H264 = rtf_h264advanced.Text;\r
+            //Preset\r
+            Properties.Settings.Default.selectedPreset = groupBox_output.Text.Replace("Output Settings (Preset: ", "").Replace("\"", "").Replace(")", "");\r
+            // Save the new default Settings\r
+            Properties.Settings.Default.Save();\r
+            MessageBox.Show("New default settings saved.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
+        }\r
+\r
+        // Preset Selection\r
+        private void listview_presets_SelectedIndexChanged(object sender, EventArgs e)\r
+        {\r
+\r
+            string selectedPreset = null;\r
+            ListView.SelectedListViewItemCollection name = null;\r
+            name = listview_presets.SelectedItems;\r
+\r
+            if (listview_presets.SelectedItems.Count != 0)\r
+                selectedPreset = name[0].SubItems[0].Text;\r
+\r
+            try\r
+            {\r
+                string appPath = Application.StartupPath.ToString() + "\\";\r
+                StreamReader presetInput = new StreamReader(appPath + "presets.dat");\r
+\r
+                while (!presetInput.EndOfStream)\r
+                {\r
+                    if ((char)presetInput.Peek() == '+')\r
+                    {\r
+                        string preset = presetInput.ReadLine().Replace("+ ", "");\r
+                        Regex r = new Regex("(:  )"); // Split on hyphens. \r
+                        string[] presetName = r.Split(preset);\r
+\r
+                        if (selectedPreset == "iPhone / iPod Touch")\r
+                        {\r
+                            selectedPreset = "iPhone";\r
+                        }\r
+\r
+                        if (selectedPreset == presetName[0])\r
+                        {\r
+                            // Need to disable anamorphic now, otherwise it may overide the width / height values later.\r
+                            CheckPixelRatio.CheckState = CheckState.Unchecked;\r
+\r
+                            // Send the query from the file to the Query Parser class\r
+                            Functions.QueryParser presetQuery = Functions.QueryParser.Parse(preset);\r
+\r
+                            // Now load the preset\r
+                            presetLoader(presetQuery, selectedPreset);\r
+                        }\r
+                        \r
+                    }\r
+                    else\r
+                    {\r
+                        presetInput.ReadLine();\r
+                    }\r
+                }\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
+\r
+           \r
+        }\r
+        #endregion\r
+\r
+        //---------------------------------------------------\r
+        // Encode / Cancel Buttons\r
+        // Encode Progress Text Handler\r
+        //---------------------------------------------------\r
+\r
+        #region Encode/CLI\r
+\r
+        Functions.CLI process = new Functions.CLI();\r
+\r
+        private void btn_encode_Click(object sender, EventArgs e)\r
+        {\r
+            if (text_source.Text == "" || text_source.Text == "Click 'Browse' to continue" || text_destination.Text == "")\r
+                MessageBox.Show("No source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            else\r
+            {\r
+                btn_eCancel.Enabled = true;\r
+                String query = "";\r
+                if (QueryEditorText.Text == "")\r
+                {\r
+                    query = GenerateTheQuery();\r
+                }\r
+                else\r
+                {\r
+                    query = QueryEditorText.Text;\r
+                }\r
+\r
+                ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+                lbl_encode.Visible = true;\r
+                lbl_encode.Text = "Encoding in Progress";\r
+            }\r
+        }\r
+\r
+        private void btn_eCancel_Click(object sender, EventArgs e)\r
+        {\r
+            process.killCLI();\r
+            process.setNull();\r
+            lbl_encode.Text = "Encoding Canceled";\r
+        }\r
+\r
+        [DllImport("user32.dll")]\r
+        public static extern void LockWorkStation();\r
+        [DllImport("user32.dll")]\r
+        public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
+\r
+        private void procMonitor(object state)\r
+        {\r
+            // Make sure we are not already encoding and if we are then display an error.\r
+            if (hbProc != null)\r
+            {\r
+                MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+            else\r
+            {\r
+                hbProc = process.runCli(this, (string)state, false, false, false, false);\r
+                hbProc.WaitForExit();\r
+\r
+                try\r
+                {\r
+                    /*\r
+                    //*****************************************************************************************\r
+                    // BUG!\r
+                    // When the below code is used and standard error is set to true, hbcli is outputing a\r
+                    // video stream which has mild corruption issues every few seconds.\r
+                    // Maybe an issue with the Parser cauing the CLI to hickup/pause?\r
+                    //*****************************************************************************************\r
+\r
+                    \r
+                    Parsing.Parser encode = new Parsing.Parser(hbProc.StandardOutput.BaseStream);\r
+                    encode.OnEncodeProgress += encode_OnEncodeProgress;\r
+                    while (!encode.EndOfStream)\r
+                    {\r
+                        encode.ReadLine();\r
+                    }\r
+\r
+                    hbProc.WaitForExit();\r
+                    process.closeCLI();\r
+                     */\r
+\r
+                }\r
+                catch (Exception exc)\r
+                {\r
+                    // Do nothing\r
+                    MessageBox.Show(exc.ToString());\r
+                }\r
+\r
+\r
+                setEncodeLabel();\r
+                hbProc = null;\r
+\r
+                // Do something whent he encode ends.\r
+                switch (Properties.Settings.Default.CompletionOption)\r
+                {\r
+                    case "Shutdown":\r
+                        System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
+                        break;\r
+                    case "Log Off":\r
+                        ExitWindowsEx(0, 0);\r
+                        break;\r
+                    case "Suspend":\r
+                        Application.SetSuspendState(PowerState.Suspend, true, true);\r
+                        break;\r
+                    case "Hibernate":\r
+                        Application.SetSuspendState(PowerState.Hibernate, true, true);\r
+                        break;\r
+                    case "Lock System":\r
+                        LockWorkStation();\r
+                        break;\r
+                    case "Quit HandBrake":\r
+                        Application.Exit();\r
+                        break;\r
+                    default:\r
+                        break;\r
+                }\r
+            }\r
+        }\r
+\r
+        private delegate void UpdateUIHandler();\r
+\r
+        private void setEncodeLabel()\r
+        {\r
+            if (this.InvokeRequired)\r
+            {\r
+                this.BeginInvoke(new UpdateUIHandler(setEncodeLabel));\r
+                return;\r
+            }\r
+            lbl_encode.Text = "Encoding Finished";\r
+        }\r
+\r
+        private void encode_OnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)\r
+        {\r
+\r
+            if (this.InvokeRequired)\r
+            {\r
+                this.BeginInvoke(new Parsing.EncodeProgressEventHandler(encode_OnEncodeProgress),\r
+                    new object[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });\r
+                return;\r
+            }\r
+            lbl_encode.Text = string.Format("Encode Progress: {0}%,       FPS: {1},       Avg FPS: {2},       Time Remaining: {3} ", PercentComplete, CurrentFps, AverageFps, TimeRemaining);\r
+        }\r
+\r
+        #endregion\r
+\r
+        //---------------------------------------------------\r
+        //  Items that require actions on frmMain\r
+        //---------------------------------------------------\r
+\r
+        #region frmMain Actions\r
 \r
         private void drop_chapterStart_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
+            drop_chapterStart.BackColor = Color.White;\r
             QueryEditorText.Text = "";\r
             if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
             {\r
-                int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
-                int chapterStart = int.Parse(drop_chapterStart.Text);\r
-\r
                 try\r
                 {\r
+                    int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
+                    int chapterStart = int.Parse(drop_chapterStart.Text);\r
+\r
                     if (chapterFinish < chapterStart)\r
                     {\r
-                        MessageBox.Show("Invalid Chapter Range! - Final chapter can not be smaller than the starting chapter.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                        drop_chapterStart.BackColor = Color.LightCoral;\r
                     }\r
                 }\r
                 catch (Exception)\r
                 {\r
-                    MessageBox.Show("Invalid Character Entered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                    drop_chapterStart.BackColor = Color.LightCoral;\r
                 }\r
             }\r
 \r
-            \r
+\r
         }\r
 \r
         private void drop_chapterFinish_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
+            drop_chapterFinish.BackColor = Color.White;\r
             QueryEditorText.Text = "";\r
             if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
             {\r
-                int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
-                int chapterStart = int.Parse(drop_chapterStart.Text);\r
-\r
                 try\r
                 {\r
-                    if (chapterFinish > chapterStart)\r
+                    int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
+                    int chapterStart = int.Parse(drop_chapterStart.Text);\r
+\r
+                    if (chapterFinish < chapterStart)\r
                     {\r
-                        MessageBox.Show("Invalid Chapter Range! - Start chapter can not be larger than the Final chapter.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                        drop_chapterFinish.BackColor = Color.LightCoral;\r
                     }\r
                 }\r
                 catch (Exception)\r
                 {\r
-                    MessageBox.Show("Invalid Character Entered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                    drop_chapterFinish.BackColor = Color.LightCoral;\r
                 }\r
             }\r
         }\r
@@ -678,6 +969,7 @@ namespace Handbrake
             slider_videoQuality.Value = 0;\r
             SliderValue.Text = "0%";\r
             CheckCRF.CheckState = CheckState.Unchecked;\r
+            CheckCRF.Enabled = false;\r
         }\r
 \r
         private void text_filesize_TextChanged(object sender, EventArgs e)\r
@@ -686,6 +978,7 @@ namespace Handbrake
             slider_videoQuality.Value = 0;\r
             SliderValue.Text = "0%";\r
             CheckCRF.CheckState = CheckState.Unchecked;\r
+            CheckCRF.Enabled = false;\r
         }\r
 \r
         private void slider_videoQuality_Scroll(object sender, EventArgs e)\r
@@ -693,45 +986,72 @@ namespace Handbrake
             SliderValue.Text = slider_videoQuality.Value.ToString() + "%";\r
             text_bitrate.Text = "";\r
             text_filesize.Text = "";\r
+            CheckCRF.Enabled = true;\r
         }\r
 \r
         private void label_h264_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
         {\r
-            System.Diagnostics.Process.Start("http://handbrake.m0k.org/trac/wiki/x264Options");\r
+            Process.Start("http://handbrake.m0k.org/trac/wiki/x264Options");\r
         }\r
 \r
         private void text_width_TextChanged(object sender, EventArgs e)\r
         {\r
             try\r
             {\r
-                if (CheckPixelRatio.CheckState.ToString() == "Checked") {\r
+                if (CheckPixelRatio.Checked)\r
+                {\r
                     text_width.Text = "";\r
-                } else {\r
-                    if ((int.Parse(text_width.Text) % 16) != 0){\r
+                    text_width.BackColor = Color.LightCoral;\r
+                    CheckPixelRatio.BackColor = Color.LightCoral;\r
+                }\r
+                else\r
+                {\r
+                    if ((int.Parse(text_width.Text) % 16) != 0)\r
+                    {\r
                         text_width.BackColor = Color.LightCoral;\r
-                    }else {\r
+                    }\r
+                    else\r
+                    {\r
                         text_width.BackColor = Color.LightGreen;\r
                     }\r
                 }\r
 \r
-                if (!lbl_Aspect.Text.Equals("Select a Title")){\r
-                    int height = int.Parse(text_width.Text) / int.Parse(lbl_Aspect.Text);\r
-                    int mod16 = height % 16;\r
+                if (lbl_Aspect.Text != "Select a Title")\r
+                {\r
+                    double height = int.Parse(text_width.Text) / double.Parse(lbl_Aspect.Text);\r
+                    double mod16 = height % 16;\r
                     height = height - mod16;\r
-                    text_height.Text = height.ToString();\r
+\r
+                    if (text_width.Text == "")\r
+                    {\r
+                        text_height.Text = "";\r
+                        text_width.BackColor = Color.White;\r
+                    }\r
+                    else\r
+                    {\r
+                        text_height.Text = height.ToString();\r
+                        text_width.BackColor = Color.LightGreen;\r
+                    }\r
                 }\r
-               \r
-            } catch(Exception){\r
             }\r
+            catch (Exception)\r
+            {\r
+                // No need to throw an error here.\r
+                // Note on non english systems, this will throw an error because of double.Parse(lbl_Aspect.Text); not working.\r
+            }\r
+\r
+\r
         }\r
 \r
         private void text_height_TextChanged(object sender, EventArgs e)\r
         {\r
             try\r
             {\r
-                if (CheckPixelRatio.CheckState.ToString() == "Checked")\r
+                if (CheckPixelRatio.Checked)\r
                 {\r
                     text_height.Text = "";\r
+                    text_height.BackColor = Color.LightCoral;\r
+                    CheckPixelRatio.BackColor = Color.LightCoral;\r
                 }\r
                 else\r
                 {\r
@@ -744,7 +1064,11 @@ namespace Handbrake
                         text_height.BackColor = Color.LightGreen;\r
                     }\r
                 }\r
-            } catch(Exception){\r
+\r
+            }\r
+            catch (Exception)\r
+            {\r
+                // No need to alert the user.\r
             }\r
         }\r
 \r
@@ -752,10 +1076,10 @@ namespace Handbrake
         {\r
             if ((string)drp_crop.SelectedItem == "Manual")\r
             {\r
-            text_left.Enabled = true;\r
-            text_right.Enabled = true;\r
-            text_top.Enabled = true;\r
-            text_bottom.Enabled = true;\r
+                text_left.Enabled = true;\r
+                text_right.Enabled = true;\r
+                text_top.Enabled = true;\r
+                text_bottom.Enabled = true;\r
             }\r
 \r
             if ((string)drp_crop.SelectedItem == "Auto Crop")\r
@@ -793,18 +1117,50 @@ namespace Handbrake
 \r
             }\r
         }\r
-        \r
+\r
         private void CheckPixelRatio_CheckedChanged(object sender, EventArgs e)\r
         {\r
             text_width.Text = "";\r
             text_height.Text = "";\r
             text_width.BackColor = Color.White;\r
             text_height.BackColor = Color.White;\r
+            CheckPixelRatio.BackColor = TabPage1.BackColor;\r
+        }\r
+\r
+        private void check_2PassEncode_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            if (check_2PassEncode.CheckState.ToString() == "Checked")\r
+            {\r
+                if (drp_videoEncoder.Text.Contains("H.264"))\r
+                {\r
+                    check_turbo.Enabled = true;\r
+                }\r
+            }\r
+            else\r
+            {\r
+                check_turbo.Enabled = false;\r
+                check_turbo.CheckState = CheckState.Unchecked;\r
+            }\r
+        }\r
+\r
+        private void check_largeFile_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            if (!text_destination.Text.Contains(".mp4"))\r
+            {\r
+                lbl_largeMp4Warning.Text = "Warning: Only mp4 files are supported";\r
+                lbl_largeMp4Warning.ForeColor = Color.Red;\r
+                check_largeFile.CheckState = CheckState.Unchecked;\r
+            }\r
+            else\r
+            {\r
+                lbl_largeMp4Warning.Text = "Warning: Breaks iPod, @TV, PS3 compatibility.";\r
+                lbl_largeMp4Warning.ForeColor = Color.Black;\r
+            }\r
         }\r
 \r
         private void drp_dvdtitle_Click(object sender, EventArgs e)\r
         {\r
-            if (drp_dvdtitle.Items.Count == 1)\r
+            if (drp_dvdtitle.Items.Count == 0)\r
             {\r
                 MessageBox.Show("There are no titles to select. Please scan the DVD by clicking the 'browse' button above before trying to select a title.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
             }\r
@@ -819,11 +1175,13 @@ namespace Handbrake
 \r
             if (drp_audioCodec.Text == "AAC")\r
             {\r
+                drp_audioMixDown.Items.Clear();\r
                 drp_audioMixDown.Items.Add("Mono");\r
                 drp_audioMixDown.Items.Add("Stereo");\r
                 drp_audioMixDown.Items.Add("Dolby Surround");\r
                 drp_audioMixDown.Items.Add("Dolby Pro Logic II");\r
                 drp_audioMixDown.Items.Add("6 Channel Discrete");\r
+\r
                 drp_audioBitrate.Items.Clear();\r
                 drp_audioBitrate.Items.Add("32");\r
                 drp_audioBitrate.Items.Add("40");\r
@@ -839,6 +1197,7 @@ namespace Handbrake
             }\r
             else\r
             {\r
+                drp_audioMixDown.Items.Clear();\r
                 drp_audioMixDown.Items.Add("Stereo");\r
                 drp_audioMixDown.Items.Add("Dolby Surround");\r
                 drp_audioMixDown.Items.Add("Dolby Pro Logic II");\r
@@ -858,7 +1217,6 @@ namespace Handbrake
                 drp_audioBitrate.Items.Add("224");\r
                 drp_audioBitrate.Items.Add("256");\r
                 drp_audioBitrate.Items.Add("320");\r
-                drp_audioBitrate.Items.Add("384");\r
             }\r
         }\r
 \r
@@ -891,29 +1249,17 @@ namespace Handbrake
 \r
         private void Check_ChapterMarkers_CheckedChanged(object sender, EventArgs e)\r
         {\r
-            if (Check_ChapterMarkers.CheckState.ToString() == "Checked")\r
+            if (Check_ChapterMarkers.Checked)\r
             {\r
                 string destination = text_destination.Text;\r
                 destination = destination.Replace(".mp4", ".m4v");\r
                 text_destination.Text = destination;\r
             }\r
-        }\r
-\r
-        private void check_largeFile_CheckedChanged(object sender, EventArgs e)\r
-        {\r
-            if (!text_destination.Text.Contains(".mp4"))\r
-            {\r
-                MessageBox.Show("This option is only compatible with the mp4 file container.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-                check_largeFile.CheckState = CheckState.Unchecked;\r
-            }\r
-        }\r
-\r
-        private void check_turbo_CheckedChanged(object sender, EventArgs e)\r
-        {\r
-            if (!drp_videoEncoder.Text.Contains("H.264"))\r
+            else\r
             {\r
-                MessageBox.Show("This option is only compatible with the H.264 encoder's", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-                check_turbo.CheckState = CheckState.Unchecked;\r
+                string destination = text_destination.Text;\r
+                destination = destination.Replace(".m4v", ".mp4");\r
+                text_destination.Text = destination;\r
             }\r
         }\r
 \r
@@ -924,30 +1270,95 @@ namespace Handbrake
             {\r
                 check_turbo.CheckState = CheckState.Unchecked;\r
                 CheckCRF.CheckState = CheckState.Unchecked;\r
+                CheckCRF.Enabled = false;\r
+                check_turbo.Enabled = false;\r
+                h264Tab.Enabled = false;\r
+                rtf_h264advanced.Text = "";\r
             }\r
+            else\r
+            {\r
+                CheckCRF.Enabled = true;\r
+                if (check_2PassEncode.CheckState.ToString() == "Checked")\r
+                {\r
+                    check_turbo.Enabled = true;\r
+                }\r
+                h264Tab.Enabled = true;\r
+            }\r
+\r
         }\r
 \r
-        private void CheckCRF_CheckedChanged(object sender, EventArgs e)\r
+        public void setStreamReader(Parsing.DVD dvd)\r
         {\r
-            if (slider_videoQuality.Value == 0)\r
-            {\r
-                MessageBox.Show("This option is can only be used with the 'Video Quality' slider.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-                CheckCRF.CheckState = CheckState.Unchecked;\r
-            }\r
+            this.thisDVD = dvd;\r
         }\r
 \r
         private void drp_dvdtitle_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            //TODO: Convert the Parsing Code.\r
-        } \r
+            // Reset some values on the form\r
+            lbl_Aspect.Text = "Select a Title";\r
+            lbl_RecomendedCrop.Text = "Select a Title";\r
+            drop_chapterStart.Items.Clear();\r
+            drop_chapterFinish.Items.Clear();\r
+            QueryEditorText.Text = "";\r
+\r
+            // If the dropdown is set to automatic nothing else needs to be done.\r
+            // Otheriwse if its not, title data has to be loased from parsing.\r
+            if (drp_dvdtitle.Text != "Automatic")\r
+            {\r
+                Parsing.Title selectedTitle = drp_dvdtitle.SelectedItem as Parsing.Title;\r
+\r
+                // Set the Aspect Ratio\r
+                lbl_Aspect.Text = selectedTitle.AspectRatio.ToString();\r
+\r
+                // Set the Recommended Cropping values\r
+                lbl_RecomendedCrop.Text = string.Format("{0}/{1}/{2}/{3}", selectedTitle.AutoCropDimensions[0], selectedTitle.AutoCropDimensions[1], selectedTitle.AutoCropDimensions[2], selectedTitle.AutoCropDimensions[3]);\r
+\r
+                // Populate the Start chapter Dropdown\r
+                drop_chapterStart.Items.Clear();\r
+                drop_chapterStart.Items.AddRange(selectedTitle.Chapters.ToArray());\r
+                if (drop_chapterStart.Items.Count > 0)\r
+                {\r
+                    drop_chapterStart.Text = drop_chapterStart.Items[0].ToString();\r
+                }\r
+\r
+                // Populate the Final Chapter Dropdown\r
+                drop_chapterFinish.Items.Clear();\r
+                drop_chapterFinish.Items.AddRange(selectedTitle.Chapters.ToArray());\r
+                if (drop_chapterFinish.Items.Count > 0)\r
+                {\r
+                    drop_chapterFinish.Text = drop_chapterFinish.Items[drop_chapterFinish.Items.Count - 1].ToString();\r
+                }\r
 \r
-        //\r
-        // The Query Generation Function\r
-        //\r
+                // Populate the Audio Channels Dropdown\r
+                drp_audioChannels.Items.Clear();\r
+                drp_audioChannels.Items.Add("Automatic");\r
+                drp_audioChannels.Items.AddRange(selectedTitle.AudioTracks.ToArray());\r
+                if (drp_audioChannels.Items.Count > 0)\r
+                {\r
+                    drp_audioChannels.Text = drp_audioChannels.Items[0].ToString();\r
+                }\r
 \r
+                // Populate the Subtitles dropdown\r
+                drp_subtitle.Items.Clear();\r
+                drp_subtitle.Items.Add("None");\r
+                drp_subtitle.Items.AddRange(selectedTitle.Subtitles.ToArray());\r
+                if (drp_subtitle.Items.Count > 0)\r
+                {\r
+                    drp_subtitle.Text = drp_subtitle.Items[0].ToString();\r
+                }\r
+            }\r
+        }\r
 \r
-        // This function was imported from old vb.net version of this application.\r
-        // It could probably do with being cleaned up a good deal at some point\r
+        #endregion\r
+\r
+        //---------------------------------------------------\r
+        //  Some Functions\r
+        //  - Query Generation\r
+        //---------------------------------------------------\r
+\r
+        #region Program Functions\r
+\r
+        // Generate a CLI Query String\r
         public string GenerateTheQuery()\r
         {\r
             string source = text_source.Text;\r
@@ -957,39 +1368,24 @@ namespace Handbrake
             int totalChapters = drop_chapterFinish.Items.Count - 1;\r
             string dvdChapter = "";\r
 \r
-            if (source ==  "")\r
-            {\r
-                MessageBox.Show("No Source has been selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-            }else{\r
-                source = " -i " + '"' + source+ '"'; //'"'+\r
-            }\r
+            source = " -i " + '"' + source + '"';\r
 \r
-            if (dvdTitle ==  "Automatic")\r
-            {\r
+            if (dvdTitle == "Automatic")\r
                 dvdTitle = "";\r
-            }else{\r
+            else\r
+            {\r
                 string[] titleInfo = dvdTitle.Split(' ');\r
-                dvdTitle = " -t "+ titleInfo[0];\r
+                dvdTitle = " -t " + titleInfo[0];\r
             }\r
 \r
-\r
-\r
-\r
-            if ((chapterFinish.Equals("Auto") && chapterStart.Equals("Auto")))\r
-            {\r
+            if (chapterFinish.Equals("Auto") && chapterStart.Equals("Auto"))\r
                 dvdChapter = "";\r
-            }\r
             else if (chapterFinish == chapterStart)\r
-            {\r
                 dvdChapter = " -c " + chapterStart;\r
-            }\r
-\r
             else\r
-            {\r
                 dvdChapter = " -c " + chapterStart + "-" + chapterFinish;\r
-            }\r
 \r
-            string querySource = source+ dvdTitle+ dvdChapter;\r
+            string querySource = source + dvdTitle + dvdChapter;\r
             // ----------------------------------------------------------------------\r
 \r
             // Destination\r
@@ -999,72 +1395,69 @@ namespace Handbrake
             string audioEncoder = drp_audioCodec.Text;\r
             string width = text_width.Text;\r
             string height = text_height.Text;\r
-            if ((destination ==  ""))\r
-            {\r
-                MessageBox.Show("No destination has been selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-            }\r
 \r
+            if (destination == "")\r
+                MessageBox.Show("No destination has been selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
             else\r
-            {\r
                 destination = " -o " + '"' + destination + '"'; //'"'+ \r
-            }\r
 \r
-            if ((videoEncoder ==  "Mpeg 4"))\r
-            {\r
-                videoEncoder = " -e ffmpeg";\r
-            }\r
 \r
-            else if ((videoEncoder ==  "Xvid"))\r
+            switch (videoEncoder)\r
             {\r
-                videoEncoder = " -e xvid";\r
+                case "Mpeg 4":\r
+                    videoEncoder = " -e ffmpeg";\r
+                    break;\r
+                case "Xvid":\r
+                    videoEncoder = " -e xvid";\r
+                    break;\r
+                case "H.264":\r
+                    videoEncoder = " -e x264";\r
+                    break;\r
+                case "H.264 Baseline 1.3":\r
+                    videoEncoder = " -e x264b13";\r
+                    break;\r
+                case "H.264 (iPod)":\r
+                    videoEncoder = " -e x264b30";\r
+                    break;\r
+                default:\r
+                    videoEncoder = " -e x264";\r
+                    break;\r
             }\r
 \r
-            else if ((videoEncoder ==  "H.264"))\r
+            switch (audioEncoder)\r
             {\r
-                videoEncoder = " -e x264";\r
+                case "AAC":\r
+                    audioEncoder = " -E faac";\r
+                    break;\r
+                case "MP3":\r
+                    audioEncoder = " -E lame";\r
+                    break;\r
+                case "Vorbis":\r
+                    audioEncoder = " -E vorbis";\r
+                    break;\r
+                case "AC3":\r
+                    audioEncoder = " -E ac3";\r
+                    break;\r
+                default:\r
+                    audioEncoder = " -E faac";\r
+                    break;\r
             }\r
 \r
-            else if ((videoEncoder ==  "H.264 Baseline 1.3"))\r
-            {\r
-                videoEncoder = " -e x264b13";\r
-            }\r
+            if (width != "")\r
+                width = " -w " + width;\r
 \r
-            else if ((videoEncoder ==  "H.264 (iPod)"))\r
-            {\r
-                videoEncoder = " -e x264b30";\r
-            }\r
 \r
-            if ((audioEncoder ==  "AAC"))\r
+            if (height == "Auto")\r
             {\r
-                audioEncoder = " -E faac";\r
+                height = "";\r
             }\r
-\r
-            else if ((audioEncoder ==  "MP3"))\r
+            else if (height != "")\r
             {\r
-                audioEncoder = " -E lame";\r
+                height = " -l " + height;\r
             }\r
 \r
-            else if ((audioEncoder ==  "Vorbis"))\r
-            {\r
-                audioEncoder = " -E vorbis";\r
-            }\r
 \r
-            else if ((audioEncoder ==  "AC3"))\r
-            {\r
-                audioEncoder = " -E ac3";\r
-            }\r
-\r
-            if ((width !=  ""))\r
-            {\r
-                width = " -w "+ width;\r
-            }\r
-\r
-            if ((height !=  ""))\r
-            {\r
-                height = " -l "+ height;\r
-            }\r
-\r
-            string queryDestination = destination+ videoEncoder+ audioEncoder+ width+ height;\r
+            string queryDestination = destination + videoEncoder + audioEncoder + width + height;\r
             // ----------------------------------------------------------------------\r
 \r
             // Picture Settings Tab\r
@@ -1076,309 +1469,447 @@ namespace Handbrake
             string cropRight = text_right.Text;\r
             string subtitles = drp_subtitle.Text;\r
             string cropOut = "";\r
+            string deInterlace_Option = drp_deInterlace_option.Text;\r
+            string deinterlace = "";\r
+            string grayscale = "";\r
+            string pixelRatio = "";\r
+            string ChapterMarkers = "";\r
             // Returns Crop Query\r
 \r
-            if (cropSetting ==  "Auto Crop")\r
-            {\r
+            if (cropSetting == "Auto Crop")\r
                 cropOut = "";\r
-            }\r
-\r
-            else if (cropSetting ==  "No Crop")\r
-            {\r
+            else if (cropSetting == "No Crop")\r
                 cropOut = " --crop 0:0:0:0 ";\r
-            }\r
-\r
             else\r
             {\r
-                cropOut = " --crop "+ cropTop+ ":"+ cropBottom+ ":"+ cropLeft+ ":"+ cropRight;\r
-            }\r
+                if (text_top.Text == "")\r
+                    cropTop = "0";\r
+                if (text_bottom.Text == "")\r
+                    cropBottom = "0";\r
+                if (text_left.Text == "")\r
+                    cropLeft = "0";\r
+                if (text_right.Text == "")\r
+                    cropRight = "0";\r
 \r
-            if ((subtitles ==  "None"))\r
-            {\r
-                subtitles = "";\r
+                cropOut = " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;\r
             }\r
 \r
-            else if ((subtitles ==  ""))\r
-            {\r
+            if (subtitles == "None")\r
+                subtitles = "";\r
+            else if (subtitles == "")\r
                 subtitles = "";\r
-            }\r
-\r
             else\r
             {\r
                 string[] tempSub;\r
                 tempSub = subtitles.Split(' ');\r
-                subtitles = " -s "+ tempSub[0];\r
-            }\r
+                subtitles = " -s " + tempSub[0];\r
+            }\r
+\r
+            switch (deInterlace_Option)\r
+            {\r
+                case "None":\r
+                    deinterlace = "";\r
+                    break;\r
+                case "Original (Fast)":\r
+                    deinterlace = " --deinterlace=fast";\r
+                    break;\r
+                case "yadif (Slow)":\r
+                    deinterlace = " --deinterlace=slow";\r
+                    break;\r
+                case "yadif + mcdeint (Slower)":\r
+                    deinterlace = " --deinterlace=slower";\r
+                    break;\r
+                case "yadif + mcdeint (Slowest)":\r
+                    deinterlace = " --deinterlace=slowest";\r
+                    break;\r
+                default:\r
+                    deinterlace = "";\r
+                    break;\r
+            }\r
+\r
+            if (check_grayscale.Checked)\r
+                grayscale = " -g ";\r
+\r
+            if (CheckPixelRatio.Checked)\r
+                pixelRatio = " -p ";\r
 \r
-            string queryPictureSettings = cropOut+ subtitles;\r
+            if (Check_ChapterMarkers.Checked)\r
+                ChapterMarkers = " -m ";\r
+\r
+            string queryPictureSettings = cropOut + subtitles + deinterlace + grayscale + pixelRatio + ChapterMarkers;\r
             // ----------------------------------------------------------------------\r
 \r
             // Video Settings Tab\r
 \r
             string videoBitrate = text_bitrate.Text;\r
             string videoFilesize = text_filesize.Text;\r
-            int videoQuality = slider_videoQuality.Value;\r
-            string vidQSetting;\r
-            string twoPassEncoding = check_2PassEncode.CheckState.ToString();\r
-            string deinterlace = check_DeInterlace.CheckState.ToString();\r
-            string grayscale = check_grayscale.CheckState.ToString();\r
+            double videoQuality = slider_videoQuality.Value;\r
+            string vidQSetting = "";\r
+            string twoPassEncoding = "";\r
             string videoFramerate = drp_videoFramerate.Text;\r
-            string pixelRatio = CheckPixelRatio.CheckState.ToString();\r
-            string ChapterMarkers = Check_ChapterMarkers.CheckState.ToString();\r
-            string turboH264 = check_turbo.CheckState.ToString();\r
-            string largeFile = check_largeFile.CheckState.ToString();\r
+            string turboH264 = "";\r
+            string largeFile = "";\r
+            string deblock = "";\r
+            string detelecine = "";\r
+            string denoise = "";\r
+            string CRF = CheckCRF.CheckState.ToString();\r
 \r
-            if ((videoBitrate !=  ""))\r
-            {\r
-                videoBitrate = " -b "+ videoBitrate;\r
-            }\r
+            if (CRF == "Checked")\r
+                CRF = " -Q ";\r
+            else\r
+                CRF = "";\r
 \r
-            if ((videoFilesize !=  ""))\r
-            {\r
-                videoFilesize = " -S "+ videoFilesize;\r
-            }\r
+            if (videoBitrate != "")\r
+                videoBitrate = " -b " + videoBitrate;\r
+\r
+            if (videoFilesize != "")\r
+                videoFilesize = " -S " + videoFilesize;\r
 \r
             // Video Quality Setting\r
 \r
-            if ((videoQuality ==  0))\r
-            {\r
+            if ((videoQuality == 0))\r
                 vidQSetting = "";\r
-            }\r
-\r
             else\r
             {\r
-                videoQuality = videoQuality/ 100;\r
-                if (videoQuality ==  1)\r
+                videoQuality = videoQuality / 100;\r
+                if (videoQuality == 1)\r
                 {\r
                     vidQSetting = "1.0";\r
                 }\r
-\r
-                vidQSetting = " -q " + videoQuality.ToString();\r
+                vidQSetting = " -q " + videoQuality.ToString(new CultureInfo("en-US")); \r
             }\r
 \r
-            if ((twoPassEncoding ==  "1"))\r
-            {\r
+            if (check_2PassEncode.Checked)\r
                 twoPassEncoding = " -2 ";\r
-            }\r
 \r
+            if (videoFramerate == "Automatic")\r
+                videoFramerate = "";\r
             else\r
-            {\r
-                twoPassEncoding = "";\r
-            }\r
+                videoFramerate = " -r " + videoFramerate;\r
 \r
-            if ((deinterlace ==  "1"))\r
-            {\r
-                deinterlace = " -d ";\r
-            }\r
+            if (check_turbo.Checked)\r
+                turboH264 = " -T ";\r
 \r
-            else\r
-            {\r
-                deinterlace = "";\r
-            }\r
+            if (check_largeFile.Checked)\r
+                largeFile = " -4 ";\r
 \r
-            if ((grayscale ==  "1"))\r
-            {\r
-                grayscale = " -g ";\r
-            }\r
+            if (check_deblock.Checked)\r
+                deblock = " --deblock";\r
 \r
-            else\r
-            {\r
-                grayscale = "";\r
-            }\r
+            if (check_detelecine.Checked)\r
+                detelecine = " --detelecine";\r
 \r
-            if ((videoFramerate ==  "Automatic"))\r
+            switch (drp_deNoise.Text)\r
             {\r
-                videoFramerate = "";\r
+                case "None":\r
+                    denoise = "";\r
+                    break;\r
+                case "Weak":\r
+                    denoise = " --denoise=weak";\r
+                    break;\r
+                case "Medium":\r
+                    denoise = " --denoise=medium";\r
+                    break;\r
+                case "Strong":\r
+                    denoise = " --denoise=strong";\r
+                    break;\r
+                default:\r
+                    denoise = "";\r
+                    break;\r
             }\r
 \r
+            string queryVideoSettings = videoBitrate + videoFilesize + vidQSetting + CRF + twoPassEncoding + videoFramerate + turboH264 + largeFile + deblock + detelecine + denoise;\r
+            // ----------------------------------------------------------------------\r
+\r
+            // Audio Settings Tab\r
+\r
+            string audioBitrate = drp_audioBitrate.Text;\r
+            string audioSampleRate = drp_audioSampleRate.Text;\r
+            string audioChannels = drp_audioChannels.Text;\r
+            string Mixdown = drp_audioMixDown.Text;\r
+            string SixChannelAudio = "";\r
+\r
+            if (audioBitrate != "")\r
+                audioBitrate = " -B " + audioBitrate;\r
+\r
+            if (audioSampleRate != "")\r
+                audioSampleRate = " -R " + audioSampleRate;\r
+\r
+            if (audioChannels == "Automatic")\r
+                audioChannels = "";\r
+            else if (audioChannels == "")\r
+                audioChannels = "";\r
             else\r
             {\r
-                videoFramerate = " -r "+ videoFramerate;\r
-            }\r
+                string[] tempSub;\r
+                tempSub = audioChannels.Split(' ');\r
+                audioChannels = " -a " + tempSub[0];\r
+            }\r
+\r
+            switch (Mixdown)\r
+            {\r
+                case "Automatic":\r
+                    Mixdown = "";\r
+                    break;\r
+                case "Mono":\r
+                    Mixdown = "mono";\r
+                    break;\r
+                case "Stereo":\r
+                    Mixdown = "stereo";\r
+                    break;\r
+                case "Dolby Surround":\r
+                    Mixdown = "dpl1";\r
+                    break;\r
+                case "Dolby Pro Logic II":\r
+                    Mixdown = "dpl2";\r
+                    break;\r
+                case "6 Channel Discrete":\r
+                    Mixdown = "6ch";\r
+                    break;\r
+                default:\r
+                    Mixdown = "";\r
+                    break;\r
+            }\r
+\r
+            if (Mixdown != "")\r
+                SixChannelAudio = " -6 " + Mixdown;\r
+            else\r
+                SixChannelAudio = "";\r
 \r
-            if ((pixelRatio ==  "1"))\r
-            {\r
-                pixelRatio = " -p ";\r
-            }\r
+            string queryAudioSettings = audioBitrate + audioSampleRate + audioChannels + SixChannelAudio;\r
+            // ----------------------------------------------------------------------\r
 \r
+            //  H.264 Tab\r
+\r
+\r
+            string h264Advanced = rtf_h264advanced.Text;\r
+\r
+            if ((h264Advanced == ""))\r
+                h264Advanced = "";\r
             else\r
-            {\r
-                pixelRatio = "";\r
-            }\r
+                h264Advanced = " -x " + h264Advanced;\r
 \r
-            if ((ChapterMarkers ==  "1"))\r
-            {\r
-                ChapterMarkers = " -m ";\r
-            }\r
 \r
+            string h264Settings = h264Advanced;\r
+            // ----------------------------------------------------------------------\r
+\r
+            // Processors (Program Settings)\r
+\r
+            string processors = Properties.Settings.Default.Processors;\r
+            //  Number of Processors Handler\r
+\r
+            if (processors == "Automatic")\r
+                processors = "";\r
             else\r
+                processors = " -C " + processors + " ";\r
+\r
+\r
+            string queryAdvancedSettings = processors;\r
+            // ----------------------------------------------------------------------\r
+\r
+            //  Verbose option (Program Settings)\r
+\r
+            string verbose = "";\r
+            if (Properties.Settings.Default.verbose == "Checked")\r
+                verbose = " -v ";\r
+\r
+            // ----------------------------------------------------------------------\r
+\r
+            return querySource + queryDestination + queryPictureSettings + queryVideoSettings + h264Settings + queryAudioSettings + queryAdvancedSettings + verbose;\r
+        }\r
+\r
+        // Load a Preset\r
+        private void presetLoader(Functions.QueryParser presetQuery, string name)\r
+        {\r
+            // ---------------------------\r
+            // Setup the GUI\r
+            // ---------------------------\r
+\r
+            // Source tab\r
+            #region source\r
+            if (presetQuery.Source != "")\r
+                text_source.Text = presetQuery.Source;\r
+\r
+            if (presetQuery.DVDTitle != 0)\r
+                drp_dvdtitle.Text = presetQuery.DVDTitle.ToString();\r
+\r
+            if (presetQuery.DVDChapterStart != 0)\r
+                drop_chapterStart.Text = presetQuery.DVDChapterStart.ToString();\r
+\r
+            if (presetQuery.DVDChapterFinish != 0)\r
+                drop_chapterFinish.Text = presetQuery.DVDChapterFinish.ToString();\r
+\r
+            if (presetQuery.Format != "")\r
             {\r
-                ChapterMarkers = "";\r
+                string destination = text_destination.Text;\r
+                destination = destination.Replace(".mp4", "." + presetQuery.Format);\r
+                destination = destination.Replace(".m4v", "." + presetQuery.Format);\r
+                destination = destination.Replace(".avi", "." + presetQuery.Format);\r
+                destination = destination.Replace(".mkv", "." + presetQuery.Format);\r
+                destination = destination.Replace(".ogm", "." + presetQuery.Format);\r
+                text_destination.Text = destination;\r
             }\r
 \r
-            if ((turboH264 ==  "1"))\r
+            #endregion\r
+\r
+            // Destination tab\r
+            #region destination\r
+\r
+            if (presetQuery.Destination != "")\r
+                text_destination.Text = presetQuery.Destination;\r
+\r
+            drp_videoEncoder.Text = presetQuery.VideoEncoder;\r
+            drp_audioCodec.Text = presetQuery.AudioEncoder;\r
+            if (presetQuery.Width != 0)\r
             {\r
-                turboH264 = " -T ";\r
+                text_width.Text = presetQuery.Width.ToString();\r
+           \r
             }\r
-\r
             else\r
             {\r
-                turboH264 = "";\r
+                text_width.Text = "";\r
+                text_width.BackColor = Color.White;\r
             }\r
 \r
-            if ((largeFile ==  "1"))\r
+            if (presetQuery.Height != 0)\r
             {\r
-                largeFile = " -4 ";\r
+                text_height.Text = presetQuery.Height.ToString();\r
             }\r
-\r
             else\r
             {\r
-                largeFile = "";\r
+                text_height.Text = "";\r
+                text_height.BackColor = Color.White;\r
             }\r
+            #endregion\r
 \r
-            string queryVideoSettings = videoBitrate + videoFilesize + vidQSetting + twoPassEncoding + deinterlace + grayscale + videoFramerate + pixelRatio + ChapterMarkers + turboH264 + largeFile;\r
-            // ----------------------------------------------------------------------\r
+            // Picture Settings Tab\r
+            #region Picture\r
+            drp_crop.Text = "Manual";\r
+            text_top.Text = presetQuery.CropTop;\r
+            text_bottom.Text = presetQuery.CropBottom;\r
+            text_left.Text = presetQuery.CropLeft;\r
+            text_right.Text = presetQuery.CropRight;\r
 \r
-            // Audio Settings Tab\r
+            drp_deInterlace_option.Text = presetQuery.DeInterlace;\r
+            drp_deNoise.Text = presetQuery.DeNoise;\r
 \r
-            string audioBitrate = drp_audioBitrate.Text;\r
-            string audioSampleRate = drp_audioSampleRate.Text;\r
-            string audioChannels = drp_audioChannels.Text;\r
-            string Mixdown = drp_audioMixDown.Text;\r
-            string SixChannelAudio = "";\r
-            if ((audioBitrate !=  ""))\r
+            if (presetQuery.DeTelecine == true)\r
             {\r
-                audioBitrate = " -B "+ audioBitrate;\r
+                check_detelecine.CheckState = CheckState.Checked;\r
             }\r
-\r
-            if ((audioSampleRate !=  ""))\r
+            else\r
             {\r
-                audioSampleRate = " -R "+ audioSampleRate;\r
+                check_detelecine.CheckState = CheckState.Unchecked;\r
             }\r
 \r
-            if ((audioChannels ==  "Automatic"))\r
-            {\r
-                audioChannels = "";\r
-            }\r
 \r
-            else if ((audioChannels ==  ""))\r
+            if (presetQuery.DeBlock == true)\r
             {\r
-                audioChannels = "";\r
+                check_deblock.CheckState = CheckState.Checked;\r
             }\r
-\r
             else\r
             {\r
-                string[] tempSub;\r
-                tempSub = audioChannels.Split(' ');\r
-                audioChannels = " -a "+ tempSub[0];\r
+                check_deblock.CheckState = CheckState.Unchecked;\r
             }\r
 \r
-            if ((Mixdown ==  "Automatic"))\r
+            if (presetQuery.ChapterMarkers == true)\r
             {\r
-                Mixdown = "";\r
+                Check_ChapterMarkers.CheckState = CheckState.Checked;\r
             }\r
-\r
-            else if (Mixdown ==  "Mono")\r
+            else\r
             {\r
-                Mixdown = "mono";\r
+                Check_ChapterMarkers.CheckState = CheckState.Unchecked;\r
             }\r
 \r
-            else if (Mixdown ==  "Stereo")\r
+            if (presetQuery.Anamorphic == true)\r
             {\r
-                Mixdown = "stereo";\r
+                CheckPixelRatio.CheckState = CheckState.Checked;\r
             }\r
-\r
-            else if (Mixdown ==  "Dolby Surround")\r
+            else\r
             {\r
-                Mixdown = "dpl1";\r
+                CheckPixelRatio.CheckState = CheckState.Unchecked;\r
             }\r
+            #endregion\r
 \r
-            else if (Mixdown ==  "Dolby Pro Logic II")\r
+            // Video Settings Tab\r
+            #region video\r
+            text_bitrate.Text = presetQuery.AverageVideoBitrate;\r
+            text_filesize.Text = presetQuery.VideoTargetSize;\r
+            slider_videoQuality.Value = presetQuery.VideoQuality;\r
+            if (slider_videoQuality.Value != 0)\r
             {\r
-                Mixdown = "dpl2";\r
+                CheckCRF.Enabled = true;\r
+                int ql = presetQuery.VideoQuality;\r
+                SliderValue.Text = ql.ToString() + "%";\r
             }\r
 \r
-            else if (Mixdown ==  "6 Channel Discrete")\r
+            if (presetQuery.TwoPass == true)\r
             {\r
-                Mixdown = "6ch";\r
+                check_2PassEncode.CheckState = CheckState.Checked;\r
             }\r
-\r
             else\r
             {\r
-                Mixdown = "stero";\r
+                check_2PassEncode.CheckState = CheckState.Unchecked;\r
             }\r
 \r
-            if ((Mixdown !=  ""))\r
+            if (presetQuery.Grayscale == true)\r
             {\r
-                SixChannelAudio = " -6 "+ Mixdown;\r
+                check_grayscale.CheckState = CheckState.Checked;\r
             }\r
-\r
             else\r
             {\r
-                SixChannelAudio = "";\r
+                check_grayscale.CheckState = CheckState.Unchecked;\r
             }\r
 \r
-            string queryAudioSettings = audioBitrate+ audioSampleRate+ audioChannels+ SixChannelAudio;\r
-            // ----------------------------------------------------------------------\r
-\r
-            //  H.264 Tab\r
+            drp_videoFramerate.Text = presetQuery.VideoFramerate;\r
 \r
-            string CRF = CheckCRF.CheckState.ToString();\r
-            string h264Advanced = rtf_h264advanced.Text;\r
-            if ((CRF ==  "1"))\r
+            if (presetQuery.TurboFirstPass == true)\r
             {\r
-                CRF = " -Q ";\r
+                check_turbo.CheckState = CheckState.Checked;\r
             }\r
-\r
             else\r
             {\r
-                CRF = "";\r
+                check_turbo.CheckState = CheckState.Unchecked;\r
             }\r
 \r
-            if ((h264Advanced ==  ""))\r
+            if (presetQuery.LargeMP4 == true)\r
             {\r
-                h264Advanced = "";\r
+                check_largeFile.CheckState = CheckState.Checked;\r
             }\r
-\r
             else\r
             {\r
-                h264Advanced = " -x "+ h264Advanced;\r
+                check_largeFile.CheckState = CheckState.Unchecked;\r
             }\r
-\r
-            string h264Settings = CRF+ h264Advanced;\r
-            // ----------------------------------------------------------------------\r
-\r
-            // Processors (Program Settings)\r
-\r
-            string processors = Properties.Settings.Default.Processors;\r
-            //  Number of Processors Handler\r
-\r
-            if ((processors ==  "Automatic"))\r
+            if (presetQuery.CRF == true)\r
             {\r
-                processors = "";\r
+                CheckCRF.CheckState = CheckState.Checked;\r
             }\r
-\r
             else\r
             {\r
-                processors = " -C "+ processors+ " ";\r
-            }\r
-\r
-            string queryAdvancedSettings = processors;\r
-            // ----------------------------------------------------------------------\r
-\r
-            //  Verbose option (Program Settings)\r
-\r
-            string verbose = "";\r
-            if ( Properties.Settings.Default.verbose ==  "1")\r
-            {\r
-                verbose = " -v ";\r
+                CheckCRF.CheckState = CheckState.Unchecked;\r
             }\r
+            #endregion\r
 \r
-            // ----------------------------------------------------------------------\r
-\r
-            return querySource+ queryDestination+ queryPictureSettings+ queryVideoSettings+ h264Settings+ queryAudioSettings+ queryAdvancedSettings+ verbose;\r
+            // Audio Settings Tab\r
+            #region audio\r
+            drp_audioBitrate.Text = presetQuery.AudioBitrate;\r
+            drp_audioSampleRate.Text = presetQuery.AudioSampleBitrate;\r
+            drp_audioChannels.Text = presetQuery.AudioTrack1;\r
+            drp_audioMixDown.Text = presetQuery.AudioTrackMix;\r
+            drp_subtitle.Text = presetQuery.Subtitles;\r
+            #endregion\r
+\r
+            // H264 Tab & Preset Name\r
+            #region other\r
+            rtf_h264advanced.Text = presetQuery.H264Query;\r
+\r
+            // Set the preset name\r
+            groupBox_output.Text = "Output Settings (Preset: " + name + ")";\r
+            #endregion\r
         }\r
 \r
+        #endregion\r
+\r
         // This is the END of the road ------------------------------------------------------------------------------\r
     }\r
 }
\ No newline at end of file