OSDN Git Service

Put the correct subtitle language in the Queue for the Mac GUI.
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmMain.cs
index ff1ec3b..76d9552 100644 (file)
@@ -1,6 +1,11 @@
+/*  frmMain.cs $\r
+       \r
+          This file is part of the HandBrake source code.\r
+          Homepage: <http://handbrake.m0k.org/>.\r
+          It may be used under the terms of the GNU General Public License. */\r
+\r
 using System;\r
-using System.Collections.Generic;\r
-using System.Collections.Specialized;\r
+using System.Collections;\r
 using System.ComponentModel;\r
 using System.Data;\r
 using System.Drawing;\r
@@ -11,11 +16,11 @@ using System.IO;
 using System.Diagnostics;\r
 using System.Threading;\r
 using System.Runtime.InteropServices;\r
-\r
+using System.Globalization;\r
+using System.Text.RegularExpressions;\r
 \r
 namespace Handbrake\r
 {\r
-\r
     public partial class frmMain : Form\r
     {\r
         // -------------------------------------------------------------- \r
@@ -24,408 +29,192 @@ namespace Handbrake
 \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
-        //private frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
+        private Parsing.DVD thisDVD;\r
+        private frmQueue queueWindow = new frmQueue();\r
+        private delegate void updateStatusChanger();\r
+        Functions.Common hb_common_func = new Functions.Common();\r
 \r
\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
+            lblStatus.Size = new Size(250, 20);\r
+            lblStatus.Location = new Point(10, 280);\r
+            splash.Controls.Add(lblStatus);\r
 \r
-            ThreadPool.QueueUserWorkItem(showSplash);\r
-            Thread.Sleep(1000);\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
-            // This is a quick Hack fix for the cross-thread problem with frmDvdIndo ************************\r
-            //dvdInfoWindow.Show();\r
-            //dvdInfoWindow.Hide();\r
-            // **********************************************************************************************\r
+            // show the form, but leave disabled until preloading is complete then show the main form\r
+            this.Enabled = false;\r
 \r
-            // System Requirements Check\r
-            systemCheck();\r
+            this.Show();\r
 \r
-            // Set the Version number lable to the corect version.\r
-            Version.Text = "Version " + Properties.Settings.Default.GuiVersion;\r
+            // Forces frmMain to draw\r
+            Application.DoEvents();\r
 \r
-            // Run the update checker.\r
-            updateCheck();\r
+            // update the status\r
+            if (Properties.Settings.Default.updateStatus == "Checked")\r
+            {\r
+                lblStatus.Text = "Checking for updates ...";\r
+                Application.DoEvents();\r
+                Thread updateCheckThread = new Thread(startupUpdateCheck);\r
+                updateCheckThread.Start();\r
+                Thread.Sleep(100);\r
+            }\r
 \r
+            // Load the presets\r
+            lblStatus.Text = "Loading Presets ...";\r
+            Application.DoEvents();\r
+            loadPresetPanel();\r
+            Thread.Sleep(200);\r
+      \r
             // Now load the users default if required. (Will overide the above setting)\r
-            loadUserDefaults();\r
-\r
-            // Display the quick start window if required.\r
-            quickStart();\r
+            if (Properties.Settings.Default.defaultSettings == "Checked")\r
+            {\r
+                lblStatus.Text = "Loading User Default Settings...";\r
+                Application.DoEvents();\r
+                loadUserDefaults();\r
+                Thread.Sleep(100);\r
+            }\r
+            else\r
+                loadNormalPreset();\r
 \r
             // Enable or disable tooltips\r
-            tooltip();\r
-\r
-\r
-            // Hide the presets part of the window\r
-            this.Width = 590;\r
-\r
-            // Create and initializes a new StringCollection.\r
-            StringCollection myCol = new StringCollection();\r
-            // Add a range of elements from an array to the end of the StringCollection.\r
-            String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };\r
-            myCol.AddRange(myArr);\r
-\r
-            Properties.Settings.Default.BuiltInPresets = myCol;\r
-  \r
-\r
-        }\r
-\r
-        // Functions to preform tasks required on startup.\r
-        #region Initializeation Functions\r
-        \r
-        private void showSplash(object sender)\r
-        {\r
-            Form splash = new frmSplashScreen();\r
-            splash.Show();\r
-            Thread.Sleep(1500);  // Display splash screen for 1.5 Seconds\r
-            splash.Close(); // Then close.\r
-        }\r
-\r
-        private void loadUserDefaults()\r
-        { \r
-            try\r
+            if (Properties.Settings.Default.tooltipEnable == "Checked")\r
             {\r
-                if (Properties.Settings.Default.defaultSettings == "Checked")\r
-                {\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
-\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
-\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
-\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
-\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_deblock.CheckState = CheckState.Checked;\r
-                    }\r
-\r
+                lblStatus.Text = "Loading Tooltips ...";\r
+                Application.DoEvents();\r
+                ToolTip.Active = true;\r
+                Thread.Sleep(100);\r
+            }\r
 \r
-                    if (Properties.Settings.Default.Grayscale == "Checked")\r
-                    {\r
-                        check_grayscale.CheckState = CheckState.Checked;\r
-                    }\r
+            // Set some defaults for the dropdown menus. Just incase the normal or user presets dont load.\r
+            drp_crop.SelectedIndex = 0;\r
+            drp_videoEncoder.SelectedIndex = 2;\r
+            drp_audioCodec.SelectedIndex = 0;\r
 \r
-                    drp_videoFramerate.Text = Properties.Settings.Default.Framerate;\r
+            //Finished Loading\r
+            lblStatus.Text = "Loading Complete!";\r
+            Application.DoEvents();\r
+            Thread.Sleep(200);\r
 \r
-                    if (Properties.Settings.Default.PixelRatio == "Checked")\r
-                    {\r
-                        CheckPixelRatio.CheckState = CheckState.Checked;\r
-                    }\r
-                    if (Properties.Settings.Default.turboFirstPass == "Checked")\r
-                    {\r
-                        check_turbo.CheckState = CheckState.Checked;\r
-                    }\r
-                    if (Properties.Settings.Default.largeFile == "Checked")\r
-                    {\r
-                        check_largeFile.CheckState = CheckState.Checked;\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
+            // Wait until splash screen is done\r
+            while (timer.IsAlive)\r
+            { Thread.Sleep(100); }\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
+            //Close the splash screen\r
+            splash.Close();\r
+            splash.Dispose();\r
 \r
-                    groupBox_output.Text = "Output Settings (Preset: " + Properties.Settings.Default.SelectedPreset + ")";\r
-                }\r
-                else\r
-                {\r
-                    // Load the default preset on lauch\r
-                    ListBox_Presets.SelectedItem = "Normal";\r
-                }\r
-            }\r
-            catch (Exception)\r
-            {\r
-                // No real need to alert the user. Try/Catch only in just incase there is a problem reading the settings xml file.\r
-            }\r
+            // Turn the interface back to the user\r
+            this.Enabled = true;\r
         }\r
-\r
-        // This is a re-usable function.\r
-        private Boolean updateCheck()\r
+    \r
+        private void startupUpdateCheck()\r
         {\r
             try\r
             {\r
-                if (Properties.Settings.Default.updateStatus == "Checked")\r
+                if (this.InvokeRequired)\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
-                    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
+                    this.BeginInvoke(new updateStatusChanger(startupUpdateCheck));\r
+                    return;\r
                 }\r
-                else\r
+                \r
+                Boolean update = hb_common_func.updateCheck();\r
+                if (update == true)\r
                 {\r
-                    return false;\r
+                    frmUpdater updateWindow = new frmUpdater();\r
+                    updateWindow.Show();\r
                 }\r
             }\r
