DynamicMenuBar.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using Terminal.Gui;
  10. namespace UICatalog {
  11. [ScenarioMetadata (Name: "Dynamic MenuBar", Description: "Demonstrates how to add and remove a MenuBar, Menus and change titles dynamically.")]
  12. [ScenarioCategory ("Dynamic")]
  13. class DynamicMenuBar : Scenario {
  14. public override void Run ()
  15. {
  16. Top.Add (new DynamicMenuBarSample (Win.Title));
  17. base.Run ();
  18. }
  19. }
  20. public class DynamicMenuItemList {
  21. public ustring Title { get; set; }
  22. public MenuItem MenuItem { get; set; }
  23. public DynamicMenuItemList () { }
  24. public DynamicMenuItemList (ustring title, MenuItem menuItem)
  25. {
  26. Title = title;
  27. MenuItem = menuItem;
  28. }
  29. public override string ToString () => $"{Title}, {MenuItem}";
  30. }
  31. public class DynamicMenuItem {
  32. public ustring title = "_New";
  33. public ustring help = "";
  34. public ustring action = "";
  35. public bool isTopLevel;
  36. public bool hasSubMenu;
  37. public MenuItemCheckStyle checkStyle;
  38. public ustring shortCut;
  39. public DynamicMenuItem () { }
  40. public DynamicMenuItem (ustring title, bool hasSubMenu = false)
  41. {
  42. this.title = title;
  43. this.hasSubMenu = hasSubMenu;
  44. }
  45. public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, ustring shortCut = null)
  46. {
  47. this.title = title;
  48. this.help = help;
  49. this.action = action;
  50. this.isTopLevel = isTopLevel;
  51. this.hasSubMenu = hasSubMenu;
  52. this.checkStyle = checkStyle;
  53. this.shortCut = shortCut;
  54. }
  55. }
  56. public class DynamicMenuBarSample : Window {
  57. MenuBar _menuBar;
  58. MenuItem _currentMenuBarItem;
  59. int _currentSelectedMenuBar;
  60. MenuItem _currentEditMenuBarItem;
  61. ListView _lstMenus;
  62. public DynamicMenuItemModel DataContext { get; set; }
  63. public DynamicMenuBarSample (ustring title) : base (title)
  64. {
  65. DataContext = new DynamicMenuItemModel ();
  66. var _frmDelimiter = new FrameView ("ShortCut Delimiter:") {
  67. X = Pos.Center (),
  68. Y = 3,
  69. Width = 25,
  70. Height = 4
  71. };
  72. var _txtDelimiter = new TextField (MenuBar.ShortCutDelimiter.ToString ()) {
  73. X = Pos.Center(),
  74. Width = 2,
  75. };
  76. _txtDelimiter.TextChanged += (_) => MenuBar.ShortCutDelimiter = _txtDelimiter.Text;
  77. _frmDelimiter.Add (_txtDelimiter);
  78. Add (_frmDelimiter);
  79. var _frmMenu = new FrameView ("Menus:") {
  80. Y = 7,
  81. Width = Dim.Percent (50),
  82. Height = Dim.Fill ()
  83. };
  84. var _btnAddMenuBar = new Button ("Add a MenuBar") {
  85. Y = 1,
  86. };
  87. _frmMenu.Add (_btnAddMenuBar);
  88. var _btnMenuBarUp = new Button ("^") {
  89. X = Pos.Center ()
  90. };
  91. _frmMenu.Add (_btnMenuBarUp);
  92. var _btnMenuBarDown = new Button ("v") {
  93. X = Pos.Center (),
  94. Y = Pos.Bottom (_btnMenuBarUp)
  95. };
  96. _frmMenu.Add (_btnMenuBarDown);
  97. var _btnRemoveMenuBar = new Button ("Remove a MenuBar") {
  98. Y = 1
  99. };
  100. _btnRemoveMenuBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveMenuBar) - Pos.Left (_btnRemoveMenuBar));
  101. _frmMenu.Add (_btnRemoveMenuBar);
  102. var _btnPrevious = new Button ("<") {
  103. X = Pos.Left (_btnAddMenuBar),
  104. Y = Pos.Top (_btnAddMenuBar) + 2
  105. };
  106. _frmMenu.Add (_btnPrevious);
  107. var _btnAdd = new Button (" Add ") {
  108. Y = Pos.Top (_btnPrevious) + 2,
  109. };
  110. _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
  111. _frmMenu.Add (_btnAdd);
  112. var _btnNext = new Button (">") {
  113. X = Pos.X (_btnAdd),
  114. Y = Pos.Top (_btnPrevious),
  115. };
  116. _frmMenu.Add (_btnNext);
  117. var _lblMenuBar = new Label () {
  118. ColorScheme = Colors.Dialog,
  119. TextAlignment = TextAlignment.Centered,
  120. X = Pos.Right (_btnPrevious) + 1,
  121. Y = Pos.Top (_btnPrevious),
  122. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1,
  123. Height = 1
  124. };
  125. _frmMenu.Add (_lblMenuBar);
  126. _lblMenuBar.WantMousePositionReports = true;
  127. _lblMenuBar.CanFocus = true;
  128. var _lblParent = new Label () {
  129. TextAlignment = TextAlignment.Centered,
  130. X = Pos.Right (_btnPrevious) + 1,
  131. Y = Pos.Top (_btnPrevious) + 1,
  132. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1
  133. };
  134. _frmMenu.Add (_lblParent);
  135. var _btnPreviowsParent = new Button ("..") {
  136. X = Pos.Left (_btnAddMenuBar),
  137. Y = Pos.Top (_btnPrevious) + 1
  138. };
  139. _frmMenu.Add (_btnPreviowsParent);
  140. _lstMenus = new ListView (new List<DynamicMenuItemList> ()) {
  141. ColorScheme = Colors.Dialog,
  142. X = Pos.Right (_btnPrevious) + 1,
  143. Y = Pos.Top (_btnPrevious) + 2,
  144. Width = _lblMenuBar.Width,
  145. Height = Dim.Fill (),
  146. };
  147. _frmMenu.Add (_lstMenus);
  148. _lblMenuBar.TabIndex = _btnPrevious.TabIndex + 1;
  149. _lstMenus.TabIndex = _lblMenuBar.TabIndex + 1;
  150. _btnNext.TabIndex = _lstMenus.TabIndex + 1;
  151. _btnAdd.TabIndex = _btnNext.TabIndex + 1;
  152. var _btnRemove = new Button ("Remove") {
  153. X = Pos.Left (_btnAdd),
  154. Y = Pos.Top (_btnAdd) + 1
  155. };
  156. _frmMenu.Add (_btnRemove);
  157. var _btnUp = new Button ("^") {
  158. X = Pos.Right (_lstMenus) + 2,
  159. Y = Pos.Top (_btnRemove) + 2
  160. };
  161. _frmMenu.Add (_btnUp);
  162. var _btnDown = new Button ("v") {
  163. X = Pos.Right (_lstMenus) + 2,
  164. Y = Pos.Top (_btnUp) + 1
  165. };
  166. _frmMenu.Add (_btnDown);
  167. Add (_frmMenu);
  168. var _frmMenuDetails = new DynamicMenuBarDetails ("Menu Details:") {
  169. X = Pos.Right (_frmMenu),
  170. Y = Pos.Top (_frmMenu),
  171. Width = Dim.Fill (),
  172. Height = Dim.Fill (2)
  173. };
  174. Add (_frmMenuDetails);
  175. _btnMenuBarUp.Clicked += () => {
  176. var i = _currentSelectedMenuBar;
  177. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  178. if (menuItem != null) {
  179. var menus = _menuBar.Menus;
  180. if (i > 0) {
  181. menus [i] = menus [i - 1];
  182. menus [i - 1] = menuItem;
  183. _currentSelectedMenuBar = i - 1;
  184. _menuBar.SetNeedsDisplay ();
  185. }
  186. }
  187. };
  188. _btnMenuBarDown.Clicked += () => {
  189. var i = _currentSelectedMenuBar;
  190. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  191. if (menuItem != null) {
  192. var menus = _menuBar.Menus;
  193. if (i < menus.Length - 1) {
  194. menus [i] = menus [i + 1];
  195. menus [i + 1] = menuItem;
  196. _currentSelectedMenuBar = i + 1;
  197. _menuBar.SetNeedsDisplay ();
  198. }
  199. }
  200. };
  201. _btnUp.Clicked += () => {
  202. var i = _lstMenus.SelectedItem;
  203. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  204. if (menuItem != null) {
  205. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  206. if (i > 0) {
  207. childrens [i] = childrens [i - 1];
  208. childrens [i - 1] = menuItem;
  209. DataContext.Menus [i] = DataContext.Menus [i - 1];
  210. DataContext.Menus [i - 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  211. _lstMenus.SelectedItem = i - 1;
  212. }
  213. }
  214. };
  215. _btnDown.Clicked += () => {
  216. var i = _lstMenus.SelectedItem;
  217. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  218. if (menuItem != null) {
  219. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  220. if (i < childrens.Length - 1) {
  221. childrens [i] = childrens [i + 1];
  222. childrens [i + 1] = menuItem;
  223. DataContext.Menus [i] = DataContext.Menus [i + 1];
  224. DataContext.Menus [i + 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  225. _lstMenus.SelectedItem = i + 1;
  226. }
  227. }
  228. };
  229. _btnPreviowsParent.Clicked += () => {
  230. if (_currentMenuBarItem != null && _currentMenuBarItem.Parent != null) {
  231. var mi = _currentMenuBarItem;
  232. _currentMenuBarItem = _currentMenuBarItem.Parent as MenuBarItem;
  233. SetListViewSource (_currentMenuBarItem, true);
  234. var i = ((MenuBarItem)_currentMenuBarItem).GetChildrenIndex (mi);
  235. if (i > -1) {
  236. _lstMenus.SelectedItem = i;
  237. }
  238. if (_currentMenuBarItem.Parent != null) {
  239. DataContext.Parent = _currentMenuBarItem.Title;
  240. } else {
  241. DataContext.Parent = ustring.Empty;
  242. }
  243. } else {
  244. DataContext.Parent = ustring.Empty;
  245. }
  246. };
  247. var _btnOk = new Button ("Ok") {
  248. X = Pos.Right (_frmMenu) + 20,
  249. Y = Pos.Bottom (_frmMenuDetails),
  250. };
  251. Add (_btnOk);
  252. var _btnCancel = new Button ("Cancel") {
  253. X = Pos.Right (_btnOk) + 3,
  254. Y = Pos.Top (_btnOk),
  255. };
  256. _btnCancel.Clicked += () => {
  257. SetFrameDetails (_currentEditMenuBarItem);
  258. };
  259. Add (_btnCancel);
  260. _lstMenus.SelectedItemChanged += (e) => {
  261. SetFrameDetails ();
  262. };
  263. _btnOk.Clicked += () => {
  264. if (ustring.IsNullOrEmpty (_frmMenuDetails._txtTitle.Text) && _currentEditMenuBarItem != null) {
  265. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  266. } else if (_currentEditMenuBarItem != null) {
  267. var menuItem = new DynamicMenuItem (_frmMenuDetails._txtTitle.Text, _frmMenuDetails._txtHelp.Text,
  268. _frmMenuDetails._txtAction.Text,
  269. _frmMenuDetails._ckbIsTopLevel != null ? _frmMenuDetails._ckbIsTopLevel.Checked : false,
  270. _frmMenuDetails._ckbSubMenu != null ? _frmMenuDetails._ckbSubMenu.Checked : false,
  271. _frmMenuDetails._rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  272. _frmMenuDetails._rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked :
  273. MenuItemCheckStyle.Radio,
  274. _frmMenuDetails._txtShortCut.Text);
  275. UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
  276. }
  277. };
  278. _btnAdd.Clicked += () => {
  279. if (MenuBar == null) {
  280. MessageBox.ErrorQuery ("Menu Bar Error", "Must add a MenuBar first!", "Ok");
  281. _btnAddMenuBar.SetFocus ();
  282. return;
  283. }
  284. var frameDetails = new DynamicMenuBarDetails (null, _currentMenuBarItem != null);
  285. var item = frameDetails.EnterMenuItem ();
  286. if (item == null) {
  287. return;
  288. }
  289. if (!(_currentMenuBarItem is MenuBarItem)) {
  290. var parent = _currentMenuBarItem.Parent as MenuBarItem;
  291. var idx = parent.GetChildrenIndex (_currentMenuBarItem);
  292. _currentMenuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { }, _currentMenuBarItem.Parent);
  293. _currentMenuBarItem.CheckType = item.checkStyle;
  294. parent.Children [idx] = _currentMenuBarItem;
  295. } else {
  296. MenuItem newMenu = CreateNewMenu (item, _currentMenuBarItem);
  297. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  298. if (menuBarItem == null) {
  299. menuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { newMenu }, _currentMenuBarItem.Parent);
  300. } else if (menuBarItem.Children == null) {
  301. menuBarItem.Children = new MenuItem [] { newMenu };
  302. } else {
  303. var childrens = menuBarItem.Children;
  304. Array.Resize (ref childrens, childrens.Length + 1);
  305. childrens [childrens.Length - 1] = newMenu;
  306. menuBarItem.Children = childrens;
  307. }
  308. DataContext.Menus.Add (new DynamicMenuItemList (newMenu.Title, newMenu));
  309. _lstMenus.MoveDown ();
  310. }
  311. };
  312. _btnRemove.Clicked += () => {
  313. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  314. if (menuItem != null) {
  315. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  316. childrens [_lstMenus.SelectedItem] = null;
  317. int i = 0;
  318. foreach (var c in childrens) {
  319. if (c != null) {
  320. childrens [i] = c;
  321. i++;
  322. }
  323. }
  324. Array.Resize (ref childrens, childrens.Length - 1);
  325. if (childrens.Length == 0) {
  326. if (_currentMenuBarItem.Parent == null) {
  327. ((MenuBarItem)_currentMenuBarItem).Children = null;
  328. //_currentMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentMenuBarItem.Title));
  329. } else {
  330. _currentMenuBarItem = new MenuItem (_currentMenuBarItem.Title, _currentMenuBarItem.Help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentEditMenuBarItem.Title)), null, _currentMenuBarItem.Parent);
  331. }
  332. } else {
  333. ((MenuBarItem)_currentMenuBarItem).Children = childrens;
  334. }
  335. DataContext.Menus.RemoveAt (_lstMenus.SelectedItem);
  336. if (_lstMenus.Source.Count > 0 && _lstMenus.SelectedItem > _lstMenus.Source.Count - 1) {
  337. _lstMenus.SelectedItem = _lstMenus.Source.Count - 1;
  338. }
  339. _lstMenus.SetNeedsDisplay ();
  340. SetFrameDetails ();
  341. }
  342. };
  343. _lstMenus.OpenSelectedItem += (e) => {
  344. _currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
  345. if (!(_currentMenuBarItem is MenuBarItem)) {
  346. MessageBox.ErrorQuery ("Menu Open Error", "Must allows sub menus first!", "Ok");
  347. return;
  348. }
  349. DataContext.Parent = _currentMenuBarItem.Title;
  350. DataContext.Menus = new List<DynamicMenuItemList> ();
  351. SetListViewSource (_currentMenuBarItem, true);
  352. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [0].MenuItem : null;
  353. SetFrameDetails (menuBarItem);
  354. };
  355. _lstMenus.Enter += (_) => {
  356. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  357. SetFrameDetails (menuBarItem);
  358. };
  359. _btnNext.Clicked += () => {
  360. if (_menuBar != null && _currentSelectedMenuBar + 1 < _menuBar.Menus.Length) {
  361. _currentSelectedMenuBar++;
  362. }
  363. SelectCurrentMenuBarItem ();
  364. };
  365. _btnPrevious.Clicked += () => {
  366. if (_currentSelectedMenuBar - 1 > -1) {
  367. _currentSelectedMenuBar--;
  368. }
  369. SelectCurrentMenuBarItem ();
  370. };
  371. _lblMenuBar.Enter += (e) => {
  372. if (_menuBar?.Menus != null) {
  373. _currentMenuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  374. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  375. }
  376. };
  377. _btnAddMenuBar.Clicked += () => {
  378. var frameDetails = new DynamicMenuBarDetails (null, false);
  379. var item = frameDetails.EnterMenuItem ();
  380. if (item == null) {
  381. return;
  382. }
  383. if (MenuBar == null) {
  384. _menuBar = new MenuBar ();
  385. Add (_menuBar);
  386. }
  387. var newMenu = CreateNewMenu (item) as MenuBarItem;
  388. var menus = _menuBar.Menus;
  389. Array.Resize (ref menus, menus.Length + 1);
  390. menus [^1] = newMenu;
  391. _menuBar.Menus = menus;
  392. _currentMenuBarItem = newMenu;
  393. _currentMenuBarItem.CheckType = item.checkStyle;
  394. _currentSelectedMenuBar = menus.Length - 1;
  395. _menuBar.Menus [_currentSelectedMenuBar] = newMenu;
  396. _lblMenuBar.Text = newMenu.Title;
  397. SetListViewSource (_currentMenuBarItem, true);
  398. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  399. _menuBar.SetNeedsDisplay ();
  400. };
  401. _btnRemoveMenuBar.Clicked += () => {
  402. if (_menuBar == null || _menuBar.Menus.Length == 0) {
  403. return;
  404. }
  405. if (_menuBar != null && _menuBar.Menus.Length > 0) {
  406. _menuBar.Menus [_currentSelectedMenuBar] = null;
  407. int i = 0;
  408. foreach (var m in _menuBar.Menus) {
  409. if (m != null) {
  410. _menuBar.Menus [i] = m;
  411. i++;
  412. }
  413. }
  414. var menus = _menuBar.Menus;
  415. Array.Resize (ref menus, menus.Length - 1);
  416. _menuBar.Menus = menus;
  417. if (_currentSelectedMenuBar - 1 >= 0 && _menuBar.Menus.Length > 0) {
  418. _currentSelectedMenuBar--;
  419. }
  420. _currentMenuBarItem = _menuBar.Menus?.Length > 0 ? _menuBar.Menus [_currentSelectedMenuBar] : null;
  421. }
  422. if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0) {
  423. Remove (_menuBar);
  424. _menuBar = null;
  425. DataContext.Menus = new List<DynamicMenuItemList> ();
  426. _currentMenuBarItem = null;
  427. _currentSelectedMenuBar = -1;
  428. _lblMenuBar.Text = ustring.Empty;
  429. } else {
  430. _lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
  431. }
  432. SetListViewSource (_currentMenuBarItem, true);
  433. SetFrameDetails (null);
  434. };
  435. SetFrameDetails ();
  436. var ustringConverter = new UStringValueConverter ();
  437. var listWrapperConverter = new ListWrapperConverter ();
  438. var lblMenuBar = new Binding (this, "MenuBar", _lblMenuBar, "Text", ustringConverter);
  439. var lblParent = new Binding (this, "Parent", _lblParent, "Text", ustringConverter);
  440. var lstMenus = new Binding (this, "Menus", _lstMenus, "Source", listWrapperConverter);
  441. void SetFrameDetails (MenuItem menuBarItem = null)
  442. {
  443. MenuItem menuItem;
  444. if (menuBarItem == null) {
  445. menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  446. } else {
  447. menuItem = menuBarItem;
  448. }
  449. _currentEditMenuBarItem = menuItem;
  450. _frmMenuDetails.EditMenuBarItem (menuItem);
  451. var f = _btnOk.CanFocus == _frmMenuDetails.CanFocus;
  452. if (!f) {
  453. _btnOk.CanFocus = _frmMenuDetails.CanFocus;
  454. _btnCancel.CanFocus = _frmMenuDetails.CanFocus;
  455. }
  456. }
  457. void SelectCurrentMenuBarItem ()
  458. {
  459. MenuBarItem menuBarItem = null;
  460. if (_menuBar?.Menus != null) {
  461. menuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  462. _lblMenuBar.Text = menuBarItem.Title;
  463. }
  464. SetFrameDetails (menuBarItem);
  465. _currentMenuBarItem = menuBarItem;
  466. DataContext.Menus = new List<DynamicMenuItemList> ();
  467. SetListViewSource (_currentMenuBarItem, true);
  468. _lblParent.Text = ustring.Empty;
  469. }
  470. void SetListViewSource (MenuItem _currentMenuBarItem, bool fill = false)
  471. {
  472. DataContext.Menus = new List<DynamicMenuItemList> ();
  473. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  474. if (menuBarItem != null && menuBarItem?.Children == null) {
  475. return;
  476. }
  477. if (!fill) {
  478. return;
  479. }
  480. if (menuBarItem != null) {
  481. foreach (var child in menuBarItem?.Children) {
  482. var m = new DynamicMenuItemList (child.Title, child);
  483. DataContext.Menus.Add (m);
  484. }
  485. }
  486. }
  487. MenuItem CreateNewMenu (DynamicMenuItem item, MenuItem parent = null)
  488. {
  489. MenuItem newMenu;
  490. if (item.hasSubMenu) {
  491. newMenu = new MenuBarItem (item.title, new MenuItem [] { }, parent);
  492. } else if (parent != null) {
  493. newMenu = new MenuItem (item.title, item.help, null, null, parent);
  494. newMenu.CheckType = item.checkStyle;
  495. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  496. newMenu.ShortCut = ShortCutHelper.GetShortCutFromTag (item.shortCut);
  497. } else if (item.isTopLevel) {
  498. newMenu = new MenuBarItem (item.title, item.help, null);
  499. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  500. } else {
  501. newMenu = new MenuBarItem (item.title, item.help, null);
  502. ((MenuBarItem)newMenu).Children [0].Action = _frmMenuDetails.CreateAction (newMenu, item);
  503. ((MenuBarItem)newMenu).Children [0].ShortCut = ShortCutHelper.GetShortCutFromTag (item.shortCut);
  504. }
  505. return newMenu;
  506. }
  507. void UpdateMenuItem (MenuItem _currentEditMenuBarItem, DynamicMenuItem menuItem, int index)
  508. {
  509. _currentEditMenuBarItem.Title = menuItem.title;
  510. _currentEditMenuBarItem.Help = menuItem.help;
  511. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  512. var parent = _currentEditMenuBarItem.Parent as MenuBarItem;
  513. if (parent != null && parent.Children.Length == 1 && _currentEditMenuBarItem.CheckType == MenuItemCheckStyle.Radio) {
  514. _currentEditMenuBarItem.Checked = true;
  515. }
  516. if (menuItem.isTopLevel && _currentEditMenuBarItem is MenuBarItem) {
  517. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  518. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  519. SetListViewSource (_currentEditMenuBarItem, true);
  520. } else if (menuItem.hasSubMenu) {
  521. _currentEditMenuBarItem.Action = null;
  522. if (_currentEditMenuBarItem is MenuBarItem && ((MenuBarItem)_currentEditMenuBarItem).Children == null) {
  523. ((MenuBarItem)_currentEditMenuBarItem).Children = new MenuItem [] { };
  524. } else if (_currentEditMenuBarItem.Parent != null) {
  525. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  526. } else {
  527. _currentEditMenuBarItem = new MenuBarItem (_currentEditMenuBarItem.Title, new MenuItem [] { }, _currentEditMenuBarItem.Parent);
  528. }
  529. SetListViewSource (_currentEditMenuBarItem, true);
  530. } else if (_currentEditMenuBarItem is MenuBarItem && _currentEditMenuBarItem.Parent != null) {
  531. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  532. _currentEditMenuBarItem = new MenuItem (menuItem.title, menuItem.help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem), null, _currentEditMenuBarItem.Parent);
  533. } else {
  534. if (_currentEditMenuBarItem is MenuBarItem) {
  535. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  536. DataContext.Menus = new List<DynamicMenuItemList> ();
  537. }
  538. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  539. _currentEditMenuBarItem.ShortCut = ShortCutHelper.GetShortCutFromTag (menuItem.shortCut);
  540. }
  541. if (_currentEditMenuBarItem.Parent == null) {
  542. DataContext.MenuBar = _currentEditMenuBarItem.Title;
  543. } else {
  544. if (DataContext.Menus.Count == 0) {
  545. DataContext.Menus.Add (new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem));
  546. }
  547. DataContext.Menus [index] = new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem);
  548. }
  549. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  550. SetFrameDetails (_currentEditMenuBarItem);
  551. }
  552. _frmMenuDetails.Initialized += (s, e) => _frmMenuDetails.CanFocus = false;
  553. }
  554. }
  555. public class DynamicMenuBarDetails : FrameView {
  556. public MenuItem _menuItem;
  557. public TextField _txtTitle;
  558. public TextField _txtHelp;
  559. public TextView _txtAction;
  560. public CheckBox _ckbIsTopLevel;
  561. public CheckBox _ckbSubMenu;
  562. public RadioGroup _rbChkStyle;
  563. public TextField _txtShortCut;
  564. bool hasParent;
  565. public DynamicMenuBarDetails (MenuItem menuItem = null, bool hasParent = false) : this (menuItem == null ? "Adding New Menu." : "Editing Menu.")
  566. {
  567. _menuItem = menuItem;
  568. this.hasParent = hasParent;
  569. }
  570. public DynamicMenuBarDetails (ustring title) : base (title)
  571. {
  572. var _lblTitle = new Label ("Title:") {
  573. Y = 1
  574. };
  575. Add (_lblTitle);
  576. _txtTitle = new TextField () {
  577. X = Pos.Right (_lblTitle) + 2,
  578. Y = Pos.Top (_lblTitle),
  579. Width = Dim.Fill ()
  580. };
  581. Add (_txtTitle);
  582. var _lblHelp = new Label ("Help:") {
  583. X = Pos.Left (_lblTitle),
  584. Y = Pos.Bottom (_lblTitle) + 1
  585. };
  586. Add (_lblHelp);
  587. _txtHelp = new TextField () {
  588. X = Pos.Left (_txtTitle),
  589. Y = Pos.Top (_lblHelp),
  590. Width = Dim.Fill ()
  591. };
  592. Add (_txtHelp);
  593. var _lblAction = new Label ("Action:") {
  594. X = Pos.Left (_lblTitle),
  595. Y = Pos.Bottom (_lblHelp) + 1
  596. };
  597. Add (_lblAction);
  598. _txtAction = new TextView () {
  599. ColorScheme = Colors.Dialog,
  600. X = Pos.Left (_txtTitle),
  601. Y = Pos.Top (_lblAction),
  602. Width = Dim.Fill (),
  603. Height = 5
  604. };
  605. Add (_txtAction);
  606. _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
  607. X = Pos.Left (_lblTitle),
  608. Y = Pos.Bottom (_lblAction) + 5
  609. };
  610. Add (_ckbIsTopLevel);
  611. _ckbSubMenu = new CheckBox ("Has sub-menus") {
  612. X = Pos.Left (_lblTitle),
  613. Y = Pos.Bottom (_ckbIsTopLevel),
  614. Checked = _menuItem == null ? !hasParent : HasSubMenus (_menuItem)
  615. };
  616. Add (_ckbSubMenu);
  617. var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" };
  618. _rbChkStyle = new RadioGroup (_rChkLabels) {
  619. X = Pos.Left (_lblTitle),
  620. Y = Pos.Bottom (_ckbSubMenu) + 1,
  621. };
  622. Add (_rbChkStyle);
  623. var _lblShortCut = new Label ("ShortCut:") {
  624. X = Pos.Right (_ckbSubMenu) + 10,
  625. Y = Pos.Top (_ckbSubMenu)
  626. };
  627. Add (_lblShortCut);
  628. _txtShortCut = new TextField () {
  629. X = Pos.X (_lblShortCut),
  630. Y = Pos.Bottom (_lblShortCut),
  631. Width = Dim.Fill (),
  632. ReadOnly = true
  633. };
  634. _txtShortCut.KeyDown += (e) => {
  635. if (!ProcessKey (e.KeyEvent)) {
  636. return;
  637. }
  638. var k = GetModifiersKey (e.KeyEvent);
  639. if (CheckShortCut (k, true)) {
  640. e.Handled = true;
  641. }
  642. };
  643. bool ProcessKey (KeyEvent ev)
  644. {
  645. switch (ev.Key) {
  646. case Key.CursorUp:
  647. case Key.CursorDown:
  648. case Key.Tab:
  649. case Key.BackTab:
  650. return false;
  651. }
  652. return true;
  653. }
  654. bool CheckShortCut (Key k, bool pre)
  655. {
  656. var m = _menuItem != null ? _menuItem : new MenuItem ();
  657. if (pre && !ShortCutHelper.PreShortCutValidation (k)) {
  658. _txtShortCut.Text = "";
  659. return false;
  660. }
  661. if (!pre) {
  662. if (!ShortCutHelper.PostShortCutValidation (ShortCutHelper.GetShortCutFromTag (_txtShortCut.Text))) {
  663. _txtShortCut.Text = "";
  664. return false;
  665. }
  666. return true;
  667. }
  668. _txtShortCut.Text = ShortCutHelper.GetShortCutTag (k);
  669. return true;
  670. }
  671. _txtShortCut.KeyUp += (e) => {
  672. var k = GetModifiersKey (e.KeyEvent);
  673. if (CheckShortCut (k, false)) {
  674. e.Handled = true;
  675. }
  676. };
  677. Add (_txtShortCut);
  678. var _btnShortCut = new Button ("Clear ShortCut") {
  679. X = Pos.X (_lblShortCut),
  680. Y = Pos.Bottom (_txtShortCut) + 1
  681. };
  682. _btnShortCut.Clicked += () => {
  683. _txtShortCut.Text = "";
  684. };
  685. Add (_btnShortCut);
  686. _ckbIsTopLevel.Toggled += (e) => {
  687. if ((_menuItem != null && _menuItem.Parent != null && _ckbIsTopLevel.Checked) ||
  688. _menuItem == null && hasParent && _ckbIsTopLevel.Checked) {
  689. MessageBox.ErrorQuery ("Invalid IsTopLevel", "Only menu bar can have top level menu item!", "Ok");
  690. _ckbIsTopLevel.Checked = false;
  691. return;
  692. }
  693. if (_ckbIsTopLevel.Checked) {
  694. _ckbSubMenu.Checked = false;
  695. _ckbSubMenu.SetNeedsDisplay ();
  696. _txtHelp.CanFocus = true;
  697. _txtAction.CanFocus = true;
  698. _txtShortCut.CanFocus = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  699. } else {
  700. if (_menuItem == null && !hasParent || _menuItem.Parent == null) {
  701. _ckbSubMenu.Checked = true;
  702. _ckbSubMenu.SetNeedsDisplay ();
  703. _txtShortCut.CanFocus = false;
  704. }
  705. _txtHelp.Text = "";
  706. _txtHelp.CanFocus = false;
  707. _txtAction.Text = "";
  708. _txtAction.CanFocus = false;
  709. }
  710. };
  711. _ckbSubMenu.Toggled += (e) => {
  712. if (_ckbSubMenu.Checked) {
  713. _ckbIsTopLevel.Checked = false;
  714. _ckbIsTopLevel.SetNeedsDisplay ();
  715. _txtHelp.Text = "";
  716. _txtHelp.CanFocus = false;
  717. _txtAction.Text = "";
  718. _txtAction.CanFocus = false;
  719. _txtShortCut.Text = "";
  720. _txtShortCut.CanFocus = false;
  721. } else {
  722. if (!hasParent) {
  723. _ckbIsTopLevel.Checked = true;
  724. _ckbIsTopLevel.SetNeedsDisplay ();
  725. _txtShortCut.CanFocus = false;
  726. }
  727. _txtHelp.CanFocus = true;
  728. _txtAction.CanFocus = true;
  729. _txtShortCut.CanFocus = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  730. }
  731. };
  732. //Add (_frmMenuDetails);
  733. }
  734. public DynamicMenuItem EnterMenuItem ()
  735. {
  736. var valid = false;
  737. if (_menuItem == null) {
  738. var m = new DynamicMenuItem ();
  739. _txtTitle.Text = m.title;
  740. _txtHelp.Text = m.help;
  741. _txtAction.Text = m.action;
  742. _ckbIsTopLevel.Checked = false;
  743. _ckbSubMenu.Checked = !hasParent;
  744. _txtHelp.CanFocus = hasParent;
  745. _txtAction.CanFocus = hasParent;
  746. _txtShortCut.CanFocus = hasParent;
  747. } else {
  748. EditMenuBarItem (_menuItem);
  749. }
  750. var _btnOk = new Button ("Ok") {
  751. IsDefault = true,
  752. };
  753. _btnOk.Clicked += () => {
  754. if (ustring.IsNullOrEmpty (_txtTitle.Text)) {
  755. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  756. } else {
  757. valid = true;
  758. Application.RequestStop ();
  759. }
  760. };
  761. var _btnCancel = new Button ("Cancel");
  762. _btnCancel.Clicked += () => {
  763. _txtTitle.Text = ustring.Empty;
  764. Application.RequestStop ();
  765. };
  766. var _dialog = new Dialog ("Please enter the menu details.", _btnOk, _btnCancel);
  767. Width = Dim.Fill ();
  768. Height = Dim.Fill () - 1;
  769. _dialog.Add (this);
  770. _txtTitle.SetFocus ();
  771. _txtTitle.CursorPosition = _txtTitle.Text.Length;
  772. Application.Run (_dialog);
  773. if (valid) {
  774. return new DynamicMenuItem (_txtTitle.Text, _txtHelp.Text, _txtAction.Text,
  775. _ckbIsTopLevel != null ? _ckbIsTopLevel.Checked : false,
  776. _ckbSubMenu != null ? _ckbSubMenu.Checked : false,
  777. _rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  778. _rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked : MenuItemCheckStyle.Radio,
  779. _txtShortCut.Text);
  780. } else {
  781. return null;
  782. }
  783. }
  784. public void EditMenuBarItem (MenuItem menuItem)
  785. {
  786. if (menuItem == null) {
  787. hasParent = false;
  788. CanFocus = false;
  789. CleanEditMenuBarItem ();
  790. return;
  791. } else {
  792. hasParent = menuItem.Parent != null;
  793. CanFocus = true;
  794. }
  795. _menuItem = menuItem;
  796. _txtTitle.Text = menuItem?.Title ?? "";
  797. _txtHelp.Text = menuItem?.Help ?? "";
  798. _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : ustring.Empty;
  799. _ckbIsTopLevel.Checked = IsTopLevel (menuItem);
  800. _ckbSubMenu.Checked = HasSubMenus (menuItem);
  801. _txtHelp.CanFocus = !_ckbSubMenu.Checked;
  802. _txtAction.CanFocus = !_ckbSubMenu.Checked;
  803. _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
  804. _txtShortCut.Text = menuItem?.ShortCutTag ?? "";
  805. _txtShortCut.CanFocus = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  806. }
  807. void CleanEditMenuBarItem ()
  808. {
  809. _txtTitle.Text = "";
  810. _txtHelp.Text = "";
  811. _txtAction.Text = "";
  812. _ckbIsTopLevel.Checked = false;
  813. _ckbSubMenu.Checked = false;
  814. _rbChkStyle.SelectedItem = (int)MenuItemCheckStyle.NoCheck;
  815. _txtShortCut.Text = "";
  816. }
  817. ustring GetTargetAction (Action action)
  818. {
  819. var me = action.Target;
  820. if (me == null) {
  821. throw new ArgumentException ();
  822. }
  823. object v = new object ();
  824. foreach (var field in me.GetType ().GetFields ()) {
  825. if (field.Name == "item") {
  826. v = field.GetValue (me);
  827. }
  828. }
  829. return v == null || !(v is DynamicMenuItem item) ? ustring.Empty : item.action;
  830. }
  831. bool IsTopLevel (MenuItem menuItem)
  832. {
  833. var topLevel = menuItem as MenuBarItem;
  834. if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0) && topLevel.Action != null) {
  835. return true;
  836. } else {
  837. return false;
  838. }
  839. }
  840. bool HasSubMenus (MenuItem menuItem)
  841. {
  842. var menuBarItem = menuItem as MenuBarItem;
  843. if (menuBarItem != null && menuBarItem.Children != null && (menuBarItem.Children.Length > 0 || menuBarItem.Action == null)) {
  844. return true;
  845. } else {
  846. return false;
  847. }
  848. }
  849. public Action CreateAction (MenuItem menuItem, DynamicMenuItem item)
  850. {
  851. switch (menuItem.CheckType) {
  852. case MenuItemCheckStyle.NoCheck:
  853. return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
  854. case MenuItemCheckStyle.Checked:
  855. return new Action (() => menuItem.Checked = !menuItem.Checked);
  856. case MenuItemCheckStyle.Radio:
  857. break;
  858. }
  859. return new Action (() => {
  860. menuItem.Checked = true;
  861. var parent = menuItem?.Parent as MenuBarItem;
  862. if (parent != null) {
  863. var childrens = parent.Children;
  864. for (int i = 0; i < childrens.Length; i++) {
  865. var child = childrens [i];
  866. if (child != menuItem) {
  867. child.Checked = false;
  868. }
  869. }
  870. }
  871. });
  872. }
  873. public void UpdateParent (ref MenuItem menuItem)
  874. {
  875. var parent = menuItem.Parent as MenuBarItem;
  876. var idx = parent.GetChildrenIndex (menuItem);
  877. if (!(menuItem is MenuBarItem)) {
  878. menuItem = new MenuBarItem (menuItem.Title, new MenuItem [] { }, menuItem.Parent);
  879. if (idx > -1) {
  880. parent.Children [idx] = menuItem;
  881. }
  882. } else {
  883. menuItem = new MenuItem (menuItem.Title, menuItem.Help, CreateAction (menuItem, new DynamicMenuItem ()), null, menuItem.Parent);
  884. if (idx > -1) {
  885. parent.Children [idx] = menuItem;
  886. }
  887. }
  888. }
  889. }
  890. public class DynamicMenuItemModel : INotifyPropertyChanged {
  891. public event PropertyChangedEventHandler PropertyChanged;
  892. private ustring menuBar;
  893. private ustring parent;
  894. private List<DynamicMenuItemList> menus;
  895. public ustring MenuBar {
  896. get => menuBar;
  897. set {
  898. if (value != menuBar) {
  899. menuBar = value;
  900. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  901. }
  902. }
  903. }
  904. public ustring Parent {
  905. get => parent;
  906. set {
  907. if (value != parent) {
  908. parent = value;
  909. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  910. }
  911. }
  912. }
  913. public List<DynamicMenuItemList> Menus {
  914. get => menus;
  915. set {
  916. if (value != menus) {
  917. menus = value;
  918. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  919. }
  920. }
  921. }
  922. public DynamicMenuItemModel ()
  923. {
  924. Menus = new List<DynamicMenuItemList> ();
  925. }
  926. public string GetPropertyName ([CallerMemberName] string propertyName = null)
  927. {
  928. return propertyName;
  929. }
  930. }
  931. public interface IValueConverter {
  932. object Convert (object value, object parameter = null);
  933. }
  934. public class Binding {
  935. public View Target { get; private set; }
  936. public View Source { get; private set; }
  937. public string SourcePropertyName { get; private set; }
  938. public string TargetPropertyName { get; private set; }
  939. private object sourceDataContext;
  940. private PropertyInfo sourceBindingProperty;
  941. private IValueConverter valueConverter;
  942. public Binding (View source, string sourcePropertyName, View target, string targetPropertyName, IValueConverter valueConverter = null)
  943. {
  944. Target = target;
  945. Source = source;
  946. SourcePropertyName = sourcePropertyName;
  947. TargetPropertyName = targetPropertyName;
  948. sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
  949. sourceBindingProperty = sourceDataContext.GetType ().GetProperty (SourcePropertyName);
  950. this.valueConverter = valueConverter;
  951. UpdateTarget ();
  952. var notifier = ((INotifyPropertyChanged)sourceDataContext);
  953. if (notifier != null) {
  954. notifier.PropertyChanged += (s, e) => {
  955. if (e.PropertyName == SourcePropertyName) {
  956. UpdateTarget ();
  957. }
  958. };
  959. }
  960. }
  961. private void UpdateTarget ()
  962. {
  963. try {
  964. var sourceValue = sourceBindingProperty.GetValue (sourceDataContext);
  965. if (sourceValue == null) {
  966. return;
  967. }
  968. var finalValue = valueConverter?.Convert (sourceValue) ?? sourceValue;
  969. var targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
  970. targetProperty.SetValue (Target, finalValue);
  971. } catch (Exception ex) {
  972. MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
  973. }
  974. }
  975. }
  976. public class ListWrapperConverter : IValueConverter {
  977. public object Convert (object value, object parameter = null)
  978. {
  979. return new ListWrapper ((IList)value);
  980. }
  981. }
  982. public class UStringValueConverter : IValueConverter {
  983. public object Convert (object value, object parameter = null)
  984. {
  985. var data = Encoding.ASCII.GetBytes (value.ToString ());
  986. return ustring.Make (data);
  987. }
  988. }
  989. }