DynamicMenuBar.cs 61 KB

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