Fixes #644. Added a UICatalog Scenario for a dynamic menu bar.

This commit is contained in:
BDisp
2020-08-03 03:08:32 +01:00
parent 5de3f6908f
commit de0a190b7c
5 changed files with 1073 additions and 76 deletions

View File

@@ -586,7 +586,7 @@ static class Demo {
new MenuItemDetails ("F_ind", "", null),
new MenuItemDetails ("_Replace", "", null),
new MenuItemDetails ("_Item1", "", null),
new MenuItemDetails ("_Not From Sub Menu", "", null)
new MenuItemDetails ("_Also From Sub Menu", "", null)
};
menuItems [0].Action = () => ShowMenuItem (menuItems [0]);
@@ -609,8 +609,8 @@ static class Demo {
new MenuItem ("_Copy", "", Copy),
new MenuItem ("C_ut", "", Cut),
new MenuItem ("_Paste", "", Paste),
new MenuItem ("_Find and Replace",
new MenuBarItem (new MenuItem[] {menuItems [0], menuItems [1] })),
new MenuBarItem ("_Find and Replace",
new MenuItem [] { menuItems [0], menuItems [1] }),
menuItems[3]
}),
new MenuBarItem ("_List Demos", new MenuItem [] {
@@ -622,18 +622,13 @@ static class Demo {
new MenuItem ("_Show text alignments", "", () => ShowTextAlignments ()),
new MenuItem ("_OnKeyDown/Press/Up", "", () => OnKeyDownPressUpDemo ())
}),
new MenuBarItem ("_Test Menu and SubMenus", new MenuItem [] {
new MenuItem ("SubMenu1Item_1",
new MenuBarItem (new MenuItem[] {
new MenuItem ("SubMenu2Item_1",
new MenuBarItem (new MenuItem [] {
new MenuItem ("SubMenu3Item_1",
new MenuBarItem (new MenuItem [] { menuItems [2] })
)
})
)
new MenuBarItem ("_Test Menu and SubMenus", new MenuBarItem [] {
new MenuBarItem ("SubMenu1Item_1", new MenuBarItem [] {
new MenuBarItem ("SubMenu2Item_1", new MenuBarItem [] {
new MenuBarItem ("SubMenu3Item_1",
new MenuItem [] { menuItems [2] })
})
)
})
}),
new MenuBarItem ("_About...", "Demonstrates top-level menu item", () => MessageBox.ErrorQuery (50, 7, "About Demo", "This is a demo app for gui.cs", "Ok")),
});

View File

@@ -253,26 +253,26 @@ namespace Terminal.Gui {
public override void Add (View view)
{
if (this == Application.Top) {
if (view is MenuBar)
MenuBar = view as MenuBar;
if (view is StatusBar)
StatusBar = view as StatusBar;
AddMenuStatusBar (view);
}
base.Add (view);
}
internal void AddMenuStatusBar (View view)
{
if (view is MenuBar) {
MenuBar = view as MenuBar;
}
if (view is StatusBar) {
StatusBar = view as StatusBar;
}
}
///<inheritdoc/>
public override void Remove (View view)
{
if (this is Toplevel && ((Toplevel)this).MenuBar != null) {
if (view is MenuBar) {
MenuBar?.Dispose ();
MenuBar = null;
}
if (view is StatusBar) {
StatusBar?.Dispose ();
StatusBar = null;
}
if (this is Toplevel toplevel && toplevel.MenuBar != null) {
RemoveMenuStatusBar (view);
}
base.Remove (view);
}
@@ -289,6 +289,18 @@ namespace Terminal.Gui {
base.RemoveAll ();
}
internal void RemoveMenuStatusBar (View view)
{
if (view is MenuBar) {
MenuBar?.Dispose ();
MenuBar = null;
}
if (view is StatusBar) {
StatusBar?.Dispose ();
StatusBar = null;
}
}
internal void EnsureVisibleBounds (Toplevel top, int x, int y, out int nx, out int ny)
{
nx = Math.Max (x, 0);

View File

@@ -135,23 +135,28 @@ namespace Terminal.Gui {
public override void Add (View view)
{
contentView.Add (view);
if (view.CanFocus)
if (view.CanFocus) {
CanFocus = true;
}
AddMenuStatusBar (view);
}
/// <inheritdoc/>
public override void Remove (View view)
{
if (view == null)
if (view == null) {
return;
}
SetNeedsDisplay ();
var touched = view.Frame;
contentView.Remove (view);
if (contentView.InternalSubviews.Count < 1)
this.CanFocus = false;
if (contentView.InternalSubviews.Count < 1) {
CanFocus = false;
}
RemoveMenuStatusBar (view);
}
/// <inheritdoc/>

View File

@@ -56,8 +56,9 @@ namespace Terminal.Gui {
/// <param name="title">Title for the menu item.</param>
/// <param name="help">Help text to display.</param>
/// <param name="action">Action to invoke when the menu item is activated.</param>
/// <param name="canExecute">Function to determine if the action can currently be executred.</param>
public MenuItem (ustring title, string help, Action action, Func<bool> canExecute = null)
/// <param name="canExecute">Function to determine if the action can currently be executed.</param>
/// <param name="parent">The parent of this menu item.</param>
public MenuItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null)
{
Title = title ?? "";
Help = help ?? "";
@@ -75,17 +76,7 @@ namespace Terminal.Gui {
nextIsHot = false;
}
}
}
/// <summary>
/// Initializes a new instance of <see cref="MenuItem"/>
/// </summary>
/// <param name="title">Title for the menu item.</param>
/// <param name="subMenu">The menu sub-menu.</param>
public MenuItem (ustring title, MenuBarItem subMenu) : this (title, "", null)
{
SubMenu = subMenu;
IsFromSubMenu = true;
Parent = parent;
}
/// <summary>
@@ -146,11 +137,15 @@ namespace Terminal.Gui {
public MenuItemCheckStyle CheckType { get; set; }
/// <summary>
/// Gets or sets the parent for this <see cref="MenuItem"/>
/// Gets or sets the parent for this <see cref="MenuItem"/>.
/// </summary>
/// <value>The parent.</value>
internal MenuBarItem SubMenu { get; set; }
internal bool IsFromSubMenu { get; set; }
public MenuItem Parent { get; internal set; }
/// <summary>
/// Gets if this <see cref="MenuItem"/> is from a sub-menu.
/// </summary>
internal bool IsFromSubMenu { get {return Parent != null; } }
/// <summary>
/// Merely a debugging aid to see the interaction with main
@@ -179,8 +174,9 @@ namespace Terminal.Gui {
/// <param name="title">Title for the menu item.</param>
/// <param name="help">Help text to display.</param>
/// <param name="action">Action to invoke when the menu item is activated.</param>
/// <param name="canExecute">Function to determine if the action can currently be executred.</param>
public MenuBarItem (ustring title, string help, Action action, Func<bool> canExecute = null) : base (title, help, action, canExecute)
/// <param name="canExecute">Function to determine if the action can currently be executed.</param>
/// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
public MenuBarItem (ustring title, ustring help, Action action, Func<bool> canExecute = null, MenuItem parent = null) : base (title, help, action, canExecute, parent)
{
SetTitle (title ?? "");
Children = null;
@@ -191,12 +187,17 @@ namespace Terminal.Gui {
/// </summary>
/// <param name="title">Title for the menu item.</param>
/// <param name="children">The items in the current menu.</param>
public MenuBarItem (ustring title, MenuItem [] children)
/// <param name="parent">The parent <see cref="MenuItem"/> of this if exist, otherwise is null.</param>
public MenuBarItem (ustring title, MenuItem [] children, MenuItem parent = null)
{
if (children == null)
if (children == null) {
throw new ArgumentNullException (nameof (children), "The parameter cannot be null. Use an empty array instead.");
}
SetTitle (title ?? "");
if (parent != null) {
Parent = parent;
}
SetChildrensParent (children);
Children = children;
}
@@ -204,24 +205,78 @@ namespace Terminal.Gui {
/// Initializes a new <see cref="MenuBarItem"/>.
/// </summary>
/// <param name="children">The items in the current menu.</param>
public MenuBarItem (MenuItem [] children) : this (new string (' ', GetMaxTitleLength (children)), children) { }
public MenuBarItem (MenuItem [] children) : this ("", children) { }
/// <summary>
/// Initializes a new <see cref="MenuBarItem"/>.
/// </summary>
public MenuBarItem () : this (children: new MenuItem [] { }) { }
public MenuBarItem () : this (children: new MenuItem [] { }) { }
static int GetMaxTitleLength (MenuItem [] children)
//static int GetMaxTitleLength (MenuItem [] children)
//{
// int maxLength = 0;
// foreach (var item in children) {
// int len = GetMenuBarItemLength (item.Title);
// if (len > maxLength)
// maxLength = len;
// item.IsFromSubMenu = true;
// }
// return maxLength;
//}
void SetChildrensParent (MenuItem [] childrens)
{
int maxLength = 0;
foreach (var item in children) {
int len = GetMenuBarItemLength (item.Title);
if (len > maxLength)
maxLength = len;
item.IsFromSubMenu = true;
foreach (var child in childrens) {
if (child != null && child.Parent == null) {
child.Parent = this;
}
}
}
return maxLength;
/// <summary>
/// Check if the children parameter is a <see cref="MenuBarItem"/>.
/// </summary>
/// <param name="children"></param>
/// <returns>Returns a <see cref="MenuBarItem"/> or null otherwise.</returns>
public MenuBarItem SubMenu (MenuItem children)
{
return children as MenuBarItem;
}
/// <summary>
/// Check if the <see cref="MenuItem"/> parameter is a child of this.
/// </summary>
/// <param name="menuItem"></param>
/// <returns>Returns <c>true</c> if it is a child of this. <c>false</c> otherwise.</returns>
public bool IsSubMenuOf (MenuItem menuItem)
{
foreach (var child in Children) {
if (child == menuItem && child.Parent == menuItem.Parent) {
return true;
}
}
return false;
}
/// <summary>
/// Get the index of the <see cref="MenuItem"/> parameter.
/// </summary>
/// <param name="children"></param>
/// <returns>Returns a value bigger than -1 if the <see cref="MenuItem"/> is a child of this.</returns>
public int GetChildrenIndex (MenuItem children)
{
if (Children?.Length == 0) {
return -1;
}
int i = 0;
foreach (var child in Children) {
if (child == children) {
return i;
}
i++;
}
return -1;
}
void SetTitle (ustring title)
@@ -229,10 +284,9 @@ namespace Terminal.Gui {
if (title == null)
title = "";
Title = title;
TitleLength = GetMenuBarItemLength (Title);
}
static int GetMenuBarItemLength (ustring title)
int GetMenuBarItemLength (ustring title)
{
int len = 0;
foreach (var ch in title) {
@@ -255,9 +309,10 @@ namespace Terminal.Gui {
/// </summary>
/// <value>The children.</value>
public MenuItem [] Children { get; set; }
internal int TitleLength { get; private set; }
internal bool IsTopLevel { get => (Children == null || Children.Length == 0); }
internal int TitleLength => GetMenuBarItemLength (Title);
internal bool IsTopLevel { get => Parent == null && (Children == null || Children.Length == 0); }
}
@@ -328,7 +383,7 @@ namespace Terminal.Gui {
for (int p = 0; p < Frame.Width - 2; p++)
if (item == null)
Driver.AddRune (Driver.HLine);
else if (p == Frame.Width - 3 && barItems.Children [i].SubMenu != null)
else if (p == Frame.Width - 3 && barItems.SubMenu(barItems.Children [i]) != null)
Driver.AddRune (Driver.RightArrow);
else
Driver.AddRune (' ');
@@ -494,7 +549,7 @@ namespace Terminal.Gui {
} else {
disabled = false;
}
if (host.UseKeysUpDownAsKeysLeftRight && barItems.Children [current]?.SubMenu != null &&
if (host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
!disabled && host.IsMenuOpen) {
CheckSubMenu ();
break;
@@ -541,7 +596,7 @@ namespace Terminal.Gui {
} else {
disabled = false;
}
if (host.UseKeysUpDownAsKeysLeftRight && barItems.Children [current]?.SubMenu != null &&
if (host.UseKeysUpDownAsKeysLeftRight && barItems.SubMenu (barItems.Children [current]) != null &&
!disabled && host.IsMenuOpen) {
CheckSubMenu ();
break;
@@ -592,7 +647,7 @@ namespace Terminal.Gui {
if (current == -1 || barItems.Children [current] == null) {
return;
}
var subMenu = barItems.Children [current].SubMenu;
var subMenu = barItems.SubMenu (barItems.Children [current]);
if (subMenu != null) {
int pos = -1;
if (host.openSubMenu != null) {
@@ -602,7 +657,7 @@ namespace Terminal.Gui {
host.CloseMenu (false, true);
}
host.Activate (host.selected, pos, subMenu);
} else if (host.openSubMenu != null && !barItems.Children [current].IsFromSubMenu) {
} else if (host.openSubMenu?.Last ().barItems.IsSubMenuOf (barItems.Children [current]) == false) {
host.CloseMenu (false, true);
} else {
SetNeedsDisplay ();
@@ -1003,7 +1058,7 @@ namespace Terminal.Gui {
switch (isSubMenu) {
case false:
if (openMenu != null) {
SuperView.Remove (openMenu);
SuperView?.Remove (openMenu);
}
SetNeedsDisplay ();
if (previousFocused != null && previousFocused is Menu && openMenu != null && previousFocused.ToString () != openCurrentMenu.ToString ())
@@ -1016,13 +1071,11 @@ namespace Terminal.Gui {
LastFocused = lastFocused;
lastFocused = null;
if (LastFocused != null) {
CanFocus = false;
if (!reopen) {
selected = -1;
}
LastFocused.SetFocus ();
} else {
CanFocus = true;
SetFocus ();
PositionCursor ();
}
@@ -1175,11 +1228,12 @@ namespace Terminal.Gui {
CloseMenu (false, true);
NextMenu ();
} else {
if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && openCurrentMenu.barItems.Children [openCurrentMenu.current].SubMenu == null) {
var subMenu = openCurrentMenu.barItems.SubMenu (openCurrentMenu.barItems.Children [openCurrentMenu.current]);
if ((selectedSub == -1 || openSubMenu == null || openSubMenu?.Count == selectedSub) && subMenu == null) {
if (openSubMenu != null)
CloseMenu (false, true);
NextMenu ();
} else if (openCurrentMenu.barItems.Children [openCurrentMenu.current].SubMenu != null ||
} else if (subMenu != null ||
!openCurrentMenu.barItems.Children [openCurrentMenu.current].IsFromSubMenu)
selectedSub++;
else

View File

@@ -0,0 +1,931 @@
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 ();
}
}
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}";
}
class DynamicMenuItem {
public ustring title = "_New";
public ustring help = "";
public ustring action = "";
public bool isTopLevel;
public bool hasSubMenu;
public MenuItemCheckStyle checkStyle;
public DynamicMenuItem () { }
public DynamicMenuItem (ustring title)
{
this.title = title;
}
public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck)
{
this.title = title;
this.help = help;
this.action = action;
this.isTopLevel = isTopLevel;
this.hasSubMenu = hasSubMenu;
this.checkStyle = checkStyle;
}
}
class DynamicMenuBarSample : Window {
MenuBar _menuBar;
MenuItem _currentMenuBarItem;
int _currentSelectedMenuBar;
MenuItem _currentEditMenuBarItem;
public DynamicMenuItemModel DataContext { get; set; }
public DynamicMenuBarSample (ustring title) : base (title)
{
DataContext = new DynamicMenuItemModel ();
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);
var _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 FrameView ("Menu Details:") {
X = Pos.Right (_frmMenu),
Y = Pos.Top (_frmMenu),
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var _lblTitle = new Label ("Title:") {
Y = 1
};
_frmMenuDetails.Add (_lblTitle);
var _txtTitle = new TextField () {
X = Pos.Right (_lblTitle) + 2,
Y = Pos.Top (_lblTitle),
Width = Dim.Fill ()
};
_frmMenuDetails.Add (_txtTitle);
var _lblHelp = new Label ("Help:") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_lblTitle) + 1
};
_frmMenuDetails.Add (_lblHelp);
var _txtHelp = new TextField () {
X = Pos.Left (_txtTitle),
Y = Pos.Top (_lblHelp),
Width = Dim.Fill ()
};
_frmMenuDetails.Add (_txtHelp);
var _lblAction = new Label ("Action:") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_lblHelp) + 1
};
_frmMenuDetails.Add (_lblAction);
var _txtAction = new TextView () {
ColorScheme = Colors.Dialog,
X = Pos.Left (_txtTitle),
Y = Pos.Top (_lblAction),
Width = Dim.Fill (),
Height = 5
};
_frmMenuDetails.Add (_txtAction);
var _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_lblAction) + 5
};
_frmMenuDetails.Add (_ckbIsTopLevel);
var _ckbSubMenu = new CheckBox ("Has sub-menus") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_ckbIsTopLevel)
};
_frmMenuDetails.Add (_ckbSubMenu);
_ckbIsTopLevel.Toggled = (e) => {
if (_ckbIsTopLevel.Checked && _currentEditMenuBarItem.Parent != null) {
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 ();
_txtAction.ReadOnly = false;
} else {
_txtAction.ReadOnly = true;
}
};
_ckbSubMenu.Toggled = (e) => {
if (_ckbSubMenu.Checked) {
_ckbIsTopLevel.Checked = false;
_ckbIsTopLevel.SetNeedsDisplay ();
_txtAction.ReadOnly = true;
} else {
_txtAction.ReadOnly = false;
}
};
var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" };
var _rbChkStyle = new RadioGroup (_rChkLabels) {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_ckbSubMenu) + 1,
};
_frmMenuDetails.Add (_rbChkStyle);
var _btnOk = new Button ("Ok") {
X = Pos.Left (_lblTitle) + 20,
Y = Pos.Bottom (_rbChkStyle) + 1,
Clicked = () => {
if (ustring.IsNullOrEmpty (_txtTitle.Text) && _currentEditMenuBarItem != null) {
MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
} else if (_currentEditMenuBarItem != null) {
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);
UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
}
}
};
_frmMenuDetails.Add (_btnOk);
var _btnCancel = new Button ("Cancel") {
X = Pos.Right (_btnOk) + 3,
Y = Pos.Top (_btnOk),
Clicked = () => {
_txtTitle.Text = ustring.Empty;
}
};
_frmMenuDetails.Add (_btnCancel);
Add (_frmMenuDetails);
_btnAdd.Clicked = () => {
if (MenuBar == null) {
MessageBox.ErrorQuery ("Menu Bar Error", "Must add a MenuBar first!", "Ok");
_btnAddMenuBar.SetFocus ();
return;
}
var item = EnterMenuItem (_currentMenuBarItem);
if (ustring.IsNullOrEmpty (item.title)) {
return;
}
if (!(_currentMenuBarItem is MenuBarItem)) {
var parent = _currentMenuBarItem.Parent as MenuBarItem;
var idx = parent.GetChildrenIndex (_currentMenuBarItem);
_currentMenuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { new MenuItem ("_New", "", CreateAction (_currentEditMenuBarItem, new DynamicMenuItem ())) }, _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 = CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentMenuBarItem.Title));
} else {
_currentMenuBarItem = new MenuItem (_currentMenuBarItem.Title, _currentMenuBarItem.Help, CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentEditMenuBarItem.Title)), null, _currentMenuBarItem.Parent);
}
} else {
((MenuBarItem)_currentMenuBarItem).Children = childrens;
}
DataContext.Menus.RemoveAt (_lstMenus.SelectedItem);
}
};
_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;
}
}
};
_btnAddMenuBar.Clicked = () => {
var item = EnterMenuItem (null);
if (ustring.IsNullOrEmpty (item.title)) {
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);
EditMenuBarItem (_menuBar.Menus [_currentSelectedMenuBar]);
_menuBar.SetNeedsDisplay ();
};
_btnRemoveMenuBar.Clicked = () => {
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);
EditMenuBarItem (null);
};
_lblMenuBar.Enter = (e) => {
if (_menuBar?.Menus != null) {
_currentMenuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
EditMenuBarItem (_menuBar.Menus [_currentSelectedMenuBar]);
}
};
_btnPrevious.Clicked = () => {
if (_currentSelectedMenuBar - 1 > -1) {
_currentSelectedMenuBar--;
}
SelectCurrentMenuBarItem ();
};
_btnNext.Clicked = () => {
if (_menuBar != null && _currentSelectedMenuBar + 1 < _menuBar.Menus.Length) {
_currentSelectedMenuBar++;
}
SelectCurrentMenuBarItem ();
};
_lstMenus.SelectedItemChanged = (e) => {
var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [e.Item].MenuItem : null;
EditMenuBarItem (menuBarItem);
};
_lstMenus.OpenSelectedItem = (e) => {
_currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
DataContext.Parent = _currentMenuBarItem.Title;
DataContext.Menus = new List<DynamicMenuItemList> ();
SetListViewSource (_currentMenuBarItem, true);
var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [0].MenuItem : null;
EditMenuBarItem (menuBarItem);
};
_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 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);
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;
}
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;
}
}
}
});
}
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);
}
}
}
void EditMenuBarItem (MenuItem menuBarItem)
{
if (menuBarItem == null) {
_frmMenuDetails.CanFocus = false;
} else {
_frmMenuDetails.CanFocus = true;
}
_currentEditMenuBarItem = menuBarItem;
_txtTitle.Text = menuBarItem?.Title ?? "";
_txtHelp.Text = menuBarItem?.Help ?? "";
_txtAction.Text = menuBarItem != null && menuBarItem.Action != null ? GetTargetAction (menuBarItem.Action) : ustring.Empty;
_ckbIsTopLevel.Checked = IsTopLevel (menuBarItem);
_ckbSubMenu.Checked = HasSubMenus (menuBarItem);
_rbChkStyle.SelectedItem = (int)(menuBarItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
}
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 = 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 [] { new MenuItem ("_New", "", CreateAction (_currentEditMenuBarItem, new DynamicMenuItem ())) };
} else if (_currentEditMenuBarItem.Parent != null) {
UpdateParent (ref _currentEditMenuBarItem);
} else {
_currentEditMenuBarItem = new MenuBarItem (_currentEditMenuBarItem.Title, new MenuItem [] { new MenuItem ("_New", "", CreateAction (_currentEditMenuBarItem, new DynamicMenuItem ())) }, _currentEditMenuBarItem.Parent);
}
SetListViewSource (_currentEditMenuBarItem, true);
} else if (_currentEditMenuBarItem is MenuBarItem && _currentEditMenuBarItem.Parent != null) {
UpdateParent (ref _currentEditMenuBarItem);
_currentEditMenuBarItem = new MenuItem (menuItem.title, menuItem.help, CreateAction (_currentEditMenuBarItem, menuItem), null, _currentEditMenuBarItem.Parent);
} else {
if (_currentEditMenuBarItem is MenuBarItem) {
((MenuBarItem)_currentEditMenuBarItem).Children = null;
DataContext.Menus = new List<DynamicMenuItemList> ();
}
_currentEditMenuBarItem.Action = CreateAction (_currentEditMenuBarItem, menuItem);
}
if (_currentEditMenuBarItem.Parent == null) {
DataContext.MenuBar = _currentEditMenuBarItem.Title;
} else {
DataContext.Menus [index] = new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem);
}
_currentEditMenuBarItem.CheckType = menuItem.checkStyle;
EditMenuBarItem (_currentEditMenuBarItem);
}
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 [] { new MenuItem ("_New", "", CreateAction (menuItem, new DynamicMenuItem ())) }, 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;
}
}
}
bool IsTopLevel (MenuItem menuItem)
{
var topLevel = menuItem as MenuBarItem;
if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0)) {
return true;
} else {
return false;
}
}
bool HasSubMenus (MenuItem menuItem)
{
var menuBarItem = menuItem as MenuBarItem;
if (menuBarItem != null && menuBarItem.Children != null && menuBarItem.Children.Length > 0) {
return true;
} else {
return false;
}
}
void SelectCurrentMenuBarItem ()
{
MenuBarItem menuBarItem = null;
if (_menuBar?.Menus != null) {
menuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
_lblMenuBar.Text = menuBarItem.Title;
}
EditMenuBarItem (menuBarItem);
_currentMenuBarItem = menuBarItem;
DataContext.Menus = new List<DynamicMenuItemList> ();
SetListViewSource (_currentMenuBarItem, true);
_lblParent.Text = ustring.Empty;
}
DynamicMenuItem EnterMenuItem (MenuItem menuItem)
{
var _lblTitle = new Label (1, 3, "Title:");
var _txtTitle = new TextField ("_New") {
X = Pos.Right (_lblTitle) + 2,
Y = Pos.Top (_lblTitle),
Width = Dim.Fill (),
};
var _lblHelp = new Label ("Help:") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_lblTitle) + 1
};
var _txtHelp = new TextField () {
X = Pos.Left (_txtTitle),
Y = Pos.Top (_lblHelp),
Width = Dim.Fill (),
};
var _lblAction = new Label ("Action:") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_lblHelp) + 1
};
var _txtAction = new TextView () {
ColorScheme = Colors.Menu,
X = Pos.Left (_txtTitle),
Y = Pos.Top (_lblAction),
Width = Dim.Fill (),
Height = 5,
ReadOnly = true
};
var _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_lblAction) + 5
};
var _ckbSubMenu = new CheckBox ("Has sub-menus") {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_ckbIsTopLevel),
Checked = menuItem == null
};
_ckbIsTopLevel.Toggled = (e) => {
if (_ckbIsTopLevel.Checked && menuItem != null) {
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 ();
_txtAction.ReadOnly = false;
} else {
_txtAction.ReadOnly = true;
}
};
_ckbSubMenu.Toggled = (e) => {
if (_ckbSubMenu.Checked) {
_ckbIsTopLevel.Checked = false;
_ckbIsTopLevel.SetNeedsDisplay ();
_txtAction.ReadOnly = true;
} else {
_txtAction.ReadOnly = false;
}
};
var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" };
var _rbChkStyle = new RadioGroup (_rChkLabels) {
X = Pos.Left (_lblTitle),
Y = Pos.Bottom (_ckbSubMenu) + 1,
};
var _btnOk = new Button ("Ok") {
IsDefault = true,
Clicked = () => {
if (ustring.IsNullOrEmpty (_txtTitle.Text)) {
MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
} else {
Application.RequestStop ();
}
}
};
var _btnCancel = new Button ("Cancel") {
Clicked = () => {
_txtTitle.Text = ustring.Empty;
Application.RequestStop ();
}
};
var _dialog = new Dialog ("Please enter the menu details.", _btnOk, _btnCancel);
_dialog.Add (_lblTitle, _txtTitle, _lblHelp, _txtHelp, _lblAction, _txtAction, _ckbIsTopLevel, _ckbSubMenu, _rbChkStyle);
_txtTitle.SetFocus ();
Application.Run (_dialog);
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);
}
MenuItem CreateNewMenu (DynamicMenuItem item, MenuItem parent = null)
{
MenuItem newMenu;
if (item.hasSubMenu) {
newMenu = new MenuBarItem (item.title, new MenuItem [] { new MenuItem ("_New", "", null) }, parent);
((MenuBarItem)newMenu).Children [0].Action = CreateAction (newMenu, new DynamicMenuItem ());
} else if (parent != null) {
newMenu = new MenuItem (item.title, item.help, null, null, parent);
newMenu.CheckType = item.checkStyle;
newMenu.Action = CreateAction (newMenu, item);
} else {
newMenu = new MenuBarItem (item.title, item.help, null);
((MenuBarItem)newMenu).Children [0].Action = CreateAction (newMenu, item);
}
return newMenu;
}
}
}
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);
}
}
}