ui/wpf/UIcore/Menu.cs

Fri, 11 Dec 2020 11:46:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 11 Dec 2020 11:46:19 +0100
changeset 3
c04433993fbb
parent 0
804d8803eade
permissions
-rw-r--r--

add drawingarea and textarea group

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