-            catch (Exception)\r
-            {\r
-                // Silently ignore the error\r
-                return false;\r
-            }\r
-        }\r
-\r
-        private void quickStart()\r
-        {\r
-            if ((Properties.Settings.Default.QuickStartWindow == "Checked") || (Properties.Settings.Default.QuickStartWindow == ""))\r
-            {\r
-                frmQuickStart quickstart = new frmQuickStart();\r
-                quickstart.ShowDialog();\r
-            }\r
-        }\r
-\r
-        private void tooltip()\r
-        {\r
-            if (Properties.Settings.Default.tooltipEnable == "Checked")\r
+            catch (Exception exc)\r
             {\r
-                ToolTip.Active = true;\r
+                MessageBox.Show(exc.ToString());\r
             }\r
         }\r
 \r
-        #region Memory Check\r
-\r
-        public struct MEMORYSTATUS\r
+        private void splashTimer(object sender)\r
         {\r
-            public UInt32 dwLength;\r
-            public UInt32 dwMemoryLoad;\r
-            public UInt32 dwTotalPhys; // Used\r
-            public UInt32 dwAvailPhys;\r
-            public UInt32 dwTotalPageFile;\r
-            public UInt32 dwAvailPageFile;\r
-            public UInt32 dwTotalVirtual;\r
-            public UInt32 dwAvailVirtual;\r
-            // Aditional Varibles left in for future usage (JIC)\r
+            Thread.Sleep(1000);  //sit for 1 seconds then exit\r
         }\r
 \r
-        [DllImport("kernel32.dll")]\r
-        public static extern void GlobalMemoryStatus\r
-        (\r
-            ref MEMORYSTATUS lpBuffer\r
-        );\r
-\r
-        public uint CheckMemeory()\r
+        private void showSplash(object sender)\r
         {\r
-            // Call the native GlobalMemoryStatus method\r
-            // with the defined structure.\r
-            MEMORYSTATUS memStatus = new MEMORYSTATUS();\r
-            GlobalMemoryStatus(ref memStatus);\r
-\r
-            // Use a StringBuilder for the message box string.\r
-            uint MemoryInfo = memStatus.dwTotalPhys;\r
-\r
-            // Return the Ram Information\r
-            return MemoryInfo;\r
-\r
+            Form splash = new frmSplashScreen();\r
+            splash.Show();\r
         }\r
 \r
