Editor.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Terminal.Gui;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Globalization;
  9. namespace UICatalog.Scenarios {
  10. [ScenarioMetadata (Name: "Editor", Description: "A Text Editor using the TextView control.")]
  11. [ScenarioCategory ("Controls")]
  12. [ScenarioCategory ("Dialogs")]
  13. [ScenarioCategory ("Text and Formatting")]
  14. [ScenarioCategory ("Top Level Windows")]
  15. [ScenarioCategory ("Files and IO")]
  16. [ScenarioCategory ("TextView")]
  17. public class Editor : Scenario {
  18. private string _fileName = "demo.txt";
  19. private TextView _textView;
  20. private bool _saved = true;
  21. private ScrollBarView _scrollBar;
  22. private byte [] _originalText;
  23. private string _textToFind;
  24. private string _textToReplace;
  25. private bool _matchCase;
  26. private bool _matchWholeWord;
  27. private Window _winDialog;
  28. private TabView _tabView;
  29. private MenuItem _miForceMinimumPosToZero;
  30. private bool _forceMinimumPosToZero = true;
  31. private readonly List<CultureInfo> _cultureInfos = Application.SupportedCultures;
  32. public override void Init (Toplevel top, ColorScheme colorScheme)
  33. {
  34. Application.Init ();
  35. Top = top;
  36. if (Top == null) {
  37. Top = Application.Top;
  38. }
  39. Win = new Window (_fileName ?? "Untitled") {
  40. X = 0,
  41. Y = 1,
  42. Width = Dim.Fill (),
  43. Height = Dim.Fill (),
  44. ColorScheme = colorScheme,
  45. };
  46. Top.Add (Win);
  47. _textView = new TextView () {
  48. X = 0,
  49. Y = 0,
  50. Width = Dim.Fill (),
  51. Height = Dim.Fill (),
  52. BottomOffset = 1,
  53. RightOffset = 1
  54. };
  55. CreateDemoFile (_fileName);
  56. var siCursorPosition = new StatusItem (Key.Null, "", null);
  57. _textView.UnwrappedCursorPosition += (e) => {
  58. siCursorPosition.Title = $"Ln {e.Y + 1}, Col {e.X + 1}";
  59. };
  60. LoadFile ();
  61. Win.Add (_textView);
  62. var menu = new MenuBar (new MenuBarItem [] {
  63. new MenuBarItem ("_File", new MenuItem [] {
  64. new MenuItem ("_New", "", () => New()),
  65. new MenuItem ("_Open", "", () => Open()),
  66. new MenuItem ("_Save", "", () => Save()),
  67. new MenuItem ("_Save As", "", () => SaveAs()),
  68. new MenuItem ("_Close", "", () => CloseFile()),
  69. null,
  70. new MenuItem ("_Quit", "", () => Quit()),
  71. }),
  72. new MenuBarItem ("_Edit", new MenuItem [] {
  73. new MenuItem ("_Copy", "", () => Copy(),null,null, Key.CtrlMask | Key.C),
  74. new MenuItem ("C_ut", "", () => Cut(),null,null, Key.CtrlMask | Key.W),
  75. new MenuItem ("_Paste", "", () => Paste(),null,null, Key.CtrlMask | Key.Y),
  76. null,
  77. new MenuItem ("_Find", "", () => Find(),null,null, Key.CtrlMask | Key.S),
  78. new MenuItem ("Find _Next", "", () => FindNext(),null,null, Key.CtrlMask | Key.ShiftMask | Key.S),
  79. new MenuItem ("Find P_revious", "", () => FindPrevious(),null,null, Key.CtrlMask | Key.ShiftMask | Key.AltMask | Key.S),
  80. new MenuItem ("_Replace", "", () => Replace(),null,null, Key.CtrlMask | Key.R),
  81. new MenuItem ("Replace Ne_xt", "", () => ReplaceNext(),null,null, Key.CtrlMask | Key.ShiftMask | Key.R),
  82. new MenuItem ("Replace Pre_vious", "", () => ReplacePrevious(),null,null, Key.CtrlMask | Key.ShiftMask | Key.AltMask | Key.R),
  83. new MenuItem ("Replace _All", "", () => ReplaceAll(),null,null, Key.CtrlMask | Key.ShiftMask | Key.AltMask | Key.A),
  84. null,
  85. new MenuItem ("_Select All", "", () => SelectAll(),null,null, Key.CtrlMask | Key.T)
  86. }),
  87. new MenuBarItem ("_ScrollBarView", CreateKeepChecked ()),
  88. new MenuBarItem ("_Cursor", CreateCursorRadio ()),
  89. new MenuBarItem ("Forma_t", new MenuItem [] {
  90. CreateWrapChecked (),
  91. CreateAutocomplete(),
  92. CreateAllowsTabChecked (),
  93. CreateReadOnlyChecked ()
  94. }),
  95. new MenuBarItem ("_Responder", new MenuItem [] {
  96. CreateCanFocusChecked (),
  97. CreateEnabledChecked (),
  98. CreateVisibleChecked ()
  99. }),
  100. new MenuBarItem ("Conte_xtMenu", new MenuItem [] {
  101. _miForceMinimumPosToZero = new MenuItem ("ForceMinimumPosTo_Zero", "", () => {
  102. _miForceMinimumPosToZero.Checked = _forceMinimumPosToZero = !_forceMinimumPosToZero;
  103. _textView.ContextMenu.ForceMinimumPosToZero = _forceMinimumPosToZero;
  104. }) { CheckType = MenuItemCheckStyle.Checked, Checked = _forceMinimumPosToZero },
  105. new MenuBarItem ("_Languages", GetSupportedCultures ())
  106. })
  107. });
  108. Top.Add (menu);
  109. var statusBar = new StatusBar (new StatusItem [] {
  110. siCursorPosition,
  111. new StatusItem(Key.F2, "~F2~ Open", () => Open()),
  112. new StatusItem(Key.F3, "~F3~ Save", () => Save()),
  113. new StatusItem(Key.F4, "~F4~ Save As", () => SaveAs()),
  114. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  115. new StatusItem(Key.Null, $"OS Clipboard IsSupported : {Clipboard.IsSupported}", null)
  116. });
  117. Top.Add (statusBar);
  118. _scrollBar = new ScrollBarView (_textView, true);
  119. _scrollBar.ChangedPosition += () => {
  120. _textView.TopRow = _scrollBar.Position;
  121. if (_textView.TopRow != _scrollBar.Position) {
  122. _scrollBar.Position = _textView.TopRow;
  123. }
  124. _textView.SetNeedsDisplay ();
  125. };
  126. _scrollBar.OtherScrollBarView.ChangedPosition += () => {
  127. _textView.LeftColumn = _scrollBar.OtherScrollBarView.Position;
  128. if (_textView.LeftColumn != _scrollBar.OtherScrollBarView.Position) {
  129. _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
  130. }
  131. _textView.SetNeedsDisplay ();
  132. };
  133. _scrollBar.VisibleChanged += () => {
  134. if (_scrollBar.Visible && _textView.RightOffset == 0) {
  135. _textView.RightOffset = 1;
  136. } else if (!_scrollBar.Visible && _textView.RightOffset == 1) {
  137. _textView.RightOffset = 0;
  138. }
  139. };
  140. _scrollBar.OtherScrollBarView.VisibleChanged += () => {
  141. if (_scrollBar.OtherScrollBarView.Visible && _textView.BottomOffset == 0) {
  142. _textView.BottomOffset = 1;
  143. } else if (!_scrollBar.OtherScrollBarView.Visible && _textView.BottomOffset == 1) {
  144. _textView.BottomOffset = 0;
  145. }
  146. };
  147. _textView.DrawContent += (e) => {
  148. _scrollBar.Size = _textView.Lines;
  149. _scrollBar.Position = _textView.TopRow;
  150. if (_scrollBar.OtherScrollBarView != null) {
  151. _scrollBar.OtherScrollBarView.Size = _textView.Maxlength;
  152. _scrollBar.OtherScrollBarView.Position = _textView.LeftColumn;
  153. }
  154. _scrollBar.LayoutSubviews ();
  155. _scrollBar.Refresh ();
  156. };
  157. Win.KeyPress += (e) => {
  158. var keys = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  159. if (_winDialog != null && (e.KeyEvent.Key == Key.Esc
  160. || e.KeyEvent.Key == (Key.Q | Key.CtrlMask))) {
  161. DisposeWinDialog ();
  162. } else if (e.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  163. Quit ();
  164. e.Handled = true;
  165. } else if (_winDialog != null && keys == (Key.Tab | Key.CtrlMask)) {
  166. if (_tabView.SelectedTab == _tabView.Tabs.ElementAt (_tabView.Tabs.Count - 1)) {
  167. _tabView.SelectedTab = _tabView.Tabs.ElementAt (0);
  168. } else {
  169. _tabView.SwitchTabBy (1);
  170. }
  171. e.Handled = true;
  172. } else if (_winDialog != null && keys == (Key.Tab | Key.CtrlMask | Key.ShiftMask)) {
  173. if (_tabView.SelectedTab == _tabView.Tabs.ElementAt (0)) {
  174. _tabView.SelectedTab = _tabView.Tabs.ElementAt (_tabView.Tabs.Count - 1);
  175. } else {
  176. _tabView.SwitchTabBy (-1);
  177. }
  178. e.Handled = true;
  179. }
  180. };
  181. Top.Closed += (_) => Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
  182. }
  183. private void DisposeWinDialog ()
  184. {
  185. _winDialog.Dispose ();
  186. Win.Remove (_winDialog);
  187. _winDialog = null;
  188. }
  189. public override void Setup ()
  190. {
  191. }
  192. private void New (bool checkChanges = true)
  193. {
  194. if (checkChanges && !CanCloseFile ()) {
  195. return;
  196. }
  197. Win.Title = "Untitled.txt";
  198. _fileName = null;
  199. _originalText = new System.IO.MemoryStream ().ToArray ();
  200. _textView.Text = _originalText;
  201. }
  202. private void LoadFile ()
  203. {
  204. if (_fileName != null) {
  205. // FIXED: BUGBUG: #452 TextView.LoadFile keeps file open and provides no way of closing it
  206. _textView.LoadFile (_fileName);
  207. //_textView.Text = System.IO.File.ReadAllText (_fileName);
  208. _originalText = _textView.Text.ToByteArray ();
  209. Win.Title = _fileName;
  210. _saved = true;
  211. }
  212. }
  213. private void Paste ()
  214. {
  215. if (_textView != null) {
  216. _textView.Paste ();
  217. }
  218. }
  219. private void Cut ()
  220. {
  221. if (_textView != null) {
  222. _textView.Cut ();
  223. }
  224. }
  225. private void Copy ()
  226. {
  227. if (_textView != null) {
  228. _textView.Copy ();
  229. }
  230. }
  231. private void SelectAll ()
  232. {
  233. _textView.SelectAll ();
  234. }
  235. private void Find ()
  236. {
  237. CreateFindReplace ();
  238. }
  239. private void FindNext ()
  240. {
  241. ContinueFind ();
  242. }
  243. private void FindPrevious ()
  244. {
  245. ContinueFind (false);
  246. }
  247. private void ContinueFind (bool next = true, bool replace = false)
  248. {
  249. if (!replace && string.IsNullOrEmpty (_textToFind)) {
  250. Find ();
  251. return;
  252. } else if (replace && (string.IsNullOrEmpty (_textToFind)
  253. || (_winDialog == null && string.IsNullOrEmpty (_textToReplace)))) {
  254. Replace ();
  255. return;
  256. }
  257. bool found;
  258. bool gaveFullTurn;
  259. if (next) {
  260. if (!replace) {
  261. found = _textView.FindNextText (_textToFind, out gaveFullTurn, _matchCase, _matchWholeWord);
  262. } else {
  263. found = _textView.FindNextText (_textToFind, out gaveFullTurn, _matchCase, _matchWholeWord,
  264. _textToReplace, true);
  265. }
  266. } else {
  267. if (!replace) {
  268. found = _textView.FindPreviousText (_textToFind, out gaveFullTurn, _matchCase, _matchWholeWord);
  269. } else {
  270. found = _textView.FindPreviousText (_textToFind, out gaveFullTurn, _matchCase, _matchWholeWord,
  271. _textToReplace, true);
  272. }
  273. }
  274. if (!found) {
  275. MessageBox.Query ("Find", $"The following specified text was not found: '{_textToFind}'", "Ok");
  276. } else if (gaveFullTurn) {
  277. MessageBox.Query ("Find", $"No more occurrences were found for the following specified text: '{_textToFind}'", "Ok");
  278. }
  279. }
  280. private void Replace ()
  281. {
  282. CreateFindReplace (false);
  283. }
  284. private void ReplaceNext ()
  285. {
  286. ContinueFind (true, true);
  287. }
  288. private void ReplacePrevious ()
  289. {
  290. ContinueFind (false, true);
  291. }
  292. private void ReplaceAll ()
  293. {
  294. if (string.IsNullOrEmpty (_textToFind) || (string.IsNullOrEmpty (_textToReplace) && _winDialog == null)) {
  295. Replace ();
  296. return;
  297. }
  298. if (_textView.ReplaceAllText (_textToFind, _matchCase, _matchWholeWord, _textToReplace)) {
  299. MessageBox.Query ("Replace All", $"All occurrences were replaced for the following specified text: '{_textToReplace}'", "Ok");
  300. } else {
  301. MessageBox.Query ("Replace All", $"None of the following specified text was found: '{_textToFind}'", "Ok");
  302. }
  303. }
  304. private bool CanCloseFile ()
  305. {
  306. if (_textView.Text == _originalText) {
  307. System.Diagnostics.Debug.Assert (!_textView.IsDirty);
  308. return true;
  309. }
  310. System.Diagnostics.Debug.Assert (_textView.IsDirty);
  311. var r = MessageBox.ErrorQuery ("Save File",
  312. $"Do you want save changes in {Win.Title}?", "Yes", "No", "Cancel");
  313. if (r == 0) {
  314. return Save ();
  315. } else if (r == 1) {
  316. return true;
  317. }
  318. return false;
  319. }
  320. private void Open ()
  321. {
  322. if (!CanCloseFile ()) {
  323. return;
  324. }
  325. var aTypes = new List<string> () { ".txt;.bin;.xml;.json", ".txt", ".bin", ".xml", ".*" };
  326. var d = new OpenDialog ("Open", "Choose the path where to open the file.", aTypes) { AllowsMultipleSelection = false };
  327. Application.Run (d);
  328. if (!d.Canceled && d.FilePaths.Count > 0) {
  329. _fileName = d.FilePaths [0];
  330. LoadFile ();
  331. }
  332. }
  333. private bool Save ()
  334. {
  335. if (_fileName != null) {
  336. // FIXED: BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  337. // As a result files saved on Windows and then read back will show invalid chars.
  338. return SaveFile (Win.Title.ToString (), _fileName);
  339. } else {
  340. return SaveAs ();
  341. }
  342. }
  343. private bool SaveAs ()
  344. {
  345. var aTypes = new List<string> () { ".txt", ".bin", ".xml", ".*" };
  346. var sd = new SaveDialog ("Save file", "Choose the path where to save the file.", aTypes);
  347. sd.FilePath = System.IO.Path.Combine (sd.FilePath.ToString (), Win.Title.ToString ());
  348. Application.Run (sd);
  349. if (!sd.Canceled) {
  350. if (System.IO.File.Exists (sd.FilePath.ToString ())) {
  351. if (MessageBox.Query ("Save File",
  352. "File already exists. Overwrite any way?", "No", "Ok") == 1) {
  353. return SaveFile (sd.FileName.ToString (), sd.FilePath.ToString ());
  354. } else {
  355. _saved = false;
  356. return _saved;
  357. }
  358. } else {
  359. return SaveFile (sd.FileName.ToString (), sd.FilePath.ToString ());
  360. }
  361. } else {
  362. _saved = false;
  363. return _saved;
  364. }
  365. }
  366. private bool SaveFile (string title, string file)
  367. {
  368. try {
  369. Win.Title = title;
  370. _fileName = file;
  371. System.IO.File.WriteAllText (_fileName, _textView.Text.ToString ());
  372. _originalText = _textView.Text.ToByteArray ();
  373. _saved = true;
  374. _textView.ClearHistoryChanges ();
  375. MessageBox.Query ("Save File", "File was successfully saved.", "Ok");
  376. } catch (Exception ex) {
  377. MessageBox.ErrorQuery ("Error", ex.Message, "Ok");
  378. return false;
  379. }
  380. return true;
  381. }
  382. private void CloseFile ()
  383. {
  384. if (!CanCloseFile ()) {
  385. return;
  386. }
  387. try {
  388. _textView.CloseFile ();
  389. New (false);
  390. } catch (Exception ex) {
  391. MessageBox.ErrorQuery ("Error", ex.Message, "Ok");
  392. }
  393. }
  394. private void Quit ()
  395. {
  396. if (!CanCloseFile ()) {
  397. return;
  398. }
  399. Application.RequestStop ();
  400. }
  401. private void CreateDemoFile (string fileName)
  402. {
  403. var sb = new StringBuilder ();
  404. // FIXED: BUGBUG: #279 TextView does not know how to deal with \r\n, only \r
  405. sb.Append ("Hello world.\n");
  406. sb.Append ("This is a test of the Emergency Broadcast System.\n");
  407. for (int i = 0; i < 30; i++) {
  408. sb.Append ($"{i} - This is a test with a very long line and many lines to test the ScrollViewBar against the TextView. - {i}\n");
  409. }
  410. var sw = System.IO.File.CreateText (fileName);
  411. sw.Write (sb.ToString ());
  412. sw.Close ();
  413. }
  414. private MenuItem [] GetSupportedCultures ()
  415. {
  416. List<MenuItem> supportedCultures = new List<MenuItem> ();
  417. var index = -1;
  418. foreach (var c in _cultureInfos) {
  419. var culture = new MenuItem {
  420. CheckType = MenuItemCheckStyle.Checked
  421. };
  422. if (index == -1) {
  423. culture.Title = "_English";
  424. culture.Help = "en-US";
  425. culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == "en-US";
  426. CreateAction (supportedCultures, culture);
  427. supportedCultures.Add (culture);
  428. index++;
  429. culture = new MenuItem {
  430. CheckType = MenuItemCheckStyle.Checked
  431. };
  432. }
  433. culture.Title = $"_{c.Parent.EnglishName}";
  434. culture.Help = c.Name;
  435. culture.Checked = Thread.CurrentThread.CurrentUICulture.Name == c.Name;
  436. CreateAction (supportedCultures, culture);
  437. supportedCultures.Add (culture);
  438. }
  439. return supportedCultures.ToArray ();
  440. void CreateAction (List<MenuItem> supportedCultures, MenuItem culture)
  441. {
  442. culture.Action += () => {
  443. Thread.CurrentThread.CurrentUICulture = new CultureInfo (culture.Help.ToString ());
  444. culture.Checked = true;
  445. foreach (var item in supportedCultures) {
  446. item.Checked = item.Help.ToString () == Thread.CurrentThread.CurrentUICulture.Name;
  447. }
  448. };
  449. }
  450. }
  451. private MenuItem [] CreateKeepChecked ()
  452. {
  453. var item = new MenuItem ();
  454. item.Title = "Keep Content Always In Viewport";
  455. item.CheckType |= MenuItemCheckStyle.Checked;
  456. item.Checked = true;
  457. item.Action += () => _scrollBar.KeepContentAlwaysInViewport = item.Checked = !item.Checked;
  458. return new MenuItem [] { item };
  459. }
  460. private MenuItem CreateWrapChecked ()
  461. {
  462. var item = new MenuItem {
  463. Title = "Word Wrap"
  464. };
  465. item.CheckType |= MenuItemCheckStyle.Checked;
  466. item.Checked = _textView.WordWrap;
  467. item.Action += () => {
  468. _textView.WordWrap = item.Checked = !item.Checked;
  469. if (_textView.WordWrap) {
  470. _scrollBar.OtherScrollBarView.ShowScrollIndicator = false;
  471. _textView.BottomOffset = 0;
  472. } else {
  473. _textView.BottomOffset = 1;
  474. }
  475. };
  476. return item;
  477. }
  478. private MenuItem CreateAutocomplete ()
  479. {
  480. var auto = new MenuItem ();
  481. auto.Title = "Autocomplete";
  482. auto.CheckType |= MenuItemCheckStyle.Checked;
  483. auto.Checked = false;
  484. auto.Action += () => {
  485. if (auto.Checked = !auto.Checked) {
  486. // setup autocomplete with all words currently in the editor
  487. _textView.Autocomplete.AllSuggestions =
  488. Regex.Matches (_textView.Text.ToString (), "\\w+")
  489. .Select (s => s.Value)
  490. .Distinct ().ToList ();
  491. } else {
  492. _textView.Autocomplete.AllSuggestions.Clear ();
  493. }
  494. };
  495. return auto;
  496. }
  497. private MenuItem CreateAllowsTabChecked ()
  498. {
  499. var item = new MenuItem {
  500. Title = "Allows Tab"
  501. };
  502. item.CheckType |= MenuItemCheckStyle.Checked;
  503. item.Checked = _textView.AllowsTab;
  504. item.Action += () => {
  505. _textView.AllowsTab = item.Checked = !item.Checked;
  506. };
  507. return item;
  508. }
  509. private MenuItem CreateReadOnlyChecked ()
  510. {
  511. var item = new MenuItem {
  512. Title = "Read Only"
  513. };
  514. item.CheckType |= MenuItemCheckStyle.Checked;
  515. item.Checked = _textView.ReadOnly;
  516. item.Action += () => _textView.ReadOnly = item.Checked = !item.Checked;
  517. return item;
  518. }
  519. private MenuItem CreateCanFocusChecked ()
  520. {
  521. var item = new MenuItem {
  522. Title = "CanFocus"
  523. };
  524. item.CheckType |= MenuItemCheckStyle.Checked;
  525. item.Checked = _textView.CanFocus;
  526. item.Action += () => {
  527. _textView.CanFocus = item.Checked = !item.Checked;
  528. if (_textView.CanFocus) {
  529. _textView.SetFocus ();
  530. }
  531. };
  532. return item;
  533. }
  534. private MenuItem CreateEnabledChecked ()
  535. {
  536. var item = new MenuItem {
  537. Title = "Enabled"
  538. };
  539. item.CheckType |= MenuItemCheckStyle.Checked;
  540. item.Checked = _textView.Enabled;
  541. item.Action += () => {
  542. _textView.Enabled = item.Checked = !item.Checked;
  543. if (_textView.Enabled) {
  544. _textView.SetFocus ();
  545. }
  546. };
  547. return item;
  548. }
  549. private MenuItem CreateVisibleChecked ()
  550. {
  551. var item = new MenuItem {
  552. Title = "Visible"
  553. };
  554. item.CheckType |= MenuItemCheckStyle.Checked;
  555. item.Checked = _textView.Visible;
  556. item.Action += () => {
  557. _textView.Visible = item.Checked = !item.Checked;
  558. if (_textView.Visible) {
  559. _textView.SetFocus ();
  560. }
  561. };
  562. return item;
  563. }
  564. MenuItem [] CreateCursorRadio ()
  565. {
  566. List<MenuItem> menuItems = new List<MenuItem> ();
  567. menuItems.Add (new MenuItem ("_Invisible", "", () => SetCursor (CursorVisibility.Invisible)) {
  568. CheckType = MenuItemCheckStyle.Radio,
  569. Checked = _textView.DesiredCursorVisibility == CursorVisibility.Invisible
  570. });
  571. menuItems.Add (new MenuItem ("_Box", "", () => SetCursor (CursorVisibility.Box)) {
  572. CheckType = MenuItemCheckStyle.Radio,
  573. Checked = _textView.DesiredCursorVisibility == CursorVisibility.Box
  574. });
  575. menuItems.Add (new MenuItem ("_Underline", "", () => SetCursor (CursorVisibility.Underline)) {
  576. CheckType = MenuItemCheckStyle.Radio,
  577. Checked = _textView.DesiredCursorVisibility == CursorVisibility.Underline
  578. });
  579. menuItems.Add (new MenuItem ("", "", () => { }, () => false));
  580. menuItems.Add (new MenuItem ("xTerm :", "", () => { }, () => false));
  581. menuItems.Add (new MenuItem ("", "", () => { }, () => false));
  582. menuItems.Add (new MenuItem (" _Default", "", () => SetCursor (CursorVisibility.Default)) {
  583. CheckType = MenuItemCheckStyle.Radio,
  584. Checked = _textView.DesiredCursorVisibility == CursorVisibility.Default
  585. });
  586. menuItems.Add (new MenuItem (" _Vertical", "", () => SetCursor (CursorVisibility.Vertical)) {
  587. CheckType = MenuItemCheckStyle.Radio,
  588. Checked = _textView.DesiredCursorVisibility == CursorVisibility.Vertical
  589. });
  590. menuItems.Add (new MenuItem (" V_ertical Fix", "", () => SetCursor (CursorVisibility.VerticalFix)) {
  591. CheckType = MenuItemCheckStyle.Radio,
  592. Checked = _textView.DesiredCursorVisibility == CursorVisibility.VerticalFix
  593. });
  594. menuItems.Add (new MenuItem (" B_ox Fix", "", () => SetCursor (CursorVisibility.BoxFix)) {
  595. CheckType = MenuItemCheckStyle.Radio,
  596. Checked = _textView.DesiredCursorVisibility == CursorVisibility.BoxFix
  597. });
  598. menuItems.Add (new MenuItem (" U_nderline Fix", "", () => SetCursor (CursorVisibility.UnderlineFix)) {
  599. CheckType = MenuItemCheckStyle.Radio,
  600. Checked = _textView.DesiredCursorVisibility == CursorVisibility.UnderlineFix
  601. });
  602. void SetCursor (CursorVisibility visibility)
  603. {
  604. _textView.DesiredCursorVisibility = visibility;
  605. var title = "";
  606. switch (visibility) {
  607. case CursorVisibility.Default:
  608. title = " _Default";
  609. break;
  610. case CursorVisibility.Invisible:
  611. title = "_Invisible";
  612. break;
  613. case CursorVisibility.Underline:
  614. title = "_Underline";
  615. break;
  616. case CursorVisibility.UnderlineFix:
  617. title = " U_nderline Fix";
  618. break;
  619. case CursorVisibility.Vertical:
  620. title = " _Vertical";
  621. break;
  622. case CursorVisibility.VerticalFix:
  623. title = " V_ertical Fix";
  624. break;
  625. case CursorVisibility.Box:
  626. title = "_Box";
  627. break;
  628. case CursorVisibility.BoxFix:
  629. title = " B_ox Fix";
  630. break;
  631. }
  632. foreach (var menuItem in menuItems) {
  633. menuItem.Checked = menuItem.Title.Equals (title) && visibility == _textView.DesiredCursorVisibility;
  634. }
  635. }
  636. return menuItems.ToArray ();
  637. }
  638. private void CreateFindReplace (bool isFind = true)
  639. {
  640. if (_winDialog != null) {
  641. _winDialog.SetFocus ();
  642. return;
  643. }
  644. _winDialog = new Window (isFind ? "Find" : "Replace") {
  645. X = Win.Bounds.Width / 2 - 30,
  646. Y = Win.Bounds.Height / 2 - 10,
  647. ColorScheme = Colors.TopLevel
  648. };
  649. _winDialog.Border.Effect3D = true;
  650. _tabView = new TabView () {
  651. X = 0,
  652. Y = 0,
  653. Width = Dim.Fill (),
  654. Height = Dim.Fill ()
  655. };
  656. _tabView.AddTab (new TabView.Tab ("Find", FindTab ()), isFind);
  657. var replace = ReplaceTab ();
  658. _tabView.AddTab (new TabView.Tab ("Replace", replace), !isFind);
  659. _tabView.SelectedTabChanged += (s, e) => _tabView.SelectedTab.View.FocusFirst ();
  660. _winDialog.Add (_tabView);
  661. Win.Add (_winDialog);
  662. _winDialog.Width = replace.Width + 4;
  663. _winDialog.Height = replace.Height + 4;
  664. _winDialog.SuperView.BringSubviewToFront (_winDialog);
  665. _winDialog.SetFocus ();
  666. }
  667. private void SetFindText ()
  668. {
  669. _textToFind = !_textView.SelectedText.IsEmpty
  670. ? _textView.SelectedText.ToString ()
  671. : string.IsNullOrEmpty (_textToFind) ? "" : _textToFind;
  672. _textToReplace = string.IsNullOrEmpty (_textToReplace) ? "" : _textToReplace;
  673. }
  674. private View FindTab ()
  675. {
  676. var d = new View ();
  677. d.DrawContent += (e) => {
  678. foreach (var v in d.Subviews) {
  679. v.SetNeedsDisplay ();
  680. }
  681. };
  682. var lblWidth = "Replace:".Length;
  683. var label = new Label ("Find:") {
  684. Y = 1,
  685. Width = lblWidth,
  686. TextAlignment = TextAlignment.Right,
  687. AutoSize = false
  688. };
  689. d.Add (label);
  690. SetFindText ();
  691. var txtToFind = new TextField (_textToFind) {
  692. X = Pos.Right (label) + 1,
  693. Y = Pos.Top (label),
  694. Width = 20
  695. };
  696. txtToFind.Enter += (_) => txtToFind.Text = _textToFind;
  697. d.Add (txtToFind);
  698. var btnFindNext = new Button ("Find _Next") {
  699. X = Pos.Right (txtToFind) + 1,
  700. Y = Pos.Top (label),
  701. Width = 20,
  702. Enabled = !txtToFind.Text.IsEmpty,
  703. TextAlignment = TextAlignment.Centered,
  704. IsDefault = true,
  705. AutoSize = false
  706. };
  707. btnFindNext.Clicked += () => FindNext ();
  708. d.Add (btnFindNext);
  709. var btnFindPrevious = new Button ("Find _Previous") {
  710. X = Pos.Right (txtToFind) + 1,
  711. Y = Pos.Top (btnFindNext) + 1,
  712. Width = 20,
  713. Enabled = !txtToFind.Text.IsEmpty,
  714. TextAlignment = TextAlignment.Centered,
  715. AutoSize = false
  716. };
  717. btnFindPrevious.Clicked += () => FindPrevious ();
  718. d.Add (btnFindPrevious);
  719. txtToFind.TextChanged += (e) => {
  720. _textToFind = txtToFind.Text.ToString ();
  721. _textView.FindTextChanged ();
  722. btnFindNext.Enabled = !txtToFind.Text.IsEmpty;
  723. btnFindPrevious.Enabled = !txtToFind.Text.IsEmpty;
  724. };
  725. var btnCancel = new Button ("Cancel") {
  726. X = Pos.Right (txtToFind) + 1,
  727. Y = Pos.Top (btnFindPrevious) + 2,
  728. Width = 20,
  729. TextAlignment = TextAlignment.Centered,
  730. AutoSize = false
  731. };
  732. btnCancel.Clicked += () => {
  733. DisposeWinDialog ();
  734. };
  735. d.Add (btnCancel);
  736. var ckbMatchCase = new CheckBox ("Match c_ase") {
  737. X = 0,
  738. Y = Pos.Top (txtToFind) + 2,
  739. Checked = _matchCase
  740. };
  741. ckbMatchCase.Toggled += (e) => _matchCase = ckbMatchCase.Checked;
  742. d.Add (ckbMatchCase);
  743. var ckbMatchWholeWord = new CheckBox ("Match _whole word") {
  744. X = 0,
  745. Y = Pos.Top (ckbMatchCase) + 1,
  746. Checked = _matchWholeWord
  747. };
  748. ckbMatchWholeWord.Toggled += (e) => _matchWholeWord = ckbMatchWholeWord.Checked;
  749. d.Add (ckbMatchWholeWord);
  750. d.Width = label.Width + txtToFind.Width + btnFindNext.Width + 2;
  751. d.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
  752. return d;
  753. }
  754. private View ReplaceTab ()
  755. {
  756. var d = new View ();
  757. d.DrawContent += (e) => {
  758. foreach (var v in d.Subviews) {
  759. v.SetNeedsDisplay ();
  760. }
  761. };
  762. var lblWidth = "Replace:".Length;
  763. var label = new Label ("Find:") {
  764. Y = 1,
  765. Width = lblWidth,
  766. TextAlignment = TextAlignment.Right,
  767. AutoSize = false
  768. };
  769. d.Add (label);
  770. SetFindText ();
  771. var txtToFind = new TextField (_textToFind) {
  772. X = Pos.Right (label) + 1,
  773. Y = Pos.Top (label),
  774. Width = 20
  775. };
  776. txtToFind.Enter += (_) => txtToFind.Text = _textToFind;
  777. d.Add (txtToFind);
  778. var btnFindNext = new Button ("Replace _Next") {
  779. X = Pos.Right (txtToFind) + 1,
  780. Y = Pos.Top (label),
  781. Width = 20,
  782. Enabled = !txtToFind.Text.IsEmpty,
  783. TextAlignment = TextAlignment.Centered,
  784. IsDefault = true,
  785. AutoSize = false
  786. };
  787. btnFindNext.Clicked += () => ReplaceNext ();
  788. d.Add (btnFindNext);
  789. label = new Label ("Replace:") {
  790. X = Pos.Left (label),
  791. Y = Pos.Top (label) + 1,
  792. Width = lblWidth,
  793. TextAlignment = TextAlignment.Right
  794. };
  795. d.Add (label);
  796. SetFindText ();
  797. var txtToReplace = new TextField (_textToReplace) {
  798. X = Pos.Right (label) + 1,
  799. Y = Pos.Top (label),
  800. Width = 20
  801. };
  802. txtToReplace.TextChanged += (e) => _textToReplace = txtToReplace.Text.ToString ();
  803. d.Add (txtToReplace);
  804. var btnFindPrevious = new Button ("Replace _Previous") {
  805. X = Pos.Right (txtToFind) + 1,
  806. Y = Pos.Top (btnFindNext) + 1,
  807. Width = 20,
  808. Enabled = !txtToFind.Text.IsEmpty,
  809. TextAlignment = TextAlignment.Centered,
  810. AutoSize = false
  811. };
  812. btnFindPrevious.Clicked += () => ReplacePrevious ();
  813. d.Add (btnFindPrevious);
  814. var btnReplaceAll = new Button ("Replace _All") {
  815. X = Pos.Right (txtToFind) + 1,
  816. Y = Pos.Top (btnFindPrevious) + 1,
  817. Width = 20,
  818. Enabled = !txtToFind.Text.IsEmpty,
  819. TextAlignment = TextAlignment.Centered,
  820. AutoSize = false
  821. };
  822. btnReplaceAll.Clicked += () => ReplaceAll ();
  823. d.Add (btnReplaceAll);
  824. txtToFind.TextChanged += (e) => {
  825. _textToFind = txtToFind.Text.ToString ();
  826. _textView.FindTextChanged ();
  827. btnFindNext.Enabled = !txtToFind.Text.IsEmpty;
  828. btnFindPrevious.Enabled = !txtToFind.Text.IsEmpty;
  829. btnReplaceAll.Enabled = !txtToFind.Text.IsEmpty;
  830. };
  831. var btnCancel = new Button ("Cancel") {
  832. X = Pos.Right (txtToFind) + 1,
  833. Y = Pos.Top (btnReplaceAll) + 1,
  834. Width = 20,
  835. TextAlignment = TextAlignment.Centered,
  836. AutoSize = false
  837. };
  838. btnCancel.Clicked += () => {
  839. DisposeWinDialog ();
  840. };
  841. d.Add (btnCancel);
  842. var ckbMatchCase = new CheckBox ("Match c_ase") {
  843. X = 0,
  844. Y = Pos.Top (txtToFind) + 2,
  845. Checked = _matchCase
  846. };
  847. ckbMatchCase.Toggled += (e) => _matchCase = ckbMatchCase.Checked;
  848. d.Add (ckbMatchCase);
  849. var ckbMatchWholeWord = new CheckBox ("Match _whole word") {
  850. X = 0,
  851. Y = Pos.Top (ckbMatchCase) + 1,
  852. Checked = _matchWholeWord
  853. };
  854. ckbMatchWholeWord.Toggled += (e) => _matchWholeWord = ckbMatchWholeWord.Checked;
  855. d.Add (ckbMatchWholeWord);
  856. d.Width = lblWidth + txtToFind.Width + btnFindNext.Width + 2;
  857. d.Height = btnFindNext.Height + btnFindPrevious.Height + btnCancel.Height + 4;
  858. return d;
  859. }
  860. }
  861. }