ui/wpf/UIcore/Menu.cs

Tue, 29 Oct 2024 11:52:01 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 29 Oct 2024 11:52:01 +0100
branch
newapi
changeset 370
822fcb83bdf1
parent 137
c9b8b9e0cfe8
permissions
-rw-r--r--

improve window close handling

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;
137
c9b8b9e0cfe8 adds drawingarea (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
8 using System.Windows.Media;
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 namespace UI
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 public class ApplicationMenu
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 private List<Menu> current = new List<Menu>();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 public List<Menu> Menus = new List<Menu>();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 public void AddMenu(String label)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 current.Clear();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 Menu menu = new Menu(label);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 current.Add(menu);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 Menus.Add(menu);
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 public Boolean IsEmpty()
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 return Menus.Count == 0 ? true : false;
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 public void AddSubMenu(String label)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 Menu menu = new Menu(label);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 current.Last().Items.Add(menu);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 current.Add(menu);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
82
0cdb8089a29f added event handler for menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 81
diff changeset
38 public void EndSubMenu()
81
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 current.Remove(current.Last());
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 if(current.Count != 0)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 MenuItem item = new MenuItem(label, action);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 current.Last().Items.Add(item);
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 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 foreach (Menu m in Menus)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 menu.Items.Add(i);
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 return menu;
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 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 public interface IMenuItem
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 }
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 class Menu : IMenuItem
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 public String Label;
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 public List<IMenuItem> Items = new List<IMenuItem>();
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 public Menu(String 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 Label = label;
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 foreach (IMenuItem item in Items)
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 menu.Items.Add(i);
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 }
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 public class MenuItem : IMenuItem
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 Label = label;
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 Action = action;
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 {
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 i.Header = Label;
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 menu.Items.Add(i);
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
5eb765a7a793 added menus (WPF)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }

mercurial