-\r
-        #endregion\r
-        Boolean preventLaunch = false;\r
-        private void systemCheck()\r
+        private void loadUserDefaults()\r
         {\r
+            string userDefaults = Properties.Settings.Default.defaultUserSettings;\r
             try\r
             {\r
-                // Make sure the screen resolution is not below 1024x768\r
-                System.Windows.Forms.Screen scr = System.Windows.Forms.Screen.PrimaryScreen;\r
-                if ((scr.Bounds.Width < 1024) || (scr.Bounds.Height < 768))\r
-                {\r
-                    MessageBox.Show("Your system does not meet the minimum requirements for HandBrake. \n Screen resolution is too Low. Must be 1024x768 or greater", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
-                    preventLaunch = true;\r
-                }\r
-\r
-                // Make sure the system has enough RAM. 384MB or greater\r
-                uint memory = CheckMemeory();\r
+                // Some things that need to be done to reset some gui components:\r
+                CheckPixelRatio.CheckState = CheckState.Unchecked;\r
 \r
-                if (memory < 319) // Set to 319 to allow for 64MB dedicated to video Memory and Windows returnig the memory figure slightly out.\r
-                {\r
-                    MessageBox.Show("Your system does not meet the minimum requirements for HandBrake. \n Insufficient Memory. 384MB or greater required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
-                    preventLaunch = true;\r
-                }\r
+                // Send the query from the file to the Query Parser class Then load the preset\r
+                Functions.QueryParser presetQuery = Functions.QueryParser.Parse(userDefaults);\r
+                hb_common_func.presetLoader(this, presetQuery, "User Defaults ");\r
             }\r
             catch (Exception exc)\r
             {\r
-                if (Properties.Settings.Default.GuiDebug == "Checked")\r
-                {\r
-                    MessageBox.Show("frmMain.cs - systemCheck() " + exc.ToString());\r
-                }\r
+                MessageBox.Show("Unable to load user Default Settings. \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
             }\r
         }\r
 \r
         #endregion\r
 \r
-        // Close the Application on main window load if required by the system Check\r
-        #region Form Load\r
-            private void frmMain_Load(object sender, EventArgs e)\r
+        #region Set Varible Function\r
+        public void setStreamReader(Parsing.DVD dvd)\r
         {\r
-            if (preventLaunch == true)\r
-            {\r
-                Application.Exit();\r
-            }\r
+            this.thisDVD = dvd;\r
         }\r
         #endregion\r
 \r
-        #endregion\r
-\r
         // -------------------------------------------------------------- \r
         // The main Menu bar.\r
         // -------------------------------------------------------------- \r
 \r
         #region File Menu\r
-\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
-                    drp_deInterlace_option.Text = line.ReadLine();\r
-\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_grayscale.CheckState = CheckState.Checked;\r
-                    }\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
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        CheckPixelRatio.CheckState = CheckState.Checked;\r
-                    }\r
-\r
-                    temporyLine = line.ReadLine();\r
-                    if (temporyLine == "Checked")\r
-                    {\r
-                        check_turbo.CheckState = CheckState.Checked;\r
-                    }\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
+                    // Send the query from the file to the Query Parser class then load the preset\r
+                    Functions.QueryParser presetQuery = Functions.QueryParser.Parse(line.ReadLine());\r
+                    hb_common_func.presetLoader(this, 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
-                    MessageBox.Show("Unable to load profile.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
+                }\r
+                catch (Exception exc)\r
+                {\r
+                    MessageBox.Show("Unable to load profile. \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
                 }\r
             }\r
         }\r
 \r
         private void mnu_save_Click(object sender, EventArgs e)\r
         {\r
-\r
             string filename;\r
             File_Save.ShowDialog();\r
             filename = File_Save.FileName;\r
@@ -436,52 +225,19 @@ namespace Handbrake
                     // Create a StreamWriter and open the file\r
                     StreamWriter line = new StreamWriter(filename);\r
 \r
-                    //Source\r
-                    line.WriteLine(text_source.Text);\r
-                    line.WriteLine(drp_dvdtitle.Text);\r
-                    line.WriteLine(drop_chapterStart.Text);\r
-                    line.WriteLine(drop_chapterFinish.Text);\r
-                    //Destination\r
-                    line.WriteLine(text_destination.Text);\r
-                    line.WriteLine(drp_videoEncoder.Text);\r
-                    line.WriteLine(drp_audioCodec.Text);\r
-                    line.WriteLine(text_width.Text);\r
-                    line.WriteLine(text_height.Text);\r
-                    //Picture Settings Tab\r
-                    line.WriteLine(text_top.Text);\r
-                    line.WriteLine(text_bottom.Text);\r
-                    line.WriteLine(text_left.Text);\r
-                    line.WriteLine(text_right.Text);\r
-                    line.WriteLine(drp_subtitle.Text);\r
-                    //Video Settings Tab\r
-                    line.WriteLine(text_bitrate.Text);\r
-                    line.WriteLine(text_filesize.Text);\r
-                    line.WriteLine(slider_videoQuality.Value.ToString());\r
-                    line.WriteLine(check_2PassEncode.CheckState.ToString());\r
-                    line.WriteLine(drp_deInterlace_option.Text);\r
-                    line.WriteLine(check_grayscale.CheckState.ToString());\r
-                    line.WriteLine(drp_videoFramerate.Text);\r
-                    line.WriteLine(Check_ChapterMarkers.CheckState.ToString());\r
-                    line.WriteLine(CheckPixelRatio.CheckState.ToString());\r
-                    line.WriteLine(check_turbo.CheckState.ToString());\r
-                    line.WriteLine(check_largeFile.CheckState.ToString());\r
-                    //Audio Settings Tab\r
-                    line.WriteLine(drp_audioBitrate.Text);\r
-                    line.WriteLine(drp_audioSampleRate.Text);\r
-                    line.WriteLine(drp_audioChannels.Text);\r
-                    line.WriteLine(drp_audioMixDown.Text);\r
-                    //H264 Tab\r
-                    line.WriteLine(CheckCRF.CheckState.ToString());\r
-                    line.WriteLine(rtf_h264advanced.Text);\r
+                    // Generate and write the query string to the file\r
+                    String query = hb_common_func.GenerateTheQuery(this);\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
 \r
@@ -498,7 +254,7 @@ namespace Handbrake
         {\r
             showQueue();\r
         }\r
-   \r
+\r
         private void mnu_viewDVDdata_Click(object sender, EventArgs e)\r
         {\r
             frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
@@ -511,48 +267,25 @@ namespace Handbrake
             Options.ShowDialog();\r
         }\r
 \r
+\r
         #endregion\r
 \r
         #region Presets Menu\r
 \r
         private void mnu_presetReset_Click(object sender, EventArgs e)\r
         {\r
-            ListBox_Presets.Items.Clear();\r
-            ListBox_Presets.Items.Add("Animation");\r
-            ListBox_Presets.Items.Add("AppleTV");\r
-            ListBox_Presets.Items.Add("Bedlam");\r
-            ListBox_Presets.Items.Add("Blind");\r
-            ListBox_Presets.Items.Add("Broke");\r
-            ListBox_Presets.Items.Add("Classic");\r
-            ListBox_Presets.Items.Add("Constant Quality Rate");\r
-            ListBox_Presets.Items.Add("Deux Six Quatre");\r
-            ListBox_Presets.Items.Add("Film");\r
-            ListBox_Presets.Items.Add("iPhone");\r
-            ListBox_Presets.Items.Add("iPod High-Rez");\r
-            ListBox_Presets.Items.Add("iPod Low-Rez");\r
-            ListBox_Presets.Items.Add("Normal");\r
-            ListBox_Presets.Items.Add("PS3");\r
-            ListBox_Presets.Items.Add("PSP");\r
-            ListBox_Presets.Items.Add("QuickTime");\r
-            ListBox_Presets.Items.Add("Television");\r
-\r
-            if (presetStatus == false)\r
-            {\r
-                this.Width = 881;\r
-                presetStatus = true;\r
-                btn_presets.Text = "Hide Presets";\r
-            }\r
+            treeView_presets.Nodes.Clear();\r
+            grabCLIPresets();\r
+            loadPresetPanel();\r
+            if (treeView_presets.Nodes.Count == 0)\r
+                MessageBox.Show("Unable to load the presets.dat file. Please select \"Update Built-in Presets\" from the Presets Menu \nMake sure you are running the program in Admin mode if running on Vista. See Windows FAQ for details!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            else\r
+                MessageBox.Show("Presets have been updated", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
         }\r
 \r
         private void mnu_SelectDefault_Click(object sender, EventArgs e)\r
         {\r
-            ListBox_Presets.SelectedItem = "Normal";\r
-            if (presetStatus == false)\r
-            {\r
-                this.Width = 881;\r
-                presetStatus = true;\r
-                btn_presets.Text = "Hide Presets";\r
-            }\r
+            loadNormalPreset();\r
         }\r
 \r
         #endregion\r
@@ -561,7 +294,7 @@ namespace Handbrake
 \r
         private void mnu_wiki_Click(object sender, EventArgs e)\r
         {\r
-           Process.Start("http://handbrake.m0k.org/trac");\r
+            Process.Start("http://handbrake.m0k.org/trac");\r
         }\r
 \r
         private void mnu_faq_Click(object sender, EventArgs e)\r
@@ -576,7 +309,7 @@ namespace Handbrake
 \r
         private void mnu_homepage_Click(object sender, EventArgs e)\r
         {\r
-           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
@@ -586,84 +319,74 @@ namespace Handbrake
 \r
         private void mnu_UpdateCheck_Click(object sender, EventArgs e)\r
         {\r
-            Boolean update = updateCheck();\r
+            Boolean update = hb_common_func.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
+                frmUpdater updateWindow = new frmUpdater();\r
+                updateWindow.Show();\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
+            Form About = new frmAbout();\r
             About.ShowDialog();\r
         }\r
 \r
         #endregion\r
 \r
         // -------------------------------------------------------------- \r
-        // Buttons on the main Window\r
+        // Buttons on the main Window and items that require actions\r
         // --------------------------------------------------------------\r
 \r
         #region Buttons\r
 \r
         private void btn_Browse_Click(object sender, EventArgs e)\r
         {\r
-            String filename =""; \r
-                   text_source.Text = "";\r
-            frmDvdInfo dvdInfoWindow = new frmDvdInfo();                \r
-                   if (RadioDVD.Checked) \r
-                   { \r
-                       DVD_Open.ShowDialog(); \r
-                       filename = DVD_Open.SelectedPath; \r
-                       if (filename != "") \r
-                       { \r
-                           Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow); \r
-                           text_source.Text = filename; \r
-                           frmRD.ShowDialog(); \r
-                       } \r
-                    } \r
-                    else \r
-                    { \r
-                       ISO_Open.ShowDialog(); \r
-                       filename = ISO_Open.FileName; \r
-                       if (filename != "") \r
-                       { \r
-                           Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow); \r
-                           text_source.Text = filename; \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
-                    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
+            String filename = "";\r
+            text_source.Text = "";\r
+            frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
+\r
+            if (RadioDVD.Checked)\r
+            {\r
+                DVD_Open.ShowDialog();\r
+                filename = DVD_Open.SelectedPath;\r
+            }\r
+            else\r
+            {\r
+                ISO_Open.ShowDialog();\r
+                filename = ISO_Open.FileName;\r
+            }\r
+\r
+            if (filename != "")\r
+            {\r
+                Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow);\r
+                text_source.Text = filename;\r
+                frmRD.ShowDialog();\r
+            }\r
+            else\r
+                text_source.Text = "Click 'Browse' to continue";\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
+                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
         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
+            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
+            // 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
-                text_destination.Text = destination;\r
-            }\r
+                text_destination.Text = text_destination.Text.Replace(".mp4", ".m4v");\r
         }\r
 \r
         private void btn_h264Clear_Click(object sender, EventArgs e)\r
@@ -671,424 +394,103 @@ namespace Handbrake
             rtf_h264advanced.Text = "";\r
         }\r
 \r
-        private void GenerateQuery_Click(object sender, EventArgs e)\r
+        private void btn_ActivityWindow_Click(object sender, EventArgs e)\r
         {\r
-            String query = GenerateTheQuery();\r
-            QueryEditorText.Text = query;\r
+            Form ActivityWindow = new frmActivityWindow();\r
+            ActivityWindow.ShowDialog();\r
         }\r
 \r
-        private void btn_ClearQuery_Click(object sender, EventArgs e)\r
-        {\r
-            QueryEditorText.Text = "";\r
-        }\r
+        #endregion\r
 \r
-        private void btn_queue_Click(object sender, EventArgs e)\r
-        {\r
-            if (text_destination.Text != "" && text_source.Text != "")\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
-            else \r
-            {\r
-                MessageBox.Show("No Source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-            }\r
-        }\r
+        #region frmMain Actions\r
 \r
-        private void btn_copy_Click(object sender, EventArgs e)\r
+        private void drp_dvdtitle_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            Clipboard.SetText(QueryEditorText.Text, TextDataFormat.Text);\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
 \r
-        private void showQueue()\r
-        {\r
-            queueWindow.Show();\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
-        #endregion\r
+                // Set the Aspect Ratio\r
+                lbl_Aspect.Text = selectedTitle.AspectRatio.ToString();\r
 \r
-        // -------------------------------------------------------------- \r
-        // Main Window Preset System\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
-        #region Preset System\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
+                    drop_chapterStart.Text = drop_chapterStart.Items[0].ToString();\r
 \r
-        // Varibles\r
-        private Boolean presetStatus = false;\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
+                    drop_chapterFinish.Text = drop_chapterFinish.Items[drop_chapterFinish.Items.Count - 1].ToString();\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
+                // Populate the Audio Channels Dropdown\r
+                drp_track1Audio.Items.Clear();\r
+                drp_track1Audio.Items.Add("Automatic");\r
+                drp_track1Audio.Items.Add("None");\r
+                drp_track1Audio.Items.AddRange(selectedTitle.AudioTracks.ToArray());\r
+                if (drp_track1Audio.Items.Count > 0)\r
+                    drp_track1Audio.Text = drp_track1Audio.Items[0].ToString();\r
+\r
+                drp_track2Audio.Items.Clear();\r
+                drp_track2Audio.Items.Add("None");\r
+                drp_track2Audio.Items.AddRange(selectedTitle.AudioTracks.ToArray());\r
+                if (drp_track2Audio.Items.Count > 0)\r
+                    drp_track2Audio.Text = drp_track2Audio.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.Add("Autoselect");\r
+                drp_subtitle.Items.AddRange(selectedTitle.Subtitles.ToArray());\r
+                if (drp_subtitle.Items.Count > 0)\r
+                    drp_subtitle.Text = drp_subtitle.Items[0].ToString();\r
 \r
-        private void btn_addPreset_Click(object sender, EventArgs e)\r
-        {\r
+            }\r
 \r
+            // Run the autoName & chapterNaming functions\r
+            hb_common_func.autoName(this);\r
+            hb_common_func.chapterNaming(this);\r
         }\r
 \r
-        private void btn_removePreset_Click(object sender, EventArgs e)\r
+        private void drop_chapterStart_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            ListBox_Presets.Items.Remove(ListBox_Presets.SelectedItem);\r
-        }\r
+            drop_chapterStart.BackColor = Color.White;\r
+            if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
+            {\r
+                try\r
+                {\r
+                    int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
+                    int chapterStart = int.Parse(drop_chapterStart.Text);\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
-            //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.SelectedPreset = ListBox_Presets.SelectedItem.ToString();\r
-            Properties.Settings.Default.Save();\r
+                    if (chapterFinish < chapterStart)\r
+                        drop_chapterStart.BackColor = Color.LightCoral;\r
+                }\r
+                catch (Exception)\r
+                {\r
+                    drop_chapterStart.BackColor = Color.LightCoral;\r
+                }\r
+            }\r
+            // Run the Autonaming function\r
+            hb_common_func.autoName(this);\r
         }\r
 \r
-\r
-        // Preset Seleciton\r
-        private void ListBox_Presets_SelectedIndexChanged(object sender, EventArgs e)\r
+        private void drop_chapterFinish_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            string selectedPreset = null;\r
-            if (ListBox_Presets.SelectedItem != null)\r
-            {\r
-                selectedPreset = ListBox_Presets.SelectedItem.ToString();\r
-            }\r
-            else\r
-            {\r
-                selectedPreset = "";\r
-            }\r
-\r
-            switch (selectedPreset)\r
-            {\r
-                case "Animation":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "1000", "", 0, "0%", "160", CheckState.Checked, "48", "ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2", "Origional (Fast)", CheckState.Checked, "No Crop", CheckState.Checked, "AAC", "Output Settings (Preset: Apple Animation)");\r
-                    setMkv();\r
-                    break;\r
-                case "AppleTV":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "2500", "", 0, "0%", "160", CheckState.Checked, "48", "bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: Apple TV)");\r
-                    break;\r
-                case "Bedlam":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "1800", "", 0, "0%", "160", CheckState.Checked, "48", "ref=16:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:me-range=64:analyse=all:8x8dct:trellis=2:no-fast-pskip:no-dct-decimate:filter=-2,-1", "None", CheckState.Checked, "No Crop", CheckState.Checked, "AAC", "Output Settings (Preset: Bedlam)");\r
-                    setMkv();\r
-                    break;\r
-                case "Blind":\r
-                    setGuiSetttings(CheckState.Unchecked, "512", "", "H.264", "512", "", 0, "0%", "128", CheckState.Checked, "48", "", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: Blind)");\r
-                    break;\r
-                case "Broke":\r
-                    setGuiSetttings(CheckState.Unchecked, "640", "", "H.264", "", "695", 0, "0%", "128", CheckState.Checked, "48", "ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:b-pyramid::direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip", "None", CheckState.Checked, "No Crop", CheckState.Checked, "AAC", "Output Settings (Preset: Broke)");\r
-                    break;\r
-                case "Classic":\r
-                    setGuiSetttings(CheckState.Unchecked, "", "", "H.264", "1000", "", 0, "0%", "160", CheckState.Unchecked, "48", "", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: Classic)");\r
-                    break;\r
-                case "Constant Quality Rate":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "", "", 64, "64%", "160", CheckState.Checked, "48", "ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AC3", "Output Settings (Preset: CQR)");\r
-                    setMkv();\r
-                    break;\r
-                case "Deux Six Quatre":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "1600", "", 0, "0%", "160", CheckState.Checked, "48", "ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip", "None", CheckState.Checked, "No Crop", CheckState.Checked, "AC3", "Output Settings (Preset: DSQ)");\r
-                    setMkv();\r
-                    break;\r
-                case "Film":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "2000", "", 0, "0%", "160", CheckState.Checked, "48", "ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:no-fast-pskip", "None", CheckState.Checked, "No Crop", CheckState.Checked, "AC3", "Output Settings (Preset: Film)");\r
-                    setMkv();\r
-                    break;\r
-                case "iPhone":\r
-                    setGuiSetttings(CheckState.Unchecked, "480", "", "H.264", "960", "", 0, "0%", "128", CheckState.Checked, "48", "cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: iPhone)");\r
-                    break;\r
-                case "iPod High-Rez":\r
-                    setGuiSetttings(CheckState.Unchecked, "640", "", "H.264", "1500", "", 0, "0%", "160", CheckState.Checked, "48", "keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: iPod High Rez)");\r
-                    break;\r
-                case "iPod Low-Rez":\r
-                    setGuiSetttings(CheckState.Unchecked, "320", "", "H.264", "700", "", 0, "0%", "160", CheckState.Checked, "48", "keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: iPod Low Rez)");\r
-                    break;\r
-                case "Normal":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "1500", "", 0, "0%", "160", CheckState.Checked, "48", "ref=2:bframes=2:subme=5:me=umh", "None", CheckState.Checked, "No Crop", CheckState.Checked, "AAC", "Output Settings (Preset: Normal)");\r
-                    break;\r
-                case "PS3":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "2500", "", 0, "0%", "160", CheckState.Checked, "48", "level=41:subme=5:me=umh", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: PS3)");\r
-                    break;\r
-                case "PSP":\r
-                    setGuiSetttings(CheckState.Unchecked, "368", "208", "Mpeg 4", "1024", "", 0, "0%", "160", CheckState.Unchecked, "48", "", "None", CheckState.Unchecked, "No Crop", CheckState.Unchecked, "AAC", "Output Settings (Preset: PSP)");\r
-                    break;\r
-                case "QuickTime":\r
-                    setGuiSetttings(CheckState.Checked, "", "", "H.264", "2000", "", 0, "0%", "160", CheckState.Checked, "48", "ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct-auto:me=umh:subme=5:analyse=all:8x8dct:trellis=1:no-fast-pskip", "None", CheckState.Checked, "No Crop", CheckState.Checked, "AAC", "Output Settings (Preset: Quicktime)");\r
-                    break;\r
-                case "Television":\r
-                    setGuiSetttings(CheckState.Unchecked, "", "", "H.264", "1300", "", 0, "0%", "160", CheckState.Checked, "48", "ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip", "Origional (Fast)", CheckState.Checked, "No Crop", CheckState.Checked, "AAC", "Output Settings (Preset: Television)");\r
-                    setMkv();\r
-                    break;\r
-                default:\r
-                    break;\r
-            }\r
-        }\r
-\r
-\r
-\r
-\r
-\r
-        // Functions - It's a bit dirty but i'll sort this out later. Simply done to reduce the amount of code above.\r
-        private void setGuiSetttings(CheckState anamorphic, string width, string height, string vencoder, string bitrate, string filesize, int quality, string qpercent, string audioBit, CheckState chpt, string audioSample, string h264, string deinterlace, CheckState twopass, string crop, CheckState turbo, string audioCodec, string preset)\r
-        {\r
-            CheckPixelRatio.CheckState = anamorphic;\r
-            text_width.Text = width;\r
-            text_height.Text = height;\r
-            drp_videoEncoder.Text = vencoder;\r
-            text_bitrate.Text = bitrate;\r
-            text_filesize.Text = filesize;\r
-            slider_videoQuality.Value = quality;\r
-            SliderValue.Text = qpercent;\r
-            drp_audioBitrate.Text = audioBit;\r
-            Check_ChapterMarkers.CheckState = chpt;\r
-            drp_audioSampleRate.Text = audioSample;\r
-            rtf_h264advanced.Text = h264;\r
-            drp_deInterlace_option.Text = deinterlace;\r
-            check_2PassEncode.CheckState = twopass;\r
-            drp_crop.Text = crop;\r
-            check_turbo.CheckState = turbo;\r
-            drp_audioCodec.Text = audioCodec;\r
-\r
-            groupBox_output.Text = preset;\r
-        }\r
-\r
-        private void setMkv()\r
-        {\r
-            // Set file extension to MKV\r
-            string destination = text_destination.Text;\r
-            destination = destination.Replace(".mp4", ".mkv");\r
-            destination = destination.Replace(".avi", ".mkv");\r
-            destination = destination.Replace(".m4v", ".mkv");\r
-            destination = destination.Replace(".ogm", ".mkv");\r
-            text_destination.Text = destination;\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
-            //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
-        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
-\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.StandardError.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
-                catch (Exception)\r
-                {\r
-                    // Do nothing\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
-                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
-                        drop_chapterStart.BackColor = Color.LightCoral;\r
-                    }\r
-                }\r
-                catch (Exception)\r
-                {\r
-                    drop_chapterStart.BackColor = Color.LightCoral;\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
+            drop_chapterFinish.BackColor = Color.White;\r
+            if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
             {\r
                 try\r
                 {\r
@@ -1096,15 +498,16 @@ namespace Handbrake
                     int chapterStart = int.Parse(drop_chapterStart.Text);\r
 \r
                     if (chapterFinish < chapterStart)\r
-                    {\r
                         drop_chapterFinish.BackColor = Color.LightCoral;\r
-                    }\r
                 }\r
                 catch (Exception)\r
                 {\r
                     drop_chapterFinish.BackColor = Color.LightCoral;\r
                 }\r
             }\r
+\r
+            // Run the Autonaming function\r
+            hb_common_func.autoName(this);\r
         }\r
 \r
         private void text_bitrate_TextChanged(object sender, EventArgs e)\r
