Editor.cs 28 KB

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