-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmMain.cs
More file actions
2079 lines (1807 loc) · 71.7 KB
/
frmMain.cs
File metadata and controls
2079 lines (1807 loc) · 71.7 KB
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.IO;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using FontFamily = System.Windows.Media.FontFamily;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Indentation.CSharp;
using ICSharpCode.AvalonEdit.Search;
using ICSharpCode.AvalonEdit.Utils;
using Application = System.Windows.Forms.Application;
using Color = System.Drawing.Color;
using Control = System.Windows.Forms.Control;
using HighlightingManager = ICSharpCode.AvalonEdit.Highlighting.HighlightingManager;
using MessageBox = System.Windows.Forms.MessageBox;
namespace NotepadSharp
{
public partial class frmMain : Form
{
public TextEditor edit;
// {
// get { return this.textEditorControl; }
// }
// private FoldingManager foldingManager = null;
// private XmlFoldingStrategy foldingStrategy = new XmlFoldingStrategy();
readonly ResourceManager LocRM = new ResourceManager("NotepadSharp.frmMain", typeof(frmMain).Assembly);
private readonly CSharpIndentationStrategy css =
new CSharpIndentationStrategy();
private enum IndentMode
{
Mode2,
Mode4,
Mode8
}
#region Editor Settings
private Boolean _isShowTab;
private Boolean isShowTab
{
get => _isShowTab;
set
{
_isShowTab = value;
TabMenuItem.Checked = value;
edit.TextArea.Options.ShowTabs = value;
Properties.Settings.Default.showTab = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isSpaceAsTab;
private Boolean isSpaceAsTab
{
get => _isSpaceAsTab;
set
{
_isSpaceAsTab = value;
UseSpaceAsTabMenuItem.Checked = value;
edit.TextArea.Options.ConvertTabsToSpaces = value;
Properties.Settings.Default.isSpaceAsTab = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isShowStatusBar;
private Boolean isShowStatusBar
{
get => _isShowStatusBar;
set
{
_isShowStatusBar = value;
IsShowStatusBarMenuItem.Checked = value;
bottomStatusBar.Visible = value;
edit.BringIntoView();
edit.TextArea.Caret.BringCaretToView();
Properties.Settings.Default.showStatusBar = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isShowSpace;
private Boolean isShowSpace
{
get => _isShowSpace;
set
{
_isShowSpace = value;
SpaceMenuItem.Checked = value;
edit.TextArea.Options.ShowSpaces = value;
Properties.Settings.Default.showSpace = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isShowEOL;
private Boolean isShowEOL
{
get => _isShowEOL;
set
{
_isShowEOL = value;
EOLMenuItem.Checked = value;
edit.TextArea.Options.ShowEndOfLine = value;
Properties.Settings.Default.showEOL = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isShowControlChar;
private Boolean isShowControlChar
{
get => _isShowControlChar;
set
{
_isShowControlChar = value;
ControlCharMenuItem.Checked = value;
RShowControlCharMenuItem.Checked = value;
edit.TextArea.Options.ShowBoxForControlCharacters = value;
Properties.Settings.Default.showControlChar = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isShowColRuler;
private Boolean isShowColRuler
{
get => _isShowColRuler;
set
{
_isShowColRuler = value;
ColRulerMenuItem.Checked = value;
edit.TextArea.Options.ShowColumnRuler = value;
Properties.Settings.Default.showColRuler = value;
Properties.Settings.Default.Save();
}
}
private Boolean _isWordWrap;
private Boolean isWordWrap
{
get => _isWordWrap;
set
{
_isWordWrap = value;
WordWarpMenuItem.Checked = value;
edit.WordWrap = value;
Properties.Settings.Default.wordWrap = value;
Properties.Settings.Default.Save();
}
}
#endregion
private class TextWithEncoding
{
public string Content { get; set; }
public Encoding TextEncoding { get; set; }
}
#region Current Props
private string _currFileEncoding;
private string currFileEncoding
{
get => _currFileEncoding;
set
{
_currFileEncoding = value;
encodingStatus.Text = currFileEncoding;
}
}
private string _currTitleName;
private string currTitleName
{
get => _currTitleName;
set
{
_currTitleName = value;
UpdateTitle();
}
}
private FileObject _currOpenedFile;
private FileObject currOpenedFile
{
get => _currOpenedFile;
set
{
_currOpenedFile = value;
currTitleName = value.fileName;
}
}
private float orgFontSize;
private int _currZoomSize = 100;
private int currZoomSize
{
get => _currZoomSize;
set
{
if (value > 500)
{
value = 500;
return;
}
if (value < 50)
{
value = 50;
return;
}
_currZoomSize = value;
zoomStatus.Text = value + "%";
edit.FontSize = Convert.ToInt32(Math.Round(orgFontSize * ((double) value / 100)));
edit.TextArea.Caret.BringCaretToView();
}
}
private Font _currFont;
private Font currFont
{
get
{
if (_currFont is null)
{
return Properties.Settings.Default.font;
}
else
{
return _currFont;
}
}
set
{
edit.FontFamily = new FontFamily(value.FontFamily.Name);
edit.FontSize =
Convert.ToInt32(Math.Round(value.Size * ((double) currZoomSize / 100))); //value.Size;
fontDialog.Font = value;
_currFont = value;
Properties.Settings.Default.font = value;
Properties.Settings.Default.Save();
}
}
private Boolean currIsAutoIndent;
private IndentMode _currIndentMode;
private IndentMode currIndentMode
{
get => _currIndentMode;
set
{
_currIndentMode = value;
int indentValue = 0;
switch (value)
{
case IndentMode.Mode2:
edit.Options.IndentationSize = 2;
TabWidthStatus.Text = LocRM.GetString("TabSize") + ": 2";
Indent2MenuItem.Checked = true;
Indent4MenuItem.Checked = false;
Indent8MenuItem.Checked = false;
indentValue = 1;
break;
case IndentMode.Mode4:
edit.Options.IndentationSize = 4;
TabWidthStatus.Text = LocRM.GetString("TabSize") + ": 4";
Indent2MenuItem.Checked = false;
Indent4MenuItem.Checked = true;
Indent8MenuItem.Checked = false;
indentValue = 2;
break;
case IndentMode.Mode8:
edit.Options.IndentationSize = 8;
TabWidthStatus.Text = LocRM.GetString("TabSize") + ": 8";
Indent2MenuItem.Checked = false;
Indent4MenuItem.Checked = false;
Indent8MenuItem.Checked = true;
indentValue = 3;
break;
}
Properties.Settings.Default.IndentMode = indentValue;
Properties.Settings.Default.Save();
}
}
private String _currSyntaxHighlighting;
private String currSyntaxHighlighting
{
get => _currSyntaxHighlighting;
set
{
_currSyntaxHighlighting = value;
foreach (ToolStripMenuItem tsmi in HighLightTypeMenuItem.DropDownItems)
{
tsmi.Checked = tsmi.Text == value;
}
IHighlightingDefinition currDefine =
HighlightingManager.Instance.GetDefinition(value);
HighLightTypeMenuItem.Text = (value != LocRM.GetString("NormalText") && currDefine is null)
? LocRM.GetString("LoadFail")
: value;
edit.SyntaxHighlighting = currDefine;
}
}
private int _currCaretLine = 1;
private int currCaretLine
{
get => _currCaretLine;
set
{
_currCaretLine = value;
cursorStatus.Text = string.Format(LocRM.GetString("LineColumnStatus"), _currCaretLine.ToString(),
_currCaretColumn.ToString());
}
}
private int _currCaretColumn = 1;
private int currCaretColumn
{
get => _currCaretColumn;
set
{
_currCaretColumn = value;
cursorStatus.Text = string.Format(LocRM.GetString("LineColumnStatus"), _currCaretLine.ToString(),
_currCaretColumn.ToString());
}
}
private int _currLength = 0;
private int currLength
{
get => _currLength;
set
{
_currLength = value;
textLengthStatus.Text = string.Format(LocRM.GetString("textLengthStatus"), _currLength.ToString(),
_currLines.ToString());
}
}
private int _currLines = 0;
private int currLines
{
get => _currLines;
set
{
_currLines = value;
textLengthStatus.Text = string.Format(LocRM.GetString("textLengthStatus"), _currLength.ToString(),
_currLines.ToString());
}
}
private string _currReturnStyle = "Windows (CRLF)";
private string currReturnStyle
{
get => _currReturnStyle;
set
{
_currReturnStyle = value;
returnStyleStatus.Text = value;
}
}
private Boolean _hasSave = true;
private Boolean hasSave
{
get => _hasSave;
set
{
_hasSave = value;
UpdateTitle();
}
}
private Boolean isLoadFile = false; //To determine whether it's loading a new file or
// text typing that results to TextChange.
#endregion
private static Task<TextWithEncoding> textReaderTask;
private readonly Stopwatch sw = new Stopwatch();
private static readonly TextWithEncoding txt = new TextWithEncoding();
readonly PrintDocument printDocument;
public frmMain()
{
InitializeComponent();
fontDialog.Apply += FontDialog_Apply;
printDocument = new PrintDocument();
printDocument.PrintPage += printDocument_PrintPage;
if (Environment.GetCommandLineArgs().Length > 1 && Environment.GetCommandLineArgs()[1] != "" &&
!(Environment.GetCommandLineArgs()[1] is null) && File.Exists(Environment.GetCommandLineArgs()[1]))
{
sw.Start();
// test = ReadTextAsync(Environment.GetCommandLineArgs()[1]);
textReaderTask = ReadFileAsync(Environment.GetCommandLineArgs()[1]);
}
}
private void FontDialog_Apply(object sender, EventArgs e)
{
currFont = fontDialog.Font;
orgFontSize = currFont.Size;
}
#region Editor Events
private Boolean inTheBrackets = false;
private void Caret_PositionChanged(object sender, EventArgs e)
{
if (edit.TextArea.Caret.Line >= 2)
{
int lastLine = edit.Document.Lines[edit.TextArea.Caret.Line - 2].EndOffset - 1;
int currCaret = edit.TextArea.Caret.Offset;
if (currCaret + 1 <= edit.Document.Text.Length && lastLine >= 0)
{
if ((edit.Document.Text.Substring(lastLine, 1) == "{" &&
edit.Document.Text.Substring(currCaret, 1) == "}") ||
(edit.Document.Text.Substring(lastLine, 1) == "[" &&
edit.Document.Text.Substring(currCaret, 1) == "]") ||
(edit.Document.Text.Substring(lastLine, 1) == "(" &&
edit.Document.Text.Substring(currCaret, 1) == ")")
)
{
inTheBrackets = true;
}
else
{
inTheBrackets = false;
}
}
else
{
inTheBrackets = false;
}
}
else
{
inTheBrackets = false;
}
updateStatusBar();
UpdateMenuItem();
}
private void Document_UpdateFinished(object sender, EventArgs e)
{
if (isLoadFile)
{
currFileEncoding = edit.Encoding?.EncodingName;
currSyntaxHighlighting = (edit.SyntaxHighlighting is null ? "普通文本" : edit.SyntaxHighlighting.Name);
if (!(currSyntaxHighlighting is null) &&
(currSyntaxHighlighting.Contains("C") || currSyntaxHighlighting.Contains("PHP") ||
currSyntaxHighlighting.Contains("Java")))
{
if (Properties.Settings.Default.isAutoIndent == -1)
{
if (MessageBox.Show(LocRM.GetString("IsAutoIndent"), LocRM.GetString("$this.Text"),
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
AutoIndentMenuItem.Checked = true;
Properties.Settings.Default.isAutoIndent = 1;
Properties.Settings.Default.Save();
}
else
{
Properties.Settings.Default.isAutoIndent = 0;
Properties.Settings.Default.Save();
}
}
else if (Properties.Settings.Default.isAutoIndent == 1)
{
AutoIndentMenuItem.Checked = true;
}
}
isLoadFile = false;
}
if (edit.Document.FileName == "\\Untitled\\" || edit.Document.FileName == "" ||
(edit.Document.FileName is null))
{
currReturnStyle = "Windows (CRLF)";
}
else
{
currReturnStyle = determineReturnStyle();
}
}
private void Document_UpdateStarted(object sender, EventArgs e)
{
int a = 1;
}
private void TextArea_MouseWheel(object sender, MouseWheelEventArgs e)
{
if ((ModifierKeys & Keys.Control) == Keys.Control)
{
if (e.Delta > 0)
{
currZoomSize += 10;
}
else
{
currZoomSize -= 10;
}
e.Handled = true;
}
}
private void TextArea_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
updateStatusBar();
UpdateMenuItem();
}
private void TextArea_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (isLoadFile)
{
isLoadFile = false; //Restore to default
}
if (e.Key == Key.F6)
{
if (inTheBrackets && isCoding())
{
String retChar = getCurrReturnChar();
int currCaret;
currCaret = edit.TextArea.Caret.Offset;
edit.Document.Insert(currCaret, retChar);
edit.TextArea.Caret.Line--;
edit.Document.Insert(currCaret, " ");
edit.TextArea.Caret.Offset = edit.Document.Lines[edit.TextArea.Caret.Line - 1].EndOffset;
}
}
updateStatusBar();
UpdateMenuItem();
}
private void TextArea_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (isCoding())
{
String retChar = "\r\n";
int currCaret = 0;
int currIndex = edit.TextArea.Caret.Offset - 1;
switch (e.Text)
{
case "{":
if (!(edit.Text.Length > currIndex + 1 && edit.Text.Substring(currIndex + 1, 1) == "}"))
{
edit.Document.Insert(currIndex + 1, "}");
}
edit.TextArea.Caret.Offset--;
break;
case "[":
if (!(edit.Text.Length > currIndex + 1 && edit.Text.Substring(currIndex + 1, 1) == "]"))
{
edit.Document.Insert(currIndex + 1, "]");
}
edit.TextArea.Caret.Offset--;
break;
case "(":
if (!(edit.Text.Length > currIndex + 1 && edit.Text.Substring(currIndex + 1, 1) == ")"))
{
edit.Document.Insert(currIndex + 1, ")");
edit.TextArea.Caret.Offset--;
}
break;
case ")":
if (edit.Text.Length > currIndex + 1 && edit.Text.Substring(currIndex + 1, 1) == ")")
{
edit.TextArea.Caret.Offset++;
e.Handled = true;
}
break;
case "}":
if (edit.Text.Length > currIndex + 1 && edit.Text.Substring(currIndex + 1, 1) == "}")
{
// edit.TextArea.Caret.Offset++;
e.Handled = true;
retChar = getCurrReturnChar();
edit.Document.BeginUpdate();
currCaret = edit.TextArea.Caret.Offset;
edit.Document.Insert(currCaret, retChar);
currCaret = edit.TextArea.Caret.Offset;
edit.Document.Insert(currCaret, retChar);
edit.TextArea.Caret.Line--;
currCaret = edit.TextArea.Caret.Offset;
edit.Document.Insert(currCaret, "\t");
if (isCoding() && currIsAutoIndent)
{
css.IndentLines(edit.Document, 0, edit.LineCount);
}
edit.TextArea.Caret.Offset = edit.Document.Lines[edit.TextArea.Caret.Line - 1].EndOffset;
edit.Document.EndUpdate();
e.Handled = true;
}
break;
case ";":
if (isCoding() && currIsAutoIndent)
{
css.IndentLines(edit.Document, 0, edit.LineCount);
}
break;
case "]":
if (edit.Text.Length > currIndex + 1 && edit.Text.Substring(currIndex + 1, 1) == "]")
{
edit.TextArea.Caret.Offset++;
e.Handled = true;
}
break;
case " ":
if (inTheBrackets)
{
retChar = getCurrReturnChar();
edit.Document.BeginUpdate();
currCaret = edit.TextArea.Caret.Offset;
edit.Document.Insert(currCaret, retChar);
edit.TextArea.Caret.Line--;
currCaret = edit.TextArea.Caret.Offset;
edit.Document.Insert(currCaret, "\t");
if (isCoding() && currIsAutoIndent)
{
css.IndentLines(edit.Document, 0, edit.LineCount);
}
edit.TextArea.Caret.Offset = edit.Document.Lines[edit.TextArea.Caret.Line - 1].EndOffset;
edit.Document.EndUpdate();
e.Handled = true;
}
break;
}
}
}
private void TextArea_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
updateStatusBar();
UpdateMenuItem();
}
private void Edit_FileNameChanged(object sender, EventArgs e)
{
if (edit.Document.FileName == "\\Untitled\\")
{
currOpenedFile =
new FileObject(LocRM.GetString("defaultTitle"), "", ".txt");
}
else
{
currOpenedFile = new FileObject(Path.GetFileName(edit.Document.FileName),
edit.Document.FileName,
Path.GetExtension(edit.Document.FileName));
edit.SyntaxHighlighting =
HighlightingManager
.Instance.GetDefinitionByExtension(Path.GetExtension(edit.Document.FileName));
hasSave = true;
isLoadFile = true;
}
}
private void Edit_Click(object sender, EventArgs e)
{
updateStatusBar();
UpdateMenuItem();
}
private void Edit_TextChanged(object sender, EventArgs e)
{
if ((edit.Document.FileName == "\\Untitled\\" && edit.TextArea.Document.Text == "") || isLoadFile)
{
hasSave = true;
}
else
{
hasSave = false;
}
updateStatusBar();
UpdateMenuItem();
}
private void Edit_DragEnter(object sender, System.Windows.DragEventArgs e)
{
}
private async void Edit_Drop(object sender, System.Windows.DragEventArgs e)
{
String fileName = ((System.Array) e.Data.GetData(System.Windows.Forms.DataFormats.FileDrop))
.GetValue(0).ToString();
if (checkUnsave())
{
showLoading();
edit.Document.FileName = "\\Untitled\\"; //To ensure triggering FileNameChanged event
edit.SelectionLength = 0;
edit.Document.FileName = fileName; //If not, open the same name file again, will not trigger this event.
sw.Start();
textReaderTask = ReadFileAsync(fileName);
readFileToEdit(await textReaderTask);
// edit.Load(openFileDialog.FileName);
}
}
#endregion
#region loadingCircle
private void frmMain_Resize(object sender, EventArgs e)
{
picLoading.Left = panelLoading.Width / 2 - picLoading.Width / 2;
picLoading.Top = panelLoading.Height / 2 - picLoading.Height / 2;
}
private void picLoading_Resize(object sender, EventArgs e)
{
picLoading.Left = panelLoading.Width / 2 - picLoading.Width / 2;
picLoading.Top = panelLoading.Height / 2 - picLoading.Height / 2;
}
private void showLoading()
{
this.Text = LocRM.GetString("LoadingFile");
updateStatusBar(true);
panelLoading.Visible = true;
panelLoading.BringToFront();
picLoading.Left = panelLoading.Width / 2 - picLoading.Width / 2;
picLoading.Top = panelLoading.Height / 2 - picLoading.Height / 2;
}
private void hideLoading()
{
panelLoading.Visible = false;
panelLoading.SendToBack();
}
#endregion
#region Util Functions
/// <summary>
/// 移除字符串末尾指定字符
/// </summary>
/// <param name="str">需要移除的字符串</param>
/// <param name="value">指定字符</param>
/// <returns>移除后的字符串</returns>
public static string RemoveLastChar(string str, string value)
{
int _finded = str.LastIndexOf(value);
if (_finded != -1)
{
return str.Substring(0, _finded);
}
return str;
}
private void replaceSelectionTextWith(string strTo)
{
if (HaveSelection())
{
//Sometime startPosition is bigger than endPosition, this is because you select text
//from back to front. REMEMBER, startPosition is the position you START to select.
int Offset1 = edit.Document.GetOffset(edit.TextArea.Selection.StartPosition.Location);
int Offset2 = edit.Document.GetOffset(edit.TextArea.Selection.EndPosition.Location);
//So we have to detect which one is smaller, the smaller one is the start position (The ahead text offset)...
int startOffset = Offset1 < Offset2 ? Offset1 : Offset2;
edit.Document.BeginUpdate();
edit.Document.Remove(startOffset, edit.TextArea.Selection.Length);
edit.Document.Insert(startOffset, strTo);
edit.Document.EndUpdate();
}
}
public void WriteToFile(string path, string content)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, edit.Encoding);
//开始写入
sw.Write(content);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
/// <summary>
/// 获取当前本地时间戳
/// </summary>
/// <returns></returns>
public long GetCurrentTimeUnix()
{
TimeSpan cha = (DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)));
long t = (long) cha.TotalSeconds;
return t;
}
private void previewMarkDown()
{
String rootPath = "";
if (edit.Document.FileName == "\\Untitled\\" || edit.Document.FileName == "" ||
(edit.Document.FileName is null))
{
rootPath = Environment.GetEnvironmentVariable("TEMP");
}
else
{
rootPath = Path.GetDirectoryName(edit.Document.FileName);
}
String fileDisplayName = (edit.Document.FileName == "\\Untitled\\"
? LocRM.GetString("defaultTitle")
: Path.GetFileNameWithoutExtension(edit.Document.FileName));
rootPath = rootPath.EndsWith("\\") ? rootPath : rootPath + "\\";
String fileName = rootPath + fileDisplayName +
"_preview@" +
GetCurrentTimeUnix() + ".html";
String header = @"
<!DOCTYPE html>
<html>
<title>" + fileDisplayName;
header += @"</title>
<xmp theme=""cerulean"" style=""display:none;"">
";
String footer = @"
</xmp>
<script src=""http://strapdownjs.com/v/0.2/strapdown.js""></script>
</html>
";
WriteToFile(fileName, header + edit.Text + footer);
Process.Start(fileName);
}
//Thanks to https://blog.csdn.net/wrgoodliness/article/details/52703660
String s;
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
StringFormat stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces, 0);
Graphics g = e.Graphics; //获得绘图对象
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
//像素偏移方式,像素在水平和垂直距离上均偏移若干个单位,以进行高速锯齿消除。
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
//也可以通过设置Graphics对不平平滑处理方式解决,代码如下:
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
SizeF sf = e.Graphics.MeasureString(s, currFont, e.MarginBounds.Size, stringFormat, out var count,
out var rows);
//MessageBox.Show("总长度:" + s.Length.ToString() + "#当页长度:" + count.ToString() + "#行数:" + rows.ToString());
e.Graphics.DrawString(s.Substring(0, count), currFont, new SolidBrush(Color.Black), e.MarginBounds,
stringFormat);
s = s.Remove(0, count < s.Length ? count : s.Length);
Debug.Print(s.Length.ToString());
if (s.Length > 0)
e.HasMorePages = true;
else
{
e.HasMorePages = false;
s = edit.Text;
}
}
private Boolean isCoding()
{
return !(edit.SyntaxHighlighting is null);
}
private string getCurrReturnChar()
{
String retChar = "\r\n";
int currCaret;
switch (determineReturnStyle())
{
case "Error":
case "Windows (CRLF)":
retChar = "\r\n";
break;
case "Unix (LF)":
retChar = "\n";
break;
case "Macintosh (CR)":
retChar = "\r";
break;
}
return retChar;
}
private string determineReturnStyle()
{
try
{
if (edit.TextArea.Document.Text.Contains("\r\n"))
{
return "Windows (CRLF)";
}
else if (edit.TextArea.Document.Text.Contains("\n"))
{
return "Unix (LF)";
}
else if (edit.TextArea.Document.Text.Contains("\r"))
{
return "Macintosh (CR)";
}
else
{
return "Windows (CRLF)";
}
}
catch
{
return "Error";
}
}
private void updateStatusBar(Boolean isClear=false)
{
if (isClear)
{
currFileEncoding = "Loading";
currCaretLine =0;
currCaretColumn = 0;
currLength = 0;
currLines = 0;
}
else
{