-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainForm.cs
719 lines (641 loc) · 35.6 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.ComponentModel;
namespace TeX2img {
public partial class MainForm : Form, IOutputController {
private OutputForm myOutputForm;
private PreambleForm myPreambleForm;
private string thisGenerateButtonText;
#region コンストラクタおよび初期化処理関連のメソッド
List<string> FirstFiles = null;
public MainForm(List<string> files) {
if(files.Count != 0) FirstFiles = files;
InitializeComponent();
thisGenerateButtonText = this.GenerateButton.Text;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
myPreambleForm = new PreambleForm(this);
myOutputForm = new OutputForm(this);
sourceTextBox.Highlighter = Sgry.Azuki.Highlighter.Highlighters.Latex;
sourceTextBox.Resize += delegate { sourceTextBox.ViewWidth = sourceTextBox.ClientSize.Width; };
sourceTextBox.ShowsHScrollBar = false;
sourceTextBox.Document.WordProc.EnableWordWrap = false;
sourceTextBox.Document.EolCode = System.Environment.NewLine;
Sgry.Azuki.UserPref.Antialias = Sgry.Azuki.Antialias.Default;
loadSettings();
if(InputFromTextboxRadioButton.Checked) ActiveControl = sourceTextBox;
else ActiveControl = inputFileNameTextBox;
}
protected override void OnShown(EventArgs e) {
if(FirstFiles != null) {
clearOutputTextBox();
if(Properties.Settings.Default.showOutputWindowFlag) showOutputWindow(true);
GenerateButton.Text = Properties.Resources.STOP;
setEnabled(false);
convertWorker.RunWorkerAsync(100);
}
base.OnShown(e);
}
#endregion
#region 設定値の読み書き
private void loadSettings() {
//sourceTextBox.Text = "\\qquad あ";
const int minLength = 50;
if(Properties.Settings.Default.Height > minLength) this.Height = Properties.Settings.Default.Height;
if(Properties.Settings.Default.Width > minLength) this.Width = Properties.Settings.Default.Width;
if(Properties.Settings.Default.Left > 0) this.Left = Properties.Settings.Default.Left;
if(Properties.Settings.Default.Top > 0) this.Top = Properties.Settings.Default.Top;
if(Properties.Settings.Default.preambleWindowHeight > minLength) myPreambleForm.Height = Properties.Settings.Default.preambleWindowHeight;
if(Properties.Settings.Default.preambleWindowWidth > minLength) myPreambleForm.Width = Properties.Settings.Default.preambleWindowWidth;
if(Properties.Settings.Default.outputWindowHeight > minLength) myOutputForm.Height = Properties.Settings.Default.outputWindowHeight;
if(Properties.Settings.Default.outputWindowWidth > minLength) myOutputForm.Width = Properties.Settings.Default.outputWindowWidth;
if(Properties.Settings.Default.outputFile != "") {
outputFileNameTextBox.Text = Properties.Settings.Default.outputFile;
} else {
outputFileNameTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\equation.eps";
}
string ext = Path.GetExtension(outputFileNameTextBox.Text).ToLower();
ExtensioncomboBox.SelectedIndex = 0;
for(int i = 0; i < ExtensioncomboBox.Items.Count; ++i) {
if(ext == "." + ExtensioncomboBox.Items[i].ToString()) ExtensioncomboBox.SelectedIndex = i;
}
inputFileNameTextBox.Text = Properties.Settings.Default.inputFile;
InputFromTextboxRadioButton.Checked = Properties.Settings.Default.inputFromTextBox;
InputFromFileRadioButton.Checked = !InputFromTextboxRadioButton.Checked;
Sgry.Azuki.WinForms.AzukiControl preambleTextBox = myPreambleForm.PreambleTextBox;
preambleTextBox.Text = Properties.Settings.Default.preamble;
// プリアンブルフォームのキャレットを最後に持っていく
preambleTextBox.SetSelection(preambleTextBox.TextLength, preambleTextBox.TextLength);
preambleTextBox.Focus();
preambleTextBox.ScrollToCaret();
ChangeSetting();
setEnabled();
}
private void saveSettings() {
if(this.WindowState != FormWindowState.Minimized) {
Properties.Settings.Default.Height = this.Height;
Properties.Settings.Default.Width = this.Width;
Properties.Settings.Default.Left = this.Left;
Properties.Settings.Default.Top = this.Top;
}
if(myPreambleForm.WindowState != FormWindowState.Minimized) {
Properties.Settings.Default.preambleWindowHeight = myPreambleForm.Height;
Properties.Settings.Default.preambleWindowWidth = myPreambleForm.Width;
}
if(myOutputForm.WindowState != FormWindowState.Minimized) {
Properties.Settings.Default.outputWindowHeight = myOutputForm.Height;
Properties.Settings.Default.outputWindowWidth = myOutputForm.Width;
}
Properties.Settings.Default.outputFile = outputFileNameTextBox.Text;
Properties.Settings.Default.inputFile = inputFileNameTextBox.Text;
Properties.Settings.Default.inputFromTextBox = InputFromTextboxRadioButton.Checked;
Properties.Settings.Default.preamble = myPreambleForm.PreambleTextBox.Text;
}
#endregion
#region 参照ボタンクリックのイベントハンドラ
private void OutputBrowseButton_Click(object sender, EventArgs e) {
var file = outputFileNameTextBox.Text;
saveFileDialog1.FileName = "";
if(file != "") {
try {
var basename = Path.GetFileName(file);
var dir = Path.GetDirectoryName(file);
var ext = Path.GetExtension(file);
if (ext.StartsWith(".")) ext = ext.Substring(1);
saveFileDialog1.FileName = basename;
saveFileDialog1.InitialDirectory = dir;
var filters = saveFileDialog1.Filter.Split(new char[] { '|' }).Where((fil,index) => (index %2 == 1)).Select(s => s.Split(new char[] { ';' })).ToArray();
for(int i = 0; i < filters.Length; ++i) {
foreach(var filext in filters[i]) {
if (filext.ToLower().EndsWith(ext.ToLower())) {
saveFileDialog1.FilterIndex = i + 1;
break;
}
}
}
}
catch (Exception) { }
}
if(saveFileDialog1.ShowDialog() == DialogResult.OK) {
outputFileNameTextBox.Text = saveFileDialog1.FileName;
}
}
private void InputFileBrowseButton_Click(object sender, EventArgs e) {
openFileDialog1.FileName = "";
try {
var file = inputFileNameTextBox.Text;
if (file != "") openFileDialog1.InitialDirectory = Path.GetDirectoryName(file);
}
catch (Exception) { }
if(openFileDialog1.ShowDialog() == DialogResult.OK) {
inputFileNameTextBox.Text = openFileDialog1.FileName;
}
}
#endregion
#region メニュークリックのイベントハンドラ関連
private void setEnabled(object sender, EventArgs e) {
setEnabled();
}
// enabled = falseだと全部disableになる。
private void setEnabled(bool enabled = true) {
menuStrip1.Enabled = enabled;
groupBox1.Enabled = enabled;
groupBox2.Enabled = enabled;
InputFileBrowseButton.Enabled = enabled;
InputFromFileRadioButton.Enabled = enabled;
InputFromTextboxRadioButton.Enabled = enabled;
OutputBrowseButton.Enabled = enabled;
outputFileNameTextBox.Enabled = enabled;
if(enabled) {
sourceTextBox.BackColor = (InputFromTextboxRadioButton.Checked ? Properties.Settings.Default.editorFontColor["テキスト"].Back : System.Drawing.SystemColors.ButtonFace);
sourceTextBox.Enabled = InputFromTextboxRadioButton.Checked;
inputFileNameTextBox.Enabled = InputFileBrowseButton.Enabled = InputFromFileRadioButton.Checked;
色入力ToolStripMenuItem.Enabled = InputFromTextboxRadioButton.Checked;
} else {
sourceTextBox.Enabled = false;
inputFileNameTextBox.Enabled = false;
}
}
private void ExitCToolStripMenuItem_Click(object sender, EventArgs e) {
this.Close();
}
private void showPreambleWindowToolStripMenuItem_Click(object sender, EventArgs e) {
showPreambleWindow(!showPreambleWindowToolStripMenuItem.Checked);
}
public void showPreambleWindow(bool show) {
showPreambleWindowToolStripMenuItem.Checked = show;
if(show) {
myPreambleForm.setLocation(Math.Max(0, Left - myPreambleForm.Width), Top);
myPreambleForm.Show();
} else {
myPreambleForm.Hide();
}
}
private void showOutputWindowToolStripMenuItem_Click(object sender, EventArgs e) {
showOutputWindow(!showOutputWindowToolStripMenuItem.Checked);
}
private void SettingToolStripMenuItem_Click(object sender, EventArgs e) {
Properties.Settings.Default.preamble = myPreambleForm.PreambleTextBox.Text;
(new SettingForm()).ShowDialog();
ChangeSetting();
}
private void AboutToolStripMenuItem_Click(object sender, EventArgs e) {
(new AboutDialog()).ShowDialog();
}
private void ColorInputHelperToolStripMenuItem_Click(object sender, EventArgs e) {
if(!InputFromTextboxRadioButton.Checked) {
MessageBox.Show(Properties.Resources.ONLY_INPUTMODE, "TeX2img");
return;
}
var enUS = new System.Globalization.CultureInfo("en-US");
Func<Color, string> GetColorString = (c) => {
return "{" +
((double)c.R / (double)255).ToString(enUS) + "," +
((double)c.G / (double)255).ToString(enUS) + "," +
((double)c.B / (double)255).ToString(enUS) + "}";
};
using(var cdg = new SupportInputColorDialog()) {
cdg.CustomColors = (int[])Properties.Settings.Default.ColorDialogCustomColors.Clone();
cdg.AnyColor = true;
if(cdg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
sourceTextBox.Document.SetSelection(sourceTextBox.CaretIndex, sourceTextBox.CaretIndex);
int startcaret = sourceTextBox.CaretIndex;
var colstring = GetColorString(cdg.Color);
switch(cdg.CheckedControlSequence) {
case SupportInputColorDialog.ControlSequence.colorbox:
sourceTextBox.Document.Replace("\\colorbox[rgb]" + colstring + "{}");
sourceTextBox.SetSelection(sourceTextBox.CaretIndex - 1, sourceTextBox.CaretIndex - 1);
break;
case SupportInputColorDialog.ControlSequence.textcolor:
sourceTextBox.Document.Replace("\\textcolor[rgb]" + colstring + "{}");
sourceTextBox.SetSelection(sourceTextBox.CaretIndex - 1, sourceTextBox.CaretIndex - 1);
break;
case SupportInputColorDialog.ControlSequence.definecolor:
sourceTextBox.Document.Replace("\\definecolor{}{rgb}" + colstring);
int shift = "\\definecolor{".Length;
sourceTextBox.SetSelection(startcaret + shift, startcaret + shift);
break;
case SupportInputColorDialog.ControlSequence.color:
default:
sourceTextBox.Document.Replace("\\color[rgb]" + colstring);
break;
}
}
cdg.CustomColors.CopyTo(Properties.Settings.Default.ColorDialogCustomColors, 0);
}
}
private void ImportToolStripMenuItem_Click(object sender, EventArgs e) {
var ofd = new OpenFileDialog {
Filter = String.Format(Properties.Resources.IMPORTDIALOG_FILTER,
String.Join(", ", Converter.imageExtensions.Select(d => "*" + d).ToArray()),
String.Join(";", Converter.imageExtensions.Select(d => "*" + d).ToArray()))
};
if(ofd.ShowDialog() == DialogResult.OK) {
try {
if(MessageBox.Show(Properties.Resources.IMPORTMSG, "TeX2img", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) {
ImportFile(ofd.FileName);
}
}
catch(FileNotFoundException) {
MessageBox.Show(String.Format(Properties.Resources.NOTEXIST, ofd.FileName));
}
}
}
private void ExportToolStripMenuItem_Click(object sender, EventArgs e) {
if(TeXsourceSaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var encoding = Converter.GetInputEncoding();
if(encoding.CodePage == Encoding.UTF8.CodePage) encoding = new System.Text.UTF8Encoding(false);
try {
using(var file = new StreamWriter(TeXsourceSaveFileDialog.FileName, false, encoding)) {
TeXSource.WriteTeXSourceFile(file, myPreambleForm.PreambleTextBox.Text, sourceTextBox.Text);
}
}
catch(UnauthorizedAccessException) {
MessageBox.Show(String.Format(Properties.Resources.AUTHORIZEDERROR, TeXsourceSaveFileDialog.FileName));
}
}
}
#endregion
#region その他のイベントハンドラ
protected override void OnClosing(CancelEventArgs e) {
if(Properties.Settings.Default.SaveSettings) saveSettings();
base.OnClosing(e);
}
private void ExtensioncomboBox_SelectionChangeCommitted(object sender, EventArgs e) {
string ext = ExtensioncomboBox.SelectedItem.ToString();
if(ext != "") {
outputFileNameTextBox.Text = Path.ChangeExtension(outputFileNameTextBox.Text, ext);
}
}
private void outputFileNameTextBox_TextChanged(object sender, EventArgs e) {
string ext = Path.GetExtension(outputFileNameTextBox.Text);
for(int i = 0; i < ExtensioncomboBox.Items.Count; ++i) {
if(ext.ToLower() == "." + ExtensioncomboBox.Items[i]) {
ExtensioncomboBox.SelectedIndex = i;
break;
}
}
}
#endregion
#region OutputController インターフェースの実装
public void showPathError(string exeName, string necessary) {
MessageBox.Show(String.Format(Properties.Resources.PATHERROR, exeName, necessary), Properties.Resources.FAILED, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
public void showNoToolError(string item, string tool) {
MessageBox.Show(string.Format(Properties.Resources.NOTOOLERROR, item, tool));
}
public void showExtensionError(string file) {
MessageBox.Show(String.Format(Properties.Resources.INVALID_EXTENSION, file),Properties.Resources.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
delegate void appendOutputDelegate(string log);
public void appendOutput(string log) {
if(myOutputForm.InvokeRequired) {
this.Invoke(new appendOutputDelegate(appendOutput), new Object[] { log });
} else {
myOutputForm.getOutputTextBox().AppendText(log.Replace("\n","\r\n"));
}
}
public void showGenerateError() {
MessageBox.Show(Properties.Resources.GENERATEERROR, Properties.Resources.FAILED, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public void errorIgnoredWarning() {
MessageBox.Show(Properties.Resources.IGNOREDERRORWARNING, Properties.Resources.WARNING, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
delegate void scrollOutputTextBoxToEndDelegate();
public void scrollOutputTextBoxToEnd() {
TextBox output = myOutputForm.getOutputTextBox();
if(output.InvokeRequired) {
this.Invoke(new scrollOutputTextBoxToEndDelegate(scrollOutputTextBoxToEnd));
} else {
output.SelectionStart = output.Text.Length;
output.ScrollToCaret();
}
}
public void showUnauthorizedError(string filePath) {
MessageBox.Show(String.Format(Properties.Resources.AUTHORIZEDERROR, filePath), Properties.Resources.FAILED, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public void showIOError(string filePath) {
MessageBox.Show(String.Format(Properties.Resources.IOERROR, filePath), Properties.Resources.FAILED, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public void showError(string msg) {
MessageBox.Show(msg, "TeX2img");
}
public bool askYesorNo(string msg) {
return (MessageBox.Show(msg, "TeX2img", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes);
}
public void showToolError(string tool) {
var path = Path.Combine(Converter.ShortToolPath, tool);
MessageBox.Show(String.Format(Properties.Resources.TOOLERROR, path, Converter.ShortToolPath), Properties.Resources.FAILED, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
#endregion
#region 画像ファイルの生成
public void showOutputWindow(bool show) {
showOutputWindowToolStripMenuItem.Checked = show;
if(show) {
myOutputForm.setLocation(Left + Width, Top);
myOutputForm.Show();
} else {
myOutputForm.Hide();
}
}
public void clearOutputTextBox() {
myOutputForm.getOutputTextBox().Text = "";
}
private void GenerateButton_Click(object sender, EventArgs arg) {
if(!convertWorker.IsBusy) {
clearOutputTextBox();
if(Properties.Settings.Default.showOutputWindowFlag) showOutputWindow(true);
GenerateButton.Text = Properties.Resources.STOP;
setEnabled(false);
myOutputForm.Activate();
this.Activate();
try { convertWorker.RunWorkerAsync(100); }
catch(InvalidOperationException e){
MessageBox.Show(String.Format(Properties.Resources.DETECTERROR, e.Message));
}
} else {
if(converter != null) converter.Abort();
}
}
Converter converter = null;// 実行中でなければnull
private void convertWorker_DoWork(object sender, DoWorkEventArgs e) {
Properties.Settings.SetCurrentLanguage();
try {
Directory.CreateDirectory(Path.GetTempPath());
}
catch(Exception) {
MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFOLDER, Path.GetTempPath()));
return;
}
if(FirstFiles != null) {
for(int i = 0 ; i < FirstFiles.Count / 2 ; ++i) {
string file = FirstFiles[2 * i];
string workdir;
if (Properties.Settings.Default.workingDirectory == "file") workdir = Path.GetDirectoryName(file);
else if (Properties.Settings.Default.workingDirectory == "current") workdir = Directory.GetCurrentDirectory();
else workdir = Path.GetTempPath();
string tmppath;
try {
string inputextension = Path.GetExtension(file);
tmppath = Path.Combine(workdir,TempFilesDeleter.GetTempFileName(inputextension));
File.Delete(tmppath);
File.Copy(file, tmppath, true);
}
catch(Exception) {
MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFILE, workdir));
continue;
}
var output = Path.GetFullPath(FirstFiles[2 * i + 1]);
converter = new Converter(this, tmppath, output);
converter.Convert();
}
FirstFiles = null;
converter = null;
return;
}
try {
string outputFilePath = outputFileNameTextBox.Text;
if(InputFromFileRadioButton.Checked) {
string inputTeXFilePath = inputFileNameTextBox.Text;
if(!File.Exists(inputTeXFilePath)) {
MessageBox.Show(String.Format(Properties.Resources.NOTEXIST, inputTeXFilePath), Properties.Resources.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
string extension = Path.GetExtension(outputFilePath).ToLower();
string tmpTeXFileName;
string tmpDir;
if (InputFromFileRadioButton.Checked) {
if (Properties.Settings.Default.workingDirectory == "file") tmpDir = Path.GetDirectoryName(inputFileNameTextBox.Text);
else if (Properties.Settings.Default.workingDirectory == "current") tmpDir = Directory.GetCurrentDirectory();
else tmpDir = Path.GetTempPath();
tmpTeXFileName = TempFilesDeleter.GetTempFileName(Path.GetExtension(inputFileNameTextBox.Text), tmpDir);
}else{
if (Properties.Settings.Default.workingDirectory == "current") tmpDir = Directory.GetCurrentDirectory();
else tmpDir = Path.GetTempPath();
tmpTeXFileName = TempFilesDeleter.GetTempFileName();
}
if(tmpTeXFileName == null) {
MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFILE, tmpDir));
return;
}
string tmpFileBaseName = Path.GetFileNameWithoutExtension(tmpTeXFileName);
using(converter = new Converter(this, Path.Combine(tmpDir, tmpTeXFileName), outputFileNameTextBox.Text)) {
if(!converter.CheckFormat()) return;
if(!converter.CheckInputFormat()) return;
#region TeX ソースファイルの準備
// 外部ファイルから入力する場合はテンポラリディレクトリにコピー
if(InputFromFileRadioButton.Checked) {
string inputTeXFilePath = inputFileNameTextBox.Text;
string tmpfile = Path.Combine(tmpDir, tmpTeXFileName);
File.Copy(inputTeXFilePath, tmpfile, true);
// 読み取り専用の場合解除しておく(後でFile.Deleteに失敗するため)。
(new FileInfo(tmpfile)).Attributes = FileAttributes.Normal;
converter.AddInputPath(Path.GetDirectoryName(inputTeXFilePath));
}
// 直接入力の場合 tex ソースを出力
if(InputFromTextboxRadioButton.Checked) {
using(StreamWriter sw = new StreamWriter(Path.Combine(tmpDir, tmpTeXFileName), false, Converter.GetInputEncoding())) {
try {
TeXSource.WriteTeXSourceFile(sw, myPreambleForm.PreambleTextBox.Text, sourceTextBox.Text);
}
catch { }
}
}
#endregion
if(converter.Convert()) {
if(Properties.Settings.Default.setFileToClipBoard) {
if(converter.OutputFileNames.Count > 0) {
Invoke(new Action(() => {
var flist = new System.Collections.Specialized.StringCollection();
foreach(var f in converter.OutputFileNames) flist.Add(f);
Clipboard.SetFileDropList(flist);
}));
}
}
}
}
}
finally {
converter = null;
}
}
private void convertWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
setEnabled(true);
this.GenerateButton.Text = thisGenerateButtonText;
}
#endregion
#region 設定変更通知関連
public void ChangeSetting() {
ChangeSettingofEditor(sourceTextBox);
ChangeSettingofEditor(myPreambleForm.PreambleTextBox);
}
public void ChangeSettingofEditor(Sgry.Azuki.WinForms.AzukiControl textBox) {
if(textBox == null) return;
textBox.FontInfo = new Sgry.Azuki.FontInfo(Properties.Settings.Default.editorFont);
var x = Properties.Settings.Default.editorFontColor;
textBox.DrawsEofMark = Properties.Settings.Default.editorDrawEOF;
textBox.DrawsEolCode = Properties.Settings.Default.editorDrawEOL;
textBox.DrawsFullWidthSpace = Properties.Settings.Default.editorDrawFullWidthSpace;
textBox.DrawsSpace = Properties.Settings.Default.editorDrawSpace;
textBox.DrawsTab = Properties.Settings.Default.editorDrawTab;
textBox.AcceptsTab = Properties.Settings.Default.editorAcceptTab;
textBox.TabWidth = Properties.Settings.Default.editorTabWidth;
textBox.ColorScheme.ForeColor = Properties.Settings.Default.editorFontColor["テキスト"].Font;
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.Normal, Properties.Settings.Default.editorFontColor["テキスト"].Font, Properties.Settings.Default.editorFontColor["テキスト"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.Heading1, Properties.Settings.Default.editorFontColor["テキスト"].Font, Properties.Settings.Default.editorFontColor["テキスト"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.Heading2, Properties.Settings.Default.editorFontColor["テキスト"].Font, Properties.Settings.Default.editorFontColor["テキスト"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.Heading3, Properties.Settings.Default.editorFontColor["テキスト"].Font, Properties.Settings.Default.editorFontColor["テキスト"].Back);
textBox.ColorScheme.SelectionFore = Properties.Settings.Default.editorFontColor["選択範囲"].Font;
textBox.ColorScheme.SelectionBack = Properties.Settings.Default.editorFontColor["選択範囲"].Back;
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.LatexCommand, Properties.Settings.Default.editorFontColor["コントロールシークエンス"].Font, Properties.Settings.Default.editorFontColor["コントロールシークエンス"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.LatexEquation, Properties.Settings.Default.editorFontColor["$"].Font, Properties.Settings.Default.editorFontColor["$"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.LatexBracket, Properties.Settings.Default.editorFontColor["中 / 大括弧"].Font, Properties.Settings.Default.editorFontColor["中 / 大括弧"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.LatexCurlyBracket, Properties.Settings.Default.editorFontColor["中 / 大括弧"].Font, Properties.Settings.Default.editorFontColor["中 / 大括弧"].Back);
textBox.ColorScheme.SetColor(Sgry.Azuki.CharClass.Comment, Properties.Settings.Default.editorFontColor["コメント"].Font, Properties.Settings.Default.editorFontColor["コメント"].Back);
textBox.ColorScheme.EofColor = Properties.Settings.Default.editorFontColor["改行,EOF"].Font;
textBox.ColorScheme.EolColor = Properties.Settings.Default.editorFontColor["改行,EOF"].Font;
textBox.ColorScheme.MatchedBracketFore = Properties.Settings.Default.editorFontColor["対応する括弧"].Font;
textBox.ColorScheme.MatchedBracketBack = Properties.Settings.Default.editorFontColor["対応する括弧"].Back;
textBox.ColorScheme.WhiteSpaceColor = Properties.Settings.Default.editorFontColor["空白"].Font;
Color backColor = new Color();
if(textBox.Enabled) backColor = Properties.Settings.Default.editorFontColor["テキスト"].Back;
else backColor = System.Drawing.SystemColors.ButtonFace;
textBox.ColorScheme.BackColor = backColor;
}
#endregion
#region 右クリック
private void Undo_Click(object sender, EventArgs e) {
sourceTextBox.Undo();
}
private void Cut_Click(object sender, EventArgs e) {
sourceTextBox.Cut();
}
private void Copy_Click(object sender, EventArgs e) {
sourceTextBox.Copy();
}
private void Paste_Click(object sender, EventArgs e) {
sourceTextBox.Paste();
}
private void Delete_Click(object sender, EventArgs e) {
sourceTextBox.Delete();
}
private void SelectAll_Click(object sender, EventArgs e) {
sourceTextBox.SelectAll();
}
private void Redo_Click(object sender, EventArgs e) {
sourceTextBox.Redo();
}
#endregion
#region ソースのインポート
public void ImportFile(string path) {
string preamble, body;
ImportFile(path, out preamble, out body);
if(preamble != null) myPreambleForm.PreambleTextBox.Text = TeXSource.ChangeReturnCode(preamble);
if(body != null) sourceTextBox.Text = TeXSource.ChangeReturnCode(body);
}
public static void ImportFile(string path, out string preamble, out string body) {
var ext = Path.GetExtension(path).ToLower();
bool image = Converter.imageExtensions.Contains(ext);
if(Properties.Settings.Default.embedTeXSource && image) ImportImageFile(path, out preamble, out body);
else ImportTeXFile(path, out preamble, out body);
}
public static void ImportImageFile(string path, out string preamble, out string body) {
preamble = null; body = null;
try {
var text = TeXSource.ReadEmbededSource(path);
if (text != null) {
using (var sr = new StringReader(text)) {
if (!TeXSource.ParseTeXSourceFile(sr, out preamble, out body)) {
MessageBox.Show(Properties.Resources.ANALYZESOURCEERROR, "TeX2img");
}
}
} else throw new IOException();
}
catch(NotImplementedException) {
MessageBox.Show(Properties.Resources.EMBED_IS_ONLY_NTFS);
}
catch(Exception) {
if(File.Exists(path)) {
MessageBox.Show(String.Format(Properties.Resources.NO_EMBEDDED_SOURCE, path), "TeX2img");
} else {
MessageBox.Show(String.Format(Properties.Resources.FAIL_OPEN, path), "TeX2img");
}
}
}
public static void ImportTeXFile(string path,out string preamble,out string body){
preamble = null; body = null;
var extension = Path.GetExtension(path).ToLower();
byte[] buf;
using(var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {
buf = new byte[fs.Length];
fs.Read(buf, 0, (int) fs.Length);
}
var GuessedEncoding = KanjiEncoding.CheckBOM(buf);
bool bom = false;
if(GuessedEncoding != null) {
bom = true;
buf = KanjiEncoding.DeleteBOM(buf, GuessedEncoding);
} else {
var guessed = KanjiEncoding.GuessKajiEncoding(buf);
if(guessed.Length == 0) GuessedEncoding = null;
else GuessedEncoding = guessed[0];
}
Encoding encoding;
switch(Properties.Settings.Default.encode) {
case "euc": encoding = Encoding.GetEncoding("euc-jp"); break;
case "jis": encoding = Encoding.GetEncoding("iso-2022-jp"); break;
case "utf8": encoding = Encoding.UTF8; break;
case "sjis": encoding = Encoding.GetEncoding("shift_jis"); break;
case "_utf8":
if(GuessedEncoding != null) encoding = GuessedEncoding;
else encoding = Encoding.UTF8;
break;
case "_sjis":
default:
if(GuessedEncoding != null) encoding = GuessedEncoding;
else encoding = Encoding.GetEncoding("shift_jis");
break;
}
if(encoding.CodePage != GuessedEncoding.CodePage) {
if (bom || MessageBox.Show(String.Format(Properties.Resources.CHECK_ENCODEMSG, encoding.EncodingName, GuessedEncoding.EncodingName, GuessedEncoding.EncodingName), "TeX2img", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) {
encoding = GuessedEncoding;
}
}
using(var sr = new StringReader(encoding.GetString(buf))) {
if(!TeXSource.ParseTeXSourceFile(sr, out preamble,out body)) {
MessageBox.Show(Properties.Resources.ANALYZESOURCEERROR,"TeX2img");
}
}
}
#endregion
#region D&D
private void sourceTextBox_DragDrop(object sender, DragEventArgs e) {
if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if(files.Length == 0)return;
if (MessageBox.Show(Properties.Resources.IMPORTMSG, "TeX2img", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) return;
ImportFile(files[0]);
}
}
private void TextBox_DragEnter(object sender, DragEventArgs e) {
if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy;
}
}
private void inputFileNameTextBox_DragDrop(object sender, DragEventArgs e) {
if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
if(files.Length == 0) return;
inputFileNameTextBox.Text = files[0];
}
}
#endregion
}
}