Repository navigation
Expand file tree
/
Copy pathMainForm.cs
More file actions
114 lines (99 loc) · 3.71 KB
/
Copy pathMainForm.cs
File metadata and controls
114 lines (99 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright Bastian Eicher
// Licensed under the MIT License
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using NanoByte.Common.Undo;
using NanoByte.StructureEditor.Sample.Controls;
namespace NanoByte.StructureEditor.Sample;
public class MainForm : Form
{
private readonly AddressBookEditor _editor = new()
{
Dock = DockStyle.Fill,
Padding = new Padding(8)
};
public MainForm()
{
SuspendLayout();
SetupComponents();
ResumeLayout(performLayout: false);
_editor.Open(CommandManager.For(SampleData.AddressBook));
UpdateCaption();
}
private void SetupComponents()
{
AutoScaleDimensions = new SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 600);
FormClosing += (_, e) => { e.Cancel = !_editor.Closing(); };
Controls.Add(_editor);
static ToolStripMenuItem MenuItem(string text, Action action, Keys hotKey = Keys.None)
{
var button = new ToolStripMenuItem {Text = text, ShortcutKeys = hotKey};
button.Click += (_, _) => action();
return button;
}
void FileAction(Action action)
{
action();
UpdateCaption();
}
var formatMenu = new ToolStripMenuItem("F&ormat");
formatMenu.DropDownItems.Add(FormatMenuItem("&XML", XmlStructureSerializer.Instance));
#if NET
formatMenu.DropDownItems.Add(FormatMenuItem("&JSON", JsonStructureSerializer.Instance));
#endif
Controls.Add(new MenuStrip
{
Items =
{
new ToolStripMenuItem("&File")
{
DropDownItems =
{
MenuItem("&Open...", () => FileAction(_editor.Open), hotKey: Keys.Control | Keys.O),
MenuItem("&Save...", () => FileAction(() => _editor.Save()), hotKey: Keys.Control | Keys.S),
MenuItem("Save &As...", () => FileAction(() => _editor.SaveAs()))
}
},
new ToolStripMenuItem("&Edit")
{
DropDownItems =
{
MenuItem("&Undo", _editor.Undo, hotKey: Keys.Control | Keys.Z),
MenuItem("&Redo", _editor.Redo, hotKey: Keys.Control | Keys.Y)
}
},
formatMenu
}
});
}
private readonly List<ToolStripMenuItem> _formatMenuItems = [];
/// <summary>
/// Creates a menu item that switches the <see cref="IStructureSerializer"/> used for the text representation.
/// </summary>
private ToolStripMenuItem FormatMenuItem(string text, IStructureSerializer serializer)
{
var item = new ToolStripMenuItem {Text = text, Tag = serializer};
item.Click += (_, _) =>
{
_editor.Serializer = serializer;
UpdateCaption();
};
_formatMenuItems.Add(item);
return item;
}
/// <summary>
/// Shows the current file name and serialization format in the window title and checks the matching format menu item.
/// </summary>
private void UpdateCaption()
{
var path = _editor.CommandManager.Path;
Text = $"Structure Editor Sample - {(string.IsNullOrEmpty(path) ? "Untitled" : Path.GetFileName(path))} [{_editor.Serializer.FormatName}]";
foreach (var item in _formatMenuItems)
item.Checked = ReferenceEquals(item.Tag, _editor.Serializer);
}
}