@@ -1112,8 +515,6 @@ namespace Handbrake
             text_filesize.Text = "";\r
             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
@@ -1121,8 +522,6 @@ namespace Handbrake
             text_bitrate.Text = "";\r
             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
@@ -1130,7 +529,6 @@ 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
@@ -1140,27 +538,23 @@ namespace Handbrake
 \r
         private void text_width_TextChanged(object sender, EventArgs e)\r
         {\r
-\r
             try\r
             {\r
                 if (CheckPixelRatio.Checked)\r
                 {\r
                     text_width.Text = "";\r
-                    text_width.BackColor = Color.White;\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
-                    }\r
                     else\r
-                    {\r
                         text_width.BackColor = Color.LightGreen;\r
-                    }\r
                 }\r
 \r
-                if (lbl_Aspect.Text != "Select a Title")\r
+                if ((lbl_Aspect.Text != "Select a Title") && (drp_crop.SelectedIndex == 2))\r
                 {\r
                     double height = int.Parse(text_width.Text) / double.Parse(lbl_Aspect.Text);\r
                     double mod16 = height % 16;\r
@@ -1172,68 +566,60 @@ namespace Handbrake
                         text_width.BackColor = Color.White;\r
                     }\r
                     else\r
-                    {\r
                         text_height.Text = height.ToString();\r
-                    }\r
                 }\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 (text_height.Text != "Auto")\r
