-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormMain.cs
More file actions
261 lines (226 loc) · 8.58 KB
/
Copy pathFormMain.cs
File metadata and controls
261 lines (226 loc) · 8.58 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright © 2016-2024 ASM-SW
//asm-sw@outlook.com https://github.com/asm-sw
using MessageBoxCenteredDll;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace DonorStatement
{
public delegate void LogMessageDelegate(string msg);
public partial class FormMain : Form
{
private static Form MainForm { get; set; }
public static Rectangle Rectangle
{
get
{
Rectangle rect = new((int)MainForm.Left, (int)MainForm.Top, (int)MainForm.Width, (int)MainForm.Height);
return rect;
}
}
public static MessageBoxCentered.ButtonTyp MessageBox(string message) => MessageBoxCentered.ShowDialog(Rectangle, string.Empty, message, MessageBoxCentered.BoxType.Ok);
public static MessageBoxCentered.ButtonTyp MessageBoxError(string message) => MessageBoxCentered.ShowDialog(Rectangle, "ERROR", message, MessageBoxCentered.BoxType.Ok);
public static MessageBoxCentered.ButtonTyp MessageBoxEx(string caption, string message, MessageBoxCentered.BoxType boxType) =>
MessageBoxCentered.ShowDialog(Rectangle, caption, message, boxType);
public enum PanelNavDirection
{
forward,
backward
}
readonly private List<Form> m_forms = [];
LogMessageDelegate m_loggerDelegate;
readonly private UInt32 LogMaxLines = 250;
DocumentCreator m_docCreator;
FileParser m_parser;
int m_activeForm = -1;
public static ConfigurationDYES Config { get; private set; }
public FormMain()
{
m_loggerDelegate = LogMessage;
ConfigurationDYES cfg = new();
if (!ConfigurationDYES.DeSerialize(cfg.ConfigFileName, ref cfg))
Environment.Exit(-1);
Config = cfg;
m_docCreator = new DocumentCreator(m_loggerDelegate);
m_parser = new FileParser(m_loggerDelegate);
MainForm = this;
ColumnMap.SetLogger(m_loggerDelegate);
InitializeComponent();
InitializeControls();
}
private void InitializeControls()
{
// Add forms in the order to be displayed
m_forms.Add(new FormConfiguration());
m_forms.Add(new FormFileParser(ref m_parser));
m_forms.Add(new FormItemIgnore());
m_forms.Add(new FormCreateDocs(ref m_parser, ref m_docCreator, ref m_loggerDelegate));
// initial configuration for each form
foreach (Form item in m_forms)
{
item.TopLevel = false;
item.AutoScroll = true;
}
panel1.Controls.Add(m_forms[0]);
m_forms[0].Show();
butBack.Enabled = false;
label1.Text = "Version: " + Application.ProductVersion;
LogMessage(label1.Text);
LogMessage("Configuration data from last run: " + Config.ConfigFileName);
// load first form
SwitchPanelForm(PanelNavDirection.forward);
}
private void SetProgressText()
{
labelProgress.Text = string.Empty;
if (m_activeForm < 0)
return;
labelProgress.Text = string.Format("Step {0} of {1}", m_activeForm + 1, m_forms.Count);
}
private bool SubFormOkToExit()
{
if (m_forms[m_activeForm] is ISubForm form)
{
if (form.CanExit(out string errorMsg))
return true;
FormMain.MessageBoxError(errorMsg);
return false;
}
return true;
}
/// <summary>
/// Switch between the forms in panel1
/// </summary>
/// <param name="direction"> if true select next form if false select previous form</param>
private void SwitchPanelForm(PanelNavDirection direction)
{
SetProgressText();
if (m_forms.Count == 0)
{
labelProgress.Text = string.Empty;
m_activeForm = -1; // no active form
if (direction == PanelNavDirection.backward)
return; // no form selected, can't go backward
}
if (direction == PanelNavDirection.forward)
{
if (m_activeForm == m_forms.Count - 1)
return; // already selected last form, can't go forward
if (m_activeForm >= 0 && !SubFormOkToExit())
return;
++m_activeForm;
}
else
{
if (m_activeForm != 0)
if (!SubFormOkToExit())
return;
--m_activeForm;
}
if (m_forms.Count > 0)
{
// remove active form
panel1.Controls[0].Hide();
panel1.Controls.RemoveAt(0);
}
panel1.Controls.Add(m_forms[m_activeForm]);
panel1.Controls[0].Show();
labelStep.Text = panel1.Controls[0].AccessibleDescription;
// figure out button state
butNext.Enabled = true;
butBack.Enabled = true;
if (m_activeForm == 0)
butBack.Enabled = false;
if (m_activeForm == m_forms.Count - 1)
butNext.Enabled = false;
SetProgressText();
} // SwitchPanelForm
/// <summary>
/// Log a message to the log window
/// </summary>
/// <param name="msg">the message</param>
public void LogMessage(string msg)
{
if (lbLogging.InvokeRequired)
{
LogMessageDelegate update = new(LogMessage);
lbLogging.Invoke(update, msg);
}
else
{
lbLogging.ClearSelected();
// add item at top, remove lines at bottom
lbLogging.Items.Insert(0, msg);
while (lbLogging.Items.Count > LogMaxLines - 1)
lbLogging.Items.RemoveAt(lbLogging.Items.Count - 1);
}
} // LogMessage
/// <summary>
/// Contect menu for the logging control. Provides way to select all, copy and clear.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ContextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
switch (e.ClickedItem.Text)
{
case "&Copy":
{
StringBuilder str = new();
if (lbLogging.SelectedItems.Count == 0)
break;
foreach (var item in lbLogging.SelectedItems)
str.AppendLine(item.ToString());
Clipboard.SetText(str.ToString());
}
break;
case "C&lear":
lbLogging.Items.Clear();
break;
case "Select &All":
for (int i = 0; i < lbLogging.Items.Count; ++i)
lbLogging.SetSelected(i, true);
break;
default:
break;
}
}// contextMenuStrip1_ItemClicked
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
m_docCreator.Close();
Config.Serialize(Config.ConfigFileName);
}
private void ButNext_Click(object sender, EventArgs e)
{
SwitchPanelForm(PanelNavDirection.forward);
}
private void ButBack_Click(object sender, EventArgs e)
{
SwitchPanelForm(PanelNavDirection.backward);
}
private void ButtonAbout_Click(object sender, EventArgs e)
{
AboutBox1 aboutBox = new(this);
aboutBox.ShowDialog();
}
private void ButtonHelp_Click(object sender, EventArgs e)
{
string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DonorStatement.pdf");
try
{
System.Diagnostics.Process.Start("explorer.exe", fileName);
}
catch (Exception)
{
System.Windows.Forms.MessageBox.Show("Unable to open help file: " + fileName);
}
}
private void Exit_Click(object sender, EventArgs e)
{
Close();
}
} // Form1
}