123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using Terminal.Gui;
- using static UICatalog.Scenarios.DynamicMenuBar;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Editor", "A Text Editor using the TextView control.")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Dialogs")]
- [ScenarioCategory ("Text and Formatting")]
- [ScenarioCategory ("Overlapped")]
- [ScenarioCategory ("Files and IO")]
- [ScenarioCategory ("TextView")]
- [ScenarioCategory ("Menus")]
- public class Editor : Scenario
- {
- private Window _appWindow;
- private List<CultureInfo> _cultureInfos;
- private string _fileName = "demo.txt";
- private bool _forceMinimumPosToZero = true;
- private bool _matchCase;
- private bool _matchWholeWord;
- private MenuItem _miForceMinimumPosToZero;
- private byte [] _originalText;
- private bool _saved = true;
- private ScrollBarView _scrollBar;
- private TabView _tabView;
- private string _textToFind;
- private string _textToReplace;
- private TextView _textView;
- private FindReplaceWindow _findReplaceWindow;
- public override void Main ()
- {
- // Init
- Application.Init ();
- // Setup - Create a top-level application window and configure it.
- _appWindow = new ()
- {
- //Title = GetQuitKeyAndName (),
- Title = _fileName ?? "Untitled",
- BorderStyle = LineStyle.None
- };
- _cultureInfos = Application.SupportedCultures;
- _textView = new ()
- {
- X = 0,
- Y = 1,
- Width = Dim.Fill (),
- Height = Dim.Fill (1),
- };
- CreateDemoFile (_fileName);
- LoadFile ();
- _appWindow.Add (_textView);
- var menu = new MenuBar
- {
- Menus =
- [
- new (
- "_File",
- new MenuItem []
- {
- new ("_New", "", () => New ()),
- new ("_Open", "", () => Open ()),
- new ("_Save", "", () => Save ()),
- new ("_Save As", "", () => SaveAs ()),
- new ("_Close", "", () => CloseFile ()),
- null,
- new ("_Quit", "", () => Quit ())
- }
- ),
- new (
- "_Edit",
- new MenuItem []
- {
- new (
- "_Copy",
- "",
- () => Copy (),
- null,
- null,
- KeyCode.CtrlMask | KeyCode.C
- ),
- new (
- "C_ut",
- "",
- () => Cut (),
- null,
- null,
- KeyCode.CtrlMask | KeyCode.W
- ),
- new (
- "_Paste",
- "",
- () => Paste (),
- null,
- null,
- KeyCode.CtrlMask | KeyCode.Y
- ),
- null,
- new (
- "_Find",
- "",
- () => Find (),
- null,
- null,
- KeyCode.CtrlMask | KeyCode.S
- ),
- new (
- "Find _Next",
- "",
- () => FindNext (),
- null,
- null,
- KeyCode.CtrlMask
- | KeyCode.ShiftMask
- | KeyCode.S
- ),
- new (
- "Find P_revious",
- "",
- () => FindPrevious (),
- null,
- null,
- KeyCode.CtrlMask
- | KeyCode.ShiftMask
- | KeyCode.AltMask
- | KeyCode.S
- ),
- new (
- "_Replace",
- "",
- () => Replace (),
- null,
- null,
- KeyCode.CtrlMask | KeyCode.R
- ),
- new (
- "Replace Ne_xt",
- "",
- () => ReplaceNext (),
- null,
- null,
- KeyCode.CtrlMask
- | KeyCode.ShiftMask
- | KeyCode.R
- ),
- new (
- "Replace Pre_vious",
- "",
- () => ReplacePrevious (),
- null,
- null,
- KeyCode.CtrlMask
- | KeyCode.ShiftMask
- | KeyCode.AltMask
- | KeyCode.R
- ),
- new (
- "Replace _All",
- "",
- () => ReplaceAll (),
- null,
- null,
- KeyCode.CtrlMask
- | KeyCode.ShiftMask
- | KeyCode.AltMask
- | KeyCode.A
- ),
- null,
- new (
- "_Select All",
- "",
- () => SelectAll (),
- null,
- null,
- KeyCode.CtrlMask | KeyCode.T
- )
- }
- ),
- new ("_ScrollBarView", CreateKeepChecked ()),
- new ("_Cursor", CreateCursorRadio ()),
- new (
- "Forma_t",
- new []
- {
- CreateWrapChecked (),
- CreateAutocomplete (),
- CreateAllowsTabChecked (),
- CreateReadOnlyChecked ()
- }
- ),
- new (
- "_Responder",
- new [] { CreateCanFocusChecked (), CreateEnabledChecked (), CreateVisibleChecked () }
- ),
- new (
- "Conte_xtMenu",
- new []
- {
- _miForceMinimumPosToZero = new (
- "ForceMinimumPosTo_Zero",
- "",
- () =>
- {
- _miForceMinimumPosToZero.Checked =
- _forceMinimumPosToZero =
- !_forceMinimumPosToZero;
- _textView.ContextMenu.ForceMinimumPosToZero =
- _forceMinimumPosToZero;
- }
- )
- {
- CheckType = MenuItemCheckStyle.Checked,
- Checked = _forceMinimumPosToZero
- },
- new MenuBarItem ("_Languages", GetSupportedCultures ())
- }
- )
- ]
- };
- _appWindow.Add (menu);
- var siCursorPosition = new Shortcut (KeyCode.Null, "", null);
- var statusBar = new StatusBar (
- new []
- {
- new (Application.QuitKey, $"Quit", Quit),
- new (Key.F2, "Open", Open),
- new (Key.F3, "Save", () => Save ()),
- new (Key.F4, "Save As", () => SaveAs ()),
- new (Key.Empty, $"OS Clipboard IsSupported : {Clipboard.IsSupported}", null),
- siCursorPosition,
- }
- )
- {
- AlignmentModes = AlignmentModes.StartToEnd | AlignmentModes.IgnoreFirstOrLast
- };
- _textView.UnwrappedCursorPosition += (s, e) =>
- {
- siCursorPosition.Title = $"Ln {e.Point.Y + 1}, Col {e.Point.X + 1}";
- };
- _appWindow.Add (statusBar);
- _scrollBar = new (_textView, true);
- _scrollBar.ChangedPosition += (s, e) =>
- {
- _textView.TopRow = _scrollBar.Position;
- if (_textView.TopRow != _scrollBar.Position)
- {
- _scrollBar.Position = _textView.TopRow;
- }
- _textView.SetNeedsDisplay ();
- };
- _scrollBar.OtherScrollBarView.ChangedPosition += (s, e) =>
- {
- _textView.LeftColumn = _scrollBar.OtherScrollBarView.Position;
- if (_textView.LeftColumn != _scrollBar.OtherScrollBarView.Position)
- {
- _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
- }
- _textView.SetNeedsDisplay ();
- };
- _textView.DrawContent += (s, e) =>
- {
- _scrollBar.Size = _textView.Lines;
- _scrollBar.Position = _textView.TopRow;
- if (_scrollBar.OtherScrollBarView != null)
- {
- _scrollBar.OtherScrollBarView.Size = _textView.Maxlength;
- _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
- }
- _scrollBar.LayoutSubviews ();
- _scrollBar.Refresh ();
- };
- _appWindow.Closed += (s, e) => Thread.CurrentThread.CurrentUICulture = new ("en-US");
- CreateFindReplace ();
- // Run - Start the application.
- Application.Run (_appWindow);
- _appWindow.Dispose ();
- // Shutdown - Calling Application.Shutdown is required.
- Application.Shutdown ();
- }
- private bool CanCloseFile ()
- {
- if (_textView.Text == Encoding.Unicode.GetString (_originalText))
- {
- //System.Diagnostics.Debug.Assert (!_textView.IsDirty);
- return true;
- }
- Debug.Assert (_textView.IsDirty);
- int r = MessageBox.ErrorQuery (
- "Save File",
- $"Do you want save changes in {_appWindow.Title}?",
- "Yes",
- "No",
- "Cancel"
- );
- if (r == 0)
- {
- return Save ();
- }
- if (r == 1)
- {
- return true;
- }
- return false;
- }
- private void CloseFile ()
- {
- if (!CanCloseFile ())
- {
- return;
- }
- try
- {
- _textView.CloseFile ();
- New (false);
- }
- catch (Exception ex)
- {
- MessageBox.ErrorQuery ("Error", ex.Message, "Ok");
- }
- }
- private void ContinueFind (bool next = true, bool replace = false)
- {
- if (!replace && string.IsNullOrEmpty (_textToFind))
- {
- Find ();
- return;
- }
- if (replace
- && (string.IsNullOrEmpty (_textToFind)
- || (_findReplaceWindow == null && string.IsNullOrEmpty (_textToReplace))))
- {
- Replace ();
- return;
- }
- bool found;
- bool gaveFullTurn;
- if (next)
- {
- if (!replace)
- {
- found = _textView.FindNextText (
- _textToFind,
- out gaveFullTurn,
- _matchCase,
- _matchWholeWord
- );
- }
- else
- {
- found = _textView.FindNextText (
- _textToFind,
- out gaveFullTurn,
- _matchCase,
- _matchWholeWord,
- _textToReplace,
- true
- );
- }
- }
- else
- {
- if (!replace)
- {
- found = _textView.FindPreviousText (
- _textToFind,
- out gaveFullTurn,
- _matchCase,
- _matchWholeWord
- );
- }
- else
- {
- found = _textView.FindPreviousText (
- _textToFind,
- out gaveFullTurn,
- _matchCase,
- _matchWholeWord,
- _textToReplace,
- true
- );
- }
- }
- if (!found)
- {
- MessageBox.Query ("Find", $"The following specified text was not found: '{_textToFind}'", "Ok");
- }
- else if (gaveFullTurn)
- {
- MessageBox.Query (
- "Find",
- $"No more occurrences were found for the following specified text: '{_textToFind}'",
- "Ok"
- );
- }
- }
- private void Copy ()
- {
- if (_textView != null)
- {
- _textView.Copy ();
- }
- }
- private MenuItem CreateAllowsTabChecked ()
- {
- var item = new MenuItem { Title = "Allows Tab" };
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = _textView.AllowsTab;
- item.Action += () => { _textView.AllowsTab = (bool)(item.Checked = !item.Checked); };
- return item;
- }
- private MenuItem CreateAutocomplete ()
- {
- var singleWordGenerator = new SingleWordSuggestionGenerator ();
- _textView.Autocomplete.SuggestionGenerator = singleWordGenerator;
- var auto = new MenuItem ();
- auto.Title = "Autocomplete";
- auto.CheckType |= MenuItemCheckStyle.Checked;
- auto.Checked = false;
- auto.Action += () =>
- {
- if ((bool)(auto.Checked = !auto.Checked))
- {
- // setup autocomplete with all words currently in the editor
- singleWordGenerator.AllSuggestions =
- Regex.Matches (_textView.Text, "\\w+")
- .Select (s => s.Value)
- .Distinct ()
- .ToList ();
- }
- else
- {
- singleWordGenerator.AllSuggestions.Clear ();
- }
- };
- return auto;
- }
- private MenuItem CreateCanFocusChecked ()
- {
- var item = new MenuItem { Title = "CanFocus" };
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = _textView.CanFocus;
- item.Action += () =>
- {
- _textView.CanFocus = (bool)(item.Checked = !item.Checked);
- if (_textView.CanFocus)
- {
- _textView.SetFocus ();
- }
- };
- return item;
- }
- private MenuItem [] CreateCursorRadio ()
- {
- List<MenuItem> menuItems = new ();
- menuItems.Add (
- new ("_Invisible", "", () => SetCursor (CursorVisibility.Invisible))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility
- == CursorVisibility.Invisible
- }
- );
- menuItems.Add (
- new ("_Box", "", () => SetCursor (CursorVisibility.Box))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility == CursorVisibility.Box
- }
- );
- menuItems.Add (
- new ("_Underline", "", () => SetCursor (CursorVisibility.Underline))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility
- == CursorVisibility.Underline
- }
- );
- menuItems.Add (new ("", "", () => { }, () => false));
- menuItems.Add (new ("xTerm :", "", () => { }, () => false));
- menuItems.Add (new ("", "", () => { }, () => false));
- menuItems.Add (
- new (" _Default", "", () => SetCursor (CursorVisibility.Default))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility
- == CursorVisibility.Default
- }
- );
- menuItems.Add (
- new (" _Vertical", "", () => SetCursor (CursorVisibility.Vertical))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility
- == CursorVisibility.Vertical
- }
- );
- menuItems.Add (
- new (" V_ertical Fix", "", () => SetCursor (CursorVisibility.VerticalFix))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility == CursorVisibility.VerticalFix
- }
- );
- menuItems.Add (
- new (" B_ox Fix", "", () => SetCursor (CursorVisibility.BoxFix))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility
- == CursorVisibility.BoxFix
- }
- );
- menuItems.Add (
- new (" U_nderline Fix", "", () => SetCursor (CursorVisibility.UnderlineFix))
- {
- CheckType = MenuItemCheckStyle.Radio,
- Checked = _textView.CursorVisibility == CursorVisibility.UnderlineFix
- }
- );
- void SetCursor (CursorVisibility visibility)
- {
- _textView.CursorVisibility = visibility;
- var title = "";
- switch (visibility)
- {
- case CursorVisibility.Default:
- title = " _Default";
- break;
- case CursorVisibility.Invisible:
- title = "_Invisible";
- break;
- case CursorVisibility.Underline:
- title = "_Underline";
- break;
- case CursorVisibility.UnderlineFix:
- title = " U_nderline Fix";
- break;
- case CursorVisibility.Vertical:
- title = " _Vertical";
- break;
- case CursorVisibility.VerticalFix:
- title = " V_ertical Fix";
- break;
- case CursorVisibility.Box:
- title = "_Box";
- break;
- case CursorVisibility.BoxFix:
- title = " B_ox Fix";
- break;
- }
- foreach (MenuItem menuItem in menuItems)
- {
- menuItem.Checked = menuItem.Title.Equals (title) && visibility == _textView.CursorVisibility;
- }
- }
- return menuItems.ToArray ();
- }
- private void CreateDemoFile (string fileName)
- {
- var sb = new StringBuilder ();
- // FIXED: BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
- sb.Append ("Hello world.\n");
- sb.Append ("This is a test of the Emergency Broadcast System.\n");
- for (var i = 0; i < 30; i++)
- {
- sb.Append (
- $"{i} - This is a test with a very long line and many lines to test the ScrollViewBar against the TextView. - {i}\n"
- );
- }
- StreamWriter sw = File.CreateText (fileName);
- sw.Write (sb.ToString ());
- sw.Close ();
- }
- private MenuItem CreateEnabledChecked ()
- {
- var item = new MenuItem { Title = "Enabled" };
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = _textView.Enabled;
- item.Action += () =>
- {
- _textView.Enabled = (bool)(item.Checked = !item.Checked);
- if (_textView.Enabled)
- {
- _textView.SetFocus ();
- }
- };
- return item;
- }
- private class FindReplaceWindow : Window
- {
- private TextView _textView;
- public FindReplaceWindow (TextView textView)
- {
- Title = "Find and Replace";
- _textView = textView;
- X = Pos.AnchorEnd () - 1;
- Y = 2;
- Width = 57;
- Height = 11;
- Arrangement = ViewArrangement.Movable;
- KeyBindings.Add (Key.Esc, KeyBindingScope.Focused, Command.Cancel);
- AddCommand (Command.Cancel, () =>
- {
- Visible = false;
- return true;
- });
- VisibleChanged += FindReplaceWindow_VisibleChanged;
- Initialized += FindReplaceWindow_Initialized;
- //var btnCancel = new Button
- //{
- // X = Pos.AnchorEnd (),
- // Y = Pos.AnchorEnd (),
- // Text = "Cancel"
- //};
- //btnCancel.Accept += (s, e) => { Visible = false; };
- //Add (btnCancel);
- }
- private void FindReplaceWindow_VisibleChanged (object sender, EventArgs e)
- {
- if (Visible == false)
- {
- _textView.SetFocus ();
- }
- else
- {
- FocusDeepest (NavigationDirection.Forward, null);
- }
- }
- private void FindReplaceWindow_Initialized (object sender, EventArgs e)
- {
- Border.LineStyle = LineStyle.Dashed;
- Border.Thickness = new (0, 1, 0, 0);
- }
- }
- private void ShowFindReplace (bool isFind = true)
- {
- _findReplaceWindow.Visible = true;
- _findReplaceWindow.SuperView.MoveSubviewToStart (_findReplaceWindow);
- _tabView.SetFocus ();
- _tabView.SelectedTab = isFind ? _tabView.Tabs.ToArray () [0] : _tabView.Tabs.ToArray () [1];
- _tabView.SelectedTab.View.FocusDeepest (NavigationDirection.Forward, null);
- }
- private void CreateFindReplace ()
- {
- _findReplaceWindow = new (_textView);
- _tabView = new ()
- {
- X = 0, Y = 0,
- Width = Dim.Fill (), Height = Dim.Fill (0)
- };
- _tabView.AddTab (new () { DisplayText = "Find", View = CreateFindTab () }, true);
- _tabView.AddTab (new () { DisplayText = "Replace", View = CreateReplaceTab () }, false);
- _tabView.SelectedTabChanged += (s, e) => _tabView.SelectedTab.View.FocusDeepest (NavigationDirection.Forward, null);
- _findReplaceWindow.Add (_tabView);
- // _tabView.SelectedTab.View.FocusLast (null); // Hack to get the first tab to be focused
- _findReplaceWindow.Visible = false;
- _appWindow.Add (_findReplaceWindow);
- }
- private MenuItem [] CreateKeepChecked ()
- {
- var item = new MenuItem ();
- item.Title = "Keep Content Always In Viewport";
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = true;
- item.Action += () => _scrollBar.KeepContentAlwaysInViewport = (bool)(item.Checked = !item.Checked);
- return new [] { item };
- }
- private MenuItem CreateReadOnlyChecked ()
- {
- var item = new MenuItem { Title = "Read Only" };
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = _textView.ReadOnly;
- item.Action += () => _textView.ReadOnly = (bool)(item.Checked = !item.Checked);
- return item;
- }
- private MenuItem CreateVisibleChecked ()
- {
- var item = new MenuItem { Title = "Visible" };
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = _textView.Visible;
- item.Action += () =>
- {
- _textView.Visible = (bool)(item.Checked = !item.Checked);
- if (_textView.Visible)
- {
- _textView.SetFocus ();
- }
- };
- return item;
- }
- private MenuItem CreateWrapChecked ()
- {
- var item = new MenuItem { Title = "Word Wrap" };
- item.CheckType |= MenuItemCheckStyle.Checked;
- item.Checked = _textView.WordWrap;
- item.Action += () =>
- {
- _textView.WordWrap = (bool)(item.Checked = !item.Checked);
- if (_textView.WordWrap)
- {
- _scrollBar.OtherScrollBarView.ShowScrollIndicator = false;
- }
- };
- return item;
- }
- private void Cut ()
- {
- if (_textView != null)
- {
- _textView.Cut ();
- }
- }
- private void Find () { ShowFindReplace (true); }
- private void FindNext () { ContinueFind (); }
- private void FindPrevious () { ContinueFind (false); }
- private View CreateFindTab ()
- {
- var d = new View ()
- {
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- int lblWidth = "Replace:".Length;
- var label = new Label
- {
- Width = lblWidth,
- TextAlignment = Alignment.End,
- Text = "Find:"
- };
- d.Add (label);
- SetFindText ();
- var txtToFind = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = Dim.Fill (1),
- Text = _textToFind
- };
- txtToFind.HasFocusChanging += (s, e) => txtToFind.Text = _textToFind;
- d.Add (txtToFind);
- var btnFindNext = new Button
- {
- X = Pos.Align (Alignment.Center),
- Y = Pos.AnchorEnd (),
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- IsDefault = true,
- Text = "Find _Next"
- };
- btnFindNext.Accept += (s, e) => FindNext ();
- d.Add (btnFindNext);
- var btnFindPrevious = new Button
- {
- X = Pos.Align (Alignment.Center),
- Y = Pos.AnchorEnd (),
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- Text = "Find _Previous"
- };
- btnFindPrevious.Accept += (s, e) => FindPrevious ();
- d.Add (btnFindPrevious);
- txtToFind.TextChanged += (s, e) =>
- {
- _textToFind = txtToFind.Text;
- _textView.FindTextChanged ();
- btnFindNext.Enabled = !string.IsNullOrEmpty (txtToFind.Text);
- btnFindPrevious.Enabled = !string.IsNullOrEmpty (txtToFind.Text);
- };
- var ckbMatchCase = new CheckBox
- {
- X = 0, Y = Pos.Top (txtToFind) + 2, CheckedState = _matchCase ? CheckState.Checked : CheckState.UnChecked, Text = "Match c_ase"
- };
- ckbMatchCase.CheckedStateChanging += (s, e) => _matchCase = e.NewValue == CheckState.Checked;
- d.Add (ckbMatchCase);
- var ckbMatchWholeWord = new CheckBox
- {
- X = 0, Y = Pos.Top (ckbMatchCase) + 1, CheckedState = _matchWholeWord ? CheckState.Checked : CheckState.UnChecked, Text = "Match _whole word"
- };
- ckbMatchWholeWord.CheckedStateChanging += (s, e) => _matchWholeWord = e.NewValue == CheckState.Checked;
- d.Add (ckbMatchWholeWord);
- return d;
- }
- private MenuItem [] GetSupportedCultures ()
- {
- List<MenuItem> supportedCultures = new ();
- int index = -1;
- foreach (CultureInfo c in _cultureInfos)
- {
- var culture = new MenuItem { CheckType = MenuItemCheckStyle.Checked };
- if (index == -1)
- {
- culture.Title = "_English";
- culture.Help = "en-US";
- culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US";
- CreateAction (supportedCultures, culture);
- supportedCultures.Add (culture);
- index++;
- culture = new () { CheckType = MenuItemCheckStyle.Checked };
- }
- culture.Title = $"_{c.Parent.EnglishName}";
- culture.Help = c.Name;
- culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name;
- CreateAction (supportedCultures, culture);
- supportedCultures.Add (culture);
- }
- return supportedCultures.ToArray ();
- void CreateAction (List<MenuItem> supportedCultures, MenuItem culture)
- {
- culture.Action += () =>
- {
- Thread.CurrentThread.CurrentUICulture = new (culture.Help);
- culture.Checked = true;
- foreach (MenuItem item in supportedCultures)
- {
- item.Checked = item.Help == Thread.CurrentThread.CurrentUICulture.Name;
- }
- };
- }
- }
- private void LoadFile ()
- {
- if (_fileName != null)
- {
- // FIXED: BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
- _textView.Load (_fileName);
- //_textView.Text = System.IO.File.ReadAllText (_fileName);
- _originalText = Encoding.Unicode.GetBytes (_textView.Text);
- _appWindow.Title = _fileName;
- _saved = true;
- }
- }
- private void New (bool checkChanges = true)
- {
- if (checkChanges && !CanCloseFile ())
- {
- return;
- }
- _appWindow.Title = "Untitled.txt";
- _fileName = null;
- _originalText = new MemoryStream ().ToArray ();
- _textView.Text = Encoding.Unicode.GetString (_originalText);
- }
- private void Open ()
- {
- if (!CanCloseFile ())
- {
- return;
- }
- List<IAllowedType> aTypes = new ()
- {
- new AllowedType (
- "Text",
- ".txt;.bin;.xml;.json",
- ".txt",
- ".bin",
- ".xml",
- ".json"
- ),
- new AllowedTypeAny ()
- };
- var d = new OpenDialog { Title = "Open", AllowedTypes = aTypes, AllowsMultipleSelection = false };
- Application.Run (d);
- if (!d.Canceled && d.FilePaths.Count > 0)
- {
- _fileName = d.FilePaths [0];
- LoadFile ();
- }
- d.Dispose ();
- }
- private void Paste ()
- {
- if (_textView != null)
- {
- _textView.Paste ();
- }
- }
- private void Quit ()
- {
- if (!CanCloseFile ())
- {
- return;
- }
- Application.RequestStop ();
- }
- private void Replace () { ShowFindReplace (false); }
- private void ReplaceAll ()
- {
- if (string.IsNullOrEmpty (_textToFind) || (string.IsNullOrEmpty (_textToReplace) && _findReplaceWindow == null))
- {
- Replace ();
- return;
- }
- if (_textView.ReplaceAllText (_textToFind, _matchCase, _matchWholeWord, _textToReplace))
- {
- MessageBox.Query (
- "Replace All",
- $"All occurrences were replaced for the following specified text: '{_textToReplace}'",
- "Ok"
- );
- }
- else
- {
- MessageBox.Query (
- "Replace All",
- $"None of the following specified text was found: '{_textToFind}'",
- "Ok"
- );
- }
- }
- private void ReplaceNext () { ContinueFind (true, true); }
- private void ReplacePrevious () { ContinueFind (false, true); }
- private View CreateReplaceTab ()
- {
- var d = new View ()
- {
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- int lblWidth = "Replace:".Length;
- var label = new Label
- {
- Width = lblWidth,
- TextAlignment = Alignment.End,
- Text = "Find:"
- };
- d.Add (label);
- SetFindText ();
- var txtToFind = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = Dim.Fill (1),
- Text = _textToFind
- };
- txtToFind.HasFocusChanging += (s, e) => txtToFind.Text = _textToFind;
- d.Add (txtToFind);
- var btnFindNext = new Button
- {
- X = Pos.Align (Alignment.Center),
- Y = Pos.AnchorEnd (),
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- IsDefault = true,
- Text = "Replace _Next"
- };
- btnFindNext.Accept += (s, e) => ReplaceNext ();
- d.Add (btnFindNext);
- label = new ()
- {
- X = Pos.Left (label),
- Y = Pos.Top (label) + 1,
- Text = "Replace:"
- };
- d.Add (label);
- SetFindText ();
- var txtToReplace = new TextField
- {
- X = Pos.Right (label) + 1,
- Y = Pos.Top (label),
- Width = Dim.Fill (1),
- Text = _textToReplace
- };
- txtToReplace.TextChanged += (s, e) => _textToReplace = txtToReplace.Text;
- d.Add (txtToReplace);
- var btnFindPrevious = new Button
- {
- X = Pos.Align (Alignment.Center),
- Y = Pos.AnchorEnd (),
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- Text = "Replace _Previous"
- };
- btnFindPrevious.Accept += (s, e) => ReplacePrevious ();
- d.Add (btnFindPrevious);
- var btnReplaceAll = new Button
- {
- X = Pos.Align (Alignment.Center),
- Y = Pos.AnchorEnd (),
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- Text = "Replace _All"
- };
- btnReplaceAll.Accept += (s, e) => ReplaceAll ();
- d.Add (btnReplaceAll);
- txtToFind.TextChanged += (s, e) =>
- {
- _textToFind = txtToFind.Text;
- _textView.FindTextChanged ();
- btnFindNext.Enabled = !string.IsNullOrEmpty (txtToFind.Text);
- btnFindPrevious.Enabled = !string.IsNullOrEmpty (txtToFind.Text);
- btnReplaceAll.Enabled = !string.IsNullOrEmpty (txtToFind.Text);
- };
- var ckbMatchCase = new CheckBox
- {
- X = 0, Y = Pos.Top (txtToFind) + 2, CheckedState = _matchCase ? CheckState.Checked : CheckState.UnChecked, Text = "Match c_ase"
- };
- ckbMatchCase.CheckedStateChanging += (s, e) => _matchCase = e.NewValue == CheckState.Checked;
- d.Add (ckbMatchCase);
- var ckbMatchWholeWord = new CheckBox
- {
- X = 0, Y = Pos.Top (ckbMatchCase) + 1, CheckedState = _matchWholeWord ? CheckState.Checked : CheckState.UnChecked, Text = "Match _whole word"
- };
- ckbMatchWholeWord.CheckedStateChanging += (s, e) => _matchWholeWord = e.NewValue == CheckState.Checked;
- d.Add (ckbMatchWholeWord);
- return d;
- }
- private bool Save ()
- {
- if (_fileName != null)
- {
- // FIXED: BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
- // As a result files saved on Windows and then read back will show invalid chars.
- return SaveFile (_appWindow.Title, _fileName);
- }
- return SaveAs ();
- }
- private bool SaveAs ()
- {
- List<IAllowedType> aTypes = new ()
- {
- new AllowedType ("Text Files", ".txt", ".bin", ".xml"), new AllowedTypeAny ()
- };
- var sd = new SaveDialog { Title = "Save file", AllowedTypes = aTypes };
- sd.Path = _appWindow.Title;
- Application.Run (sd);
- bool canceled = sd.Canceled;
- string path = sd.Path;
- string fileName = sd.FileName;
- sd.Dispose ();
- if (!canceled)
- {
- if (File.Exists (path))
- {
- if (MessageBox.Query (
- "Save File",
- "File already exists. Overwrite any way?",
- "No",
- "Ok"
- )
- == 1)
- {
- return SaveFile (fileName, path);
- }
- _saved = false;
- return _saved;
- }
- return SaveFile (fileName, path);
- }
- _saved = false;
- return _saved;
- }
- private bool SaveFile (string title, string file)
- {
- try
- {
- _appWindow.Title = title;
- _fileName = file;
- File.WriteAllText (_fileName, _textView.Text);
- _originalText = Encoding.Unicode.GetBytes (_textView.Text);
- _saved = true;
- _textView.ClearHistoryChanges ();
- MessageBox.Query ("Save File", "File was successfully saved.", "Ok");
- }
- catch (Exception ex)
- {
- MessageBox.ErrorQuery ("Error", ex.Message, "Ok");
- return false;
- }
- return true;
- }
- private void SelectAll () { _textView.SelectAll (); }
- private void SetFindText ()
- {
- _textToFind = !string.IsNullOrEmpty (_textView.SelectedText) ? _textView.SelectedText :
- string.IsNullOrEmpty (_textToFind) ? "" : _textToFind;
- _textToReplace = string.IsNullOrEmpty (_textToReplace) ? "" : _textToReplace;
- }
- }
|