+                if (CheckPixelRatio.Checked)\r
                 {\r
-                    if (CheckPixelRatio.Checked)\r
-                    {\r
-                        text_height.Text = "";\r
-                        text_width.BackColor = Color.White;\r
-                    }\r
+                    text_height.Text = "";\r
+                    text_height.BackColor = Color.LightCoral;\r
+                    CheckPixelRatio.BackColor = Color.LightCoral;\r
+                }\r
+                else\r
+                {\r
+                    if ((int.Parse(text_height.Text) % 16) != 0)\r
+                        text_height.BackColor = Color.LightCoral;\r
                     else\r
-                    {\r
-                        if ((int.Parse(text_height.Text) % 16) != 0)\r
-                        {\r
-                            text_height.BackColor = Color.LightCoral;\r
-                        }\r
-                        else\r
-                        {\r
-                            text_height.BackColor = Color.LightGreen;\r
-                        }\r
-                    }\r
+                        text_height.BackColor = Color.LightGreen;\r
                 }\r
-            } catch(Exception){\r
+\r
+            }\r
+            catch (Exception)\r
+            {\r
                 // No need to alert the user.\r
             }\r
         }\r
 \r
         private void drp_crop_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            if ((string)drp_crop.SelectedItem == "Manual")\r
+            if ((string)drp_crop.SelectedItem == "Custom")\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
+                text_left.Text = "0";\r
+                text_right.Text = "0";\r
+                text_top.Text = "0";\r
+                text_bottom.Text = "0";\r
             }\r
 \r
