-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYataGrid_loadtable.cs
More file actions
1248 lines (1035 loc) · 32.4 KB
/
YataGrid_loadtable.cs
File metadata and controls
1248 lines (1035 loc) · 32.4 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
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.Globalization;
using System.IO;
using System.Text;
//using System.Threading;
using System.Windows.Forms;
namespace yata
{
// Various routines for loading a 2da-file.
sealed partial class YataGrid
{
#region Fields (static)
const int LINE_VERSION = 0;
const int LINE_DEFAULT = 1;
const int LINE_COLABEL = 2;
internal const int LOADRESULT_FALSE = 0;
internal const int LOADRESULT_TRUE = 1;
internal const int LOADRESULT_CHANGED = 2;
static int CodePage = -1;
/// <summary>
/// This <c>static bool</c> is set <c>true</c> when loading
/// Crafting.2da, Spells.2da, Feat.2da, or Classes.2da to indicate that
/// the <c><see cref="Options._pathall">Options._pathall</see></c>
/// directories have been groped.
/// </summary>
static bool Groped;
static int _heightColheadCached;
#endregion Fields (static)
#region Fields
bool _initFrozenLabels = true;
/// <summary>
/// A static <c>List</c> used to pass parsed row-fields from
/// <c><see cref="LoadTable()">LoadTable()</see></c> to
/// <c><see cref="CreateRows()">CreateRows()</see></c> and then is
/// cleared when no longer needed.
/// </summary>
static readonly List<string[]> _rows = new List<string[]>();
#endregion Fields
#region Methods (static)
/// <summary>
/// Checks if <paramref name="codepage"/> is recognized by .NET.
/// </summary>
/// <param name="codepage"></param>
/// <returns></returns>
internal static bool CheckCodepage(int codepage)
{
EncodingInfo[] encs = Encoding.GetEncodings();
for (int i = 0; i != encs.Length; ++i)
{
if (encs[i].CodePage == codepage)
return true;
}
return false;
}
/// <summary>
/// An <c><see cref="Infobox"/></c> if something goes wonky while
/// loading a 2da-file.
/// </summary>
/// <param name="head">the warning</param>
/// <param name="copy">copyable text</param>
/// <returns>a <c>DialogResult</c>
/// <list type="bullet">
/// <item><c>Cancel</c> - abort load</item>
/// <item><c>OK</c> - ignore further errors and try to load the 2da-file</item>
/// <item><c>Retry</c> - check for next error</item>
/// </list></returns>
static DialogResult ShowLoadWarning(string head, string copy)
{
using (var ib = new Infobox(Infobox.Title_warni,
head,
copy,
InfoboxType.Warn,
InfoboxButtons.AbortLoadNext))
{
return ib.ShowDialog(Yata.that);
}
}
/// <summary>
/// Parses a single row of text out to its fields.
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
internal static string[] ParseTableRow(string line)
{
var list = new List<string>();
var field = new List<char>();
bool @add = false;
bool inQuotes = false;
char c;
int posLast = line.Length + 1; // include an extra iteration to get the last field
for (int pos = 0; pos != posLast; ++pos)
{
if (pos == line.Length) // hit lineend -> add the last field
{
if (@add)
{
list.Add(new string(field.ToArray()));
}
}
else
{
c = line[pos];
if (c == '"' || inQuotes) // start or continue quotation
{
inQuotes = (!inQuotes || c != '"'); // end quotation
@add = true;
field.Add(c);
}
else if (c != ' ' && c != '\t') // any non-whitespace char (except double-quote)
// else if (!Char.IsWhiteSpace(c))
{
@add = true;
field.Add(c);
}
else if (@add) // hit a space or tab
{
@add = false;
list.Add(new string(field.ToArray()));
field.Clear();
}
}
}
return list.ToArray();
}
#endregion Methods (static)
#region Methods (load)
/// <summary>
/// Tries to load a 2da-file.
/// </summary>
/// <returns>
/// <list type="bullet">
/// <item><c><see cref="LOADRESULT_FALSE"/></c></item>
/// <item><c><see cref="LOADRESULT_TRUE"/></c></item>
/// <item><c><see cref="LOADRESULT_CHANGED"/></c></item>
/// </list></returns>
/// <seealso cref="CreateTable()"><c>CreateTable()</c></seealso>
internal int LoadTable()
{
// �
// byte[] asciiBytes = Encoding.ASCII.GetBytes("�");
// logfile.Log("� = " + asciiBytes);
// foreach (var b in asciiBytes)
// logfile.Log(((int)b).ToString());
//
// byte[] utf8Bytes = Encoding.UTF8.GetBytes("�");
// logfile.Log("� = " + utf8Bytes);
// foreach (var b in utf8Bytes)
// logfile.Log(((int)b).ToString());
// logfile.Log();
// logfile.Log("default encoding= " + Encoding.GetEncoding(0));
// EncodingInfo[] encs = Encoding.GetEncodings();
// foreach (var enc in encs)
// {
// logfile.Log();
// logfile.Log(". enc= " + enc.Name);
// logfile.Log(". DisplayName= " + enc.DisplayName);
// logfile.Log(". CodePage= " + enc.CodePage);
// }
Lastwrite = File.GetLastWriteTime(Fullpath);
_rows.Clear();
int loadresult = LOADRESULT_TRUE;
string[] lines = File.ReadAllLines(Fullpath); // default decoding is UTF-8
// 0. test character decoding ->
for (int i = 0; i != lines.Length; ++i)
{
if (lines[i].Contains("�"))
{
if (CodePage == -1)
CodePage = Options._codepage; // init.
Encoding enc;
if (CodePage == 0 || CheckCodepage(CodePage))
{
// CodePage is default or user-valid.
enc = Encoding.GetEncoding(CodePage);
}
else
enc = null;
using (var cpd = new CodePageDialog(_f, enc))
{
int result;
if (cpd.ShowDialog(_f) == DialogResult.OK
&& Int32.TryParse(cpd.GetCodePage(), out result)
&& result > -1 && result < 65536
&& CheckCodepage(result))
{
lines = File.ReadAllLines(Fullpath, Encoding.GetEncoding(result));
}
else
return LOADRESULT_FALSE; // silently fail.
}
break;
}
}
string line, head, copy;
// 1. test for fatal errors ->
if (lines.Length > LINE_VERSION) line = lines[LINE_VERSION].Trim();
else line = String.Empty;
if (line != gs.TwodaVer && line != gs.TwodaVer_tab) // tab is not fatal - autocorrect it later
{
head = "The 2da-file contains an incorrect version header on its 1st line.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ line;
using (var ib = new Infobox(Infobox.Title_error,
head,
copy,
InfoboxType.Error,
InfoboxButtons.Abort))
{
ib.ShowDialog(_f);
}
return LOADRESULT_FALSE;
}
if (lines.Length > LINE_COLABEL) line = lines[LINE_COLABEL].Trim();
else line = String.Empty;
if (line.Length == 0)
{
head = "The 2da-file does not have any fields. Yata wants a file to have at least one colhead label on its 3rd line.";
using (var ib = new Infobox(Infobox.Title_error,
head,
Fullpath,
InfoboxType.Error,
InfoboxButtons.Abort))
{
ib.ShowDialog(_f);
}
return LOADRESULT_FALSE;
}
bool quelch = false; // bypass warnings and try to load the file directly.
// 2. test for Tabs ->
if (Options._strict && Options._alignoutput != Options.AoTabs)
{
for (int i = 0; i != lines.Length; ++i)
{
if (i != LINE_DEFAULT && lines[i].Contains(gs.Tab))
{
head = "Tab characters are detected in the 2da-file. They will be replaced with space characters (or deleted where redundant) if the file is saved.";
switch (ShowLoadWarning(head, Fullpath))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
break;
}
}
}
bool autordered = false;
bool whitespacewarned = false;
string tr;
int id = -1;
int total = lines.Length;
if (total < LINE_COLABEL + 1) total = LINE_COLABEL + 1; // scan at least 3 'lines' in the file
// 3. test for ignorable/recoverable errors ->
for (int i = LINE_VERSION; i != total; ++i)
{
if (i < lines.Length) line = lines[i];
else line = String.Empty;
switch (i)
{
case LINE_VERSION:
if (!quelch && Options._strict)
{
if (line != (tr = line.Trim()))
{
head = "The 1st line (version header) has extraneous whitespace. It will be trimmed if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ line;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
if (!quelch && tr.Contains(gs.Tab))
{
head = "The 1st line (version header) contains a tab-character. It will be corrected if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ tr;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
// if (!quelch && tr.Contains(" ")) // don't bother. This is a fatal error above.
// {
// head = "The header on the first line contains redundant spaces. It will be corrected if the file is saved.";
// copy = Fullpath + Environment.NewLine + Environment.NewLine
// + tr;
//
// switch (ShowLoadWarning(head, copy))
// {
// case DialogResult.Cancel:
// return LOADRESULT_FALSE;
//
// case DialogResult.OK:
// quelch = true;
// goto case DialogResult.Retry;
//
// case DialogResult.Retry:
// loadresult = LOADRESULT_CHANGED;
// break;
// }
// }
}
break;
case LINE_DEFAULT:
tr = line.Trim();
if (!quelch && Options._strict && line != tr)
{
head = "The 2nd line (default value) has extraneous whitespace. It will be trimmed if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ line;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
if (tr.StartsWith("DEFAULT:", StringComparison.Ordinal)) // do not 'strict' this feedback ->
{
_defaultval = tr.Substring(8).TrimStart();
if (_defaultval.Length == 0)
{
if (!quelch)
{
head = "The Default is blank. The 2nd line (default value) will be cleared if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ tr;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
if (Options._strict) loadresult = LOADRESULT_CHANGED;
break;
}
}
}
else
{
InputDialog.VerifyDefaultval(ref _defaultval, true);
if (!quelch && Options._strict && tr != gs.Default + _defaultval)
{
head = "The Default on the 2nd line has been changed.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ gs.Default + _defaultval;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
}
}
else
{
_defaultval = String.Empty;
if (!quelch && Options._strict && tr.Length != 0)
{
head = "The 2nd line (default value) in the 2da contains garbage. It will be cleared if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ line; //.Replace("\t", "\u2192")
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
}
break;
case LINE_COLABEL:
tr = line.TrimEnd();
// TODO: check for redundant whitespace at the start of the line also
// flag Changed if found ...
if (!quelch && Options._strict && line != tr)
{
head = "The 3nd line (colhead labels) has extraneous whitespace. It will be trimmed if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ line;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
if (!quelch
&& Options._strict // line.Length shall not be 0
&& line[0] != 32 // space
&& !(line[0] == 9 && Options._alignoutput == Options.AoTabs)) // tab
{
// NOTE: This is an autocorrecting error and there was
// really no need for the Bioware spec. to indent the 3rd line.
// The fact it's the 3rd line alone is enough to signify
// that the line is the colhead fields.
head = "The 3rd line (colhead labels) is not indented properly. It will be corrected if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ line;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
tr = tr.TrimStart();
if (!quelch)
{
var chars = new List<char>(); // warn only once per character
foreach (char character in tr)
{
// construct this condition in the positive and put a NOT in front of it
// to avoid logical pretzels ...
if (!chars.Contains(character)
&& !( character == 32 // space
|| (character == 9 // tab
&& (Options._alignoutput == Options.AoTabs
|| !Options._strict))
|| Util.isAsciiAlphanumericOrUnderscore(character)
|| (!Options._strict
&& Util.isPrintableAsciiNotDoublequote(character))))
{
head = "Detected a suspect character in the colhead labels ...";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ character;
// + (character == 9 ? "\u2192" : character.ToString());
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
break;
case DialogResult.Retry:
chars.Add(character);
break;
}
}
if (quelch) break;
}
}
Fields = tr.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
break;
default: // line #3+ datarows ->
tr = line.Trim();
if (tr.Length == 0)
{
if (!quelch && Options._strict)
{
head = "A blank row is detected. It will be deleted if the file is saved.";
copy = Fullpath;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
}
else
{
++id;
if (!quelch && Options._strict && !whitespacewarned
&& ( (Options._alignoutput == Options.AoElectron && line != line.TrimEnd()) // 'AoElectron' indents each row with at least one space
|| (Options._alignoutput != Options.AoElectron && line != tr)))
{
whitespacewarned = true;
head = "At least one row has extraneous whitespace. This will be trimmed if the file is saved.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ "id " + id;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
goto case DialogResult.Retry;
case DialogResult.Retry:
loadresult = LOADRESULT_CHANGED;
break;
}
}
string[] celltexts = ParseTableRow(tr);
if (!quelch) // show these warnings even if not Strict.
{
// test for id
int result;
if (!Int32.TryParse(celltexts[0], out result))
head = "The 2da-file contains an id that is not an integer.";
else if (result != id)
head = "The 2da-file contains an id that is out of order.";
else
head = null;
if (head != null) // show this warning even if Autorder true - table shall be flagged Changed
{
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ "id " + id + " \u2192 " + celltexts[0];
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
break;
}
}
// test for matching cell-fields under cols
if (!quelch)
{
if (celltexts.Length != Fields.Length + 1)
{
head = "The 2da-file contains fields that do not align with its cols.";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ "Colcount " + (Fields.Length + 1) + Environment.NewLine
+ "id " + id + " fields \u2192 " + celltexts.Length;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
break;
}
}
}
// test for an odd quantity of double-quote characters
if (!quelch)
{
int quotes = 0;
foreach (char character in tr)
if (character == '"')
++quotes;
if (quotes % 2 == 1)
{
head = "A row contains an odd quantity of double-quote characters. This could be bad ...";
copy = Fullpath + Environment.NewLine + Environment.NewLine
+ "id " + id;
switch (ShowLoadWarning(head, copy))
{
case DialogResult.Cancel:
return LOADRESULT_FALSE;
case DialogResult.OK:
quelch = true;
break;
}
}
}
}
if (Options._autorder && id.ToString(CultureInfo.InvariantCulture) != celltexts[0])
{
celltexts[0] = id.ToString(CultureInfo.InvariantCulture);
autordered = true;
}
// NOTE: Tests for well-formed fields will be done later so that their
// respective cells can be flagged as loadchanged if applicable.
_rows.Add(celltexts);
}
break;
}
}
if (autordered)
{
using (var ib = new Infobox(Infobox.Title_infor, "Row ids have been corrected."))
ib.ShowDialog(_f);
loadresult = LOADRESULT_CHANGED;
}
if (_rows.Count == 0) // add a row of stars so grid is not left blank ->
{
var cells = new string[Fields.Length + 1]; // NOTE: 'Fields' does not contain the ID-col.
int c = 0;
if (Options._autorder)
cells[c++] = "0";
for (; c <= Fields.Length; ++c)
cells[c] = gs.Stars;
_rows.Add(cells);
return LOADRESULT_CHANGED; // flag the Table as changed
}
return loadresult;
}
/// <summary>
/// Initializes a loaded, reloaded, or created
/// <c><see cref="YataGrid"/></c>.
/// </summary>
/// <param name="changed"></param>
/// <param name="reload"></param>
internal void Init(bool changed = false, bool reload = false)
{
if (reload)
{
_init = true;
_scrollVert.Value =
_scrollHori.Value = 0;
_sortcol = 0;
_sortdir = SORT_ASC;
RangeSelect = 0;
FrozenCount = YataGrid.FreezeId;
Cols.Clear();
Rows.Clear();
Controls.Remove(_panelCols);
Controls.Remove(_panelRows);
Controls.Remove(FrozenPanel);
// _panelCols .Dispose(); // breaks the frozen-labels
// _panelRows .Dispose();
// _panelFrozen.Dispose();
}
else
{
switch (Path.GetFileNameWithoutExtension(Fullpath).ToUpperInvariant())
{
case gs.Crafting:
Info = InfoType.INFO_CRAFT;
break;
case gs.Spells:
Info = InfoType.INFO_SPELL;
break;
case gs.Feat:
Info = InfoType.INFO_FEAT;
break;
case gs.Classes:
Info = InfoType.INFO_CLASS;
break;
case gs.Baseitems:
Info = InfoType.INFO_ITEM;
break;
}
if (!Groped && Info != InfoType.INFO_NONE)
{
Groped = true;
// _f.CreateInfoStructs();
if (Directory.Exists(Options._pathzipdata))
_f.GropeZipData();
foreach (var dir in Options._pathall)
_f.GropeLabels(dir);
// _f.ClearInfoStructs();
}
}
Changed = changed;
_panelCols = new YataPanelCols(this);
_panelRows = new YataPanelRows(this);
CreateCols();
CreateRows();
FrozenPanel = new YataPanelFrozen(this, Cols[0].Width);
InitializeFrozenLabels();
MetricStaticHeads(_f);
Controls.Add(FrozenPanel);
Controls.Add(_panelRows);
Controls.Add(_panelCols);
_scrollVert.LargeChange =
_scrollHori.LargeChange = HeightRow;
InitScroll();
Select();
_init = false;
}
/// <summary>
/// Creates the cols and caches the 2da's colhead data.
/// </summary>
/// <param name="rewidth"><c>true</c> to bypass col-creation and only
/// re-width the cols - ie. Font changed</param>
/// <seealso cref="CreateCol()"><c>CreateCol()</c></seealso>
internal void CreateCols(bool rewidth = false)
{
int c = 0;
if (!rewidth)
{
ColCount = Fields.Length + 1; // 'Fields' does not include rowhead or id-col
for (; c != ColCount; ++c)
Cols.Add(new Col());
Cols[0].text = gs.Id; // NOTE: Is not measured - the cells below it determine col-width.
}
int widthtext; c = 0;
foreach (string field in Fields) // set initial col-widths based on colheads only ->
{
++c; // start at col 1 - skip id col
if (!rewidth)
Cols[c].text = field;
widthtext = YataGraphics.MeasureWidth(field, _f.FontAccent);
Cols[c]._widthtext = widthtext;
Cols[c].SetWidth(widthtext + _padHori * 2 + _padHoriSort, rewidth);
}
}
/// <summary>
/// Creates the <c><see cref="Row">Rows</see></c> and adds
/// <c><see cref="Cell">Cells</see></c> to each <c>Row</c>. Also sets
/// <c><see cref="Cell.loadchanged">Cell.loadchanged</see></c> if a
/// cell-field's text was altered/corrected while loading the 2da for
/// this <c>YataGrid</c>.
/// </summary>
void CreateRows()
{
RowCount = _rows.Count;
bool changed = false, loadchanged; string text;
bool isLoadchanged = false;
for (int r = 0; r != RowCount; ++r)
{
changed = changed
|| _rows[r].Length > ColCount; // flag Changed if any field(s) get cut off.
Rows.Add(new Row(r,
ColCount,
(r % 2 == 0) ? ColorOptions._rowa
: ColorOptions._rowb,
this));
for (int c = 0; c != ColCount; ++c)
{
loadchanged = false;
if (c < _rows[r].Length)
{
text = _rows[r][c];
if (VerifyText(ref text, true))
{
changed = loadchanged = isLoadchanged = true;
}
}
else
{
text = gs.Stars;
changed = loadchanged = isLoadchanged = true;
}
(this[r,c] = new Cell(r,c, text)).loadchanged = loadchanged;
}
}
if (isLoadchanged) // inform user regardless of Strict setting ->
{
_f.EnableGotoLoadchanged(anyLoadchanged());
using (var ib = new Infobox(Infobox.Title_infor, "Cell-texts changed."))
ib.ShowDialog(_f);
}
Changed |= changed;
_rows.Clear(); // done w/ '_rows'
int w, wT; // adjust col-widths based on fields ->
for (int c = 0; c != ColCount; ++c)
{
w = _wId; // start each col at min colwidth
for (int r = 0; r != RowCount; ++r)
{
if ((text = this[r,c].text) == gs.Stars) // bingo.
wT = _wStars;
else
wT = YataGraphics.MeasureWidth(text, Font);
this[r,c]._widthtext = wT;
if ((wT += _padHori * 2) > w) w = wT;
}
Cols[c].SetWidth(w);
}
// var threads = new Thread[ColCount];
// for (int c = 0; c != ColCount; ++c)
// {
// int cT = c;
// threads[c] = new Thread(() => doCol(cT));
// //logfile.Log("c= " + c + " IsBackground= " + threads[c].IsBackground); // default false
// threads[c].IsBackground = true;
// threads[c].Start();
// }
// int procs = Environment.ProcessorCount;
// logfile.Log("ProcessorCount= " + procs);
// var threads = new Thread[procs];