/src/Forms/Split.cs
C# | 353 lines | 309 code | 44 blank | 0 comment | 57 complexity | 81b7597f7ec36fec954625cc841630e3 MD5 | raw file
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Text;
5using System.Windows.Forms;
6using Nikse.SubtitleEdit.Logic;
7using Nikse.SubtitleEdit.Logic.SubtitleFormats;
8
9namespace Nikse.SubtitleEdit.Forms
10{
11 public sealed partial class Split : Form
12 {
13 Subtitle _subtitle;
14 SubtitleFormat _format;
15 Encoding _encoding;
16 public bool ShowBasic { get; private set; }
17 int _totalNumberOfCharacters;
18 bool _loading = true;
19 List<Subtitle> _parts;
20 string _fileName;
21
22 public Split()
23 {
24 InitializeComponent();
25
26 var l = Configuration.Settings.Language.Split;
27 Text = l.Title;
28 groupBoxSplitOptions.Text = l.SplitOptions;
29 RadioButtonLines.Text = l.Lines;
30 radioButtonCharacters.Text = l.Characters;
31 labelNumberOfParts.Text = l.NumberOfEqualParts;
32 groupBoxSubtitleInfo.Text = l.SubtitleInfo;
33 groupBoxOutput.Text = l.Output;
34 labelFileName.Text = l.FileName;
35 labelChooseOutputFolder.Text = l.OutputFolder;
36 labelOutputFormat.Text = Configuration.Settings.Language.Main.Controls.SubtitleFormat;
37 labelEncoding.Text = Configuration.Settings.Language.Main.Controls.FileEncoding;
38 groupBoxPreview.Text = Configuration.Settings.Language.General.Preview;
39 buttonOpenOutputFolder.Text = Configuration.Settings.Language.Main.Menu.File.Open;
40
41 listViewParts.Columns[0].Text = l.Lines;
42 listViewParts.Columns[1].Text = l.Characters;
43 listViewParts.Columns[2].Text = l.FileName;
44
45 buttonSplit.Text = l.DoSplit;
46 buttonBasic.Text = l.Basic;
47 buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
48
49 comboBoxSubtitleFormats.Left = labelOutputFormat.Left + labelOutputFormat.Width + 3;
50 comboBoxEncoding.Left = labelEncoding.Left + labelEncoding.Width + 3;
51
52 FixLargeFonts();
53 }
54
55 private void FixLargeFonts()
56 {
57 Graphics graphics = this.CreateGraphics();
58 SizeF textSize = graphics.MeasureString(buttonSplit.Text, this.Font);
59 if (textSize.Height > buttonSplit.Height - 4)
60 {
61 int newButtonHeight = (int)(textSize.Height + 7 + 0.5);
62 Utilities.SetButtonHeight(this, newButtonHeight, 1);
63 }
64 }
65
66 public void Initialize(Subtitle subtitle, string fileName, SubtitleFormat format, Encoding encoding, double lengthInSeconds)
67 {
68 ShowBasic = false;
69 _subtitle = subtitle;
70 if (string.IsNullOrEmpty(fileName))
71 textBoxFileName.Text = Configuration.Settings.Language.SplitSubtitle.Untitled;
72 else
73 textBoxFileName.Text = fileName;
74 _fileName = fileName;
75 _format = format;
76 _encoding = encoding;
77 foreach (Paragraph p in _subtitle.Paragraphs)
78 _totalNumberOfCharacters += p.Text.Length;
79 labelLines.Text = string.Format(Configuration.Settings.Language.Split.NumberOfLinesX, _subtitle.Paragraphs.Count);
80 labelCharacters.Text = string.Format(Configuration.Settings.Language.Split.NumberOfCharactersX, _totalNumberOfCharacters);
81
82 try
83 {
84 numericUpDownParts.Value = Configuration.Settings.Tools.SplitNumberOfParts;
85 }
86 catch
87 {
88 }
89
90 if (Configuration.Settings.Tools.SplitVia.Trim().ToLower() == "lines")
91 RadioButtonLines.Checked = true;
92 else
93 radioButtonCharacters.Checked = true;
94
95
96 foreach (SubtitleFormat f in SubtitleFormat.AllSubtitleFormats)
97 {
98 if (!f.IsVobSubIndexFile)
99 comboBoxSubtitleFormats.Items.Add(f.FriendlyName);
100 if (f.FriendlyName == format.FriendlyName)
101 comboBoxSubtitleFormats.SelectedIndex = comboBoxSubtitleFormats.Items.Count - 1;
102 }
103
104
105 comboBoxEncoding.Items.Clear();
106 int encodingSelectedIndex = 0;
107 comboBoxEncoding.Items.Add(Encoding.UTF8.EncodingName);
108 foreach (EncodingInfo ei in Encoding.GetEncodings())
109 {
110 if (ei.Name != Encoding.UTF8.BodyName && ei.CodePage >= 949 && !ei.DisplayName.Contains("EBCDIC") && ei.CodePage != 1047)
111 {
112 comboBoxEncoding.Items.Add(ei.CodePage + ": " + ei.DisplayName);
113 if (ei.Name == Configuration.Settings.General.DefaultEncoding)
114 encodingSelectedIndex = comboBoxEncoding.Items.Count - 1;
115 }
116 }
117 comboBoxEncoding.SelectedIndex = encodingSelectedIndex;
118
119
120 if (numericUpDownParts.Maximum > _subtitle.Paragraphs.Count)
121 numericUpDownParts.Maximum = _subtitle.Paragraphs.Count / 2;
122
123 if (!string.IsNullOrEmpty(_fileName))
124 textBoxOutputFolder.Text = System.IO.Path.GetDirectoryName(_fileName);
125 else if (string.IsNullOrEmpty(Configuration.Settings.Tools.SplitOutputFolder) || !System.IO.Directory.Exists(Configuration.Settings.Tools.SplitOutputFolder))
126 textBoxOutputFolder.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
127 else
128 textBoxOutputFolder.Text = Configuration.Settings.Tools.SplitOutputFolder;
129 }
130
131 private void CalculateParts()
132 {
133 if (_loading)
134 return;
135
136 _loading = true;
137 _parts = new List<Subtitle>();
138 if (string.IsNullOrEmpty(textBoxOutputFolder.Text) || !System.IO.Directory.Exists(textBoxOutputFolder.Text))
139 textBoxOutputFolder.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
140 var format = Utilities.GetSubtitleFormatByFriendlyName(comboBoxSubtitleFormats.SelectedItem.ToString());
141 string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(textBoxFileName.Text);
142 if (fileNameNoExt.Trim().Length == 0)
143 fileNameNoExt = Configuration.Settings.Language.SplitSubtitle.Untitled;
144 listViewParts.Items.Clear();
145 int startNumber = 0;
146 if (RadioButtonLines.Checked)
147 {
148 int partSize = (int)(_subtitle.Paragraphs.Count / numericUpDownParts.Value);
149 for (int i = 0; i < numericUpDownParts.Value; i++)
150 {
151 int noOfLines = (int) partSize;
152 if (i == numericUpDownParts.Value -1)
153 noOfLines = (int) (_subtitle.Paragraphs.Count - ((numericUpDownParts.Value-1) * partSize));
154
155 Subtitle temp = new Subtitle();
156 temp.Header = _subtitle.Header;
157 int size = 0;
158 for (int number = 0; number < noOfLines; number++)
159 {
160 Paragraph p = _subtitle.Paragraphs[startNumber + number];
161 temp.Paragraphs.Add(new Paragraph(p));
162 size += p.Text.Length;
163 }
164 startNumber += noOfLines;
165 _parts.Add(temp);
166
167 ListViewItem lvi = new ListViewItem(string.Format("{0:#,###,###}", noOfLines));
168 lvi.SubItems.Add(string.Format("{0:#,###,###}", size));
169 lvi.SubItems.Add(fileNameNoExt + ".Part" + (i + 1) + format.Extension);
170 listViewParts.Items.Add(lvi);
171 }
172 }
173 else if (radioButtonCharacters.Checked)
174 {
175 int partSize = (int)(_totalNumberOfCharacters / numericUpDownParts.Value);
176 int nextLimit = partSize;
177 int currentSize = 0;
178 Subtitle temp = new Subtitle();
179 temp.Header = _subtitle.Header;
180 for (int i = 0; i < _subtitle.Paragraphs.Count; i++)
181 {
182 Paragraph p = _subtitle.Paragraphs[i];
183 int size = p.Text.Length;
184 if (currentSize + size > nextLimit + 4 && _parts.Count < numericUpDownParts.Value-1)
185 {
186 _parts.Add(temp);
187 ListViewItem lvi = new ListViewItem(string.Format("{0:#,###,###}", temp.Paragraphs.Count));
188 lvi.SubItems.Add(string.Format("{0:#,###,###}", currentSize));
189 lvi.SubItems.Add(fileNameNoExt + ".Part" + _parts.Count + format.Extension);
190 listViewParts.Items.Add(lvi);
191 currentSize = size;
192 temp = new Subtitle();
193 temp.Header = _subtitle.Header;
194 temp.Paragraphs.Add(new Paragraph(p));
195 }
196 else
197 {
198 currentSize += size;
199 temp.Paragraphs.Add(new Paragraph(p));
200 }
201 }
202 _parts.Add(temp);
203 ListViewItem lvi2 = new ListViewItem(string.Format("{0:#,###,###}", temp.Paragraphs.Count));
204 lvi2.SubItems.Add(string.Format("{0:#,###,###}", currentSize));
205 lvi2.SubItems.Add(fileNameNoExt + ".Part" + numericUpDownParts.Value + ".srt");
206 listViewParts.Items.Add(lvi2);
207 }
208 _loading = false;
209 }
210
211 private void FormSplitSubtitle_KeyDown(object sender, KeyEventArgs e)
212 {
213 if (e.KeyCode == Keys.Escape)
214 DialogResult = DialogResult.Cancel;
215 }
216
217 private void buttonBasic_Click(object sender, EventArgs e)
218 {
219 ShowBasic = true;
220 DialogResult = DialogResult.Cancel;
221 }
222
223 private void buttonSplit_Click(object sender, EventArgs e)
224 {
225 bool overwrite = false;
226 var format = Utilities.GetSubtitleFormatByFriendlyName(comboBoxSubtitleFormats.SelectedItem.ToString());
227 string fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(textBoxFileName.Text);
228 if (fileNameNoExt.Trim().Length == 0)
229 fileNameNoExt = Configuration.Settings.Language.SplitSubtitle.Untitled;
230
231 int number = 1;
232 try
233 {
234 foreach (Subtitle sub in _parts)
235 {
236 string fileName = System.IO.Path.Combine(textBoxOutputFolder.Text, fileNameNoExt + ".Part" + number + format.Extension);
237 string allText = sub.ToText(format);
238 if (System.IO.File.Exists(fileName) && !overwrite)
239 {
240 if (MessageBox.Show(Configuration.Settings.Language.SplitSubtitle.OverwriteExistingFiles, "", MessageBoxButtons.YesNo) == DialogResult.No)
241 return;
242 overwrite = true;
243 }
244 System.IO.File.WriteAllText(fileName, allText, GetCurrentEncoding());
245 number++;
246 }
247 }
248 catch (Exception exception)
249 {
250 MessageBox.Show(exception.Message);
251 return;
252 }
253
254 Configuration.Settings.Tools.SplitNumberOfParts = (int)numericUpDownParts.Value;
255 Configuration.Settings.Tools.SplitOutputFolder = textBoxOutputFolder.Text;
256 if (RadioButtonLines.Checked)
257 Configuration.Settings.Tools.SplitVia = "Lines";
258 else
259 Configuration.Settings.Tools.SplitVia = "Characters";
260 DialogResult = DialogResult.OK;
261 }
262
263 private void buttonCancel_Click(object sender, EventArgs e)
264 {
265 DialogResult = DialogResult.Cancel;
266 }
267
268 private void numericUpDownParts_ValueChanged(object sender, EventArgs e)
269 {
270 CalculateParts();
271 }
272
273 private void radioButtonCharacters_CheckedChanged(object sender, EventArgs e)
274 {
275 CalculateParts();
276 }
277
278 private void RadioButtonLines_CheckedChanged(object sender, EventArgs e)
279 {
280 CalculateParts();
281 }
282
283 private void textBoxOutputFolder_TextChanged(object sender, EventArgs e)
284 {
285 CalculateParts();
286 }
287
288 private void Split_ResizeEnd(object sender, EventArgs e)
289 {
290 columnHeaderFileName.Width = -2;
291 }
292
293 private void buttonChooseFolder_Click(object sender, EventArgs e)
294 {
295 if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
296 {
297 textBoxOutputFolder.Text = folderBrowserDialog1.SelectedPath;
298 }
299 }
300
301 private Encoding GetCurrentEncoding()
302 {
303 if (comboBoxEncoding.Text == Encoding.UTF8.BodyName || comboBoxEncoding.Text == Encoding.UTF8.EncodingName || comboBoxEncoding.Text == "utf-8")
304 {
305 return Encoding.UTF8;
306 }
307
308 foreach (EncodingInfo ei in Encoding.GetEncodings())
309 {
310 if (ei.CodePage + ": " + ei.DisplayName == comboBoxEncoding.Text)
311 return ei.GetEncoding();
312 }
313
314 return Encoding.UTF8;
315 }
316
317 private void Split_Shown(object sender, EventArgs e)
318 {
319 _loading = false;
320 CalculateParts();
321 }
322
323 private void Split_Resize(object sender, EventArgs e)
324 {
325 columnHeaderFileName.Width = -2;
326 }
327
328 private void Split_KeyDown(object sender, KeyEventArgs e)
329 {
330 if (e.KeyCode == Keys.Escape)
331 DialogResult = DialogResult.Cancel;
332 }
333
334 private void textBoxFileName_TextChanged(object sender, EventArgs e)
335 {
336 CalculateParts();
337 }
338
339 private void comboBoxSubtitleFormats_SelectedIndexChanged(object sender, EventArgs e)
340 {
341 CalculateParts();
342 }
343
344 private void buttonOpenOutputFolder_Click(object sender, EventArgs e)
345 {
346 if (System.IO.Directory.Exists(textBoxOutputFolder.Text))
347 System.Diagnostics.Process.Start(textBoxOutputFolder.Text);
348 else
349 MessageBox.Show(string.Format(Configuration.Settings.Language.SplitSubtitle.FolderNotFoundX, textBoxOutputFolder.Text));
350 }
351
352 }
353}