-            if ((string)drp_crop.SelectedItem == "Auto Crop")\r
+            if ((string)drp_crop.SelectedItem == "Automatic")\r
             {\r
                 text_left.Enabled = false;\r
                 text_right.Enabled = false;\r
                 text_top.Enabled = false;\r
                 text_bottom.Enabled = false;\r
-                text_left.Text = "";\r
-                text_right.Text = "";\r
-                text_top.Text = "";\r
-                text_bottom.Text = "";\r
 \r
                 if (lbl_RecomendedCrop.Text != "Select a Title")\r
                 {\r
@@ -1244,6 +630,13 @@ namespace Handbrake
                     text_top.Text = temp[0];\r
                     text_bottom.Text = temp[1];\r
                 }\r
+                else\r
+                {\r
+                    text_left.Text = "";\r
+                    text_right.Text = "";\r
+                    text_top.Text = "";\r
+                    text_bottom.Text = "";\r
+                }\r
             }\r
 \r
             if ((string)drp_crop.SelectedItem == "No Crop")\r
@@ -1259,30 +652,130 @@ namespace Handbrake
 \r
             }\r
         }\r
-        \r
+\r
+        private void check_vfr_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            if (check_vfr.CheckState == CheckState.Checked)\r
+            {\r
+                check_detelecine.Enabled = false;\r
+                check_detelecine.CheckState = CheckState.Checked;\r
+                drp_videoFramerate.Enabled = false;\r
+                drp_videoFramerate.SelectedItem = "29.97";\r
+                lbl_vfr.Visible = true;\r
+            }\r
+            else\r
+            {\r
+                check_detelecine.Enabled = true;\r
+                drp_videoFramerate.Enabled = true;\r
+                drp_videoFramerate.SelectedItem = "Automatic";\r
+                lbl_vfr.Visible = false;\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
+            if (CheckPixelRatio.Checked)\r
+            {\r
+                check_lAnamorphic.Enabled = false;\r
+                check_lAnamorphic.Checked = false;\r
+                text_height.BackColor = Color.LightGray;\r
+                text_width.BackColor = Color.LightGray;\r
+                text_height.Enabled = false;\r
+                text_width.Enabled = false;\r
+            }\r
+            else\r
+            {\r
+                check_lAnamorphic.Enabled = true;\r
+                text_height.BackColor = Color.White;\r
+                text_width.BackColor = Color.White;\r
+                text_height.Enabled = true;\r
+                text_width.Enabled = true;\r
+            }\r
         }\r
 \r
-        private void drp_dvdtitle_Click(object sender, EventArgs e)\r
+        private void check_lAnamorphic_CheckedChanged(object sender, EventArgs e)\r
         {\r
-            if (drp_dvdtitle.Items.Count == 0)\r
+            if (check_lAnamorphic.Checked)\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
+                CheckPixelRatio.Enabled = false;\r
+                CheckPixelRatio.Checked = false;\r
+                text_height.Text = "";\r
+                text_height.Enabled = false;\r
+                text_height.BackColor = Color.LightGray;\r
+            }\r
+            else\r
+            {\r
+                CheckPixelRatio.Enabled = true;\r
+                text_height.Enabled = true;\r
+                text_height.BackColor = Color.White;\r
             }\r
         }\r
 \r
-        private void drp_audioCodec_SelectedIndexChanged(object sender, EventArgs e)\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
+                    check_turbo.Enabled = true;\r
+            }\r
+            else\r
+            {\r
+                check_turbo.Enabled = false;\r
+                check_turbo.CheckState = CheckState.Unchecked;\r
+            }\r
+        }\r
 \r
-            //CLI Audio mixdown Names: mono stereo dpl1 dpl2 6ch\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
-            drp_audioMixDown.Items.Clear();\r
+        private void check_iPodAtom_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            if (!text_destination.Text.Contains(".mp4"))\r
+            {\r
+                lbl_ipodAtom.Visible = true;\r
+                check_iPodAtom.CheckState = CheckState.Unchecked;\r
+            }\r
+            else\r
+                lbl_ipodAtom.Visible = false;\r
+        }\r
 \r
+        private void check_optimiseMP4_CheckedChanged(object sender, EventArgs e)\r
+        {\r
+            if (!text_destination.Text.Contains(".mp4"))\r
+            {\r
+                check_optimiseMP4.BackColor = Color.LightCoral;\r
+                check_optimiseMP4.CheckState = CheckState.Unchecked;\r
+            }\r
+            else\r
+                check_optimiseMP4.BackColor = Color.Transparent;\r
+        }\r
+\r
+        private void drp_dvdtitle_Click(object sender, EventArgs e)\r
+        {\r
+            if (drp_dvdtitle.Items.Count == 0)\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
+\r
+        private void drp_audioCodec_SelectedIndexChanged(object sender, EventArgs e)\r
+        {\r
             if (drp_audioCodec.Text == "AAC")\r
             {\r
                 drp_audioMixDown.Items.Clear();\r
@@ -1291,7 +784,7 @@ namespace Handbrake
                 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
+\r
                 drp_audioBitrate.Items.Clear();\r
                 drp_audioBitrate.Items.Add("32");\r
                 drp_audioBitrate.Items.Add("40");\r
@@ -1332,29 +825,44 @@ namespace Handbrake
 \r
         private void drp_audioMixDown_SelectedIndexChanged(object sender, EventArgs e)\r
         {\r
-            if (drp_audioCodec.Text == "AAC")\r
+            if ((drp_audioCodec.Text == "AAC") && (drp_audioMixDown.Text == "6 Channel Discrete"))\r
             {\r
-                if (drp_audioMixDown.Text == "6 Channel Discrete")\r
-                {\r
+                drp_audioBitrate.Items.Clear();\r
+                drp_audioBitrate.Items.Add("32");\r
+                drp_audioBitrate.Items.Add("40");\r
+                drp_audioBitrate.Items.Add("48");\r
+                drp_audioBitrate.Items.Add("56");\r
+                drp_audioBitrate.Items.Add("64");\r
+                drp_audioBitrate.Items.Add("80");\r
+                drp_audioBitrate.Items.Add("86");\r
+                drp_audioBitrate.Items.Add("112");\r
+                drp_audioBitrate.Items.Add("128");\r
+                drp_audioBitrate.Items.Add("160");\r
+                drp_audioBitrate.Items.Add("192");\r
+                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
-                    drp_audioBitrate.Items.Clear();\r
-                    drp_audioBitrate.Items.Add("32");\r
-                    drp_audioBitrate.Items.Add("40");\r
-                    drp_audioBitrate.Items.Add("48");\r
-                    drp_audioBitrate.Items.Add("56");\r
-                    drp_audioBitrate.Items.Add("64");\r
-                    drp_audioBitrate.Items.Add("80");\r
-                    drp_audioBitrate.Items.Add("86");\r
-                    drp_audioBitrate.Items.Add("112");\r
-                    drp_audioBitrate.Items.Add("128");\r
-                    drp_audioBitrate.Items.Add("160");\r
-                    drp_audioBitrate.Items.Add("192");\r
-                    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
+        private void slider_drc_Scroll(object sender, EventArgs e)\r
+        {\r
+            double value = slider_drc.Value / 10.0;\r
+            value++;\r
+\r
+            lbl_drc.Text = value.ToString();\r
+        }\r
+\r
+        private void drp_subtitle_SelectedIndexChanged(object sender, EventArgs e)\r
+        {\r
+            if (drp_subtitle.Text.Contains("None"))\r
+            {\r
+                check_forced.Enabled = false;\r
+                check_forced.Checked = false;\r
             }\r
+            else\r
+                check_forced.Enabled = true;\r
         }\r
 \r
         private void Check_ChapterMarkers_CheckedChanged(object sender, EventArgs e)\r
@@ -1364,21 +872,17 @@ namespace Handbrake
                 string destination = text_destination.Text;\r
                 destination = destination.Replace(".mp4", ".m4v");\r
                 text_destination.Text = destination;\r
+                data_chpt.Rows.Clear();\r
+                data_chpt.Enabled = true;\r
+                hb_common_func.chapterNaming(this);\r
             }\r
             else\r
             {\r
                 string destination = text_destination.Text;\r
                 destination = destination.Replace(".m4v", ".mp4");\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
+                data_chpt.Rows.Clear();\r
+                data_chpt.Enabled = false;\r
             }\r
         }\r
 \r
@@ -1388,459 +892,297 @@ namespace Handbrake
             if (!drp_videoEncoder.Text.Contains("H.264"))\r
             {\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
+                check_iPodAtom.Enabled = false;\r
+                check_iPodAtom.Checked = false;\r
+                lbl_ipodAtom.Visible = false;\r
+                check_optimiseMP4.Enabled = false;\r
+                check_lAnamorphic.Enabled = false;\r
+                check_lAnamorphic.Checked = false;\r
             }\r
             else\r
             {\r
-                CheckCRF.Enabled = true;\r
-                check_turbo.Enabled = true;\r
+                if (check_2PassEncode.CheckState == CheckState.Checked)\r
+                    check_turbo.Enabled = true;\r
+\r
                 h264Tab.Enabled = true;\r
+                check_iPodAtom.Enabled = true;\r
+                lbl_ipodAtom.Visible = false;\r
+                check_optimiseMP4.Enabled = true;\r
+                check_lAnamorphic.Enabled = true;\r
             }\r
 \r
         }\r
 \r
-        public void setStreamReader(Parsing.DVD dvd)\r
+        #endregion\r
+\r
+        #region Query Editor Tab\r
+\r
+        private void btn_clear_Click(object sender, EventArgs e)\r
         {\r
-            this.thisDVD = dvd;\r
+            rtf_query.Clear();\r
         }\r
 \r
-        private void drp_dvdtitle_SelectedIndexChanged(object sender, EventArgs e)\r
+        private void btn_generate_Query_Click(object sender, EventArgs e)\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
+            rtf_query.Text = hb_common_func.GenerateTheQuery(this);\r
+        }\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
+        private void btn_copy2C_Click(object sender, EventArgs e)\r
+        {\r
+            if (rtf_query.Text != "")\r
+                Clipboard.SetText(rtf_query.Text, TextDataFormat.Text);\r
+        }\r
 \r
-                // Set the Aspect Ratio\r
-                lbl_Aspect.Text = selectedTitle.AspectRatio.ToString();\r
+        #endregion\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
+        // Main Window Preset System\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
+        #region Preset System\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
+        // Import Current Presets\r
+        private void loadPresetPanel()\r
+        {\r
+            ArrayList presetNameList = new ArrayList();\r
+       \r
+                string appPath = Application.StartupPath.ToString() + "\\presets.dat";\r
+                if (File.Exists(appPath))\r
                 {\r
-                    drp_audioChannels.Text = drp_audioChannels.Items[0].ToString();\r
+                    StreamReader presetInput = new StreamReader(appPath);\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
+                            presetNameList.Add(r.Split(preset));\r
+                        }\r
+                        else\r
+                            presetInput.ReadLine();\r
+                    }\r
                 }\r
+         \r
+            TreeNode preset_treeview = new TreeNode();\r
+            foreach (string[] preset in presetNameList)\r
+            {\r
+                preset_treeview = new TreeNode(preset[0]);\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
+                // Now Fill Out List View with Items\r
+                treeView_presets.Nodes.Add(preset_treeview);\r
             }\r
         }\r
 \r
