-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
483 lines (431 loc) · 14.8 KB
/
MainForm.cs
File metadata and controls
483 lines (431 loc) · 14.8 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
namespace ForestPlot
{
using System;
using System.IO;
using System.Windows.Forms;
using Bitmap = System.Drawing.Bitmap;
using Size = System.Drawing.Size;
public partial class MainForm : Form
{
private const int SaveFileFilterIndexCmykTiff = 3;
private readonly Plotter plotter = new Plotter();
private readonly Form helpForm;
private readonly string initialText;
private float dpiX, dpiY;
private string dataFileName = "";
private bool dirty;
private PlotTarget plotTarget = PlotTarget.Printer;
private Zoom plotZoom = Zoom.Inches100;
private enum Zoom
{
None,
Pixels50,
Pixels100,
Pixels200,
Inches50,
Inches100,
Inches200
}
public MainForm()
{
this.InitializeComponent();
this.helpForm = new HelpForm();
this.initialText = this.Text ?? "";
}
private PlotTarget PlotTarget
{
get
{
return this.plotTarget;
}
set
{
if (value != this.plotTarget)
{
this.plotTarget = value;
this.screenImageMenu.Checked = value == PlotTarget.Screen;
this.printerImageMenu.Checked = value == PlotTarget.Printer;
this.RenderImage();
}
}
}
private Zoom PlotZoom
{
get
{
return this.plotZoom;
}
set
{
if (value != this.plotZoom)
{
this.plotZoom = value;
this.zoomPixels50.Checked = value == Zoom.Pixels50;
this.zoomPixels100.Checked = value == Zoom.Pixels100;
this.zoomPixels200.Checked = value == Zoom.Pixels200;
this.zoomInches50.Checked = value == Zoom.Inches50;
this.zoomInches100.Checked = value == Zoom.Inches100;
this.zoomInches200.Checked = value == Zoom.Inches200;
this.ZoomImage();
}
}
}
private bool Dirty
{
get
{
return this.dirty;
}
set
{
if (value != this.Dirty)
{
this.UpdateDataFileName(this.DataFileName, value);
}
}
}
private string DataFileName
{
get
{
return this.dataFileName;
}
set
{
var valueOrEmpty = value ?? "";
if (valueOrEmpty != this.dataFileName)
{
this.UpdateDataFileName(valueOrEmpty, this.Dirty);
}
}
}
private void OnFormLoaded(object sender, EventArgs e)
{
using (var g = this.CreateGraphics())
{
this.dpiX = g.DpiX;
this.dpiY = g.DpiY;
}
var sample = Resource.Sample;
this.data.Text = sample;
this.data.Select(0, 0);
this.plotter.DataChanged += this.PlotterDataChanged;
this.plotter.DataText = sample;
this.Dirty = false;
}
private void OnFormClosing(object sender, FormClosingEventArgs e)
{
if (!this.QuerySaveData())
{
e.Cancel = true;
}
}
private void PlotterDataChanged(object sender, EventArgs e)
{
this.RenderImage();
}
private void OpenDataClicked(object sender, EventArgs e)
{
try
{
if (this.QuerySaveData() &&
this.openDataDialog.ShowDialog(this) == DialogResult.OK)
{
string filename = this.openDataDialog.FileName;
this.data.Text = File.ReadAllText(filename);
this.UpdateDataFileName(filename, false);
this.plotter.DataText = this.data.Text;
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
MessageBox.Show(this, ex.Message, "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SaveDataClicked(object sender, EventArgs e)
{
this.SaveData(false);
}
private void SaveDataAsClicked(object sender, EventArgs e)
{
this.SaveData(true);
}
private void RedrawImageClicked(object sender, EventArgs e)
{
this.plotter.DataText = this.data.Text;
}
private void SaveScreenImageClicked(object sender, EventArgs e)
{
SaveImage(this.saveScreenImageDialog, PlotTarget.Screen);
}
private void SavePrinterImageClicked(object sender, EventArgs e)
{
SaveImage(this.savePrinterImageDialog, PlotTarget.Printer);
}
private void SaveImage(SaveFileDialog saveFileDialog, PlotTarget plotTarget)
{
try
{
this.plotter.DataText = this.data.Text;
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
var saveFileName = saveFileDialog.FileName;
var emf = saveFileName.EndsWith(".emf", StringComparison.OrdinalIgnoreCase);
using (var bmp = this.plotter.Render(plotTarget, emf ? saveFileName : null))
{
if (!emf)
{
BitmapSaver.SaveRgb32(
bmp,
saveFileName,
this.optimizeImageFilesMenu.Checked,
saveFileDialog.FilterIndex == SaveFileFilterIndexCmykTiff);
}
}
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
MessageBox.Show(this, ex.Message, "Error saving file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ExitClicked(object sender, EventArgs e)
{
this.Close();
}
private void HelpViewHelpClicked(object sender, EventArgs e)
{
if (!this.helpForm.Visible)
{
this.helpForm.Show(this);
}
}
private void HelpViewLicenceClicked(object sender, EventArgs e)
{
MessageBox.Show(
this,
Resource.License,
"License Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private void ScreenImageClicked(object sender, EventArgs e)
{
this.PlotTarget = PlotTarget.Screen;
}
private void PrinterImageClicked(object sender, EventArgs e)
{
this.PlotTarget = PlotTarget.Printer;
}
private void ZoomPixels50(object sender, EventArgs e)
{
this.PlotZoom = Zoom.Pixels50;
}
private void ZoomPixels100(object sender, EventArgs e)
{
this.PlotZoom = Zoom.Pixels100;
}
private void ZoomPixels200(object sender, EventArgs e)
{
this.PlotZoom = Zoom.Pixels200;
}
private void ZoomInches50(object sender, EventArgs e)
{
this.PlotZoom = Zoom.Inches50;
}
private void ZoomInches100(object sender, EventArgs e)
{
this.PlotZoom = Zoom.Inches100;
}
private void ZoomInches200(object sender, EventArgs e)
{
this.PlotZoom = Zoom.Inches200;
}
private void PicturePanelResized(object sender, EventArgs e)
{
return;
}
private void DataChanged(object sender, EventArgs e)
{
this.Dirty = true;
}
private bool QuerySaveData()
{
bool canContinue;
if (this.Dirty)
{
var result = MessageBox.Show(
this,
"Do you want to save changes to your data?",
"Data changed",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1);
if (result == DialogResult.Cancel)
{
canContinue = false;
}
else if (result == DialogResult.No)
{
canContinue = true;
}
else
{
canContinue = this.SaveData(false);
}
}
else
{
canContinue = true;
}
return canContinue;
}
private bool SaveData(bool saveAs)
{
bool canContinue = false;
try
{
string filename;
if (!saveAs && !string.IsNullOrEmpty(this.dataFileName))
{
filename = this.dataFileName;
}
else if (this.saveDataDialog.ShowDialog(this) == DialogResult.OK)
{
filename = this.saveDataDialog.FileName;
}
else
{
filename = null;
}
if (filename != null)
{
File.WriteAllText(filename, this.data.Text);
this.UpdateDataFileName(filename, false);
canContinue = true;
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
MessageBox.Show(this, ex.Message, "Error saving file", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return canContinue;
}
private void UpdateDataFileName(string filename, bool dirty)
{
this.dataFileName = filename;
this.dirty = dirty;
this.UpdateWindowTitle();
}
private void UpdateWindowTitle()
{
string filename = string.IsNullOrEmpty(this.dataFileName)
? "SampleData.csv"
: this.dataFileName;
this.Text = this.initialText + " - " + filename + (this.dirty ? "*" : "");
}
private void RenderImage()
{
try
{
Bitmap bmp;
var old = this.picture.Image;
try
{
bmp = this.plotter.Render(
this.screenImageMenu.Checked
? PlotTarget.Screen
: PlotTarget.Printer);
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
bmp = this.plotter.RenderText(ex.Message);
}
this.picture.Image = bmp;
this.ZoomImage();
if (old != null)
{
old.Dispose();
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
MessageBox.Show(this, ex.Message, "Unexpected error rendering image", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ZoomImage()
{
try
{
var bmp = this.picture.Image as Bitmap;
if (bmp != null)
{
var scaleX = this.dpiX / bmp.HorizontalResolution;
var scaleY = this.dpiY / bmp.VerticalResolution;
Size size = bmp.Size;
switch (this.plotZoom)
{
default:
break;
case Zoom.Pixels50:
size = new Size(
size.Width / 2,
size.Height / 2);
break;
case Zoom.Pixels100:
break;
case Zoom.Pixels200:
size = new Size(
size.Width * 2,
size.Height * 2);
break;
case Zoom.Inches50:
size = new Size(
(int)((float)size.Width * scaleX * 0.5f),
(int)((float)size.Height * scaleY * 0.5f));
break;
case Zoom.Inches100:
size = new Size(
(int)((float)size.Width * scaleX * 1.0f),
(int)((float)size.Height * scaleY * 1.0f));
break;
case Zoom.Inches200:
size = new Size(
(int)((float)size.Width * scaleX * 2.0f),
(int)((float)size.Height * scaleY * 2.0f));
break;
}
this.picture.Size = size;
}
}
catch (Exception ex)
{
if (Program.ExceptionIsDangerous(ex))
{
throw;
}
MessageBox.Show(this, ex.Message, "Unexpected error zooming image", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}