DynamicMenuBar.cs 29 KB

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