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