ui/wpf/UIcore/Menu.cs

Thu, 21 Jan 2016 20:40:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 21 Jan 2016 20:40:14 +0100
changeset 104
3efe0210e27e
parent 82
0cdb8089a29f
child 137
c9b8b9e0cfe8
permissions
-rw-r--r--

added grid spacing (WPF)

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

mercurial