-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yata_menufile.cs
650 lines (558 loc) · 17.2 KB
/
Yata_menufile.cs
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
using System;
using System.IO;
using System.Windows.Forms;
namespace yata
{
sealed partial class Yata
{
#region Fields
string _preset;
string _lastSaveasDirectory;
/// <summary>
/// A flag that prevents a Readonly warning/error from showing twice.
/// </summary>
bool _warned;
#endregion Fields
#region Handlers (file)
/// <summary>
/// Handles opening the File menu along with the preset-dirs and
/// recent-files subits.
/// </summary>
/// <param name="sender"><c><see cref="it_MenuFile"/></c></param>
/// <param name="e"></param>
void file_dropdownopening(object sender, EventArgs e)
{
it_Reload.Enabled = Table != null && File.Exists(Table.Fullpath);
if (Options._dirpreset.Count != 0) // directory presets ->
{
ToolStripItemCollection presets = it_OpenFolder.DropDownItems;
for (int i = presets.Count - 1; i != -1; --i)
presets[i].Dispose();
presets.Clear();
ToolStripItem preset;
foreach (var dir in Options._dirpreset)
{
if (Directory.Exists(dir))
{
preset = presets.Add(dir);
preset.Click += fileclick_OpenFolder;
}
}
it_OpenFolder.Visible = presets.Count != 0;
}
if (Options._recent != 0) // recent files ->
{
ToolStripItem it;
ToolStripItemCollection recents = it_Recent.DropDownItems;
for (int i = recents.Count - 1; i != -1; --i)
{
if (!File.Exists((it = recents[i]).Text))
{
recents.Remove(it);
it.Dispose();
}
}
it_Recent.Visible = recents.Count != 0;
}
}
/// <summary>
/// Handles it-click to create a new 2da-file.
/// </summary>
/// <param name="sender"><c><see cref="it_Create"/></c></param>
/// <param name="e"></param>
/// <seealso cref="YataGrid.LoadTable()"><c>YataGrid.LoadTable()</c></seealso>
/// <seealso cref="CreatePage()"><c>CreatePage()</c></seealso>
void fileclick_Create(object sender, EventArgs e)
{
Table = new YataGrid(this, String.Empty, false);
Table.CreateTable(); // <- instead of LoadTable()
_isCreate = true;
fileclick_SaveAs(it_SaveAs, EventArgs.Empty); // shall set Fullpath (incl. tab-string).
_isCreate = false;
if (File.Exists(Table.Fullpath) // ~safety // instead of CreatePage() ->
&& Path.GetFileNameWithoutExtension(Table.Fullpath).Length != 0) // what idjut would ... oh wait.
{
AddRecentFile(Table.Fullpath);
// DrawRegulator.SuspendDrawing(Table);
var tab = new TabPage();
Tabs.TabPages.Add(tab);
tab.Tag = Table;
tab.Text = Path.GetFileNameWithoutExtension(Table.Fullpath);
tab.Controls.Add(Table);
Tabs.SelectedTab = tab;
Table.Init();
// DrawRegulator.ResumeDrawing(Table);
}
else
{
YataGrid._init = false;
Table.Dispose();
}
_bypassVerifyFile = true;
tab_SelectedIndexChanged(null, EventArgs.Empty);
_bypassVerifyFile = false;
}
bool _isCreate;
/// <summary>
/// Handles it-click to open a 2da-file in a preset folder.
/// </summary>
/// <param name="sender"><c><see cref="it_OpenFolder"/></c> subits</param>
/// <param name="e"></param>
void fileclick_OpenFolder(object sender, EventArgs e)
{
_preset = (sender as ToolStripItem).Text;
fileclick_Open(sender, e);
}
/// <summary>
/// Handles it-click to open a 2da-file.
/// </summary>
/// <param name="sender">
/// <list type="bullet">
/// <item><c><see cref="it_Open"/></c></item>
/// <item><c><see cref="it_OpenFolder"/></c> subits</item>
/// </list></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|Open <c>[Ctrl+o]</c></item>
/// <item>File|Open@Folder ...
/// <c><see cref="fileclick_OpenFolder()">fileclick_OpenFolder()</see></c>
/// subits</item>
/// </list></remarks>
void fileclick_Open(object sender, EventArgs e)
{
using (var ofd = new OpenFileDialog())
{
if (sender != it_Open) // invoked by fileclick_OpenFolder() ->
{
ofd.RestoreDirectory = true;
ofd.FileName = Path.Combine(_preset, "*.2da");
}
else
ofd.FileName = Path.Combine(GetCurrentDirectory(), "*.2da");
ofd.Title = " Select a 2da file";
ofd.Filter = GetFileFilter("2da");
ofd.ShowReadOnly = // <- forces (AutoUpgradeEnabled=false)
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
bool read = ofd.ReadOnlyChecked;
foreach (var pfe in ofd.FileNames)
CreatePage(pfe, read); // load 1+ file(s) by openfiledialog
}
}
}
/// <summary>
/// Handles it-click to reload the current table. Requests
/// user-confirmation if data has changed etc.
/// </summary>
/// <param name="sender">
/// <list type="bullet">
/// <item><c><see cref="it_Reload"/></c></item>
/// <item><c><see cref="tabit_Reload"/></c></item>
/// <item><c>null</c></item>
/// </list></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|Reload <c>[Ctrl+r]</c></item>
/// <item>tab|Reload</item>
/// <item><c><see cref="VerifyCurrentFileState()">VerifyCurrentFileState()</see></c></item>
/// </list></remarks>
internal void fileclick_Reload(object sender, EventArgs e)
{
if (File.Exists(Table.Fullpath)) // check 'Table.Fullpath' in case user presses [Ctrl+r] after deleting the 2da-file on the hardrive.
{
bool reload = !Table.Changed;
if (!reload)
{
using (var ib = new Infobox(Infobox.Title_alert,
"Data has changed. Okay to reload ...",
null,
InfoboxType.Warn,
InfoboxButtons.CancelYes))
{
reload = ib.ShowDialog(this) == DialogResult.OK;
}
}
if (reload)
{
Obfuscate();
if (_diff1 == Table) _diff1 = null;
else if (_diff2 == Table) _diff2 = null;
_bypassVerifyFile = true;
int result = Table.LoadTable();
if (result != YataGrid.LOADRESULT_FALSE)
{
// DrawRegulator.SuspendDrawing(Table);
Table._ur.Clear();
it_freeze1.Checked =
it_freeze2.Checked = false;
Table.Init(result == YataGrid.LOADRESULT_CHANGED, true);
if (Table.Propanel != null)
{
Table.Controls.Remove(Table.Propanel);
Table.Propanel = null;
}
// DrawRegulator.ResumeDrawing(Table);
}
else
{
Table.Changed = false; // bypass Close warn
fileclick_CloseTabpage(sender, e);
}
_bypassVerifyFile = false;
if (Table != null)
{
Obfuscate(false);
VerifyCurrentFileState();
}
}
}
else
{
using (var ib = new Infobox(Infobox.Title_error,
"File could not be found.",
Table.Fullpath,
InfoboxType.Error))
{
ib.ShowDialog(this);
}
}
}
/// <summary>
/// Opens a 2da-file from the recent-files list.
/// </summary>
/// <param name="sender"><c><see cref="it_Recent"/></c> subits</param>
/// <param name="e"></param>
/// <remarks>Invalid subits get deleted when dropdown opens.</remarks>
void fileclick_Recent(object sender, EventArgs e)
{
CreatePage((sender as ToolStripItem).Text); // load file from recents
}
/// <summary>
/// Allows user to set the current table's readonly flag after it has
/// been opened.
/// </summary>
/// <param name="sender"><c><see cref="it_Readonly"/></c></param>
/// <param name="e"></param>
/// <remarks>The readonly setter appears otherwise only in the file-open
/// dialog.
///
///
/// Called by
/// <list type="bullet">
/// <item>File|Readonly <c>[F12]</c></item>
/// </list></remarks>
void fileclick_Readonly(object sender, EventArgs e)
{
it_Save .Enabled = !(Table.Readonly = it_Readonly.Checked);
it_SaveAll.Enabled = AllowSaveAll();
EnableCelleditOperations();
EnableRoweditOperations();
// col-edit operations are detered by its dropdown event
if (_replacer != null)
_replacer.EnableReplace();
it_OrderRows.Enabled = !Table.Readonly;
Tabs.Invalidate();
}
/// <summary>
/// Handles several it-clicks that write a <c><see cref="YataGrid"/></c>
/// to a 2da-file.
/// </summary>
/// <param name="sender">
/// <list type="bullet">
/// <item><c><see cref="it_Save"/></c></item>
/// <item><c><see cref="tabit_Save"/></c></item>
/// <item><c><see cref="it_SaveAs"/></c></item>
/// <item><c><see cref="it_SaveAll"/></c></item>
/// <item><c>null</c></item>
/// </list></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|Save <c>[Ctrl+s]</c></item>
/// <item>tab|Save</item>
/// <item>File|SaveAs <c>[Ctrl+e]</c> <c><see cref="fileclick_SaveAs()">fileclick_SaveAs()</see></c></item>
/// <item>File|SaveAll <c>[Ctrl+a]</c> <c><see cref="fileclick_SaveAll()">fileclick_SaveAll()</see></c></item>
/// <item><c><see cref="VerifyCurrentFileState()">VerifyCurrentFileState()</see></c></item>
/// </list></remarks>
internal void fileclick_Save(object sender, EventArgs e)
{
bool overwrite; // force a Readonly file to overwrite itself (only if invoked by SaveAs)
bool bypassReadonly;
if (sender == it_SaveAs)
{
_table = Table;
// '_pfeT' is set by caller
overwrite = (_pfeT == _table.Fullpath);
bypassReadonly = false;
}
else if (sender == it_SaveAll)
{
// '_table' and '_pfeT' are set by caller
overwrite = false;
bypassReadonly = false;
}
else // is rego-save or tab-save or 'FileWatcherDialog' save
{
_table = Table;
_pfeT = _table.Fullpath;
overwrite = false;
if (sender == it_Save || sender == tabit_Save)
bypassReadonly = false;
else
bypassReadonly = true; // only 'VerifyCurrentFileState()' gets to bypass Readonly.
}
_warned = false;
if (!_table.Readonly || bypassReadonly
|| (overwrite && SaveWarning("The 2da-file is opened as readonly.")))
{
// if ((_table._sortcol == 0 && _table._sortdir == YataGrid.SORT_ASC)
// || SaveWarning("The 2da is not sorted by ascending ID."))
// {
if (CheckRowOrder() || SaveWarning("Faulty row ids are detected."))
{
_table.Fullpath = _pfeT;
SetTitlebarText();
if (!_isCreate) // stuff that's unneeded and/or unwanted when creating a 2da ->
{
if (overwrite) _table.Readonly = false; // <- IMPORTANT: If a file that was opened Readonly
// is saved *as itself* it loses its Readonly flag.
_table.Changed = false;
_table._ur.ResetSaved();
_table.ClearReplaced(); // these toggle YataGrid._init so don't do that when creating a 2da ->
_table.ClearLoadchanged();
if (_table == Table)
_table.Invalidator(YataGrid.INVALID_GRID | YataGrid.INVALID_FROZ);
}
FileOutput.Write(_table);
if (sender == it_SaveAs && File.Exists(_table.Fullpath))
{
AddRecentFile(_table.Fullpath);
}
}
// }
}
else if (!_warned)
{
using (var ib = new Infobox(Infobox.Title_error,
"The 2da-file is opened as readonly.",
null,
InfoboxType.Error))
{
ib.ShowDialog(this);
}
}
}
/// <summary>
/// Requests user-confirmation when saving a file when readonly or when
/// a faulty row-id is detected.
/// </summary>
/// <param name="head"></param>
/// <returns><c>true</c> to proceed - <c>false</c> to stop</returns>
bool SaveWarning(string head)
{
_warned = true;
using (var ib = new Infobox(Infobox.Title_alert,
head + " Save anyway ...",
null,
InfoboxType.Warn,
InfoboxButtons.CancelYes))
{
return ib.ShowDialog(this) == DialogResult.OK;
}
}
/// <summary>
/// Checks the row-order before save.
/// </summary>
/// <returns><c>true</c> if row-order is okay</returns>
static bool CheckRowOrder()
{
int result;
for (int r = 0; r != Table.RowCount; ++r)
if (!Int32.TryParse(Table[r,0].text, out result) || result != r)
return false;
return true;
}
/// <summary>
/// Handles it-click on File|SaveAs.
/// </summary>
/// <param name="sender"><c><see cref="it_SaveAs"/></c></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|SaveAs <c>[Ctrl+e]</c></item>
/// <item>File|Create ...</item>
/// </list></remarks>
void fileclick_SaveAs(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog())
{
sfd.Title = " Save as ...";
sfd.Filter = GetFileFilter("2da");
sfd.AutoUpgradeEnabled = false;
string dir;
if (Directory.Exists(_lastSaveasDirectory))
{
dir = _lastSaveasDirectory;
}
else if (Table.Fullpath.Length == 0
|| !Directory.Exists(dir = Path.GetDirectoryName(Table.Fullpath)))
{
dir = GetCurrentDirectory();
}
sfd.FileName = Path.Combine(dir, Path.GetFileName(Table.Fullpath));
if (sfd.ShowDialog() == DialogResult.OK)
{
_lastSaveasDirectory = Path.GetDirectoryName(sfd.FileName);
_pfeT = sfd.FileName;
fileclick_Save(sender, e);
}
}
}
/// <summary>
/// Handles it-click on File|SaveAll.
/// </summary>
/// <param name="sender"><c><see cref="it_SaveAll"/></c></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|SaveAll <c>[Ctrl+a]</c></item>
/// </list></remarks>
void fileclick_SaveAll(object sender, EventArgs e)
{
IsSaveAll = true;
bool changed = false;
for (int i = 0; i != Tabs.TabCount; ++i)
{
_table = Tabs.TabPages[i].Tag as YataGrid;
if (!_table.Readonly)
{
if (_table.Changed)
changed = true;
_pfeT = _table.Fullpath;
fileclick_Save(sender, e);
}
}
if (changed) SetAllTabTexts();
IsSaveAll = false;
}
/// <summary>
/// Handles it-click on File|Close.
/// </summary>
/// <param name="sender">
/// <list type="bullet">
/// <item><c><see cref="it_Close"/></c></item>
/// <item><c><see cref="it_Reload"/></c></item>
/// <item><c><see cref="tabit_Close"/></c></item>
/// <item><c><see cref="tabit_Reload"/></c></item>
/// <item><c>null</c></item>
/// </list></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|Close <c>[F4]</c></item>
/// <item>tab|Close</item>
/// <item>File|Reload <c>[Ctrl+r]</c>
/// <c><see cref="fileclick_Reload()">fileclick_Reload()</see></c></item>
/// <item>tab|Reload
/// <c><see cref="fileclick_Reload()">fileclick_Reload()</see></c></item>
/// <item><c><see cref="VerifyCurrentFileState()">VerifyCurrentFileState()</see></c></item>
/// </list></remarks>
internal void fileclick_CloseTabpage(object sender, EventArgs e)
{
bool close = !Table.Changed;
if (!close)
{
using (var ib = new Infobox(Infobox.Title_alert,
"Data has changed. Okay to close ...",
null,
InfoboxType.Warn,
InfoboxButtons.CancelYes))
{
close = ib.ShowDialog(this) == DialogResult.OK;
}
}
if (close)
{
// DrawRegulator.SuspendDrawing(this); // stops tab-flickering on Remove tab
CloseTabpage(Tabs.SelectedTab);
// DrawRegulator.ResumeDrawing(this);
}
}
/// <summary>
/// Handles it-click on File|CloseAll.
/// </summary>
/// <param name="sender">
/// <list type="bullet">
/// <item><c><see cref="it_CloseAll"/></c></item>
/// <item><c><see cref="tabit_CloseAll"/></c></item>
/// </list></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|CloseAll <c>[Ctrl+F4]</c></item>
/// <item>tab|CloseAll</item>
/// </list></remarks>
void fileclick_CloseTabpages(object sender, EventArgs e)
{
if (!CancelChangedTables("close"))
{
// DrawRegulator.SuspendDrawing(this); // stops tab-flickering on Remove tab
for (int tabid = Tabs.TabCount - 1; tabid != -1; --tabid)
CloseTabpage(Tabs.TabPages[tabid], true);
_lasttabs.Clear();
// DrawRegulator.ResumeDrawing(this);
}
}
/// <summary>
/// Handles it-click on File|Quit.
/// </summary>
/// <param name="sender"><c><see cref="it_Quit"/></c></param>
/// <param name="e"></param>
/// <remarks>Called by
/// <list type="bullet">
/// <item>File|Quit <c>[Ctrl+q]</c></item>
/// </list></remarks>
void fileclick_Quit(object sender, EventArgs e)
{
Close(); // let yata_Closing() handle it ...
}
#endregion Handlers (file)
#region Methods (file)
/// <summary>
/// Checks if there is a non-readonly table open. Also checks if there
/// are two non-readonly instances of the same 2da-file open.
/// </summary>
/// <returns><c>true</c> if SaveAll is allowed</returns>
bool AllowSaveAll()
{
if (Options._allowdupls)
{
// iterate through all tables and if a different table has the
// same Fullpath and neither table is Readonly return false ->
YataGrid table; string pfe;
for (int i = 0; i != Tabs.TabPages.Count; ++i)
if (!(table = Tabs.TabPages[i].Tag as YataGrid).Readonly)
{
pfe = table.Fullpath;
for (int j = 0; j != Tabs.TabPages.Count; ++j)
if (j != i
&& !(table = Tabs.TabPages[j].Tag as YataGrid).Readonly
&& table.Fullpath == pfe)
{
return false;
}
}
}
// next just iterate over all tables and if any is not Readonly
// allow SaveAll ->
for (int i = 0; i != Tabs.TabCount; ++i)
if (!(Tabs.TabPages[i].Tag as YataGrid).Readonly)
return true;
return false;
}
#endregion Methods (file)
}
}