Editor.cs 28 KB

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