DynamicMenuBar.cs 30 KB

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