OSDN Git Service

WinGui: minor bug fixes.
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmMain.cs
1 /*  frmMain.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr/>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Collections;\r
9 using System.ComponentModel;\r
10 using System.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.Net;\r
15 using System.IO;\r
16 using System.Diagnostics;\r
17 using System.Threading;\r
18 using System.Runtime.InteropServices;\r
19 using System.Globalization;\r
20 using System.Text.RegularExpressions;\r
21 \r
22 namespace Handbrake\r
23 {\r
24     public partial class frmMain : Form\r
25     {\r
26         // -------------------------------------------------------------- \r
27         // Applicaiton Startup Stuff\r
28         // --------------------------------------------------------------\r
29 \r
30         #region Application Startup\r
31 \r
32         // Some stuff that needs to be Initialized. \r
33         private Process hbProc;\r
34         private Parsing.DVD thisDVD;\r
35         private frmQueue queueWindow = new frmQueue();\r
36         private delegate void updateStatusChanger();\r
37         Functions.Common hb_common_func = new Functions.Common();\r
38 \r
39         public frmMain()\r
40         {\r
41             // Load the splash screen in this thread\r
42             Form splash = new frmSplashScreen();\r
43             splash.Show();\r
44 \r
45             //Create a label that can be updated from the parent thread.\r
46             Label lblStatus = new Label();\r
47             lblStatus.Size = new Size(250, 20);\r
48             lblStatus.Location = new Point(10, 280);\r
49             splash.Controls.Add(lblStatus);\r
50 \r
51             //Fire a thread to wait for 2 seconds.  The splash screen will exit when the time expires\r
52             Thread timer = new Thread(splashTimer);\r
53             timer.Start();\r
54 \r
55             InitializeComponent();\r
56 \r
57             // show the form, but leave disabled until preloading is complete then show the main form\r
58             this.Enabled = false;\r
59 \r
60             this.Show();\r
61 \r
62             // Forces frmMain to draw\r
63             Application.DoEvents();\r
64 \r
65             // update the status\r
66             if (Properties.Settings.Default.updateStatus == "Checked")\r
67             {\r
68                 lblStatus.Text = "Checking for updates ...";\r
69                 Application.DoEvents();\r
70                 Thread updateCheckThread = new Thread(startupUpdateCheck);\r
71                 updateCheckThread.Start();\r
72                 Thread.Sleep(100);\r
73             }\r
74 \r
75             // Load the presets\r
76             // Set some defaults for the dropdown menus. Just incase the normal or user presets dont load.\r
77             drp_crop.SelectedIndex = 0;\r
78 \r
79             lblStatus.Text = "Loading Presets ...";\r
80             Application.DoEvents();\r
81             if (Properties.Settings.Default.updatePresets == "Checked")\r
82             {\r
83                 grabCLIPresets();\r
84             }\r
85             loadPresetPanel();\r
86             Thread.Sleep(200);\r
87       \r
88             // Now load the users default if required. (Will overide the above setting)\r
89             if (Properties.Settings.Default.defaultSettings == "Checked")\r
90             {\r
91                 lblStatus.Text = "Loading User Default Settings...";\r
92                 Application.DoEvents();\r
93                 loadUserDefaults();\r
94                 Thread.Sleep(100);\r
95             }\r
96             else\r
97                 loadNormalPreset();\r
98 \r
99             // Enable or disable tooltips\r
100             if (Properties.Settings.Default.tooltipEnable == "Checked")\r
101             {\r
102                 lblStatus.Text = "Loading Tooltips ...";\r
103                 Application.DoEvents();\r
104                 ToolTip.Active = true;\r
105                 Thread.Sleep(100);\r
106             }\r
107 \r
108             //Finished Loading\r
109             lblStatus.Text = "Loading Complete!";\r
110             Application.DoEvents();\r
111             Thread.Sleep(200);\r
112 \r
113             // Wait until splash screen is done\r
114             while (timer.IsAlive)\r
115             { Thread.Sleep(100); }\r
116 \r
117             //Close the splash screen\r
118             splash.Close();\r
119             splash.Dispose();\r
120 \r
121             // Turn the interface back to the user\r
122             this.Enabled = true;\r
123         }\r
124     \r
125         private void startupUpdateCheck()\r
126         {\r
127             try\r
128             {\r
129                 if (this.InvokeRequired)\r
130                 {\r
131                     this.BeginInvoke(new updateStatusChanger(startupUpdateCheck));\r
132                     return;\r
133                 }\r
134                 \r
135                 Boolean update = hb_common_func.updateCheck();\r
136                 if (update == true)\r
137                 {\r
138                     frmUpdater updateWindow = new frmUpdater();\r
139                     updateWindow.Show();\r
140                 }\r
141             }\r
142             catch (Exception exc)\r
143             {\r
144                 MessageBox.Show(exc.ToString());\r
145             }\r
146         }\r
147 \r
148         private void splashTimer(object sender)\r
149         {\r
150             Thread.Sleep(1000);  //sit for 1 seconds then exit\r
151         }\r
152 \r
153         private void showSplash(object sender)\r
154         {\r
155             Form splash = new frmSplashScreen();\r
156             splash.Show();\r
157         }\r
158 \r
159         private void loadUserDefaults()\r
160         {\r
161             string userDefaults = Properties.Settings.Default.defaultUserSettings;\r
162             try\r
163             {\r
164                 // Some things that need to be done to reset some gui components:\r
165                 CheckPixelRatio.CheckState = CheckState.Unchecked;\r
166 \r
167                 // Send the query from the file to the Query Parser class Then load the preset\r
168                 Functions.QueryParser presetQuery = Functions.QueryParser.Parse(userDefaults);\r
169                 hb_common_func.presetLoader(this, presetQuery, "User Defaults ");\r
170             }\r
171             catch (Exception exc)\r
172             {\r
173                 MessageBox.Show("Unable to load user Default Settings. \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
174             }\r
175         }\r
176 \r
177         #endregion\r
178 \r
179         #region Set Varible Function\r
180         public void setStreamReader(Parsing.DVD dvd)\r
181         {\r
182             this.thisDVD = dvd;\r
183         }\r
184         #endregion\r
185 \r
186         // -------------------------------------------------------------- \r
187         // The main Menu bar.\r
188         // -------------------------------------------------------------- \r
189 \r
190         #region File Menu\r
191         private void mnu_open_Click(object sender, EventArgs e)\r
192         {\r
193             string filename;\r
194             File_Open.ShowDialog();\r
195             filename = File_Open.FileName;\r
196 \r
197             if (filename != "")\r
198             {\r
199                 try\r
200                 {\r
201                     // Create StreamReader & open file\r
202                     StreamReader line = new StreamReader(filename);\r
203 \r
204                     // Send the query from the file to the Query Parser class then load the preset\r
205                     Functions.QueryParser presetQuery = Functions.QueryParser.Parse(line.ReadLine());\r
206                     hb_common_func.presetLoader(this, presetQuery, filename);\r
207 \r
208                     // Close the stream\r
209                     line.Close();\r
210                 }\r
211                 catch (Exception exc)\r
212                 {\r
213                     MessageBox.Show("Unable to load profile. \n\n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
214                 }\r
215             }\r
216         }\r
217 \r
218         private void mnu_save_Click(object sender, EventArgs e)\r
219         {\r
220             string filename;\r
221             File_Save.ShowDialog();\r
222             filename = File_Save.FileName;\r
223             if (filename != "")\r
224             {\r
225                 try\r
226                 {\r
227                     // Create a StreamWriter and open the file\r
228                     StreamWriter line = new StreamWriter(filename);\r
229 \r
230                     // Generate and write the query string to the file\r
231                     String query = hb_common_func.GenerateTheQuery(this);\r
232                     line.WriteLine(query);\r
233 \r
234                     // close the stream\r
235                     line.Close();\r
236                     MessageBox.Show("Your profile has been sucessfully saved.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
237                 }\r
238                 catch (Exception)\r
239                 {\r
240                     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
241                 }\r
242 \r
243             }\r
244         }\r
245 \r
246         private void mnu_exit_Click(object sender, EventArgs e)\r
247         {\r
248             Application.Exit();\r
249         }\r
250 \r
251         #endregion\r
252 \r
253         #region Tools Menu\r
254 \r
255         private void mnu_encode_Click(object sender, EventArgs e)\r
256         {\r
257             showQueue();\r
258         }\r
259 \r
260         private void mnu_viewDVDdata_Click(object sender, EventArgs e)\r
261         {\r
262             frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
263             dvdInfoWindow.Show();\r
264         }\r
265 \r
266         private void mnu_options_Click(object sender, EventArgs e)\r
267         {\r
268             Form Options = new frmOptions();\r
269             Options.ShowDialog();\r
270         }\r
271 \r
272 \r
273         #endregion\r
274 \r
275         #region Presets Menu\r
276 \r
277         private void mnu_presetReset_Click(object sender, EventArgs e)\r
278         {\r
279             treeView_presets.Nodes.Clear();\r
280             grabCLIPresets();\r
281             loadPresetPanel();\r
282             if (treeView_presets.Nodes.Count == 0)\r
283                 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
284             else\r
285                 MessageBox.Show("Presets have been updated", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
286         }\r
287 \r
288         private void mnu_SelectDefault_Click(object sender, EventArgs e)\r
289         {\r
290             loadNormalPreset();\r
291         }\r
292 \r
293         #endregion\r
294 \r
295         #region Help Menu\r
296 \r
297         private void mnu_wiki_Click(object sender, EventArgs e)\r
298         {\r
299             Process.Start("http://handbrake.fr/trac");\r
300         }\r
301 \r
302         private void mnu_faq_Click(object sender, EventArgs e)\r
303         {\r
304             Process.Start("http://handbrake.fr/trac/wiki/SupportFAQ");\r
305         }\r
306 \r
307         private void mnu_onlineDocs_Click(object sender, EventArgs e)\r
308         {\r
309             Process.Start("http://handbrake.fr/?article=documentation");\r
310         }\r
311 \r
312         private void mnu_homepage_Click(object sender, EventArgs e)\r
313         {\r
314             Process.Start("http://handbrake.fr");\r
315         }\r
316 \r
317         private void mnu_forum_Click(object sender, EventArgs e)\r
318         {\r
319             Process.Start("http://handbrake.fr/forum");\r
320         }\r
321 \r
322         private void mnu_UpdateCheck_Click(object sender, EventArgs e)\r
323         {\r
324             Boolean update = hb_common_func.updateCheck();\r
325             if (update == true)\r
326             {\r
327                 frmUpdater updateWindow = new frmUpdater();\r
328                 updateWindow.Show();\r
329             }\r
330             else\r
331                 MessageBox.Show("There are no new updates at this time.", "Update Check", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
332         }\r
333 \r
334         private void mnu_about_Click(object sender, EventArgs e)\r
335         {\r
336             Form About = new frmAbout();\r
337             About.ShowDialog();\r
338         }\r
339 \r
340         #endregion\r
341 \r
342         // -------------------------------------------------------------- \r
343         // Buttons on the main Window and items that require actions\r
344         // --------------------------------------------------------------\r
345 \r
346         #region Buttons\r
347 \r
348         private void btn_Browse_Click(object sender, EventArgs e)\r
349         {\r
350             String filename = "";\r
351             text_source.Text = "";\r
352             frmDvdInfo dvdInfoWindow = new frmDvdInfo();\r
353 \r
354             if (RadioDVD.Checked)\r
355             {\r
356                 DVD_Open.ShowDialog();\r
357                 filename = DVD_Open.SelectedPath;\r
358             }\r
359             else\r
360             {\r
361                 ISO_Open.ShowDialog();\r
362                 filename = ISO_Open.FileName;\r
363             }\r
364 \r
365             if (filename != "")\r
366             {\r
367                 Form frmRD = new frmReadDVD(filename, this, dvdInfoWindow);\r
368                 text_source.Text = filename;\r
369                 frmRD.ShowDialog();\r
370             }\r
371             else\r
372                 text_source.Text = "Click 'Browse' to continue";\r
373 \r
374             // If there are no titles in the dropdown menu then the scan has obviously failed. Display an error message explaining to the user.\r
375             if (drp_dvdtitle.Items.Count == 0)\r
376                 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
377         }\r
378 \r
379         private void btn_destBrowse_Click(object sender, EventArgs e)\r
380         {\r
381             // This removes the file extension from the filename box on the save file dialog.\r
382             // It's daft but some users don't realise that typing an extension overrides the dropdown extension selected.\r
383             DVD_Save.FileName = DVD_Save.FileName.Replace(".mp4", "").Replace(".m4v", "").Replace(".mkv", "").Replace(".ogm", "").Replace(".avi", "");\r
384 \r
385             // Show the dialog and set the main form file path\r
386             DVD_Save.ShowDialog();\r
387             text_destination.Text = DVD_Save.FileName;\r
388 \r
389             // Quicktime requires .m4v file for chapter markers to work. If checked, change the extension to .m4v (mp4 and m4v are the same thing)\r
390             if (Check_ChapterMarkers.Checked)\r
391                 text_destination.Text = text_destination.Text.Replace(".mp4", ".m4v");\r
392         }\r
393 \r
394         private void btn_h264Clear_Click(object sender, EventArgs e)\r
395         {\r
396             rtf_h264advanced.Text = "";\r
397         }\r
398 \r
399         private void btn_ActivityWindow_Click(object sender, EventArgs e)\r
400         {\r
401             Form ActivityWindow = new frmActivityWindow();\r
402             ActivityWindow.ShowDialog();\r
403         }\r
404 \r
405         #endregion\r
406 \r
407         #region frmMain Actions\r
408 \r
409         private void drp_dvdtitle_SelectedIndexChanged(object sender, EventArgs e)\r
410         {\r
411             // Reset some values on the form\r
412             lbl_Aspect.Text = "Select a Title";\r
413             lbl_RecomendedCrop.Text = "Select a Title";\r
414             drop_chapterStart.Items.Clear();\r
415             drop_chapterFinish.Items.Clear();\r
416 \r
417             // If the dropdown is set to automatic nothing else needs to be done.\r
418             // Otheriwse if its not, title data has to be loased from parsing.\r
419             if (drp_dvdtitle.Text != "Automatic")\r
420             {\r
421                 Parsing.Title selectedTitle = drp_dvdtitle.SelectedItem as Parsing.Title;\r
422 \r
423                 // Set the Aspect Ratio\r
424                 lbl_Aspect.Text = selectedTitle.AspectRatio.ToString();\r
425 \r
426                 // Set the Recommended Cropping values\r
427                 lbl_RecomendedCrop.Text = string.Format("{0}/{1}/{2}/{3}", selectedTitle.AutoCropDimensions[0], selectedTitle.AutoCropDimensions[1], selectedTitle.AutoCropDimensions[2], selectedTitle.AutoCropDimensions[3]);\r
428 \r
429                 // Populate the Start chapter Dropdown\r
430                 drop_chapterStart.Items.Clear();\r
431                 drop_chapterStart.Items.AddRange(selectedTitle.Chapters.ToArray());\r
432                 if (drop_chapterStart.Items.Count > 0)\r
433                     drop_chapterStart.Text = drop_chapterStart.Items[0].ToString();\r
434 \r
435                 // Populate the Final Chapter Dropdown\r
436                 drop_chapterFinish.Items.Clear();\r
437                 drop_chapterFinish.Items.AddRange(selectedTitle.Chapters.ToArray());\r
438                 if (drop_chapterFinish.Items.Count > 0)\r
439                     drop_chapterFinish.Text = drop_chapterFinish.Items[drop_chapterFinish.Items.Count - 1].ToString();\r
440 \r
441                 // Populate the Audio Channels Dropdown\r
442                 drp_track1Audio.Items.Clear();\r
443                 drp_track1Audio.Items.Add("Automatic");\r
444                 drp_track1Audio.Items.Add("None");\r
445                 drp_track1Audio.Items.AddRange(selectedTitle.AudioTracks.ToArray());\r
446                 if (drp_track1Audio.Items.Count > 0)\r
447                     drp_track1Audio.Text = drp_track1Audio.Items[0].ToString();\r
448 \r
449                 drp_track2Audio.Items.Clear();\r
450                 drp_track2Audio.Items.Add("None");\r
451                 drp_track2Audio.Items.AddRange(selectedTitle.AudioTracks.ToArray());\r
452                 if (drp_track2Audio.Items.Count > 0)\r
453                     drp_track2Audio.Text = drp_track2Audio.Items[0].ToString();\r
454 \r
455                 // Populate the Subtitles dropdown\r
456                 drp_subtitle.Items.Clear();\r
457                 drp_subtitle.Items.Add("None");\r
458                 drp_subtitle.Items.Add("Autoselect");\r
459                 drp_subtitle.Items.AddRange(selectedTitle.Subtitles.ToArray());\r
460                 if (drp_subtitle.Items.Count > 0)\r
461                     drp_subtitle.Text = drp_subtitle.Items[0].ToString();\r
462 \r
463             }\r
464 \r
465             // Run the autoName & chapterNaming functions\r
466             hb_common_func.autoName(this);\r
467             hb_common_func.chapterNaming(this);\r
468         }\r
469 \r
470         private void drop_chapterStart_SelectedIndexChanged(object sender, EventArgs e)\r
471         {\r
472             drop_chapterStart.BackColor = Color.White;\r
473             if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
474             {\r
475                 try\r
476                 {\r
477                     int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
478                     int chapterStart = int.Parse(drop_chapterStart.Text);\r
479 \r
480                     if (chapterFinish < chapterStart)\r
481                         drop_chapterStart.BackColor = Color.LightCoral;\r
482                 }\r
483                 catch (Exception)\r
484                 {\r
485                     drop_chapterStart.BackColor = Color.LightCoral;\r
486                 }\r
487             }\r
488             // Run the Autonaming function\r
489             hb_common_func.autoName(this);\r
490         }\r
491 \r
492         private void drop_chapterFinish_SelectedIndexChanged(object sender, EventArgs e)\r
493         {\r
494             drop_chapterFinish.BackColor = Color.White;\r
495             if ((drop_chapterFinish.Text != "Auto") && (drop_chapterStart.Text != "Auto"))\r
496             {\r
497                 try\r
498                 {\r
499                     int chapterFinish = int.Parse(drop_chapterFinish.Text);\r
500                     int chapterStart = int.Parse(drop_chapterStart.Text);\r
501 \r
502                     if (chapterFinish < chapterStart)\r
503                         drop_chapterFinish.BackColor = Color.LightCoral;\r
504                 }\r
505                 catch (Exception)\r
506                 {\r
507                     drop_chapterFinish.BackColor = Color.LightCoral;\r
508                 }\r
509             }\r
510 \r
511             // Run the Autonaming function\r
512             hb_common_func.autoName(this);\r
513         }\r
514 \r
515         private void text_bitrate_TextChanged(object sender, EventArgs e)\r
516         {\r
517             text_filesize.Text = "";\r
518             slider_videoQuality.Value = 0;\r
519             SliderValue.Text = "0%";\r
520         }\r
521 \r
522         private void text_filesize_TextChanged(object sender, EventArgs e)\r
523         {\r
524             text_bitrate.Text = "";\r
525             slider_videoQuality.Value = 0;\r
526             SliderValue.Text = "0%";\r
527         }\r
528 \r
529         private void slider_videoQuality_Scroll(object sender, EventArgs e)\r
530         {\r
531             SliderValue.Text = slider_videoQuality.Value.ToString() + "%";\r
532             text_bitrate.Text = "";\r
533             text_filesize.Text = "";\r
534         }\r
535 \r
536         private void label_h264_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)\r
537         {\r
538             Process.Start("http://handbrake.fr/trac/wiki/x264Options");\r
539         }\r
540 \r
541         private void text_width_TextChanged(object sender, EventArgs e)\r
542         {\r
543             try\r
544             {\r
545                 if (CheckPixelRatio.Checked)\r
546                 {\r
547                     text_width.Text = "";\r
548                     text_width.BackColor = Color.LightCoral;\r
549                     CheckPixelRatio.BackColor = Color.LightCoral;\r
550                 }\r
551                 else\r
552                 {\r
553                     if ((int.Parse(text_width.Text) % 16) != 0)\r
554                         text_width.BackColor = Color.LightCoral;\r
555                     else\r
556                         text_width.BackColor = Color.LightGreen;\r
557                 }\r
558 \r
559                 if ((lbl_Aspect.Text != "Select a Title") && (drp_crop.SelectedIndex == 2))\r
560                 {\r
561                     double height = int.Parse(text_width.Text) / double.Parse(lbl_Aspect.Text);\r
562                     double mod16 = height % 16;\r
563                     height = height - mod16;\r
564 \r
565                     if (text_width.Text == "")\r
566                     {\r
567                         text_height.Text = "";\r
568                         text_width.BackColor = Color.White;\r
569                     }\r
570                     else\r
571                         text_height.Text = height.ToString();\r
572                 }\r
573             }\r
574             catch (Exception)\r
575             {\r
576                 // No need to throw an error here.\r
577             }\r
578         }\r
579 \r
580         private void text_height_TextChanged(object sender, EventArgs e)\r
581         {\r
582             try\r
583             {\r
584                 if (CheckPixelRatio.Checked)\r
585                 {\r
586                     text_height.Text = "";\r
587                     text_height.BackColor = Color.LightCoral;\r
588                     CheckPixelRatio.BackColor = Color.LightCoral;\r
589                 }\r
590                 else\r
591                 {\r
592                     if ((int.Parse(text_height.Text) % 16) != 0)\r
593                         text_height.BackColor = Color.LightCoral;\r
594                     else\r
595                         text_height.BackColor = Color.LightGreen;\r
596                 }\r
597 \r
598             }\r
599             catch (Exception)\r
600             {\r
601                 // No need to alert the user.\r
602             }\r
603         }\r
604 \r
605         private void drp_crop_SelectedIndexChanged(object sender, EventArgs e)\r
606         {\r
607             if ((string)drp_crop.SelectedItem == "Custom")\r
608             {\r
609                 text_left.Enabled = true;\r
610                 text_right.Enabled = true;\r
611                 text_top.Enabled = true;\r
612                 text_bottom.Enabled = true;\r
613                 text_left.Text = "0";\r
614                 text_right.Text = "0";\r
615                 text_top.Text = "0";\r
616                 text_bottom.Text = "0";\r
617             }\r
618 \r
619             if ((string)drp_crop.SelectedItem == "Automatic")\r
620             {\r
621                 text_left.Enabled = false;\r
622                 text_right.Enabled = false;\r
623                 text_top.Enabled = false;\r
624                 text_bottom.Enabled = false;\r
625 \r
626                 if (lbl_RecomendedCrop.Text != "Select a Title")\r
627                 {\r
628                     string[] temp = new string[4];\r
629                     temp = lbl_RecomendedCrop.Text.Split('/');\r
630                     text_left.Text = temp[2];\r
631                     text_right.Text = temp[3];\r
632                     text_top.Text = temp[0];\r
633                     text_bottom.Text = temp[1];\r
634                 }\r
635                 else\r
636                 {\r
637                     text_left.Text = "";\r
638                     text_right.Text = "";\r
639                     text_top.Text = "";\r
640                     text_bottom.Text = "";\r
641                 }\r
642             }\r
643 \r
644             if ((string)drp_crop.SelectedItem == "No Crop")\r
645             {\r
646                 text_left.Enabled = false;\r
647                 text_right.Enabled = false;\r
648                 text_top.Enabled = false;\r
649                 text_bottom.Enabled = false;\r
650                 text_left.Text = "0";\r
651                 text_right.Text = "0";\r
652                 text_top.Text = "0";\r
653                 text_bottom.Text = "0";\r
654 \r
655             }\r
656         }\r
657 \r
658         private void check_vfr_CheckedChanged(object sender, EventArgs e)\r
659         {\r
660             if (check_vfr.CheckState == CheckState.Checked)\r
661             {\r
662                 check_detelecine.Enabled = false;\r
663                 check_detelecine.CheckState = CheckState.Checked;\r
664                 drp_videoFramerate.Enabled = false;\r
665                 drp_videoFramerate.SelectedItem = "29.97";\r
666                 lbl_vfr.Visible = true;\r
667             }\r
668             else\r
669             {\r
670                 check_detelecine.Enabled = true;\r
671                 drp_videoFramerate.Enabled = true;\r
672                 drp_videoFramerate.SelectedItem = "Automatic";\r
673                 lbl_vfr.Visible = false;\r
674             }\r
675         }\r
676 \r
677         private void CheckPixelRatio_CheckedChanged(object sender, EventArgs e)\r
678         {\r
679             text_width.Text = "";\r
680             text_height.Text = "";\r
681             text_width.BackColor = Color.White;\r
682             text_height.BackColor = Color.White;\r
683             CheckPixelRatio.BackColor = TabPage1.BackColor;\r
684 \r
685             if (CheckPixelRatio.Checked)\r
686             {\r
687                 check_lAnamorphic.Enabled = false;\r
688                 check_lAnamorphic.Checked = false;\r
689                 text_height.BackColor = Color.LightGray;\r
690                 text_width.BackColor = Color.LightGray;\r
691                 text_height.Enabled = false;\r
692                 text_width.Enabled = false;\r
693             }\r
694             else\r
695             {\r
696                 check_lAnamorphic.Enabled = true;\r
697                 text_height.BackColor = Color.White;\r
698                 text_width.BackColor = Color.White;\r
699                 text_height.Enabled = true;\r
700                 text_width.Enabled = true;\r
701             }\r
702         }\r
703 \r
704         private void check_lAnamorphic_CheckedChanged(object sender, EventArgs e)\r
705         {\r
706             if (check_lAnamorphic.Checked)\r
707             {\r
708                 CheckPixelRatio.Enabled = false;\r
709                 CheckPixelRatio.Checked = false;\r
710                 text_height.Text = "";\r
711                 text_height.Enabled = false;\r
712                 text_height.BackColor = Color.LightGray;\r
713             }\r
714             else\r
715             {\r
716                 CheckPixelRatio.Enabled = true;\r
717                 text_height.Enabled = true;\r
718                 text_height.BackColor = Color.White;\r
719             }\r
720         }\r
721 \r
722         private void check_2PassEncode_CheckedChanged(object sender, EventArgs e)\r
723         {\r
724             if (check_2PassEncode.CheckState.ToString() == "Checked")\r
725             {\r
726                 if (drp_videoEncoder.Text.Contains("H.264"))\r
727                     check_turbo.Enabled = true;\r
728             }\r
729             else\r
730             {\r
731                 check_turbo.Enabled = false;\r
732                 check_turbo.CheckState = CheckState.Unchecked;\r
733             }\r
734         }\r
735 \r
736         private void check_largeFile_CheckedChanged(object sender, EventArgs e)\r
737         {\r
738             if ((!text_destination.Text.Contains(".mp4")) && (!text_destination.Text.Contains(".m4v")))\r
739             {\r
740                 check_largeFile.BackColor = Color.LightCoral;\r
741                 check_largeFile.CheckState = CheckState.Unchecked;\r
742             }\r
743             else\r
744             {\r
745                 check_largeFile.BackColor = Color.Transparent;\r
746             }\r
747         }\r
748 \r
749         private void check_iPodAtom_CheckedChanged(object sender, EventArgs e)\r
750         {\r
751             if ((!text_destination.Text.Contains(".mp4")) && (!text_destination.Text.Contains(".m4v")))\r
752             {\r
753                 text_destination.BackColor = Color.LightCoral;\r
754                 check_iPodAtom.BackColor = Color.LightCoral;\r
755                 check_iPodAtom.CheckState = CheckState.Unchecked;\r
756             }\r
757             else\r
758             {\r
759                 check_iPodAtom.BackColor = Color.Transparent;\r
760                 text_destination.BackColor = Color.White;\r
761             }\r
762         }\r
763 \r
764         private void check_optimiseMP4_CheckedChanged(object sender, EventArgs e)\r
765         {\r
766             if ((!text_destination.Text.Contains(".mp4")) && (!text_destination.Text.Contains(".m4v")))\r
767             {\r
768                 check_optimiseMP4.BackColor = Color.LightCoral;\r
769                 text_destination.BackColor = Color.LightCoral;\r
770                 check_optimiseMP4.CheckState = CheckState.Unchecked;\r
771             }\r
772             else\r
773             {\r
774                 check_optimiseMP4.BackColor = Color.Transparent;\r
775                 text_destination.BackColor = Color.White;\r
776             }\r
777         }\r
778 \r
779         private void drp_dvdtitle_Click(object sender, EventArgs e)\r
780         {\r
781             if (drp_dvdtitle.Items.Count == 0)\r
782                 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
783         }\r
784 \r
785         private void drp_audioCodec_SelectedIndexChanged(object sender, EventArgs e)\r
786         {\r
787             if (drp_audioCodec.Text == "AAC + AC3")\r
788             {\r
789                 drp_audioMixDown.Enabled = false;\r
790                 drp_audioMixDown.Text = "Automatic";\r
791                 text_destination.Text.Replace(".mp4", ".m4v");\r
792             }\r
793             else\r
794             {\r
795                 drp_audioMixDown.Enabled = true;\r
796             }\r
797 \r
798             if (drp_audioCodec.Text == "AC3")\r
799             {\r
800                 drp_audioBitrate.Enabled = false;\r
801                 drp_audioSampleRate.Enabled = false;\r
802                 drp_audioBitrate.Text = "";\r
803                 drp_audioSampleRate.Text = "";\r
804             }\r
805             else\r
806             {\r
807                 drp_audioBitrate.Enabled = true;\r
808                 drp_audioSampleRate.Enabled = true;\r
809                 drp_audioBitrate.Text = "160";\r
810                 drp_audioSampleRate.Text = "48";\r
811             }\r
812             if (drp_audioCodec.Text == "AAC")\r
813             {\r
814                 drp_audioMixDown.Items.Clear();\r
815                 drp_audioMixDown.Items.Add("Mono");\r
816                 drp_audioMixDown.Items.Add("Stereo");\r
817                 drp_audioMixDown.Items.Add("Dolby Surround");\r
818                 drp_audioMixDown.Items.Add("Dolby Pro Logic II");\r
819                 drp_audioMixDown.Items.Add("6 Channel Discrete");\r
820 \r
821                 drp_audioBitrate.Items.Clear();\r
822                 drp_audioBitrate.Items.Add("32");\r
823                 drp_audioBitrate.Items.Add("40");\r
824                 drp_audioBitrate.Items.Add("48");\r
825                 drp_audioBitrate.Items.Add("56");\r
826                 drp_audioBitrate.Items.Add("64");\r
827                 drp_audioBitrate.Items.Add("80");\r
828                 drp_audioBitrate.Items.Add("86");\r
829                 drp_audioBitrate.Items.Add("112");\r
830                 drp_audioBitrate.Items.Add("128");\r
831                 drp_audioBitrate.Items.Add("160");\r
832 \r
833             }\r
834             else\r
835             {\r
836                 drp_audioMixDown.Items.Clear();\r
837                 drp_audioMixDown.Items.Add("Stereo");\r
838                 drp_audioMixDown.Items.Add("Dolby Surround");\r
839                 drp_audioMixDown.Items.Add("Dolby Pro Logic II");\r
840 \r
841                 drp_audioBitrate.Items.Clear();\r
842                 drp_audioBitrate.Items.Add("32");\r
843                 drp_audioBitrate.Items.Add("40");\r
844                 drp_audioBitrate.Items.Add("48");\r
845                 drp_audioBitrate.Items.Add("56");\r
846                 drp_audioBitrate.Items.Add("64");\r
847                 drp_audioBitrate.Items.Add("80");\r
848                 drp_audioBitrate.Items.Add("86");\r
849                 drp_audioBitrate.Items.Add("112");\r
850                 drp_audioBitrate.Items.Add("128");\r
851                 drp_audioBitrate.Items.Add("160");\r
852                 drp_audioBitrate.Items.Add("192");\r
853                 drp_audioBitrate.Items.Add("224");\r
854                 drp_audioBitrate.Items.Add("256");\r
855                 drp_audioBitrate.Items.Add("320");\r
856             }\r
857         }\r
858 \r
859         private void drp_audioMixDown_SelectedIndexChanged(object sender, EventArgs e)\r
860         {\r
861             if ((drp_audioCodec.Text == "AAC") && (drp_audioMixDown.Text == "6 Channel Discrete"))\r
862             {\r
863                 drp_audioBitrate.Items.Clear();\r
864                 drp_audioBitrate.Items.Add("32");\r
865                 drp_audioBitrate.Items.Add("40");\r
866                 drp_audioBitrate.Items.Add("48");\r
867                 drp_audioBitrate.Items.Add("56");\r
868                 drp_audioBitrate.Items.Add("64");\r
869                 drp_audioBitrate.Items.Add("80");\r
870                 drp_audioBitrate.Items.Add("86");\r
871                 drp_audioBitrate.Items.Add("112");\r
872                 drp_audioBitrate.Items.Add("128");\r
873                 drp_audioBitrate.Items.Add("160");\r
874                 drp_audioBitrate.Items.Add("192");\r
875                 drp_audioBitrate.Items.Add("224");\r
876                 drp_audioBitrate.Items.Add("256");\r
877                 drp_audioBitrate.Items.Add("320");\r
878                 drp_audioBitrate.Items.Add("384");\r
879             }\r
880         }\r
881 \r
882         private void slider_drc_Scroll(object sender, EventArgs e)\r
883         {\r
884             double value = slider_drc.Value / 10.0;\r
885             value++;\r
886 \r
887             lbl_drc.Text = value.ToString();\r
888         }\r
889 \r
890         private void drp_subtitle_SelectedIndexChanged(object sender, EventArgs e)\r
891         {\r
892             if (drp_subtitle.Text.Contains("None"))\r
893             {\r
894                 check_forced.Enabled = false;\r
895                 check_forced.Checked = false;\r
896             }\r
897             else\r
898                 check_forced.Enabled = true;\r
899         }\r
900 \r
901         private void Check_ChapterMarkers_CheckedChanged(object sender, EventArgs e)\r
902         {\r
903             if (Check_ChapterMarkers.Checked)\r
904             {\r
905                 string destination = text_destination.Text;\r
906                 destination = destination.Replace(".mp4", ".m4v");\r
907                 text_destination.Text = destination;\r
908                 data_chpt.Rows.Clear();\r
909                 data_chpt.Enabled = true;\r
910                 hb_common_func.chapterNaming(this);\r
911             }\r
912             else\r
913             {\r
914                 string destination = text_destination.Text;\r
915                 destination = destination.Replace(".m4v", ".mp4");\r
916                 text_destination.Text = destination;\r
917                 data_chpt.Rows.Clear();\r
918                 data_chpt.Enabled = false;\r
919             }\r
920         }\r
921 \r
922         private void drp_videoEncoder_SelectedIndexChanged(object sender, EventArgs e)\r
923         {\r
924             //Turn off some options which are H.264 only when the user selects a non h.264 encoder\r
925             if (!drp_videoEncoder.Text.Contains("H.264"))\r
926             {\r
927                 check_turbo.CheckState = CheckState.Unchecked;\r
928                 check_turbo.Enabled = false;\r
929                 h264Tab.Enabled = false;\r
930                 rtf_h264advanced.Text = "";\r
931                 check_iPodAtom.Enabled = false;\r
932                 check_iPodAtom.Checked = false;\r
933                 check_optimiseMP4.Enabled = false;\r
934                 check_lAnamorphic.Enabled = false;\r
935                 check_lAnamorphic.Checked = false;\r
936             }\r
937             else\r
938             {\r
939                 if (check_2PassEncode.CheckState == CheckState.Checked)\r
940                     check_turbo.Enabled = true;\r
941 \r
942                 h264Tab.Enabled = true;\r
943                 check_iPodAtom.Enabled = true;\r
944                 check_optimiseMP4.Enabled = true;\r
945                 check_lAnamorphic.Enabled = true;\r
946             }\r
947 \r
948         }\r
949 \r
950         #endregion\r
951 \r
952         #region Query Editor Tab\r
953 \r
954         private void btn_clear_Click(object sender, EventArgs e)\r
955         {\r
956             rtf_query.Clear();\r
957         }\r
958 \r
959         private void btn_generate_Query_Click(object sender, EventArgs e)\r
960         {\r
961             rtf_query.Text = hb_common_func.GenerateTheQuery(this);\r
962         }\r
963 \r
964         private void btn_copy2C_Click(object sender, EventArgs e)\r
965         {\r
966             if (rtf_query.Text != "")\r
967                 Clipboard.SetText(rtf_query.Text, TextDataFormat.Text);\r
968         }\r
969 \r
970         #endregion\r
971 \r
972         // -------------------------------------------------------------- \r
973         // Main Window Preset System\r
974         // --------------------------------------------------------------\r
975 \r
976         #region Preset System\r
977 \r
978         // Import Current Presets\r
979         private void loadPresetPanel()\r
980         {\r
981             ArrayList presetNameList = new ArrayList();\r
982        \r
983                 string appPath = Application.StartupPath.ToString() + "\\presets.dat";\r
984                 if (File.Exists(appPath))\r
985                 {\r
986                     StreamReader presetInput = new StreamReader(appPath);\r
987                     while (!presetInput.EndOfStream)\r
988                     {\r
989                         if ((char)presetInput.Peek() == '+')\r
990                         {\r
991                             string preset = presetInput.ReadLine().Replace("+ ", "");\r
992                             Regex r = new Regex("(:  )"); // Split on hyphens. \r
993                             presetNameList.Add(r.Split(preset));\r
994                         }\r
995                         else\r
996                             presetInput.ReadLine();\r
997                     }\r
998                 }\r
999          \r
1000             TreeNode preset_treeview = new TreeNode();\r
1001             foreach (string[] preset in presetNameList)\r
1002             {\r
1003                 preset_treeview = new TreeNode(preset[0]);\r
1004 \r
1005                 // Now Fill Out List View with Items\r
1006                 treeView_presets.Nodes.Add(preset_treeview);\r
1007             }\r
1008         }\r
1009 \r
1010         // Generate a new presets.dat file from the CLI\r
1011         private void grabCLIPresets()\r
1012         {\r
1013             string appPath = Application.StartupPath.ToString() + "\\";\r
1014             string strCmdLine = "cmd /c " + '"' + '"' + appPath + "HandBrakeCLI.exe" + '"' + " --preset-list >" + '"' + appPath + "presets.dat" + '"' + " 2>&1" + '"';\r
1015             Process hbproc = Process.Start("CMD.exe", strCmdLine);\r
1016             hbproc.WaitForExit();\r
1017             hbproc.Dispose();\r
1018             hbproc.Close();\r
1019         }\r
1020 \r
1021         // Selects the preset called "normal". This is HandBrakes default settings\r
1022         private void loadNormalPreset()\r
1023         {\r
1024             try\r
1025             {\r
1026                 string appPath = Application.StartupPath.ToString() + "\\presets.dat";\r
1027                 if (File.Exists(appPath))\r
1028                 {\r
1029 \r
1030                     int normal = 0;\r
1031                     foreach (TreeNode treenode in treeView_presets.Nodes)\r
1032                     {\r
1033                         if (treenode.ToString().Equals("TreeNode: Normal"))\r
1034                             normal = treenode.Index;\r
1035                     }\r
1036 \r
1037                     TreeNode np = treeView_presets.Nodes[normal];\r
1038 \r
1039                     treeView_presets.SelectedNode = np;\r
1040                 }\r
1041             }\r
1042             catch (Exception)\r
1043             {\r
1044                 // Do nothing\r
1045             }\r
1046         }\r
1047 \r
1048         // Set's the current options set in the program as the new defaults for the program.\r
1049         private void btn_setDefault_Click(object sender, EventArgs e)\r
1050         {\r
1051             String query = hb_common_func.GenerateTheQuery(this);\r
1052             Properties.Settings.Default.defaultUserSettings = query;\r
1053             // Save the new default Settings\r
1054             Properties.Settings.Default.Save();\r
1055             MessageBox.Show("New default settings saved.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
1056         }\r
1057 \r
1058         // When the user select a preset from the treeview, load it\r
1059         private void treeView_presets_AfterSelect(object sender, TreeViewEventArgs e)\r
1060         {\r
1061             string selectedPreset = null;\r
1062             selectedPreset = treeView_presets.SelectedNode.Text;\r
1063 \r
1064             try\r
1065             {\r
1066                 string appPath = Application.StartupPath.ToString() + "\\presets.dat";\r
1067                 if (File.Exists(appPath))\r
1068                 {\r
1069                     StreamReader presetInput = new StreamReader(appPath);\r
1070                     while (!presetInput.EndOfStream)\r
1071                     {\r
1072                         if ((char)presetInput.Peek() == '+')\r
1073                         {\r
1074                             string preset = presetInput.ReadLine().Replace("+ ", "");\r
1075                             Regex r = new Regex("(:  )"); // Split on hyphens. \r
1076                             string[] presetName = r.Split(preset);\r
1077 \r
1078                             if (selectedPreset == presetName[0])\r
1079                             {\r
1080                                 // Need to disable anamorphic now, otherwise it may overide the width / height values later.\r
1081                                 CheckPixelRatio.CheckState = CheckState.Unchecked;\r
1082 \r
1083                                 // Send the query from the file to the Query Parser class\r
1084                                 Functions.QueryParser presetQuery = Functions.QueryParser.Parse(preset);\r
1085 \r
1086                                 // Now load the preset\r
1087                                 hb_common_func.presetLoader(this, presetQuery, selectedPreset);\r
1088                             }\r
1089                         }\r
1090                         else\r
1091                             presetInput.ReadLine();\r
1092                     }\r
1093                 }\r
1094             }\r
1095             catch (Exception exc)\r
1096             {\r
1097                 MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
1098             }\r
1099         }\r
1100 \r
1101         #endregion\r
1102 \r
1103         //---------------------------------------------------\r
1104         // Encode / Cancel Buttons\r
1105         // Encode Progress Text Handler\r
1106         //---------------------------------------------------\r
1107 \r
1108         #region Encodeing and Queue\r
1109 \r
1110         Functions.CLI process = new Functions.CLI();\r
1111         private delegate void UpdateUIHandler();\r
1112 \r
1113         [DllImport("user32.dll")]\r
1114         public static extern void LockWorkStation();\r
1115         [DllImport("user32.dll")]\r
1116         public static extern int ExitWindowsEx(int uFlags, int dwReason);\r
1117 \r
1118         private void btn_start_Click(object sender, EventArgs e)\r
1119         {\r
1120             if (text_source.Text == "" || text_source.Text == "Click 'Browse' to continue" || text_destination.Text == "")\r
1121                 MessageBox.Show("No source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
1122             else\r
1123             {\r
1124                 String query;\r
1125                 if (rtf_query.Text != "")\r
1126                     query = rtf_query.Text;\r
1127                 else\r
1128                     query = hb_common_func.GenerateTheQuery(this);\r
1129 \r
1130                 ThreadPool.QueueUserWorkItem(procMonitor, query);\r
1131                 lbl_encode.Visible = true;\r
1132                 lbl_encode.Text = "Encoding in Progress";\r
1133             }\r
1134         }\r
1135 \r
1136         private void btn_add2Queue_Click(object sender, EventArgs e)\r
1137         {\r
1138             if (text_source.Text == "" || text_source.Text == "Click 'Browse' to continue" || text_destination.Text == "")\r
1139                 MessageBox.Show("No source OR destination selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
1140             else\r
1141             {\r
1142                 String query;\r
1143                 if (rtf_query.Text != "")\r
1144                     query = rtf_query.Text;\r
1145                 else\r
1146                     query = hb_common_func.GenerateTheQuery(this);\r
1147 \r
1148                 queueWindow.list_queue.Items.Add(query);\r
1149                 queueWindow.Show();\r
1150             }\r
1151         }\r
1152 \r
1153         private void btn_showQueue_Click(object sender, EventArgs e)\r
1154         {\r
1155             showQueue();\r
1156         }\r
1157 \r
1158         private void showQueue()\r
1159         {\r
1160             queueWindow.Show();\r
1161         }\r
1162 \r
1163         private void procMonitor(object state)\r
1164         {\r
1165             // Make sure we are not already encoding and if we are then display an error.\r
1166             if (hbProc != null)\r
1167                 MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
1168             else\r
1169             {\r
1170                 hbProc = process.runCli(this, (string)state, false, false, false, false);\r
1171                 hbProc.WaitForExit();\r
1172 \r
1173                 setEncodeLabel();\r
1174                 hbProc = null;\r
1175 \r
1176                 // Do something whent he encode ends.\r
1177                 switch (Properties.Settings.Default.CompletionOption)\r
1178                 {\r
1179                     case "Shutdown":\r
1180                         System.Diagnostics.Process.Start("Shutdown", "-s -t 60");\r
1181                         break;\r
1182                     case "Log Off":\r
1183                         ExitWindowsEx(0, 0);\r
1184                         break;\r
1185                     case "Suspend":\r
1186                         Application.SetSuspendState(PowerState.Suspend, true, true);\r
1187                         break;\r
1188                     case "Hibernate":\r
1189                         Application.SetSuspendState(PowerState.Hibernate, true, true);\r
1190                         break;\r
1191                     case "Lock System":\r
1192                         LockWorkStation();\r
1193                         break;\r
1194                     case "Quit HandBrake":\r
1195                         Application.Exit();\r
1196                         break;\r
1197                     default:\r
1198                         break;\r
1199                 }\r
1200             }\r
1201         }\r
1202 \r
1203         private void setEncodeLabel()\r
1204         {\r
1205             if (this.InvokeRequired)\r
1206             {\r
1207                 this.BeginInvoke(new UpdateUIHandler(setEncodeLabel));\r
1208                 return;\r
1209             }\r
1210             lbl_encode.Text = "Encoding Finished";\r
1211         }\r
1212 \r
1213         #endregion\r
1214 \r
1215 \r
1216 \r
1217         // This is the END of the road ------------------------------------------------------------------------------\r
1218     }\r
1219 }