1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120 |
- using NStack;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Text;
- using Terminal.Gui;
- namespace UICatalog {
- [ScenarioMetadata (Name: "Dynamic MenuBar", Description: "Demonstrates how to add and remove a MenuBar, Menus and change titles dynamically.")]
- [ScenarioCategory ("Dynamic")]
- class DynamicMenuBar : Scenario {
- public override void Run ()
- {
- Top.Add (new DynamicMenuBarSample (Win.Title));
- base.Run ();
- }
- }
- public class DynamicMenuItemList {
- public ustring Title { get; set; }
- public MenuItem MenuItem { get; set; }
- public DynamicMenuItemList () { }
- public DynamicMenuItemList (ustring title, MenuItem menuItem)
- {
- Title = title;
- MenuItem = menuItem;
- }
- public override string ToString () => $"{Title}, {MenuItem}";
- }
- public class DynamicMenuItem {
- public ustring title = "_New";
- public ustring help = "";
- public ustring action = "";
- public bool isTopLevel;
- public bool hasSubMenu;
- public MenuItemCheckStyle checkStyle;
- public ustring shortCut;
- public DynamicMenuItem () { }
- public DynamicMenuItem (ustring title, bool hasSubMenu = false)
- {
- this.title = title;
- this.hasSubMenu = hasSubMenu;
- }
- public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, ustring shortCut = null)
- {
- this.title = title;
- this.help = help;
- this.action = action;
- this.isTopLevel = isTopLevel;
- this.hasSubMenu = hasSubMenu;
- this.checkStyle = checkStyle;
- this.shortCut = shortCut;
- }
- }
- public class DynamicMenuBarSample : Window {
- MenuBar _menuBar;
- MenuItem _currentMenuBarItem;
- int _currentSelectedMenuBar;
- MenuItem _currentEditMenuBarItem;
- ListView _lstMenus;
- public DynamicMenuItemModel DataContext { get; set; }
- public DynamicMenuBarSample (ustring title) : base (title)
- {
- DataContext = new DynamicMenuItemModel ();
- var _frmDelimiter = new FrameView ("ShortCut Delimiter:") {
- X = Pos.Center (),
- Y = 3,
- Width = 25,
- Height = 4
- };
- var _txtDelimiter = new TextField (MenuBar.ShortCutDelimiter.ToString ()) {
- X = Pos.Center(),
- Width = 2,
- };
- _txtDelimiter.TextChanged += (_) => MenuBar.ShortCutDelimiter = _txtDelimiter.Text;
- _frmDelimiter.Add (_txtDelimiter);
- Add (_frmDelimiter);
- var _frmMenu = new FrameView ("Menus:") {
- Y = 7,
- Width = Dim.Percent (50),
- Height = Dim.Fill ()
- };
- var _btnAddMenuBar = new Button ("Add a MenuBar") {
- Y = 1,
- };
- _frmMenu.Add (_btnAddMenuBar);
- var _btnMenuBarUp = new Button ("^") {
- X = Pos.Center ()
- };
- _frmMenu.Add (_btnMenuBarUp);
- var _btnMenuBarDown = new Button ("v") {
- X = Pos.Center (),
- Y = Pos.Bottom (_btnMenuBarUp)
- };
- _frmMenu.Add (_btnMenuBarDown);
- var _btnRemoveMenuBar = new Button ("Remove a MenuBar") {
- Y = 1
- };
- _btnRemoveMenuBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveMenuBar) - Pos.Left (_btnRemoveMenuBar));
- _frmMenu.Add (_btnRemoveMenuBar);
- var _btnPrevious = new Button ("<") {
- X = Pos.Left (_btnAddMenuBar),
- Y = Pos.Top (_btnAddMenuBar) + 2
- };
- _frmMenu.Add (_btnPrevious);
- var _btnAdd = new Button (" Add ") {
- Y = Pos.Top (_btnPrevious) + 2,
- };
- _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
- _frmMenu.Add (_btnAdd);
- var _btnNext = new Button (">") {
- X = Pos.X (_btnAdd),
- Y = Pos.Top (_btnPrevious),
- };
- _frmMenu.Add (_btnNext);
- var _lblMenuBar = new Label () {
- ColorScheme = Colors.Dialog,
- TextAlignment = TextAlignment.Centered,
- X = Pos.Right (_btnPrevious) + 1,
- Y = Pos.Top (_btnPrevious),
- Width = Dim.Fill () - Dim.Width (_btnAdd) - 1,
- Height = 1
- };
- _frmMenu.Add (_lblMenuBar);
- _lblMenuBar.WantMousePositionReports = true;
- _lblMenuBar.CanFocus = true;
- var _lblParent = new Label () {
- TextAlignment = TextAlignment.Centered,
- X = Pos.Right (_btnPrevious) + 1,
- Y = Pos.Top (_btnPrevious) + 1,
- Width = Dim.Fill () - Dim.Width (_btnAdd) - 1
- };
- _frmMenu.Add (_lblParent);
- var _btnPreviowsParent = new Button ("..") {
- X = Pos.Left (_btnAddMenuBar),
- Y = Pos.Top (_btnPrevious) + 1
- };
- _frmMenu.Add (_btnPreviowsParent);
- _lstMenus = new ListView (new List<DynamicMenuItemList> ()) {
- ColorScheme = Colors.Dialog,
- X = Pos.Right (_btnPrevious) + 1,
- Y = Pos.Top (_btnPrevious) + 2,
- Width = _lblMenuBar.Width,
- Height = Dim.Fill (),
- };
- _frmMenu.Add (_lstMenus);
- _lblMenuBar.TabIndex = _btnPrevious.TabIndex + 1;
- _lstMenus.TabIndex = _lblMenuBar.TabIndex + 1;
- _btnNext.TabIndex = _lstMenus.TabIndex + 1;
- _btnAdd.TabIndex = _btnNext.TabIndex + 1;
- var _btnRemove = new Button ("Remove") {
- X = Pos.Left (_btnAdd),
- Y = Pos.Top (_btnAdd) + 1
- };
- _frmMenu.Add (_btnRemove);
- var _btnUp = new Button ("^") {
- X = Pos.Right (_lstMenus) + 2,
- Y = Pos.Top (_btnRemove) + 2
- };
- _frmMenu.Add (_btnUp);
- var _btnDown = new Button ("v") {
- X = Pos.Right (_lstMenus) + 2,
- Y = Pos.Top (_btnUp) + 1
- };
- _frmMenu.Add (_btnDown);
- Add (_frmMenu);
- var _frmMenuDetails = new DynamicMenuBarDetails ("Menu Details:") {
- X = Pos.Right (_frmMenu),
- Y = Pos.Top (_frmMenu),
- Width = Dim.Fill (),
- Height = Dim.Fill (2)
- };
- Add (_frmMenuDetails);
- _btnMenuBarUp.Clicked += () => {
- var i = _currentSelectedMenuBar;
- var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
- if (menuItem != null) {
- var menus = _menuBar.Menus;
- if (i > 0) {
- menus [i] = menus [i - 1];
- menus [i - 1] = menuItem;
- _currentSelectedMenuBar = i - 1;
- _menuBar.SetNeedsDisplay ();
- }
- }
- };
- _btnMenuBarDown.Clicked += () => {
- var i = _currentSelectedMenuBar;
- var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
- if (menuItem != null) {
- var menus = _menuBar.Menus;
- if (i < menus.Length - 1) {
- menus [i] = menus [i + 1];
- menus [i + 1] = menuItem;
- _currentSelectedMenuBar = i + 1;
- _menuBar.SetNeedsDisplay ();
- }
- }
- };
- _btnUp.Clicked += () => {
- var i = _lstMenus.SelectedItem;
- var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
- if (menuItem != null) {
- var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
- if (i > 0) {
- childrens [i] = childrens [i - 1];
- childrens [i - 1] = menuItem;
- DataContext.Menus [i] = DataContext.Menus [i - 1];
- DataContext.Menus [i - 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
- _lstMenus.SelectedItem = i - 1;
- }
- }
- };
- _btnDown.Clicked += () => {
- var i = _lstMenus.SelectedItem;
- var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
- if (menuItem != null) {
- var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
- if (i < childrens.Length - 1) {
- childrens [i] = childrens [i + 1];
- childrens [i + 1] = menuItem;
- DataContext.Menus [i] = DataContext.Menus [i + 1];
- DataContext.Menus [i + 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
- _lstMenus.SelectedItem = i + 1;
- }
- }
- };
- _btnPreviowsParent.Clicked += () => {
- if (_currentMenuBarItem != null && _currentMenuBarItem.Parent != null) {
- var mi = _currentMenuBarItem;
- _currentMenuBarItem = _currentMenuBarItem.Parent as MenuBarItem;
- SetListViewSource (_currentMenuBarItem, true);
- var i = ((MenuBarItem)_currentMenuBarItem).GetChildrenIndex (mi);
- if (i > -1) {
- _lstMenus.SelectedItem = i;
- }
- if (_currentMenuBarItem.Parent != null) {
- DataContext.Parent = _currentMenuBarItem.Title;
- } else {
- DataContext.Parent = ustring.Empty;
- }
- } else {
- DataContext.Parent = ustring.Empty;
- }
- };
- var _btnOk = new Button ("Ok") {
- X = Pos.Right (_frmMenu) + 20,
- Y = Pos.Bottom (_frmMenuDetails),
- };
- Add (_btnOk);
- var _btnCancel = new Button ("Cancel") {
- X = Pos.Right (_btnOk) + 3,
- Y = Pos.Top (_btnOk),
- };
- _btnCancel.Clicked += () => {
- SetFrameDetails (_currentEditMenuBarItem);
- };
- Add (_btnCancel);
- _lstMenus.SelectedItemChanged += (e) => {
- SetFrameDetails ();
- };
- _btnOk.Clicked += () => {
- if (ustring.IsNullOrEmpty (_frmMenuDetails._txtTitle.Text) && _currentEditMenuBarItem != null) {
- MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
- } else if (_currentEditMenuBarItem != null) {
- var menuItem = new DynamicMenuItem (_frmMenuDetails._txtTitle.Text, _frmMenuDetails._txtHelp.Text,
- _frmMenuDetails._txtAction.Text,
- _frmMenuDetails._ckbIsTopLevel != null ? _frmMenuDetails._ckbIsTopLevel.Checked : false,
- _frmMenuDetails._ckbSubMenu != null ? _frmMenuDetails._ckbSubMenu.Checked : false,
- _frmMenuDetails._rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
- _frmMenuDetails._rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked :
- MenuItemCheckStyle.Radio,
- _frmMenuDetails._txtShortCut.Text);
- UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
- }
- };
- _btnAdd.Clicked += () => {
- if (MenuBar == null) {
- MessageBox.ErrorQuery ("Menu Bar Error", "Must add a MenuBar first!", "Ok");
- _btnAddMenuBar.SetFocus ();
- return;
- }
- var frameDetails = new DynamicMenuBarDetails (null, _currentMenuBarItem != null);
- var item = frameDetails.EnterMenuItem ();
- if (item == null) {
- return;
- }
- if (!(_currentMenuBarItem is MenuBarItem)) {
- var parent = _currentMenuBarItem.Parent as MenuBarItem;
- var idx = parent.GetChildrenIndex (_currentMenuBarItem);
- _currentMenuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { }, _currentMenuBarItem.Parent);
- _currentMenuBarItem.CheckType = item.checkStyle;
- parent.Children [idx] = _currentMenuBarItem;
- } else {
- MenuItem newMenu = CreateNewMenu (item, _currentMenuBarItem);
- var menuBarItem = _currentMenuBarItem as MenuBarItem;
- if (menuBarItem == null) {
- menuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { newMenu }, _currentMenuBarItem.Parent);
- } else if (menuBarItem.Children == null) {
- menuBarItem.Children = new MenuItem [] { newMenu };
- } else {
- var childrens = menuBarItem.Children;
- Array.Resize (ref childrens, childrens.Length + 1);
- childrens [childrens.Length - 1] = newMenu;
- menuBarItem.Children = childrens;
- }
- DataContext.Menus.Add (new DynamicMenuItemList (newMenu.Title, newMenu));
- _lstMenus.MoveDown ();
- }
- };
- _btnRemove.Clicked += () => {
- var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
- if (menuItem != null) {
- var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
- childrens [_lstMenus.SelectedItem] = null;
- int i = 0;
- foreach (var c in childrens) {
- if (c != null) {
- childrens [i] = c;
- i++;
- }
- }
- Array.Resize (ref childrens, childrens.Length - 1);
- if (childrens.Length == 0) {
- if (_currentMenuBarItem.Parent == null) {
- ((MenuBarItem)_currentMenuBarItem).Children = null;
- //_currentMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentMenuBarItem.Title));
- } else {
- _currentMenuBarItem = new MenuItem (_currentMenuBarItem.Title, _currentMenuBarItem.Help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentEditMenuBarItem.Title)), null, _currentMenuBarItem.Parent);
- }
- } else {
- ((MenuBarItem)_currentMenuBarItem).Children = childrens;
- }
- DataContext.Menus.RemoveAt (_lstMenus.SelectedItem);
- if (_lstMenus.Source.Count > 0 && _lstMenus.SelectedItem > _lstMenus.Source.Count - 1) {
- _lstMenus.SelectedItem = _lstMenus.Source.Count - 1;
- }
- _lstMenus.SetNeedsDisplay ();
- SetFrameDetails ();
- }
- };
- _lstMenus.OpenSelectedItem += (e) => {
- _currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
- if (!(_currentMenuBarItem is MenuBarItem)) {
- MessageBox.ErrorQuery ("Menu Open Error", "Must allows sub menus first!", "Ok");
- return;
- }
- DataContext.Parent = _currentMenuBarItem.Title;
- DataContext.Menus = new List<DynamicMenuItemList> ();
- SetListViewSource (_currentMenuBarItem, true);
- var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [0].MenuItem : null;
- SetFrameDetails (menuBarItem);
- };
- _lstMenus.Enter += (_) => {
- var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
- SetFrameDetails (menuBarItem);
- };
- _btnNext.Clicked += () => {
- if (_menuBar != null && _currentSelectedMenuBar + 1 < _menuBar.Menus.Length) {
- _currentSelectedMenuBar++;
- }
- SelectCurrentMenuBarItem ();
- };
- _btnPrevious.Clicked += () => {
- if (_currentSelectedMenuBar - 1 > -1) {
- _currentSelectedMenuBar--;
- }
- SelectCurrentMenuBarItem ();
- };
- _lblMenuBar.Enter += (e) => {
- if (_menuBar?.Menus != null) {
- _currentMenuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
- SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
- }
- };
- _btnAddMenuBar.Clicked += () => {
- var frameDetails = new DynamicMenuBarDetails (null, false);
- var item = frameDetails.EnterMenuItem ();
- if (item == null) {
- return;
- }
- if (MenuBar == null) {
- _menuBar = new MenuBar ();
- Add (_menuBar);
- }
- var newMenu = CreateNewMenu (item) as MenuBarItem;
- var menus = _menuBar.Menus;
- Array.Resize (ref menus, menus.Length + 1);
- menus [^1] = newMenu;
- _menuBar.Menus = menus;
- _currentMenuBarItem = newMenu;
- _currentMenuBarItem.CheckType = item.checkStyle;
- _currentSelectedMenuBar = menus.Length - 1;
- _menuBar.Menus [_currentSelectedMenuBar] = newMenu;
- _lblMenuBar.Text = newMenu.Title;
- SetListViewSource (_currentMenuBarItem, true);
- SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
- _menuBar.SetNeedsDisplay ();
- };
- _btnRemoveMenuBar.Clicked += () => {
- if (_menuBar == null || _menuBar.Menus.Length == 0) {
- return;
- }
- if (_menuBar != null && _menuBar.Menus.Length > 0) {
- _menuBar.Menus [_currentSelectedMenuBar] = null;
- int i = 0;
- foreach (var m in _menuBar.Menus) {
- if (m != null) {
- _menuBar.Menus [i] = m;
- i++;
- }
- }
- var menus = _menuBar.Menus;
- Array.Resize (ref menus, menus.Length - 1);
- _menuBar.Menus = menus;
- if (_currentSelectedMenuBar - 1 >= 0 && _menuBar.Menus.Length > 0) {
- _currentSelectedMenuBar--;
- }
- _currentMenuBarItem = _menuBar.Menus?.Length > 0 ? _menuBar.Menus [_currentSelectedMenuBar] : null;
- }
- if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0) {
- Remove (_menuBar);
- _menuBar = null;
- DataContext.Menus = new List<DynamicMenuItemList> ();
- _currentMenuBarItem = null;
- _currentSelectedMenuBar = -1;
- _lblMenuBar.Text = ustring.Empty;
- } else {
- _lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
- }
- SetListViewSource (_currentMenuBarItem, true);
- SetFrameDetails (null);
- };
- SetFrameDetails ();
- var ustringConverter = new UStringValueConverter ();
- var listWrapperConverter = new ListWrapperConverter ();
- var lblMenuBar = new Binding (this, "MenuBar", _lblMenuBar, "Text", ustringConverter);
- var lblParent = new Binding (this, "Parent", _lblParent, "Text", ustringConverter);
- var lstMenus = new Binding (this, "Menus", _lstMenus, "Source", listWrapperConverter);
- void SetFrameDetails (MenuItem menuBarItem = null)
- {
- MenuItem menuItem;
- if (menuBarItem == null) {
- menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
- } else {
- menuItem = menuBarItem;
- }
- _currentEditMenuBarItem = menuItem;
- _frmMenuDetails.EditMenuBarItem (menuItem);
- var f = _btnOk.CanFocus == _frmMenuDetails.CanFocus;
- if (!f) {
- _btnOk.CanFocus = _frmMenuDetails.CanFocus;
- _btnCancel.CanFocus = _frmMenuDetails.CanFocus;
- }
- }
- void SelectCurrentMenuBarItem ()
- {
- MenuBarItem menuBarItem = null;
- if (_menuBar?.Menus != null) {
- menuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
- _lblMenuBar.Text = menuBarItem.Title;
- }
- SetFrameDetails (menuBarItem);
- _currentMenuBarItem = menuBarItem;
- DataContext.Menus = new List<DynamicMenuItemList> ();
- SetListViewSource (_currentMenuBarItem, true);
- _lblParent.Text = ustring.Empty;
- }
- void SetListViewSource (MenuItem _currentMenuBarItem, bool fill = false)
- {
- DataContext.Menus = new List<DynamicMenuItemList> ();
- var menuBarItem = _currentMenuBarItem as MenuBarItem;
- if (menuBarItem != null && menuBarItem?.Children == null) {
- return;
- }
- if (!fill) {
- return;
- }
- if (menuBarItem != null) {
- foreach (var child in menuBarItem?.Children) {
- var m = new DynamicMenuItemList (child.Title, child);
- DataContext.Menus.Add (m);
- }
- }
- }
- MenuItem CreateNewMenu (DynamicMenuItem item, MenuItem parent = null)
- {
- MenuItem newMenu;
- if (item.hasSubMenu) {
- newMenu = new MenuBarItem (item.title, new MenuItem [] { }, parent);
- } else if (parent != null) {
- newMenu = new MenuItem (item.title, item.help, null, null, parent);
- newMenu.CheckType = item.checkStyle;
- newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
- newMenu.ShortCut = ShortCutHelper.GetShortCutFromTag (item.shortCut);
- } else if (item.isTopLevel) {
- newMenu = new MenuBarItem (item.title, item.help, null);
- newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
- } else {
- newMenu = new MenuBarItem (item.title, item.help, null);
- ((MenuBarItem)newMenu).Children [0].Action = _frmMenuDetails.CreateAction (newMenu, item);
- ((MenuBarItem)newMenu).Children [0].ShortCut = ShortCutHelper.GetShortCutFromTag (item.shortCut);
- }
- return newMenu;
- }
- void UpdateMenuItem (MenuItem _currentEditMenuBarItem, DynamicMenuItem menuItem, int index)
- {
- _currentEditMenuBarItem.Title = menuItem.title;
- _currentEditMenuBarItem.Help = menuItem.help;
- _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
- var parent = _currentEditMenuBarItem.Parent as MenuBarItem;
- if (parent != null && parent.Children.Length == 1 && _currentEditMenuBarItem.CheckType == MenuItemCheckStyle.Radio) {
- _currentEditMenuBarItem.Checked = true;
- }
- if (menuItem.isTopLevel && _currentEditMenuBarItem is MenuBarItem) {
- ((MenuBarItem)_currentEditMenuBarItem).Children = null;
- _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
- SetListViewSource (_currentEditMenuBarItem, true);
- } else if (menuItem.hasSubMenu) {
- _currentEditMenuBarItem.Action = null;
- if (_currentEditMenuBarItem is MenuBarItem && ((MenuBarItem)_currentEditMenuBarItem).Children == null) {
- ((MenuBarItem)_currentEditMenuBarItem).Children = new MenuItem [] { };
- } else if (_currentEditMenuBarItem.Parent != null) {
- _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
- } else {
- _currentEditMenuBarItem = new MenuBarItem (_currentEditMenuBarItem.Title, new MenuItem [] { }, _currentEditMenuBarItem.Parent);
- }
- SetListViewSource (_currentEditMenuBarItem, true);
- } else if (_currentEditMenuBarItem is MenuBarItem && _currentEditMenuBarItem.Parent != null) {
- _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
- _currentEditMenuBarItem = new MenuItem (menuItem.title, menuItem.help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem), null, _currentEditMenuBarItem.Parent);
- } else {
- if (_currentEditMenuBarItem is MenuBarItem) {
- ((MenuBarItem)_currentEditMenuBarItem).Children = null;
- DataContext.Menus = new List<DynamicMenuItemList> ();
- }
- _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
- _currentEditMenuBarItem.ShortCut = ShortCutHelper.GetShortCutFromTag (menuItem.shortCut);
- }
- if (_currentEditMenuBarItem.Parent == null) {
- DataContext.MenuBar = _currentEditMenuBarItem.Title;
- } else {
- if (DataContext.Menus.Count == 0) {
- DataContext.Menus.Add (new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem));
- }
- DataContext.Menus [index] = new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem);
- }
- _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
- SetFrameDetails (_currentEditMenuBarItem);
- }
- _frmMenuDetails.Initialized += (s, e) => _frmMenuDetails.CanFocus = false;
- }
- }
- public class DynamicMenuBarDetails : FrameView {
- public MenuItem _menuItem;
- public TextField _txtTitle;
- public TextField _txtHelp;
- public TextView _txtAction;
- public CheckBox _ckbIsTopLevel;
- public CheckBox _ckbSubMenu;
- public RadioGroup _rbChkStyle;
- public TextField _txtShortCut;
- bool hasParent;
- public DynamicMenuBarDetails (MenuItem menuItem = null, bool hasParent = false) : this (menuItem == null ? "Adding New Menu." : "Editing Menu.")
- {
- _menuItem = menuItem;
- this.hasParent = hasParent;
- }
- public DynamicMenuBarDetails (ustring title) : base (title)
- {
- var _lblTitle = new Label ("Title:") {
- Y = 1
- };
- Add (_lblTitle);
- _txtTitle = new TextField () {
- X = Pos.Right (_lblTitle) + 2,
- Y = Pos.Top (_lblTitle),
- Width = Dim.Fill ()
- };
- Add (_txtTitle);
- var _lblHelp = new Label ("Help:") {
- X = Pos.Left (_lblTitle),
- Y = Pos.Bottom (_lblTitle) + 1
- };
- Add (_lblHelp);
- _txtHelp = new TextField () {
- X = Pos.Left (_txtTitle),
- Y = Pos.Top (_lblHelp),
- Width = Dim.Fill ()
- };
- Add (_txtHelp);
- var _lblAction = new Label ("Action:") {
- X = Pos.Left (_lblTitle),
- Y = Pos.Bottom (_lblHelp) + 1
- };
- Add (_lblAction);
- _txtAction = new TextView () {
- ColorScheme = Colors.Dialog,
- X = Pos.Left (_txtTitle),
- Y = Pos.Top (_lblAction),
- Width = Dim.Fill (),
- Height = 5
- };
- Add (_txtAction);
- _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
- X = Pos.Left (_lblTitle),
- Y = Pos.Bottom (_lblAction) + 5
- };
- Add (_ckbIsTopLevel);
- _ckbSubMenu = new CheckBox ("Has sub-menus") {
- X = Pos.Left (_lblTitle),
- Y = Pos.Bottom (_ckbIsTopLevel),
- Checked = _menuItem == null ? !hasParent : HasSubMenus (_menuItem)
- };
- Add (_ckbSubMenu);
- var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" };
- _rbChkStyle = new RadioGroup (_rChkLabels) {
- X = Pos.Left (_lblTitle),
- Y = Pos.Bottom (_ckbSubMenu) + 1,
- };
- Add (_rbChkStyle);
- var _lblShortCut = new Label ("ShortCut:") {
- X = Pos.Right (_ckbSubMenu) + 10,
- Y = Pos.Top (_ckbSubMenu)
- };
- Add (_lblShortCut);
- _txtShortCut = new TextField () {
- X = Pos.X (_lblShortCut),
- Y = Pos.Bottom (_lblShortCut),
- Width = Dim.Fill (),
- ReadOnly = true
- };
- _txtShortCut.KeyDown += (e) => {
- if (!ProcessKey (e.KeyEvent)) {
- return;
- }
- var k = GetModifiersKey (e.KeyEvent);
- if (CheckShortCut (k, true)) {
- e.Handled = true;
- }
- };
- bool ProcessKey (KeyEvent ev)
- {
- switch (ev.Key) {
- case Key.CursorUp:
- case Key.CursorDown:
- case Key.Tab:
- case Key.BackTab:
- return false;
- }
- return true;
- }
- bool CheckShortCut (Key k, bool pre)
- {
- var m = _menuItem != null ? _menuItem : new MenuItem ();
- if (pre && !ShortCutHelper.PreShortCutValidation (k)) {
- _txtShortCut.Text = "";
- return false;
- }
- if (!pre) {
- if (!ShortCutHelper.PostShortCutValidation (ShortCutHelper.GetShortCutFromTag (_txtShortCut.Text))) {
- _txtShortCut.Text = "";
- return false;
- }
- return true;
- }
- _txtShortCut.Text = ShortCutHelper.GetShortCutTag (k);
- return true;
- }
- _txtShortCut.KeyUp += (e) => {
- var k = GetModifiersKey (e.KeyEvent);
- if (CheckShortCut (k, false)) {
- e.Handled = true;
- }
- };
- Add (_txtShortCut);
- var _btnShortCut = new Button ("Clear ShortCut") {
- X = Pos.X (_lblShortCut),
- Y = Pos.Bottom (_txtShortCut) + 1
- };
- _btnShortCut.Clicked += () => {
- _txtShortCut.Text = "";
- };
- Add (_btnShortCut);
- _ckbIsTopLevel.Toggled += (e) => {
- if ((_menuItem != null && _menuItem.Parent != null && _ckbIsTopLevel.Checked) ||
- _menuItem == null && hasParent && _ckbIsTopLevel.Checked) {
- MessageBox.ErrorQuery ("Invalid IsTopLevel", "Only menu bar can have top level menu item!", "Ok");
- _ckbIsTopLevel.Checked = false;
- return;
- }
- if (_ckbIsTopLevel.Checked) {
- _ckbSubMenu.Checked = false;
- _ckbSubMenu.SetNeedsDisplay ();
- _txtHelp.CanFocus = true;
- _txtAction.CanFocus = true;
- _txtShortCut.CanFocus = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
- } else {
- if (_menuItem == null && !hasParent || _menuItem.Parent == null) {
- _ckbSubMenu.Checked = true;
- _ckbSubMenu.SetNeedsDisplay ();
- _txtShortCut.CanFocus = false;
- }
- _txtHelp.Text = "";
- _txtHelp.CanFocus = false;
- _txtAction.Text = "";
- _txtAction.CanFocus = false;
- }
- };
- _ckbSubMenu.Toggled += (e) => {
- if (_ckbSubMenu.Checked) {
- _ckbIsTopLevel.Checked = false;
- _ckbIsTopLevel.SetNeedsDisplay ();
- _txtHelp.Text = "";
- _txtHelp.CanFocus = false;
- _txtAction.Text = "";
- _txtAction.CanFocus = false;
- _txtShortCut.Text = "";
- _txtShortCut.CanFocus = false;
- } else {
- if (!hasParent) {
- _ckbIsTopLevel.Checked = true;
- _ckbIsTopLevel.SetNeedsDisplay ();
- _txtShortCut.CanFocus = false;
- }
- _txtHelp.CanFocus = true;
- _txtAction.CanFocus = true;
- _txtShortCut.CanFocus = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
- }
- };
- //Add (_frmMenuDetails);
- }
- public DynamicMenuItem EnterMenuItem ()
- {
- var valid = false;
- if (_menuItem == null) {
- var m = new DynamicMenuItem ();
- _txtTitle.Text = m.title;
- _txtHelp.Text = m.help;
- _txtAction.Text = m.action;
- _ckbIsTopLevel.Checked = false;
- _ckbSubMenu.Checked = !hasParent;
- _txtHelp.CanFocus = hasParent;
- _txtAction.CanFocus = hasParent;
- _txtShortCut.CanFocus = hasParent;
- } else {
- EditMenuBarItem (_menuItem);
- }
- var _btnOk = new Button ("Ok") {
- IsDefault = true,
- };
- _btnOk.Clicked += () => {
- if (ustring.IsNullOrEmpty (_txtTitle.Text)) {
- MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
- } else {
- valid = true;
- Application.RequestStop ();
- }
- };
- var _btnCancel = new Button ("Cancel");
- _btnCancel.Clicked += () => {
- _txtTitle.Text = ustring.Empty;
- Application.RequestStop ();
- };
- var _dialog = new Dialog ("Please enter the menu details.", _btnOk, _btnCancel);
- Width = Dim.Fill ();
- Height = Dim.Fill () - 1;
- _dialog.Add (this);
- _txtTitle.SetFocus ();
- _txtTitle.CursorPosition = _txtTitle.Text.Length;
- Application.Run (_dialog);
- if (valid) {
- 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,
- _txtShortCut.Text);
- } else {
- return null;
- }
- }
- public void EditMenuBarItem (MenuItem menuItem)
- {
- if (menuItem == null) {
- hasParent = false;
- CanFocus = false;
- CleanEditMenuBarItem ();
- return;
- } else {
- hasParent = menuItem.Parent != null;
- CanFocus = true;
- }
- _menuItem = menuItem;
- _txtTitle.Text = menuItem?.Title ?? "";
- _txtHelp.Text = menuItem?.Help ?? "";
- _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : ustring.Empty;
- _ckbIsTopLevel.Checked = IsTopLevel (menuItem);
- _ckbSubMenu.Checked = HasSubMenus (menuItem);
- _txtHelp.CanFocus = !_ckbSubMenu.Checked;
- _txtAction.CanFocus = !_ckbSubMenu.Checked;
- _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
- _txtShortCut.Text = menuItem?.ShortCutTag ?? "";
- _txtShortCut.CanFocus = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
- }
- void CleanEditMenuBarItem ()
- {
- _txtTitle.Text = "";
- _txtHelp.Text = "";
- _txtAction.Text = "";
- _ckbIsTopLevel.Checked = false;
- _ckbSubMenu.Checked = false;
- _rbChkStyle.SelectedItem = (int)MenuItemCheckStyle.NoCheck;
- _txtShortCut.Text = "";
- }
- ustring GetTargetAction (Action action)
- {
- var me = action.Target;
- if (me == null) {
- throw new ArgumentException ();
- }
- object v = new object ();
- foreach (var field in me.GetType ().GetFields ()) {
- if (field.Name == "item") {
- v = field.GetValue (me);
- }
- }
- return v == null || !(v is DynamicMenuItem item) ? ustring.Empty : item.action;
- }
- bool IsTopLevel (MenuItem menuItem)
- {
- var topLevel = menuItem as MenuBarItem;
- if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0) && topLevel.Action != null) {
- return true;
- } else {
- return false;
- }
- }
- bool HasSubMenus (MenuItem menuItem)
- {
- var menuBarItem = menuItem as MenuBarItem;
- if (menuBarItem != null && menuBarItem.Children != null && (menuBarItem.Children.Length > 0 || menuBarItem.Action == null)) {
- return true;
- } else {
- return false;
- }
- }
- public Action CreateAction (MenuItem menuItem, DynamicMenuItem item)
- {
- switch (menuItem.CheckType) {
- case MenuItemCheckStyle.NoCheck:
- return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
- case MenuItemCheckStyle.Checked:
- return new Action (() => menuItem.Checked = !menuItem.Checked);
- case MenuItemCheckStyle.Radio:
- break;
- }
- return new Action (() => {
- menuItem.Checked = true;
- var parent = menuItem?.Parent as MenuBarItem;
- if (parent != null) {
- var childrens = parent.Children;
- for (int i = 0; i < childrens.Length; i++) {
- var child = childrens [i];
- if (child != menuItem) {
- child.Checked = false;
- }
- }
- }
- });
- }
- public void UpdateParent (ref MenuItem menuItem)
- {
- var parent = menuItem.Parent as MenuBarItem;
- var idx = parent.GetChildrenIndex (menuItem);
- if (!(menuItem is MenuBarItem)) {
- menuItem = new MenuBarItem (menuItem.Title, new MenuItem [] { }, menuItem.Parent);
- if (idx > -1) {
- parent.Children [idx] = menuItem;
- }
- } else {
- menuItem = new MenuItem (menuItem.Title, menuItem.Help, CreateAction (menuItem, new DynamicMenuItem ()), null, menuItem.Parent);
- if (idx > -1) {
- parent.Children [idx] = menuItem;
- }
- }
- }
- }
- public class DynamicMenuItemModel : INotifyPropertyChanged {
- public event PropertyChangedEventHandler PropertyChanged;
- private ustring menuBar;
- private ustring parent;
- private List<DynamicMenuItemList> menus;
- public ustring MenuBar {
- get => menuBar;
- set {
- if (value != menuBar) {
- menuBar = value;
- PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
- }
- }
- }
- public ustring Parent {
- get => parent;
- set {
- if (value != parent) {
- parent = value;
- PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
- }
- }
- }
- public List<DynamicMenuItemList> Menus {
- get => menus;
- set {
- if (value != menus) {
- menus = value;
- PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
- }
- }
- }
- public DynamicMenuItemModel ()
- {
- Menus = new List<DynamicMenuItemList> ();
- }
- public string GetPropertyName ([CallerMemberName] string propertyName = null)
- {
- return propertyName;
- }
- }
- public interface IValueConverter {
- object Convert (object value, object parameter = null);
- }
- public class Binding {
- public View Target { get; private set; }
- public View Source { get; private set; }
- public string SourcePropertyName { get; private set; }
- public string TargetPropertyName { get; private set; }
- private object sourceDataContext;
- private PropertyInfo sourceBindingProperty;
- private IValueConverter valueConverter;
- public Binding (View source, string sourcePropertyName, View target, string targetPropertyName, IValueConverter valueConverter = null)
- {
- Target = target;
- Source = source;
- SourcePropertyName = sourcePropertyName;
- TargetPropertyName = targetPropertyName;
- sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
- sourceBindingProperty = sourceDataContext.GetType ().GetProperty (SourcePropertyName);
- this.valueConverter = valueConverter;
- UpdateTarget ();
- var notifier = ((INotifyPropertyChanged)sourceDataContext);
- if (notifier != null) {
- notifier.PropertyChanged += (s, e) => {
- if (e.PropertyName == SourcePropertyName) {
- UpdateTarget ();
- }
- };
- }
- }
- private void UpdateTarget ()
- {
- try {
- var sourceValue = sourceBindingProperty.GetValue (sourceDataContext);
- if (sourceValue == null) {
- return;
- }
- var finalValue = valueConverter?.Convert (sourceValue) ?? sourceValue;
- var targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
- targetProperty.SetValue (Target, finalValue);
- } catch (Exception ex) {
- MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
- }
- }
- }
- public class ListWrapperConverter : IValueConverter {
- public object Convert (object value, object parameter = null)
- {
- return new ListWrapper ((IList)value);
- }
- }
- public class UStringValueConverter : IValueConverter {
- public object Convert (object value, object parameter = null)
- {
- var data = Encoding.ASCII.GetBytes (value.ToString ());
- return ustring.Make (data);
- }
- }
- }
|