ui/wpf/UIcore/MainToolBar.cs

Sun, 17 Nov 2024 15:19:32 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 17 Nov 2024 15:19:32 +0100
changeset 87
5360027fb282
parent 0
2483f517c562
permissions
-rw-r--r--

download/upload refactoring, add cancel button, resolves #498 fixes #506

0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 using System;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 using System.Collections.Generic;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 using System.Linq;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 using System.Text;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 using System.Threading.Tasks;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 using System.Windows;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 using System.Windows.Controls;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 using System.Windows.Media;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 namespace UI
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 public class MainToolBar
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 Dictionary<string, IToolItem> Items = new Dictionary<string, IToolItem>();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 List<string> Defaults = new List<string>();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 public MainToolBar()
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 public bool HasItems()
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 return Defaults.Count > 0 ? true : false;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 public void AddDefault(string itemName)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 Defaults.Add(itemName);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 public void AddToolItem(string name, string label, Action<IntPtr> action)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 ToolItem item = new ToolItem();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 item.Label = label;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 item.Action = action;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 Items.Add(name, item);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 public ToolBarTray CreateToolBarTray(IntPtr objptr)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 ToolBarTray tray = new ToolBarTray();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 ToolBar toolbar = new ToolBar();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 tray.ToolBars.Add(toolbar);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 foreach(string s in Defaults)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 IToolItem item = Items[s];
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 item.AddTo(toolbar, objptr);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 return tray;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 public interface IToolItem
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 void AddTo(System.Windows.Controls.ToolBar toolbar, IntPtr uiobj);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 public class ToolItem : IToolItem
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 public string Label { get; set; }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 // TODO: icon
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 public Action<IntPtr> Action { get; set; }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 public void AddTo(System.Windows.Controls.ToolBar toolbar, IntPtr uiobj)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 Button button = new Button();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 button.Content = Label;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 EventCallback e = new EventCallback(uiobj, Action);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 button.Click += e.Callback;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 toolbar.Items.Add(button);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }

mercurial