Editor.cs 27 KB

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