Editor.cs 28 KB

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