-
Notifications
You must be signed in to change notification settings - Fork 42
/
Main.cs
784 lines (619 loc) · 27.4 KB
/
Main.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
// TODO: In order to mimic Notepad exactly the status bar should be hidden if Word Wrap is turned off and the option should be disabled. Setting should be restored if Word Wrap is turned back off.
namespace MyNotepad {
public partial class Main : Form {
public Main() {
InitializeComponent();
if (!Settings.WindowPosition.IsEmpty) {
Bounds = Settings.WindowPosition;
StartPosition = FormStartPosition.Manual;
}
}
private Encoding _encoding = Encoding.ASCII;
private string _Filename;
public string Filename {
get {
return _Filename;
}
set {
var oldvalue = value;
_Filename = value;
OnFilenameChanged(oldvalue, value);
}
}
private void OnFilenameChanged(string oldvalue, string value) {
OnDocumentNameChanged();
}
private void OnDocumentNameChanged() {
UpdateTitle();
}
private void UpdateTitle() {
if (this.Tag == null) {
this.Tag = base.Text;
}
base.Text = ((string)this.Tag).FormatUsingObject(new { DocumentName });
}
public string DocumentName {
get {
if (Filename == null) return "Untitled";
return Path.GetFileName(Filename);
}
}
public string Content {
get { return controlContentTextBox.Text; }
set { controlContentTextBox.Text = value; }
}
private void controlContentTextBox_TextChanged(object sender, EventArgs e) {
IsDirty = true;
}
private void Main_Load(object sender, EventArgs e) {
UpdateTitle();
menuitemFormatWordWrap.Checked = controlContentTextBox.WordWrap;
CurrentFont = Settings.CurrentFont;
UpdateStatusBar();
menuitemViewStatusBar.Checked = controlStatusBar.Visible = Settings.IsStatusBarVisible;
controlContentTextBox.BringToFront(); // in order to docking to respond correctly to the status bar being turned off and on
}
public bool IsStatusBarVisible {
get {
return Settings.IsStatusBarVisible;
}
set {
menuitemViewStatusBar.Checked = controlStatusBar.Visible = Settings.IsStatusBarVisible = value;
Settings.Save();
}
}
public bool WordWrap {
get {
return controlContentTextBox.WordWrap;
}
set {
menuitemFormatWordWrap.Checked = controlContentTextBox.WordWrap = value;
}
}
private void menuitemFormatWordWrap_Click(object sender, EventArgs e) {
WordWrap = !WordWrap;
}
private void menuitemFormatWordWrap_CheckedChanged(object sender, EventArgs e) {
var Sender = (ToolStripMenuItem)sender;
WordWrap = Sender.Checked;
}
private static Properties.Settings Settings {
get { return Properties.Settings.Default; }
}
private Font CurrentFont {
get {
return Settings.CurrentFont;
}
set {
controlContentTextBox.Font = Settings.CurrentFont = value;
Settings.Save();
}
}
private void menuitemFormatFont_Click(object sender, EventArgs e) {
var FontDialog = new FontDialog();
FontDialog.Font = CurrentFont;
if (FontDialog.ShowDialog(this) != DialogResult.OK) return;
CurrentFont = FontDialog.Font;
}
private bool _IsDirty;
public bool IsDirty {
get {
if (Filename == null && Content.IsEmpty()) return false;
return _IsDirty;
}
set {
_IsDirty = value;
}
}
private bool Save() {
if (!IsDirty) return true;
if ((Filename == null) || new FileInfo(Filename).IsReadOnly) {
return SaveAs();
}
File.WriteAllText(Filename, Content);
IsDirty = false;
return true;
}
private bool SaveAs() {
var SaveDialog = new SaveOpenDialog();
SaveDialog.FileDlgFileName = Filename;
SaveDialog.FileDlgDefaultExt = ".txt";
SaveDialog.FileDlgFilter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*";
SaveDialog.Encoding = _encoding;
SaveDialog.FileDlgCaption = "Save";
SaveDialog.FileDlgOkCaption = "Save";
if (SaveDialog.ShowDialog(this) != DialogResult.OK) return false;
var PotentialFilename = SaveDialog.MSDialog.FileName;
_encoding = SaveDialog.Encoding;
File.WriteAllText(PotentialFilename, Content, _encoding);
Filename = PotentialFilename;
IsDirty = false;
return true;
}
private void menuitemFileOpen_Click(object sender, EventArgs e) {
if (!EnsureWorkNotLost()) return;
var OpenDialog = new SaveOpenDialog();
OpenDialog.FileDlgDefaultExt = ".txt";
OpenDialog.FileDlgFileName = Filename;
OpenDialog.FileDlgFilter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*";
OpenDialog.FileDlgType = Win32Types.FileDialogType.OpenFileDlg;
OpenDialog.FileDlgCaption = "Open";
OpenDialog.FileDlgOkCaption = "Open";
if (OpenDialog.ShowDialog(this) != DialogResult.OK) return;
Open(OpenDialog.MSDialog.FileName, OpenDialog.Encoding);
}
public void Open(string pFilename, Encoding encoding = null) {
var Filename = pFilename;
if (!File.Exists(Filename)) {
var FileExists = false;
var Extension = Path.GetExtension(Filename);
if (Extension == "") {
Filename = Filename + ".txt";
FileExists = File.Exists(Filename);
}
if (!FileExists) {
#region Message
var Message = @"Cannot find the {Filename} file.
Do you want to create a new file?
".FormatUsingObject(new { Filename = Filename });
#endregion
var Result = MessageBox.Show(Message, "Notepad Clone", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
switch (Result) {
case DialogResult.Yes:
File.WriteAllText(Filename, "");
break;
case DialogResult.No:
case DialogResult.Cancel:
return;
default:
throw new Exception();
}
}
}
#region Determine Encoding
if (encoding == null) { // generally this means it was not opened by a user using the open file dialog
using (var streamReader = new StreamReader(Filename, detectEncodingFromByteOrderMarks: true)) {
var text = streamReader.ReadToEnd();
_encoding = streamReader.CurrentEncoding;
}
}
#endregion
Content = ReadAllText(Filename, encoding);
SelectionStart = 0;
this.Filename = Filename;
IsDirty = false;
}
private static string ReadAllText(string pFilename, Encoding encoding) {
using (var FileStream = new FileStream(pFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
if (encoding == null) {
using (var StreamReader = new StreamReader(FileStream)) {
var text = StreamReader.ReadToEnd();
return text;
}
} else {
using (var StreamReader = new StreamReader(FileStream, encoding, false)) {
var text = StreamReader.ReadToEnd();
return text;
}
}
}
}
private void menuitemFileSave_Click(object sender, EventArgs e) {
Save();
}
private void menuitemFileSaveAs_Click(object sender, EventArgs e) {
SaveAs();
}
private void menuitemFileNew_Click(object sender, EventArgs e) {
New();
}
private bool EnsureWorkNotLost() {
if (!IsDirty) return true;
var DialogResult = new SaveChangesPrompt(Filename).ShowDialog(this);
switch(DialogResult) {
case DialogResult.Yes:
return Save();
case DialogResult.No:
return true;
case DialogResult.Cancel:
return false;
default:
throw new Exception();
}
}
private bool New() {
if (!EnsureWorkNotLost()) return false;
Filename = null;
Content = "";
IsDirty = false;
_encoding = Encoding.ASCII;
return true;
}
private void menuitemFilePageSetup_Click(object sender, EventArgs e) {
var PageSetupDialog = new PageSetupDialog();
PageSetupDialog.PageSettings = PageSettings;
if (PageSetupDialog.ShowDialog(this) != DialogResult.OK) return;
PageSettings = PageSetupDialog.PageSettings;
}
private PageSettings _PageSettings;
private PageSettings PageSettings {
get {
if (_PageSettings == null) {
if (Settings.MoreSettings.PageSettings != null) {
_PageSettings = Settings.MoreSettings.PageSettings;
} else {
var PageSettings = new PageSettings() {
Margins = new Margins(75, 75, 100, 100), // 100 = 1 inch
};
_PageSettings = PageSettings;
}
}
return _PageSettings;
}
set {
Settings.MoreSettings.PageSettings = value;
Settings.Save();
}
}
private void menuitemFilePrint_Click(object IGNORE_sender, EventArgs IGNORE_e) {
var PrintDialog = new PrintDialog();
if (Settings.MoreSettings.PrinterSettings != null) {
PrintDialog.PrinterSettings = Settings.MoreSettings.PrinterSettings;
}
if (PrintDialog.ShowDialog(this) != DialogResult.OK) return;
Settings.MoreSettings.PrinterSettings = PrintDialog.PrinterSettings;
Settings.Save();
var PrintDocument = new PrintDocument();
PrintDocument.DefaultPageSettings = PageSettings;
PrintDocument.PrinterSettings = Settings.MoreSettings.PrinterSettings;
PrintDocument.DocumentName = DocumentName + " - Notepad Clone";
var RemainingContentToPrint = Content;
var PageIndex = 0;
PrintDocument.PrintPage += (sender, e) => {
{ // header
var HeaderText = FormatHeaderFooterText(Settings.Header, PageIndex);
var Top = PageSettings.Margins.Top;
DrawStringAtPosition(e.Graphics, HeaderText.Left, Top, DrawStringPosition.Left);
DrawStringAtPosition(e.Graphics, HeaderText.Center, Top, DrawStringPosition.Center);
DrawStringAtPosition(e.Graphics, HeaderText.Right, Top, DrawStringPosition.Right);
}
{ // body
var CharactersFitted = 0;
var LinesFilled = 0;
var MarginBounds = new RectangleF(e.MarginBounds.X, e.MarginBounds.Y + /* header */ CurrentFont.Height, e.MarginBounds.Width, e.MarginBounds.Height - (/* header and footer */ CurrentFont.Height * 2));
e.Graphics.MeasureString(RemainingContentToPrint, CurrentFont, MarginBounds.Size, StringFormat.GenericTypographic, out CharactersFitted, out LinesFilled);
e.Graphics.DrawString(RemainingContentToPrint, CurrentFont, Brushes.Black, MarginBounds, StringFormat.GenericTypographic);
RemainingContentToPrint = RemainingContentToPrint.Substring(CharactersFitted);
e.HasMorePages = (RemainingContentToPrint.Length > 0);
}
{ // footer
var FooterText = FormatHeaderFooterText(Settings.Footer, PageIndex);
var Top = PageSettings.Bounds.Bottom - PageSettings.Margins.Bottom - CurrentFont.Height;
DrawStringAtPosition(e.Graphics, FooterText.Left, Top, DrawStringPosition.Left);
DrawStringAtPosition(e.Graphics, FooterText.Center, Top, DrawStringPosition.Center);
DrawStringAtPosition(e.Graphics, FooterText.Right, Top, DrawStringPosition.Right);
}
PageIndex++;
};
PrintDocument.Print();
}
private enum DrawStringPosition {
Left,
Center,
Right,
}
private void DrawStringAtPosition(Graphics pGraphics, string pText, int Top, DrawStringPosition pPosition) {
var HeaderTextSize = new SizeF(pGraphics.MeasureString(pText, CurrentFont));
var HeaderTextWidth = HeaderTextSize.Width;
var PageWidth = PageSettings.Bounds.Right - PageSettings.Bounds.Left;
float Left;
if (pPosition == DrawStringPosition.Left) {
Left = PageSettings.Margins.Left;
} else if (pPosition == DrawStringPosition.Center) {
Left = ((PageWidth - HeaderTextWidth) / 2);
} else if (pPosition == DrawStringPosition.Right) {
Left = PageWidth - PageSettings.Margins.Right - HeaderTextWidth;
} else {
throw new Exception();
}
pGraphics.DrawString(pText, CurrentFont, Brushes.Black, Left, Top);
}
private class HeaderOrFooterInfo {
public string Left = "";
public string Center = "";
public string Right = "";
}
private HeaderOrFooterInfo FormatHeaderFooterText(string pText, int PageIndex) {
var HeaderOrFooterInfo = GetHeaderOrFooterInfo(pText);
HeaderOrFooterInfo.Left = FormatSingleHeaderFooterText(HeaderOrFooterInfo.Left, PageIndex);
HeaderOrFooterInfo.Center = FormatSingleHeaderFooterText(HeaderOrFooterInfo.Center, PageIndex);
HeaderOrFooterInfo.Right = FormatSingleHeaderFooterText(HeaderOrFooterInfo.Right, PageIndex);
return HeaderOrFooterInfo;
}
private string FormatSingleHeaderFooterText(string pText, int PageIndex) {
return pText
.Replace("&f", DocumentName)
.Replace("&p", (PageIndex + 1).ToString())
.Replace("&d", DateTime.Now.ToLongDateString())
.Replace("&t", DateTime.Now.ToLongTimeString())
;
}
private static HeaderOrFooterInfo GetHeaderOrFooterInfo(string pText) {
const string CONST_Left = "Left";
const string CONST_Center = "Center";
const string CONST_Right = "Right";
var LeftIndexes = Helper.GetIndexes(pText, "&l", false);
var CenterIndexes = Helper.GetIndexes(pText, "&c", false);
var RightIndexes = Helper.GetIndexes(pText, "&r", false);
var SideInfos =
LeftIndexes.Select(o => new { Side = CONST_Left, Index = o })
.Union(CenterIndexes.Select(o => new { Side = CONST_Center, Index = o }))
.Union(RightIndexes.Select(o => new { Side = CONST_Right, Index = o }))
.OrderBy(o => o.Index)
.ToList()
;
var HeaderOrFooterInfo = new HeaderOrFooterInfo();
if (SideInfos.Count == 0) {
HeaderOrFooterInfo.Center = pText;
return HeaderOrFooterInfo;
}
for (int i = 0; i < SideInfos.Count; i++) {
var SideInfo = SideInfos[i];
var IsFirstSideInfo = (i == 0);
var IsLastSideInfo = (i == (SideInfos.Count - 1));
if (IsFirstSideInfo) {
if (SideInfo.Index != 0) {
HeaderOrFooterInfo.Center = pText.Substring(0, SideInfo.Index - 1);
}
}
var StartIndex = SideInfo.Index + 2;
var EndIndex = 0;
if (IsLastSideInfo) {
EndIndex = pText.Length - 1;
} else {
var NextSideInfo = SideInfos[i + 1];
EndIndex = NextSideInfo.Index - 1;
}
var Length = EndIndex - StartIndex + 1;
var Text = pText.Substring(StartIndex, Length);
switch (SideInfo.Side) {
case CONST_Left:
HeaderOrFooterInfo.Left += Text;
break;
case CONST_Center:
HeaderOrFooterInfo.Center += Text;
break;
case CONST_Right:
HeaderOrFooterInfo.Right += Text;
break;
default:
throw new Exception();
}
}
return HeaderOrFooterInfo;
}
private void menuitemFileExit_Click(object sender, EventArgs e) {
Close();
}
private void menuitemEditUndo_Click(object sender, EventArgs e) {
controlContentTextBox.Undo();
}
private void menuitemEditCut_Click(object sender, EventArgs e) {
controlContentTextBox.Cut();
}
private void menuitemEditCopy_Click(object sender, EventArgs e) {
controlContentTextBox.Copy();
}
private void menuitemEditPaste_Click(object sender, EventArgs e) {
controlContentTextBox.Paste();
}
private void menuitemEditDelete_Click(object sender, EventArgs e) {
if (SelectionLength == 0) {
SelectionLength = 1;
}
SelectedText = "";
}
public string SelectedText {
get { return controlContentTextBox.SelectedText; }
set {
controlContentTextBox.SelectedText = value;
IsDirty = true;
}
}
private void menuitemEditSelectAll_Click(object sender, EventArgs e) {
controlContentTextBox.SelectAll();
}
private void menuitemEditTimeDate_Click(object sender, EventArgs e) {
SelectedText = DateTime.Now.ToShortTimeString() + " " + DateTime.Now.ToShortDateString();
}
private void menuitemEditGoTo_Click(object sender, EventArgs e) {
var GoToLinePrompt = new GoToLinePrompt(LineIndex + 1);
GoToLinePrompt.Left = Left + 5;
GoToLinePrompt.Top = Top + 44;
if (GoToLinePrompt.ShowDialog(this) != DialogResult.OK) return;
var TargetLineIndex = GoToLinePrompt.LineNumber - 1;
if (TargetLineIndex > controlContentTextBox.Lines.Length) {
MessageBox.Show(this, "The line number is beyond the total number of lines", "NotepadClone.Net - Goto Line");
return;
}
LineIndex = TargetLineIndex;
}
private class ContentPosition {
public int LineIndex;
public int ColumnIndex;
}
private ContentPosition CaretPosition {
get {
return CharIndexToPosition(SelectionStart);
}
}
private ContentPosition CharIndexToPosition(int pCharIndex) {
var CurrentCharIndex = 0;
if (controlContentTextBox.Lines.Length == 0 && CurrentCharIndex == 0) return new ContentPosition { LineIndex = 0, ColumnIndex = 0 };
for (var CurrentLineIndex = 0; CurrentLineIndex < controlContentTextBox.Lines.Length; CurrentLineIndex++) {
var LineStartCharIndex = CurrentCharIndex;
var Line = controlContentTextBox.Lines[CurrentLineIndex];
var LineEndCharIndex = LineStartCharIndex + Line.Length + 1;
if (pCharIndex >= LineStartCharIndex && pCharIndex <= LineEndCharIndex) {
var ColumnIndex = pCharIndex - LineStartCharIndex;
return new ContentPosition { LineIndex = CurrentLineIndex, ColumnIndex = ColumnIndex };
}
CurrentCharIndex += controlContentTextBox.Lines[CurrentLineIndex].Length + Environment.NewLine.Length;
}
return null;
}
private void UpdateStatusBar() {
if (controlCaretPositionLabel.Tag == null) {
controlCaretPositionLabel.Tag = controlCaretPositionLabel.Text;
}
controlCaretPositionLabel.Text = ((string)controlCaretPositionLabel.Tag).FormatUsingObject(new {
LineNumber = CaretPosition.LineIndex + 1,
ColumnNumber = CaretPosition.ColumnIndex + 1,
});
}
private int LineIndex {
get {
return CaretPosition.LineIndex;
}
set {
var TargetLineIndex = value;
if (TargetLineIndex < 0) {
TargetLineIndex = 0;
}
if (TargetLineIndex >= controlContentTextBox.Lines.Length) {
TargetLineIndex = controlContentTextBox.Lines.Length - 1;
}
var CharIndex = 0;
for (var CurrentLineIndex = 0; CurrentLineIndex < TargetLineIndex; CurrentLineIndex++) {
CharIndex += controlContentTextBox.Lines[CurrentLineIndex].Length + Environment.NewLine.Length;
}
SelectionStart = CharIndex;
controlContentTextBox.ScrollToCaret();
}
}
public int SelectionEnd {
get { return SelectionStart + SelectionLength; }
}
public int SelectionStart {
get { return controlContentTextBox.SelectionStart; }
set {
controlContentTextBox.SelectionStart = value;
controlContentTextBox.ScrollToCaret();
}
}
public int SelectionLength {
get { return controlContentTextBox.SelectionLength; }
set { controlContentTextBox.SelectionLength = value; }
}
private void menuitemAbout_Click(object sender, EventArgs e) {
new About().ShowDialog(this);
}
private void controlContentTextBox_KeyDown(object sender, KeyEventArgs e) {
UpdateStatusBar();
}
private void controlContentTextBox_KeyUp(object sender, KeyEventArgs e) {
UpdateStatusBar();
}
private void menuitemViewStatusBar_Click(object sender, EventArgs e) {
IsStatusBarVisible = !IsStatusBarVisible;
}
private void menuitemEdit_DropDownOpening(object sender, EventArgs e) {
menuitemEditCut.Enabled =
menuitemEditCopy.Enabled =
menuitemEditDelete.Enabled = (SelectionLength > 0);
menuitemEditFind.Enabled =
menuitemEditFindNext.Enabled = (Content.Length > 0);
}
private string _LastSearchText;
private bool _LastMatchCase;
private bool _LastSearchDown;
public bool FindAndSelect(string pSearchText, bool pMatchCase, bool pSearchDown) {
int Index;
var eStringComparison = pMatchCase ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
if (pSearchDown) {
Index = Content.IndexOf(pSearchText, SelectionEnd, eStringComparison);
} else {
Index = Content.LastIndexOf(pSearchText, SelectionStart, SelectionStart, eStringComparison);
}
if (Index == -1) return false;
_LastSearchText = pSearchText;
_LastMatchCase = pMatchCase;
_LastSearchDown = pSearchDown;
SelectionStart = Index;
SelectionLength = pSearchText.Length;
return true;
}
private FindDialog _FindDialog;
private void menuitemEditFind_Click(object sender, EventArgs e) {
Find();
}
private void Find() {
if (Content.Length == 0) return;
if (_FindDialog == null) {
_FindDialog = new FindDialog(this);
}
_FindDialog.Left = this.Left + 56;
_FindDialog.Top = this.Top + 160;
if (!_FindDialog.Visible) {
_FindDialog.Show(this);
} else {
_FindDialog.Show();
}
_FindDialog.Triggered();
}
private void Main_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = !EnsureWorkNotLost();
}
private void menuitemEditFindNext_Click(object sender, EventArgs e) {
if (_LastSearchText == null) {
Find();
return;
}
if(!FindAndSelect(_LastSearchText, _LastMatchCase, _LastSearchDown)) {
MessageBox.Show(this, CONST.CannotFindMessage.FormatUsingObject(new { SearchText = _LastSearchText }), "Notepad Clone");
}
}
private ReplaceDialog _ReplaceDialog;
private void menuitemEditReplace_Click(object sender, EventArgs e) {
if (Content.Length == 0) return;
if (_ReplaceDialog == null) {
_ReplaceDialog = new ReplaceDialog(this);
}
_ReplaceDialog.Left = this.Left + 56;
_ReplaceDialog.Top = this.Top + 113;
if (!_ReplaceDialog.Visible) {
_ReplaceDialog.Show(this);
} else {
_ReplaceDialog.Show();
}
_ReplaceDialog.Triggered();
}
private void Main_FormClosed(object sender, FormClosedEventArgs e) {
Settings.WindowPosition = Bounds;
Settings.Save();
}
private void menuitemFileHeaderAndFooter_Click(object sender, EventArgs e) {
var PageSetupHeaderFooter = new PageSetupHeaderFooter();
PageSetupHeaderFooter.Header = Settings.Header;
PageSetupHeaderFooter.Footer = Settings.Footer;
if (PageSetupHeaderFooter.ShowDialog(this) != DialogResult.OK) return;
Settings.Header = PageSetupHeaderFooter.Header;
Settings.Footer = PageSetupHeaderFooter.Footer;
Settings.Save();
}
private void controlContentTextBox_MouseDown(object sender, MouseEventArgs e)
{
UpdateStatusBar();
}
}
}