12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331 |
- 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;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Editor", "A Text Editor using the TextView control.")]
- [ScenarioCategory ("Controls")]
- [ScenarioCategory ("Dialogs")]
- [ScenarioCategory ("Text and Formatting")]
- [ScenarioCategory ("Top Level Windows")]
- [ScenarioCategory ("Files and IO")]
- [ScenarioCategory ("TextView")]
- public class Editor : Scenario
- {
- 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 Window _winDialog;
- public override void Init ()
- {
- Application.Init ();
- _cultureInfos = Application.SupportedCultures;
- ConfigurationManager.Themes.Theme = Theme;
- ConfigurationManager.Apply ();
- Top = new ();
- Win = new()
- {
- Title = _fileName ?? "Untitled",
- X = 0,
- Y = 1,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- ColorScheme = Colors.ColorSchemes [TopLevelColorScheme]
- };
- Top.Add (Win);
- _textView = new()
- {
- X = 0,
- Y = 0,
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- BottomOffset = 1,
- RightOffset = 1
- };
- CreateDemoFile (_fileName);
- LoadFile ();
- Win.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 ())
- }
- )
- ]
- };
- Top.Add (menu);
- var siCursorPosition = new StatusItem (KeyCode.Null, "", null);
- var statusBar = new StatusBar (
- new []
- {
- siCursorPosition,
- new (KeyCode.F2, "~F2~ Open", () => Open ()),
- new (KeyCode.F3, "~F3~ Save", () => Save ()),
- new (KeyCode.F4, "~F4~ Save As", () => SaveAs ()),
- new (
- Application.QuitKey,
- $"{Application.QuitKey} to Quit",
- () => Quit ()
- ),
- new (
- KeyCode.Null,
- $"OS Clipboard IsSupported : {Clipboard.IsSupported}",
- null
- )
- }
- );
- _textView.UnwrappedCursorPosition += (s, e) =>
- {
- siCursorPosition.Title = $"Ln {e.Point.Y + 1}, Col {e.Point.X + 1}";
- };
- Top.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 ();
- };
- _scrollBar.VisibleChanged += (s, e) =>
- {
- if (_scrollBar.Visible && _textView.RightOffset == 0)
- {
- _textView.RightOffset = 1;
- }
- else if (!_scrollBar.Visible && _textView.RightOffset == 1)
- {
- _textView.RightOffset = 0;
- }
- };
- _scrollBar.OtherScrollBarView.VisibleChanged += (s, e) =>
- {
- if (_scrollBar.OtherScrollBarView.Visible && _textView.BottomOffset == 0)
- {
- _textView.BottomOffset = 1;
- }
- else if (!_scrollBar.OtherScrollBarView.Visible && _textView.BottomOffset == 1)
- {
- _textView.BottomOffset = 0;
- }
- };
- _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 ();
- };
- Win.KeyDown += (s, e) =>
- {
- if (_winDialog != null && (e.KeyCode == KeyCode.Esc || e == Application.QuitKey))
- {
- DisposeWinDialog ();
- }
- else if (e == Application.QuitKey)
- {
- Quit ();
- e.Handled = true;
- }
- else if (_winDialog != null && e.KeyCode == (KeyCode.Tab | KeyCode.CtrlMask))
- {
- if (_tabView.SelectedTab == _tabView.Tabs.ElementAt (_tabView.Tabs.Count - 1))
- {
- _tabView.SelectedTab = _tabView.Tabs.ElementAt (0);
- }
- else
- {
- _tabView.SwitchTabBy (1);
- }
- e.Handled = true;
- }
- else if (_winDialog != null && e.KeyCode == (KeyCode.Tab | KeyCode.CtrlMask | KeyCode.ShiftMask))
- {
- if (_tabView.SelectedTab == _tabView.Tabs.ElementAt (0))
- {
- _tabView.SelectedTab = _tabView.Tabs.ElementAt (_tabView.Tabs.Count - 1);
- }
- else
- {
- _tabView.SwitchTabBy (-1);
- }
- e.Handled = true;
- }
- };
- Top.Closed += (s, e) => Thread.CurrentThread.CurrentUICulture = new ("en-US");
- }
- public override void Setup () { }
- 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 {Win.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)
- || (_winDialog == 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 void CreateFindReplace (bool isFind = true)
- {
- if (_winDialog != null)
- {
- _winDialog.SetFocus ();
- return;
- }
- _winDialog = new()
- {
- Title = isFind ? "Find" : "Replace",
- X = Win.Viewport.Width / 2 - 30,
- Y = Win.Viewport.Height / 2 - 10,
- ColorScheme = Colors.ColorSchemes ["TopLevel"]
- };
- _tabView = new() { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
- _tabView.AddTab (new() { DisplayText = "Find", View = FindTab () }, isFind);
- View replace = ReplaceTab ();
- _tabView.AddTab (new() { DisplayText = "Replace", View = replace }, !isFind);
- _tabView.SelectedTabChanged += (s, e) => _tabView.SelectedTab.View.FocusFirst ();
- _winDialog.Add (_tabView);
- Win.Add (_winDialog);
- _winDialog.Width = replace.Width + 4;
- _winDialog.Height = replace.Height + 4;
- _winDialog.SuperView.BringSubviewToFront (_winDialog);
- _winDialog.SetFocus ();
- }
- 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;
- _textView.BottomOffset = 0;
- }
- else
- {
- _textView.BottomOffset = 1;
- }
- };
- return item;
- }
- private void Cut ()
- {
- if (_textView != null)
- {
- _textView.Cut ();
- }
- }
- private void DisposeWinDialog ()
- {
- _winDialog.Dispose ();
- Win.Remove (_winDialog);
- _winDialog = null;
- }
- private void Find () { CreateFindReplace (); }
- private void FindNext () { ContinueFind (); }
- private void FindPrevious () { ContinueFind (false); }
- private View FindTab ()
- {
- var d = new View ();
- d.DrawContent += (s, e) =>
- {
- foreach (View v in d.Subviews)
- {
- v.SetNeedsDisplay ();
- }
- };
- int lblWidth = "Replace:".Length;
- var label = new Label
- {
- Y = 1,
- Width = lblWidth,
- TextAlignment = TextAlignment.Right,
- Text = "Find:"
- };
- d.Add (label);
- SetFindText ();
- var txtToFind = new TextField
- {
- X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 20, Text = _textToFind
- };
- txtToFind.Enter += (s, e) => txtToFind.Text = _textToFind;
- d.Add (txtToFind);
- var btnFindNext = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (label),
- Width = 20,
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- TextAlignment = TextAlignment.Centered,
- IsDefault = true,
- Text = "Find _Next"
- };
- btnFindNext.Accept += (s, e) => FindNext ();
- d.Add (btnFindNext);
- var btnFindPrevious = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (btnFindNext) + 1,
- Width = 20,
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- TextAlignment = TextAlignment.Centered,
- 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 btnCancel = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (btnFindPrevious) + 2,
- Width = 20,
- TextAlignment = TextAlignment.Centered,
- Text = "Cancel"
- };
- btnCancel.Accept += (s, e) => { DisposeWinDialog (); };
- d.Add (btnCancel);
- var ckbMatchCase = new CheckBox
- {
- X = 0, Y = Pos.Top (txtToFind) + 2, Checked = _matchCase, Text = "Match c_ase"
- };
- ckbMatchCase.Toggled += (s, e) => _matchCase = (bool)ckbMatchCase.Checked;
- d.Add (ckbMatchCase);
- var ckbMatchWholeWord = new CheckBox
- {
- X = 0, Y = Pos.Top (ckbMatchCase) + 1, Checked = _matchWholeWord, Text = "Match _whole word"
- };
- ckbMatchWholeWord.Toggled += (s, e) => _matchWholeWord = (bool)ckbMatchWholeWord.Checked;
- d.Add (ckbMatchWholeWord);
- d.Width = label.Width + txtToFind.Width + btnFindNext.Width + 2;
- d.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
- 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);
- Win.Title = _fileName;
- _saved = true;
- }
- }
- private void New (bool checkChanges = true)
- {
- if (checkChanges && !CanCloseFile ())
- {
- return;
- }
- Win.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 () { CreateFindReplace (false); }
- private void ReplaceAll ()
- {
- if (string.IsNullOrEmpty (_textToFind) || (string.IsNullOrEmpty (_textToReplace) && _winDialog == 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 ReplaceTab ()
- {
- var d = new View ();
- d.DrawContent += (s, e) =>
- {
- foreach (View v in d.Subviews)
- {
- v.SetNeedsDisplay ();
- }
- };
- int lblWidth = "Replace:".Length;
- var label = new Label
- {
- Y = 1,
- Width = lblWidth,
- TextAlignment = TextAlignment.Right,
- Text = "Find:"
- };
- d.Add (label);
- SetFindText ();
- var txtToFind = new TextField
- {
- X = Pos.Right (label) + 1, Y = Pos.Top (label), Width = 20, Text = _textToFind
- };
- txtToFind.Enter += (s, e) => txtToFind.Text = _textToFind;
- d.Add (txtToFind);
- var btnFindNext = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (label),
- Width = 20,
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- TextAlignment = TextAlignment.Centered,
- 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 = 20, Text = _textToReplace
- };
- txtToReplace.TextChanged += (s, e) => _textToReplace = txtToReplace.Text;
- d.Add (txtToReplace);
- var btnFindPrevious = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (btnFindNext) + 1,
- Width = 20,
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- TextAlignment = TextAlignment.Centered,
- Text = "Replace _Previous"
- };
- btnFindPrevious.Accept += (s, e) => ReplacePrevious ();
- d.Add (btnFindPrevious);
- var btnReplaceAll = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (btnFindPrevious) + 1,
- Width = 20,
- Enabled = !string.IsNullOrEmpty (txtToFind.Text),
- TextAlignment = TextAlignment.Centered,
- 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 btnCancel = new Button
- {
- X = Pos.Right (txtToFind) + 1,
- Y = Pos.Top (btnReplaceAll) + 1,
- Width = 20,
- TextAlignment = TextAlignment.Centered,
- Text = "Cancel"
- };
- btnCancel.Accept += (s, e) => { DisposeWinDialog (); };
- d.Add (btnCancel);
- var ckbMatchCase = new CheckBox
- {
- X = 0, Y = Pos.Top (txtToFind) + 2, Checked = _matchCase, Text = "Match c_ase"
- };
- ckbMatchCase.Toggled += (s, e) => _matchCase = (bool)ckbMatchCase.Checked;
- d.Add (ckbMatchCase);
- var ckbMatchWholeWord = new CheckBox
- {
- X = 0, Y = Pos.Top (ckbMatchCase) + 1, Checked = _matchWholeWord, Text = "Match _whole word"
- };
- ckbMatchWholeWord.Toggled += (s, e) => _matchWholeWord = (bool)ckbMatchWholeWord.Checked;
- d.Add (ckbMatchWholeWord);
- d.Width = lblWidth + txtToFind.Width + btnFindNext.Width + 2;
- d.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
- 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 (Win.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 = Win.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
- {
- Win.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;
- }
- }
|