ui/wpf/UIcore/Menu.cs

changeset 0
804d8803eade
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/UIcore/Menu.cs	Wed Dec 09 11:32:01 2020 +0100
@@ -0,0 +1,119 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+
+namespace UI
+{
+    public class ApplicationMenu
+    {
+        private List<Menu> current = new List<Menu>();
+
+        public List<Menu> Menus = new List<Menu>();
+
+        public void AddMenu(String label)
+        {
+            current.Clear();
+            Menu menu = new Menu(label);
+            current.Add(menu);
+            Menus.Add(menu);
+        }
+
+        public Boolean IsEmpty()
+        {
+            return Menus.Count == 0 ? true : false;
+        }
+
+        public void AddSubMenu(String label)
+        {
+            Menu menu = new Menu(label);
+            current.Last().Items.Add(menu);
+            current.Add(menu);
+        }
+
+        public void EndSubMenu()
+        {
+            current.Remove(current.Last());
+        }
+
+        public void AddMenuItem(String label, Action<IntPtr> action)
+        {
+            if(current.Count != 0)
+            {
+                MenuItem item = new MenuItem(label, action);
+                current.Last().Items.Add(item);
+            }
+        }
+
+        public System.Windows.Controls.Menu CreateMenu(IntPtr uiobj)
+        {
+            System.Windows.Controls.Menu menu = new System.Windows.Controls.Menu();
+            menu.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
+            foreach (Menu m in Menus)
+            {
+                System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
+                i.Header = m.Label;
+                m.AddItems(i, uiobj);
+                menu.Items.Add(i);
+            }
+            return menu;
+        }
+    }
+
+    public interface IMenuItem
+    {
+        void AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj);
+    }
+
+    public class Menu : IMenuItem
+    {
+        public String Label;
+        public List<IMenuItem> Items = new List<IMenuItem>();
+
+        public Menu(String label)
+        {
+            Label = label;
+        }
+
+        public void AddItems(System.Windows.Controls.MenuItem i, IntPtr uiobj)
+        {
+            foreach (IMenuItem item in Items)
+            {
+                item.AddTo(i, uiobj);
+            }
+        }
+
+        void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj)
+        {
+            System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
+            i.Header = Label;
+            AddItems(i, uiobj);
+            menu.Items.Add(i);
+        }
+    }
+
+    public class MenuItem : IMenuItem
+    {
+        String Label;
+        Action<IntPtr> Action;
+
+        public MenuItem(String label, Action<IntPtr> action)
+        {
+            Label = label;
+            Action = action;
+        }
+
+        void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj)
+        {
+            System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
+            EventCallback cb = new EventCallback(uiobj, Action);
+            i.Click += cb.Callback;
+            i.Header = Label;
+            menu.Items.Add(i);
+        }
+    }
+}

mercurial