-        #endregion\r
-\r
-        //---------------------------------------------------\r
-        //  Some Functions\r
-        //  - Query Generation\r
-        //---------------------------------------------------\r
-\r
-        #region Program Functions\r
-\r
-        public string GenerateTheQuery()\r
+        // Generate a new presets.dat file from the CLI\r
+        private void grabCLIPresets()\r
         {\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
-            int totalChapters = drop_chapterFinish.Items.Count - 1;\r
-            string dvdChapter = "";\r
-\r
-            if (source ==  "")\r
-                MessageBox.Show("No Source has been selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-            else\r
-            {\r
-                source = " -i " + '"' + source+ '"'; //'"'+\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
-            if (dvdTitle ==  "Automatic")\r
-                dvdTitle = "";\r
-            else\r
+        // Selects the preset called "normal". This is HandBrakes default settings\r
+        private void loadNormalPreset()\r
+        {\r
+            try\r
             {\r
-                string[] titleInfo = dvdTitle.Split(' ');\r
-                dvdTitle = " -t "+ titleInfo[0];\r
-            }\r
-\r
-            if (chapterFinish.Equals("Auto") && chapterStart.Equals("Auto"))\r
-                dvdChapter = "";\r
-            else if (chapterFinish == chapterStart)\r
-                dvdChapter = " -c " + chapterStart;\r
-            else\r
-                dvdChapter = " -c " + chapterStart + "-" + chapterFinish;\r
-\r
-            string querySource = source+ dvdTitle+ dvdChapter;\r
-            // ----------------------------------------------------------------------\r
-\r
-            // Destination\r
-\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
+                string appPath = Application.StartupPath.ToString() + "\\presets.dat";\r
+                if (File.Exists(appPath))\r
+                {\r
 \r
-            if (destination ==  "")\r
-                MessageBox.Show("No destination has been selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
-            else\r
-                destination = " -o " + '"' + destination + '"'; //'"'+ \r
+                    int normal = 0;\r
+                    foreach (TreeNode treenode in treeView_presets.Nodes)\r
+                    {\r
+                        if (treenode.ToString().Equals("TreeNode: Normal"))\r
+                            normal = treenode.Index;\r
+                    }\r
 \r
+                    TreeNode np = treeView_presets.Nodes[normal];\r
 \r
-            switch (videoEncoder)\r
-            {\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
+                    treeView_presets.SelectedNode = np;\r
+                }\r
             }\r
-           \r
-            switch (audioEncoder)\r
+            catch (Exception)\r
             {\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
+                // Do nothing\r
             }\r
+        }\r
 \r
-            if (width !=  "")\r
-                width = " -w "+ width;\r
-\r
-\r
-            if (height == "Auto")\r
-            {\r
-                height = "";\r
-            }\r
-            else if (height != "")\r
-            {\r
-                height = " -l " + height;\r
-            }\r
-            \r
-\r
-            string queryDestination = destination+ videoEncoder+ audioEncoder+ width+ height;\r
-            // ----------------------------------------------------------------------\r
-\r
-            // Picture Settings Tab\r
-\r
-            string cropSetting = drp_crop.Text;\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
-            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
-                cropOut = "";\r
-            else if (cropSetting == "No Crop")\r
-                cropOut = " --crop 0:0:0:0 ";\r
-            else\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
-                cropOut = " --crop " + cropTop + ":" + cropBottom + ":" + cropLeft + ":" + cropRight;\r
-            }\r
+        // Set's the current options set in the program as the new defaults for the program.\r
+        private void btn_setDefault_Click(object sender, EventArgs e)\r
+        {\r
+            String query = hb_common_func.GenerateTheQuery(this);\r
+            Properties.Settings.Default.defaultUserSettings = query;\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
-            if (subtitles ==  "None")\r
-                subtitles = "";\r
-            else if (subtitles ==  "")\r
-                subtitles = "";\r
-            else\r
-            {\r
-                string[] tempSub;\r
-                tempSub = subtitles.Split(' ');\r
-                subtitles = " -s "+ tempSub[0];\r
-            }\r
+        // When the user select a preset from the treeview, load it\r
+        private void treeView_presets_AfterSelect(object sender, TreeViewEventArgs e)\r
+        {\r
+            string selectedPreset = null;\r
+            selectedPreset = treeView_presets.SelectedNode.Text;\r
 \r
-            switch (deInterlace_Option)\r
+            try\r
             {\r
-                case "None":\r
-                    deinterlace = "";\r
-                    break;\r
-                case "Origional (Fast)":\r
-                    deinterlace = " --deinterlace=1";\r
-                    break;\r
-                case "yadif (Slow)":\r
-                    deinterlace = " --deinterlace=" + '"' + "0" + '"';\r
-                    break;\r
-                case "yadif + mcdeint (Slower)":\r
-                    deinterlace = " --deinterlace=" + '"' + "2:-1:1" + '"';\r
-                    break;\r
-                case "yadif + mcdeint (Slowest)":\r
-                    deinterlace = " --deinterlace=" + '"' + "1:-1:1" + '"';\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
-            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
-            double videoQuality = slider_videoQuality.Value;\r
-            string vidQSetting = "";\r
-            string twoPassEncoding = "";\r
-            string videoFramerate = drp_videoFramerate.Text;\r
-            string turboH264 = "";\r
-            string largeFile = "";\r
-            string deblock = "";\r
-            string detelecine = "";\r
-            string denoise = "";\r
-            string CRF = CheckCRF.CheckState.ToString();\r
-\r
-\r
-            if ((CRF == "Checked"))\r
-                CRF = " -Q ";\r
-            else\r
-                CRF = "";\r
-\r
-            if (videoBitrate !=  "")\r
-                videoBitrate = " -b "+ videoBitrate;\r
+                string appPath = Application.StartupPath.ToString() + "\\presets.dat";\r
+                if (File.Exists(appPath))\r
+                {\r
+                    StreamReader presetInput = new StreamReader(appPath);\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 (videoFilesize !=  "")\r
-                videoFilesize = " -S "+ videoFilesize;\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
-            // Video Quality Setting\r
+                                // Send the query from the file to the Query Parser class\r
+                                Functions.QueryParser presetQuery = Functions.QueryParser.Parse(preset);\r
 \r
-            if ((videoQuality ==  0))\r
-                vidQSetting = "";\r
-            else\r
-            {\r
-                videoQuality = videoQuality / 100;\r
-                if (videoQuality ==  1)\r
-                {\r
-                    vidQSetting = "1.0";\r
+                                // Now load the preset\r
+                                hb_common_func.presetLoader(this, presetQuery, selectedPreset);\r
+                            }\r
+                        }\r
+                        else\r
+                            presetInput.ReadLine();\r
+                    }\r
                 }\r
-\r
-                vidQSetting = " -q " + videoQuality.ToString();\r
             }\r
-\r
-            if (check_2PassEncode.Checked)\r
-                twoPassEncoding = " -2 ";\r
-\r
-            if (videoFramerate ==  "Automatic")\r
-                videoFramerate = "";\r
-            else\r
-                videoFramerate = " -r "+ videoFramerate;\r
-\r
-            if (check_turbo.Checked)\r
-                turboH264 = " -T ";\r
-\r
-            if (check_largeFile.Checked)\r
-                largeFile = " -4 ";\r
-\r
-            if (check_deblock.Checked)\r
-                deblock = " --deblock";\r
-\r
-            if (check_detelecine.Checked)\r
-                detelecine = " --detelecine";\r
-\r
-            switch (drp_deNoise.Text)\r
+            catch (Exception exc)\r
             {\r
-                case "None":\r
-                    denoise = "";\r
-                    break;\r
-                case "Weak":\r
-                    denoise = " --denoise=2:1:2:3";\r
-                    break;\r
-                case "Medium":\r
-                    denoise = " --denoise=3:2:2:3";\r
-                    break;\r
-                case "Strong":\r
-                    denoise = " --denoise=7:7:5:5";\r
-                    break;\r
-                default:\r
-                    denoise = "";\r
-                    break;\r
+                MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
+        }\r
 \r
-            string queryVideoSettings = videoBitrate + videoFilesize + vidQSetting + twoPassEncoding + videoFramerate + turboH264 + largeFile + deblock + detelecine + denoise;\r
-            // ----------------------------------------------------------------------\r
+        #endregion\r
 \r
-            // Audio Settings Tab\r
+        //---------------------------------------------------\r
+        // Encode / Cancel Buttons\r
+        // Encode Progress Text Handler\r
+        //---------------------------------------------------\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
+        #region Encodeing and Queue\r
 \r
-            if (audioBitrate !=  "")\r
-                audioBitrate = " -B "+ audioBitrate;\r
+        Functions.CLI process = new Functions.CLI();\r
+        private delegate void UpdateUIHandler();\r
 \r
-            if (audioSampleRate !=  "")\r
-                audioSampleRate = " -R "+ audioSampleRate;\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
-            if (audioChannels ==  "Automatic")\r
-                audioChannels = "";\r
-            else if (audioChannels ==  "")\r
-                audioChannels = "";\r
+        private void btn_start_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
-                string[] tempSub;\r
-                tempSub = audioChannels.Split(' ');\r
-                audioChannels = " -a "+ tempSub[0];\r
-            }\r
+                String query;\r
+                if (rtf_query.Text != "")\r
+                    query = rtf_query.Text;\r
+                else\r
+                    query = hb_common_func.GenerateTheQuery(this);\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 = "stero";\r
-                    break;\r
+                ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+                lbl_encode.Visible = true;\r
+                lbl_encode.Text = "Encoding in Progress";\r
             }\r
+        }\r
 \r
-            if (Mixdown !=  "")\r
-                SixChannelAudio = " -6 "+ Mixdown;\r
-            else\r
-                SixChannelAudio = "";\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
+        private void btn_add2Queue_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
-                h264Advanced = " -x "+ h264Advanced;\r
-    \r
+            {\r
+                String query;\r
+                if (rtf_query.Text != "")\r
+                    query = rtf_query.Text;\r
+                else\r
+                    query = hb_common_func.GenerateTheQuery(this);\r
 \r
-            string h264Settings = h264Advanced;\r
-            // ----------------------------------------------------------------------\r
+                queueWindow.list_queue.Items.Add(query);\r
+                queueWindow.Show();\r
+            }\r
+        }\r
 \r
