UNIXworkcode

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Media; 9 10 namespace UI 11 { 12 public class ApplicationMenu 13 { 14 private List<Menu> current = new List<Menu>(); 15 16 public List<Menu> Menus = new List<Menu>(); 17 18 public void AddMenu(String label) 19 { 20 current.Clear(); 21 Menu menu = new Menu(label); 22 current.Add(menu); 23 Menus.Add(menu); 24 } 25 26 public Boolean IsEmpty() 27 { 28 return Menus.Count == 0 ? true : false; 29 } 30 31 public void AddSubMenu(String label) 32 { 33 Menu menu = new Menu(label); 34 current.Last().Items.Add(menu); 35 current.Add(menu); 36 } 37 38 public void EndSubMenu() 39 { 40 current.Remove(current.Last()); 41 } 42 43 public void AddMenuItem(String label, Action<IntPtr> action) 44 { 45 if(current.Count != 0) 46 { 47 MenuItem item = new MenuItem(label, action); 48 current.Last().Items.Add(item); 49 } 50 } 51 52 public System.Windows.Controls.Menu CreateMenu(IntPtr uiobj) 53 { 54 System.Windows.Controls.Menu menu = new System.Windows.Controls.Menu(); 55 menu.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255)); 56 foreach (Menu m in Menus) 57 { 58 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem(); 59 i.Header = m.Label; 60 m.AddItems(i, uiobj); 61 menu.Items.Add(i); 62 } 63 return menu; 64 } 65 } 66 67 public interface IMenuItem 68 { 69 void AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj); 70 } 71 72 public class Menu : IMenuItem 73 { 74 public String Label; 75 public List<IMenuItem> Items = new List<IMenuItem>(); 76 77 public Menu(String label) 78 { 79 Label = label; 80 } 81 82 public void AddItems(System.Windows.Controls.MenuItem i, IntPtr uiobj) 83 { 84 foreach (IMenuItem item in Items) 85 { 86 item.AddTo(i, uiobj); 87 } 88 } 89 90 void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj) 91 { 92 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem(); 93 i.Header = Label; 94 AddItems(i, uiobj); 95 menu.Items.Add(i); 96 } 97 } 98 99 public class MenuItem : IMenuItem 100 { 101 String Label; 102 Action<IntPtr> Action; 103 104 public MenuItem(String label, Action<IntPtr> action) 105 { 106 Label = label; 107 Action = action; 108 } 109 110 void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj) 111 { 112 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem(); 113 EventCallback cb = new EventCallback(uiobj, Action); 114 i.Click += cb.Callback; 115 i.Header = Label; 116 menu.Items.Add(i); 117 } 118 } 119 } 120