-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYata.cs
1801 lines (1488 loc) · 51.4 KB
/
Yata.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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace yata
{
/// <summary>
/// Yata ....
/// </summary>
/// <remarks>Public access is required for the pointers in
/// <c><see cref="YataDialog"/></c>.</remarks>
public sealed partial class Yata
: Form
{
#region Enumerators
/// <summary>
/// Defines field fill-types used by
/// <c><see cref="RowCreatorDialog"/></c>.
/// </summary>
internal enum CrFillType
{ Stars, Selected, Copied }
#endregion Enumerators
#region Triggers
/// <summary>
/// whatever. Don't beep at us.
/// </summary>
internal event DontBeepEventHandler DontBeepEvent;
#endregion Triggers
#region Fields (static)
internal static Yata that;
const string TITLE = " Yata";
const string ASTERICS = " *";
const string RECENTCFG = "recent.cfg";
internal static string PfeLoad; // cl arg
static Graphics graphics;
const int FROZEN_COL_Id = 0;
const int FROZEN_COL_First = 1;
const int FROZEN_COL_Second = 2;
#endregion Fields (static)
#region Fields
readonly PropanelButton bu_Propanel = new PropanelButton();
/// <summary>
/// The <c><see cref="ClipboardEditor"/></c> dialog/editor.
/// </summary>
internal ClipboardEditor _fclip;
/// <summary>
/// A 2d-array of <c>strings</c> used for copy/paste cell.
/// </summary>
/// <remarks>A cell's text shall never be <c>null</c> or blank therefore
/// <c>_copytext</c> shall never be <c>null</c> or blank.</remarks>
internal string[,] _copytext = {{ gs.Stars }};
/// <summary>
/// The count of rows in <c><see cref="_copytext"/></c>.
/// </summary>
internal int _copyvert;
/// <summary>
/// The count of cols in <c><see cref="_copytext"/></c>.
/// </summary>
internal int _copyhori;
/// <summary>
/// A <c>List</c> of <c>string[]</c> arrays used for copy/paste row(s).
/// </summary>
internal List<string[]> _copyr = new List<string[]>();
/// <summary>
/// A <c>List</c> of <c>strings</c> used for copy/paste col.
/// </summary>
internal List<string> _copyc = new List<string>();
internal int _startCr, _lengthCr;
internal CrFillType _fillCr;
/// <summary>
/// The <c><see cref="FontDialog"/></c> font-picker.
/// </summary>
FontDialog _ffont;
Font FontDefault;
internal Font FontAccent;
internal DifferDialog _fdiffer;
internal YataGrid _diff1, _diff2;
internal ReplaceTextDialog _replacer;
/// <summary>
/// Caches a fullpath when doing SaveAs.
/// So that the Table's new path-variables don't get assigned unless the
/// save is successful - ie. verify several conditions first.
/// </summary>
string _pfeT = String.Empty;
/// <summary>
/// A pointer to a <c><see cref="YataGrid"/></c> that shall be used
/// during the save-routine. Is required because it can't be assumed
/// that the current <c><see cref="Table"/></c> is the table being
/// saved; that is, the SaveAll operation needs to cycle through all
/// tables.
/// </summary>
/// <seealso cref="fileclick_SaveAll()"><c>fileclick_SaveAll()</c></seealso>
YataGrid _table;
/// <summary>
/// String-input for InfoInputSpells or InfoInputFeat or
/// InfoInputClasses (re PathInfo). <c>str0</c> is the current value;
/// <c>str1</c> will be the user-chosen value that's assigned on Accept.
/// </summary>
internal string str0, str1;
/// <summary>
/// Int-input for InfoInputSpells or InfoInputFeat or InfoInputClasses
/// (re PathInfo). <c>int0</c> is the current value; <c>int1</c> will be
/// the user-chosen value that's assigned on Accept.
/// </summary>
internal int int0, int1;
// NOTE: These are to initialize 'int0' and 'int1' and need to be
// different to recognize that an invalid current value should be
// changed to stars (iff the user accepts the dialog).
internal const int Info_INIT_INVALID = -2; // for 'int0' only
internal const int Info_ASSIGN_STARS = -1; // for 'int1' or 'int0'
/// <summary>
/// Works in conjunction w/
/// <c><see cref="YataGrid"></see>.OnResize()</c>.
/// </summary>
internal bool IsMin;
internal int _track_x = -1; // tracks last mouseover coords ->
internal int _track_y = -1;
/// <summary>
/// Hides any info that's currently displayed on the statusbar when the
/// cursor leaves the table-area.
/// </summary>
/// <remarks>Maintain namespace to differentiate
/// <c>System.Threading.Timer</c>. jic.</remarks>
System.Windows.Forms.Timer _t1 = new System.Windows.Forms.Timer();
/// <summary>
/// A <c>bool</c> indicating that a
/// <c><see cref="FileWatcherDialog"/></c> is already invoked so don't
/// try to invoke another one.
/// </summary>
/// <remarks>Can also be used to bypass
/// <c><see cref="VerifyCurrentFileState()">VerifyCurrentFileState()</see></c>
/// when loading or creating a 2da-file or closing Yata etc.
///
///
/// Set <c>true</c> by
/// <list type="bullet">
/// <item><c><see cref="VerifyCurrentFileState()">VerifyCurrentFileState()</see></c></item>
/// <item><c><see cref="OnFormClosing()">OnFormClosing()</see></c></item>
/// <item><c><see cref="CreatePage()">CreatePage()</see></c></item>
/// <item><c><see cref="fileclick_Create()">fileclick_Create()</see></c></item>
/// <item><c><see cref="fileclick_Reload()">fileclick_Reload()</see></c></item>
/// </list></remarks>
bool _bypassVerifyFile;
/// <summary>
/// A result returned by
/// <c><see cref="FileWatcherDialog"/>.OnFormClosing()</c>.
/// </summary>
internal FileWatcherDialog.Output _fileresult;
/// <summary>
/// Stores previously focused <c><see cref="TabPage">TabPages</see></c>
/// in a <c>List</c>. The most recently deselected page is last in the
/// list. This is used to revert focus to the last selected table when a
/// table is closed. The currently focused table (if any) is not in the
/// list.
/// </summary>
readonly List<TabPage> _lasttabs = new List<TabPage>();
#endregion Fields
#region Properties (static)
/// <summary>
/// There can be only 1 <c>Table</c>.
/// </summary>
internal static YataGrid Table
{ get; private set; }
#endregion Properties (static)
#region Properties
readonly YataTabs tabControl = new YataTabs();
internal YataTabs Tabs
{ get { return tabControl; } }
internal bool IsSaveAll
{ get; private set; }
#endregion Properties
#region cTor
/// <summary>
/// cTor. This is Yata.
/// </summary>
internal Yata()
{
// Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
that = this;
// init 'Tabs' ->
Tabs.DrawItem += tab_DrawItem;
Tabs.SelectedIndexChanged += tab_SelectedIndexChanged;
Controls.Add(Tabs);
// init 'bu_Propanel' ->
bu_Propanel.MouseDown += mousedown_buPropanel;
bu_Propanel.MouseUp += mouseup_buPropanel;
Controls.Add(bu_Propanel);
InitializeComponent();
_bar.setYata(this);
statusbar.Renderer = new StripRenderer();
tb_Search.BackColor =
tb_Goto .BackColor = Color.GhostWhite;
Tabs.MouseClick += click_Tabs;
// DrawRegulator.SetDoubleBuffered(this);
SetStyle(ControlStyles.OptimizedDoubleBuffer
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.UserPaint
| ControlStyles.ResizeRedraw, true);
// IMPORTANT: The Client-area apart from the Menubar and Statusbar
// has both a TabControl and a solid-colored Panel that *overlay*
// each other and fill the area. The Panel is on top and is used
// only to color the Client-area (else TabControl is pure white) -
// it is shown when there are no TabPages and hides when there are.
// It also appears when loading a 2da in an attempt to hide
// unsightly graphical glitches.
//
// TODO: Instead of using BringToFront() and SendToBack() to show or
// hide the panel try using its Visible bool.
YataGraphics.graphics = CreateGraphics();
FontDefault = new Font("Georgia", 8F, FontStyle.Regular, GraphicsUnit.Point, (byte)0);
Font = FontDefault.Clone() as Font;
YataGraphics.hFontDefault = YataGraphics.MeasureHeight(YataGraphics.HEIGHT_TEST, Font);
Options .ScanOptions(); // load the Optional settings file 'Settings.Cfg'
ColorOptions.ScanColorOptions(); // load the Optional settings file 'Colors.Cfg'
if (Options._font != null)
Font = Options._font;
else
Options._fontdialog = Options.CreateDialogFont(Font);
FontAccent = new Font(Font, getStyleAccented(Font.FontFamily));
if (Options._font2 != null)
{
// Relative Font-sizes (as defined in the Designers):
//
// _bar, statusbar, _contextTa, _contextRo, _contextCe = all unity.
// rowit_Header = +0.75
// statbar_Cords = -0.75
// statbar_Info = +1.50
_bar.Font.Dispose();
_bar.Font = Options._font2;
statusbar.Font.Dispose();
statusbar.Font = Options._font2;
statbar_Cords.Font.Dispose();
statbar_Cords.Font = new Font(Options._font2.FontFamily,
Options._font2.SizeInPoints - 0.75f);
statbar_Info.Font.Dispose();
statbar_Info.Font = new Font(Options._font2.FontFamily,
Options._font2.SizeInPoints + 1.5f);
int hBar = YataGraphics.MeasureHeight(YataGraphics.HEIGHT_TEST, statbar_Info.Font) + 2;
statusbar .Height = (hBar + 5 < 22) ? 22 : hBar + 5;
statbar_Cords.Height =
statbar_Icon .Height =
statbar_Info .Height = (hBar < 17) ? 17 : hBar;
statbar_Cords.Width = YataGraphics.MeasureWidth(YataGraphics.WIDTH_CORDS, statbar_Cords.Font) + 20;
rowit_Header.Font.Dispose();
rowit_Header.Font = new Font(Options._font2.FontFamily,
Options._font2.SizeInPoints + 0.75f,
getStyleAccented(Options._font2.FontFamily));
_contextTa.Font.Dispose();
_contextTa.Font = Options._font2;
_contextRo.Font.Dispose();
_contextRo.Font = Options._font2;
_contextCe.Font.Dispose();
_contextCe.Font = Options._font2;
}
int
x = Options._x,
y = Options._y,
w = Options._w,
h = Options._h;
if (x != -1 || y != -1)
{
StartPosition = FormStartPosition.Manual;
if (x == -1) x = Left;
if (y == -1) y = Top;
Location = new Point(x,y);
}
if (w == -1) w = Width;
if (h == -1) h = Height;
if (w != Width || h != Height)
ClientSize = new Size(w,h);
cb_SearchOption.Items.AddRange(new []
{
"subfield",
"wholefield"
});
cb_SearchOption.SelectedIndex = 0;
YataGrid.SetStaticMetrics(this);
bu_Propanel.Left = ClientSize.Width - bu_Propanel.Width + 1;
bu_Propanel.Top = -1; // NOTE: This won't work in PP button's cTor. So do it here.
if (Options._recent != 0)
CreateRecentsSubits(); // init recents before (potentially) loading a table from FileExplorer
if (File.Exists(PfeLoad))
CreatePage(PfeLoad); // start Yata and load file w/ file-association
else
Obfuscate();
_t1.Interval = 223;
_t1.Tick += t1_tick;
DontBeepEvent += HandleDontBeepEvent;
TalkReader.LoadTalkingHeads(Strrefheads);
TalkReader.Load(Options._dialog, it_PathTalkD);
TalkReader.Load(Options._dialogalt, it_PathTalkC);
if (Options._maximized)
WindowState = FormWindowState.Maximized;
// _bar.TabStop = true; // can be set in the designer <-
// if focus is not forced here focus will be given to the File it.
// _bar.Select(); // focuses the File it's container, the Menubar itself.
// Tabs.Select(); // this happens by default if _bar's TabStop property is left False.
}
/// <summary>
/// Initializes the recent-files list from entries in the user-file
/// "recent.cfg".
/// </summary>
void CreateRecentsSubits()
{
string dir = Application.StartupPath;
string pfe = Path.Combine(dir, RECENTCFG);
if (File.Exists(pfe))
{
ToolStripItemCollection recents = it_Recent.DropDownItems;
ToolStripItem it;
string[] lines = File.ReadAllLines(pfe);
foreach (string line in lines)
{
if (File.Exists(line))
{
it = new ToolStripMenuItem(line);
it.Click += fileclick_Recent;
recents.Add(it);
if (recents.Count == Options._recent)
break;
}
}
}
}
#endregion cTor
/// <summary>
/// Handles timer ticks - clears statusbar coordinates and path-info
/// when the mouse-cursor leaves the grid.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void t1_tick(object sender, EventArgs e)
{
if (_track_x != -1)
Table.MouseLeaveTicker();
}
/// <summary>
/// Updates the <c>BackColor</c> of
/// <c><see cref="YataGrid._editor">YataGrid._editor</see></c> and
/// <c><see cref="Propanel._editor">Propanel._editor</see></c> when the
/// user changes it in <c><see cref="ColorOptionsEditor"/></c>.
/// </summary>
/// <param name="color"></param>
internal void UpdateEditorColor(Color color)
{
YataGrid table;
foreach (TabPage page in Tabs.TabPages)
{
(table = page.Tag as YataGrid)._editor.BackColor = color;
if (table.Propanel != null)
table.Propanel._editor.BackColor = color;
}
}
/// <summary>
/// Updates the <c>ForeColor</c> of
/// <c><see cref="YataGrid._editor">YataGrid._editor</see></c> and
/// <c><see cref="Propanel._editor">Propanel._editor</see></c> when the
/// user changes it in <c><see cref="ColorOptionsEditor"/></c>.
/// </summary>
internal void UpdateEditorTextColor()
{
YataGrid table;
foreach (TabPage page in Tabs.TabPages)
{
(table = page.Tag as YataGrid)._editor.ForeColor = ColorOptions._celledit_t;
if (table.Propanel != null)
table.Propanel._editor.ForeColor = ColorOptions._celledit_t;
}
}
/// <summary>
/// Updates the <c>ForeColor</c> of the <c>ToolStripStatusLabels</c>
/// on the <c><see cref="statusbar"/></c> when the user changes it in
/// <c><see cref="ColorOptionsEditor"/></c>.
/// </summary>
internal void UpdateStatusbarTextColor()
{
statbar_Cords.ForeColor =
statbar_Icon .ForeColor =
statbar_Info .ForeColor = ColorOptions._statusbar_t;
}
#region Methods (close)
/// <summary>
/// Checks if any currently opened tables have their
/// <c><see cref="YataGrid.Changed">YataGrid.Changed</see></c> flag set.
/// </summary>
/// <param name="descriptor">"close" files or "quit" Yata</param>
/// <param name="excludecurrent"><c>true</c> to exclude the current
/// table - used by
/// <c><see cref="tabclick_CloseOtherTabpages()">tabclick_CloseOtherTabpages()</see></c></param>
/// <returns><c>true</c> if there are any changed tables and user
/// chooses to cancel; <c>false</c> if there are no changed tables or
/// user chooses to close/quit anyway</returns>
bool CancelChangedTables(string descriptor, bool excludecurrent = false)
{
string tables = String.Empty;
YataGrid table;
foreach (TabPage page in Tabs.TabPages)
{
if ((table = page.Tag as YataGrid).Changed
&& (!excludecurrent || table != Table))
{
if (tables.Length != 0) tables += Environment.NewLine;
tables += Path.GetFileNameWithoutExtension(table.Fullpath).ToUpperInvariant();
}
}
if (tables.Length != 0)
{
using (var ib = new Infobox(Infobox.Title_alert,
"Data has changed. Okay to " + descriptor + " ...",
tables,
InfoboxType.Warn,
InfoboxButtons.CancelYes))
{
return ib.ShowDialog(this) == DialogResult.Cancel;
}
}
return false;
}
#endregion Methods (close)
#region Handlers (override)
/// <summary>
/// Overrides Yata's <c>Activated</c> eventhandler. Checks if the
/// currently active table has been changed on the hardrive.
/// </summary>
/// <param name="e"></param>
protected override void OnActivated(EventArgs e)
{
if (Table != null && !_isCreate)
{
// NOTE: This could cause VerifyCurrentFileState() to run twice
// if user activates Yata by clicking on a tab that changes the
// currently selected tab - see: tab_SelectedIndexChanged()
VerifyCurrentFileState();
}
}
/// <summary>
/// Overrides Yata's <c>Deactivate</c> eventhandler. Ensures that focus
/// gets removed from the Menubar.
/// </summary>
/// <param name="e"></param>
protected override void OnDeactivate(EventArgs e)
{
if (Table != null)
{
Table.editresultdefault();
Table.Select();
}
else
Tabs.Select();
base.OnDeactivate(e);
}
/// <summary>
/// Checks whether the 2da-file of the current
/// <c><see cref="Table">Table's</see></c>
/// <c><see cref="YataGrid.Fullpath">YataGrid.Fullpath</see></c> has
/// been deleted or overwritten. Invokes a
/// <c><see cref="FileWatcherDialog"/></c> if so.
/// </summary>
/// <remarks>Called by
/// <list type="bullet">
/// <item><c><see cref="OnActivated()">OnActivated()</see></c></item>
/// <item><c><see cref="OnFormClosing()">OnFormClosing()</see></c> - cancelled</item>
/// <item><c><see cref="tab_SelectedIndexChanged()">tab_SelectedIndexChanged()</see></c></item>
/// <item><c><see cref="fileclick_Reload()">fileclick_Reload()</see></c></item>
/// </list></remarks>
void VerifyCurrentFileState()
{
//logfile.Log("Yata.VerifyCurrentFileState()");
if (!_bypassVerifyFile)
{
_bypassVerifyFile = true; // bypass this funct when the FileWatcherDialog closes and this form's Activate event fires.
_fileresult = FileWatcherDialog.Output.non;
// There appears to be a bug when closing the form or perhaps
// just a tabpage but is cancelled ... the obscuration panel
// doesn't go away (the remaining tabs/tables remain obscured)
// so add a safety here ->
if (Table != null) // safety.
{
if (!File.Exists(Table.Fullpath))
{
using (var fwd = new FileWatcherDialog(Table, FileWatcherDialog.Input.FileDeleted))
fwd.ShowDialog(this);
}
else if (File.GetLastWriteTime(Table.Fullpath) != Table.Lastwrite)
{
using (var fwd = new FileWatcherDialog(Table, FileWatcherDialog.Input.FileChanged))
fwd.ShowDialog(this);
}
//logfile.Log(". _fileresult= " + _fileresult);
switch (_fileresult)
{
// case FileWatcherDialog.Output.non: break;
case FileWatcherDialog.Output.Cancel:
Table.Readonly = false;
Table.Changed = true;
if (File.Exists(Table.Fullpath))
Table.Lastwrite = File.GetLastWriteTime(Table.Fullpath);
break;
case FileWatcherDialog.Output.Close2da:
Table.Changed = false; // <- bypass Close warn
fileclick_CloseTabpage(null, EventArgs.Empty);
break;
case FileWatcherDialog.Output.Resave:
Table.Changed = false; // <- bypass Close warn
fileclick_Save(null, EventArgs.Empty);
if (File.Exists(Table.Fullpath))
Table.Lastwrite = File.GetLastWriteTime(Table.Fullpath);
break;
case FileWatcherDialog.Output.Reload:
Table.Changed = false; // <- bypass Close warn
fileclick_Reload(null, EventArgs.Empty);
if (Table != null && File.Exists(Table.Fullpath)) // Table can fail on reload
Table.Lastwrite = File.GetLastWriteTime(Table.Fullpath);
break;
}
}
_bypassVerifyFile = false;
}
//else logfile.Log(". _bypassVerifyFile");
}
/// <summary>
/// Overrides Yata's <c>FormClosing</c> handler. Requests
/// user-confirmation if data has changed and writes a recent-files list
/// if appropriate.
/// </summary>
/// <param name="e"></param>
protected override void OnFormClosing(FormClosingEventArgs e)
{
_bypassVerifyFile = true;
if (Tabs.TabPages.Count != 0)
{
if (Tabs.TabPages.Count == 1)
{
if (e.Cancel = Table.Changed)
{
using (var ib = new Infobox(Infobox.Title_alert,
"Data has changed. Okay to quit ...",
null,
InfoboxType.Warn,
InfoboxButtons.CancelYes))
{
e.Cancel = ib.ShowDialog() == DialogResult.Cancel;
}
}
}
else
e.Cancel = CancelChangedTables("quit");
}
if (e.Cancel)
{
_bypassVerifyFile = false;
VerifyCurrentFileState();
}
else if (Options._recent != 0)
{
int i = -1;
var recents = new string[it_Recent.DropDownItems.Count];
foreach (ToolStripItem recent in it_Recent.DropDownItems)
recents[++i] = recent.Text;
string pfe = Path.Combine(Application.StartupPath, RECENTCFG);
try
{
File.WriteAllLines(pfe, recents);
}
catch (Exception ex)
{
using (var ib = new Infobox(Infobox.Title_excep,
"Failed to write Recent.cfg to the application directory.",
ex.ToString(),
InfoboxType.Error))
{
ib.ShowDialog(this);
}
}
}
base.OnFormClosing(e);
}
/// <summary>
/// Sends the <c>MouseWheel</c> event to the active
/// <c><see cref="YataGrid"/></c>.
/// </summary>
/// <param name="e"></param>
protected override void OnMouseWheel(MouseEventArgs e)
{
if (Table != null) Table.Scroll(e);
}
/// <summary>
/// Sets <c><see cref="IsMin"/></c> true so that when the form is
/// minimized then restored/maximized the ensure-displayed call(s) are
/// bypassed by <c><see cref="YataGrid"/>.OnResize()</c> event(s).
/// Because if the user wants to simply minimize the window temporarily
/// to check something out in another app you don't want the view to be
/// changed.
/// </summary>
/// <param name="e"></param>
protected override void OnResize(EventArgs e)
{
if (Table != null)
{
Table.editresultdefault();
Table.Select();
}
if (WindowState == FormWindowState.Minimized)
IsMin = true;
base.OnResize(e);
}
#endregion Handlers (override)
#region Handlers (override - Receive Message - PfeLoad arg)
/// <summary>
/// Disables message-blocking in Vista+ 64-bit systems.
/// </summary>
/// <param name="e"></param>
/// <remarks>https://www.codeproject.com/Tips/1017834/How-to-Send-Data-from-One-Process-to-Another-in-Cs</remarks>
protected override void OnLoad(EventArgs e)
{
GC.Collect(); // .net appears to load ~38mb of garbage at program start.
GC.WaitForPendingFinalizers();
var filter = new Crap.CHANGEFILTERSTRUCT();
filter.size = (uint)Marshal.SizeOf(filter);
filter.info = 0;
if (!Crap.ChangeWindowMessageFilterEx(Handle,
Crap.WM_COPYDATA,
Crap.ChangeWindowMessageFilterExAction.Allow,
ref filter))
{
using (var ib = new Infobox(Infobox.Title_error,
"The MessageFilter could not be changed.",
"LastWin32Error " + Marshal.GetLastWin32Error(),
InfoboxType.Error))
{
ib.ShowDialog(this);
}
}
}
/// <summary>
/// Receives data via WM_COPYDATA from other applications/processes.
/// </summary>
/// <param name="m"></param>
/// <remarks>https://www.codeproject.com/Tips/1017834/How-to-Send-Data-from-One-Process-to-Another-in-Cs</remarks>
protected override void WndProc(ref Message m)
{
if (m.Msg == Crap.WM_COPYDATA)
{
var copyData = (Crap.COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(Crap.COPYDATASTRUCT));
if ((int)copyData.dwData == Crap.CopyDataStructType) // extract the file-string ->
{
PfeLoad = Marshal.PtrToStringAnsi(copyData.lpData);
if (File.Exists(PfeLoad))
CreatePage(PfeLoad); // load file w/ file-association
}
}
else
base.WndProc(ref m);
}
#endregion Handlers (override - Receive Message - PfeLoad arg)
#region Methods (static)
/// <summary>
/// Gets a standard-ish <c>FontStyle</c> given a <c>FontFamily</c>.
/// </summary>
/// <param name="ff"><c>FontFamily</c></param>
/// <returns><c>FontStyle</c></returns>
internal static FontStyle getStyleStandard(FontFamily ff)
{
if (ff.IsStyleAvailable(FontStyle.Regular)) return FontStyle.Regular;
if (ff.IsStyleAvailable(FontStyle.Italic)) return FontStyle.Italic;
if (ff.IsStyleAvailable(FontStyle.Bold)) return FontStyle.Bold;
foreach (FontStyle style in Enum.GetValues(typeof(FontStyle)))
{
if (ff.IsStyleAvailable(style)) // determine first available style (any) of Family ->
return style;
}
return FontStyle.Regular; // this ought never happen.
}
/// <summary>
/// Gets an accented-ish <c>FontStyle</c> given a <c>FontFamily</c>.
/// </summary>
/// <param name="ff"><c>FontFamily</c></param>
/// <returns><c>FontStyle</c></returns>
static FontStyle getStyleAccented(FontFamily ff)
{
if (ff.IsStyleAvailable(FontStyle.Bold)) return FontStyle.Bold;
if (ff.IsStyleAvailable(FontStyle.Underline)) return FontStyle.Underline;
if (ff.IsStyleAvailable(FontStyle.Italic)) return FontStyle.Italic;
foreach (FontStyle style in Enum.GetValues(typeof(FontStyle)))
{
if (ff.IsStyleAvailable(style)) // determine first available style (any) of Family ->
return style;
}
return FontStyle.Regular; // this ought never happen.
}
#endregion Methods (static)
#region Methods
/// <summary>
/// Obscures or unobscures the table behind a dedicated color-panel.
/// Can be called before and after calibrating and drawing the table in
/// order to hide unsightly .NET spaz-attacks (despite double-buffering
/// etc).
/// </summary>
/// <param name="obscure"><c>true</c> to bring
/// <c><see cref="panel_ColorFill"/></c> to front or <c>false</c> to
/// send it to back</param>
internal void Obfuscate(bool obscure = true)
{
if (obscure) panel_ColorFill.BringToFront();
else panel_ColorFill.SendToBack();
}
#endregion Methods
#region Methods (create)
/// <summary>
/// Creates a tab-page and instantiates a table-grid for it.
/// </summary>
/// <param name="pfe">path_file_extension</param>
/// <param name="read"><c>true</c> to create table as
/// <c><see cref="YataGrid.Readonly">YataGrid.Readonly</see></c></param>
/// <seealso cref="fileclick_Create()"><c>fileclick_Create()</c></seealso>
void CreatePage(string pfe, bool read = false)
{
if (File.Exists(pfe) // ~safety
&& Path.GetFileNameWithoutExtension(pfe).Length != 0) // what idjut would ... oh wait.
{
AddRecentFile(pfe);
// check if 2da-file is already open ->
for (int i = 0; i != Tabs.TabPages.Count; ++i)
if ((Tabs.TabPages[i].Tag as YataGrid).Fullpath == pfe) // TODO: case insensitive <-
{
TopMost = true; // drag&drop from FileExplorer could leave the Infobox hidden behind other windows.
TopMost = false;
Tabs.SelectedIndex = i;
if (!Options._allowdupls) return;
using (var ib = new Infobox(Infobox.Title_warni,
"The 2da-file is already open. Do you want another instance ...",
null,
InfoboxType.Warn,
InfoboxButtons.CancelYes))
{
if (ib.ShowDialog(this) == DialogResult.Cancel)
return;
}
break;
}
Obfuscate();
// Refresh(); // NOTE: If a table is already loaded the color-panel doesn't show
// but a refresh turns the client-area gray at least instead of glitchy.
// NOTE: It went away; the table-area turns gray.
var table = new YataGrid(this, pfe, read);
_bypassVerifyFile = true;
int result = table.LoadTable();
if (result != YataGrid.LOADRESULT_FALSE)
{
Table = table; // NOTE: Is done in tab_SelectedIndexChanged() also.
// DrawRegulator.SuspendDrawing(Table);
var tab = new TabPage();
Tabs.TabPages.Add(tab);
tab.Tag = Table;
tab.Text = Path.GetFileNameWithoutExtension(pfe);
tab.Controls.Add(Table);
Tabs.SelectedTab = tab;
Table.Init(result == YataGrid.LOADRESULT_CHANGED);
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
TopMost = true;
TopMost = false;
// DrawRegulator.ResumeDrawing(Table);
}
else
{
YataGrid._init = false;
table.Dispose();
}
_bypassVerifyFile = false;
tab_SelectedIndexChanged(null, EventArgs.Empty);
}
}
/// <summary>
/// Adds a recently opened file to the list of recently opened files.
/// </summary>
/// <param name="pfe">fullpath of file to add</param>
void AddRecentFile(string pfe)
{
if (Options._recent != 0)
{
ToolStripItemCollection recents = it_Recent.DropDownItems;
ToolStripItem it;
bool found = false;
for (int i = 0; i != recents.Count; ++i)
{
if ((it = recents[i]).Text == pfe)
{
found = true;
if (i != 0)
{
recents.Remove(it);
recents.Insert(0, it);
}
break;
}
}
if (!found)
{
it = new ToolStripMenuItem(pfe);
it.Click += fileclick_Recent;
recents.Insert(0, it);
if (recents.Count > Options._recent)
{
recents.Remove(it = recents[recents.Count - 1]);
it.Dispose();
}
}
}
}
/// <summary>
/// Sets the titlebar text to something reasonable.
/// </summary>
void SetTitlebarText()
{