-            // Processors (Program Settings)\r
+        private void btn_showQueue_Click(object sender, EventArgs e)\r
+        {\r
+            showQueue();\r
+        }\r
 \r
-            string processors = Properties.Settings.Default.Processors;\r
-            //  Number of Processors Handler\r
+        private void showQueue()\r
+        {\r
+            queueWindow.Show();\r
+        }\r
 \r
-            if (processors ==  "Automatic")\r
-                processors = "";\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
+                MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
             else\r
-                processors = " -C "+ processors+ " ";\r
-\r
-\r
-            string queryAdvancedSettings = processors;\r
-            // ----------------------------------------------------------------------\r
-\r
-            //  Verbose option (Program Settings)\r
+            {\r
+                hbProc = process.runCli(this, (string)state, false, false, false, false);\r
+                hbProc.WaitForExit();\r
 \r
-            string verbose = "";\r
-            if (Properties.Settings.Default.verbose ==  "Checked")\r
-                verbose = " -v ";\r
+                setEncodeLabel();\r
+                hbProc = null;\r
 \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
-            return querySource+ queryDestination+ queryPictureSettings+ queryVideoSettings+ h264Settings+ queryAudioSettings+ queryAdvancedSettings+ verbose;\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
         #endregion\r
 \r
+\r
+\r
         // This is the END of the road ------------------------------------------------------------------------------\r
     }\r
-}\r
+}
\ No newline at end of file