Editor.cs 28 KB

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