ui/wpf/UIcore/Menu.cs

Fri, 15 Nov 2024 21:50:20 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 15 Nov 2024 21:50:20 +0100
changeset 86
8e7c57c23133
parent 0
2483f517c562
permissions
-rw-r--r--

add reference counting to download window

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