Sun, 22 Sep 2024 19:47:26 +0200
add headerbar (GTK)
81 | 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; | |
137
c9b8b9e0cfe8
adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
8 | using System.Windows.Media; |
81 | 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 | ||
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
38 | public void EndSubMenu() |
81 | 39 | { |
40 | current.Remove(current.Last()); | |
41 | } | |
42 | ||
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
43 | public void AddMenuItem(String label, Action<IntPtr> action) |
81 | 44 | { |
45 | if(current.Count != 0) | |
46 | { | |
47 | MenuItem item = new MenuItem(label, action); | |
48 | current.Last().Items.Add(item); | |
49 | } | |
50 | } | |
51 | ||
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
52 | public System.Windows.Controls.Menu CreateMenu(IntPtr uiobj) |
81 | 53 | { |
54 | System.Windows.Controls.Menu menu = new System.Windows.Controls.Menu(); | |
137
c9b8b9e0cfe8
adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
55 | menu.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255)); |
81 | 56 | foreach (Menu m in Menus) |
57 | { | |
58 | System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem(); | |
59 | i.Header = m.Label; | |
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
60 | m.AddItems(i, uiobj); |
81 | 61 | menu.Items.Add(i); |
62 | } | |
63 | return menu; | |
64 | } | |
65 | } | |
66 | ||
67 | public interface IMenuItem | |
68 | { | |
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
69 | void AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj); |
81 | 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
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
82 | public void AddItems(System.Windows.Controls.MenuItem i, IntPtr uiobj) |
81 | 83 | { |
84 | foreach (IMenuItem item in Items) | |
85 | { | |
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
86 | item.AddTo(i, uiobj); |
81 | 87 | } |
88 | } | |
89 | ||
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
90 | void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj) |
81 | 91 | { |
92 | System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem(); | |
93 | i.Header = Label; | |
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
94 | AddItems(i, uiobj); |
81 | 95 | menu.Items.Add(i); |
96 | } | |
97 | } | |
98 | ||
99 | public class MenuItem : IMenuItem | |
100 | { | |
101 | String Label; | |
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
102 | Action<IntPtr> Action; |
81 | 103 | |
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
104 | public MenuItem(String label, Action<IntPtr> action) |
81 | 105 | { |
106 | Label = label; | |
107 | Action = action; | |
108 | } | |
109 | ||
82
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
110 | void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj) |
81 | 111 | { |
112 | 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
|
113 | EventCallback cb = new EventCallback(uiobj, Action); |
0cdb8089a29f
added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
81
diff
changeset
|
114 | i.Click += cb.Callback; |
81 | 115 | i.Header = Label; |
116 | menu.Items.Add(i); | |
117 | } | |
118 | } | |
119 | } |