-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathInterlinDocRootSiteBase.cs
More file actions
1262 lines (1120 loc) · 39.8 KB
/
InterlinDocRootSiteBase.cs
File metadata and controls
1262 lines (1120 loc) · 39.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
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
// Copyright (c) 2015-2021 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using SIL.FieldWorks.Common.Controls;
using SIL.FieldWorks.Common.FwUtils;
using SIL.FieldWorks.Common.RootSites;
using SIL.FieldWorks.Common.ViewsInterfaces;
using SIL.FieldWorks.FwCoreDlgControls;
using SIL.LCModel;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.LCModel.Core.Text;
using SIL.LCModel.DomainServices;
using SIL.LCModel.Infrastructure;
using XCore;
namespace SIL.FieldWorks.IText
{
/// <summary>
/// Ideally this would be an abstract class, but Designer does not handle abstract classes.
/// </summary>
public partial class InterlinDocRootSiteBase : RootSite,
IVwNotifyChange, IHandleBookmark, ISelectOccurrence,
IStyleSheet, ISetupLineChoices, IInterlinConfigurable
{
private ISilDataAccess m_sda;
protected internal int m_hvoRoot; // IStText
protected ICmObjectRepository m_objRepo;
/// <summary>
/// Context menu for use when user right-clicks on Interlinear segment labels.
/// </summary>
private ContextMenuStrip m_labelContextMenu;
/// <summary>
/// Blue circle button to alert user to the presence of the Configure Interlinear context menu.
/// </summary>
private BlueCircleButton m_contextButton;
/// <summary>
/// Index of Interlinear line clicked on to generate above blue button.
/// Allows context menu to be context-sensitive.
/// </summary>
private int m_iLineChoice;
/// <summary>
/// Helps determine if a rt-click is opening or closing the context menu.
/// </summary>
private long m_ticksWhenContextMenuClosed = 0;
private readonly HashSet<IWfiWordform> m_wordformsToUpdate;
public InterlinVc Vc { get; set; }
// Necessary for IInterlinConfigurable
public PropertyTable PropertyTable
{
get
{
return m_propertyTable;
}
set
{
m_propertyTable = value;
}
}
public IVwRootBox Rootb { get; set; }
public InterlinDocRootSiteBase()
{
m_wordformsToUpdate = new HashSet<IWfiWordform>();
InitializeComponent();
}
public override void MakeRoot()
{
if (m_cache == null || DesignMode)
return;
base.MakeRoot();
MakeRootInternal();
}
protected virtual void MakeRootInternal()
{
// Setting this result too low can result in moving a cursor from an editable field
// to a non-editable field (e.g. with Control-Right and Control-Left cursor
// commands). Normally we could set this to only a few (e.g. 4). but in
// Interlinearizer we may want to jump from one sentence annotation to the next over
// several read-only paragraphs contained in a word bundle. Make sure that
// procedures that use this limit do not move the cursor from an editable to a
// non-editable field.
m_rootb.MaxParasToScan = 2000;
EnsureVc();
// We want to get notified when anything changes.
m_sda = m_cache.MainCacheAccessor;
m_sda.AddNotification(this);
Vc.LineChoices = LineChoices;
Vc.ShowDefaultSense = true;
m_rootb.DataAccess = m_cache.MainCacheAccessor;
m_rootb.SetRootObject(m_hvoRoot, Vc, InterlinVc.kfragStText, m_styleSheet);
m_objRepo = m_cache.ServiceLocator.GetInstance<ICmObjectRepository>();
}
public bool OnDisplayExportInterlinear(object commandObject, ref UIItemDisplayProperties display)
{
if (m_hvoRoot != 0)
display.Enabled = true;
else
display.Enabled = false;
display.Visible = true;
return true;
}
public bool OnExportInterlinear(object argument)
{
// If the currently selected text is from Scripture, then we need to give the dialog
// the list of Scripture texts that have been selected for interlinearization.
var parent = Parent;
while (parent != null && !(parent is InterlinMaster))
parent = parent.Parent;
var master = parent as InterlinMaster;
if (master != null)
{
var clerk = master.Clerk as InterlinearTextsRecordClerk;
if (clerk != null)
{
clerk.GetScriptureIds(); // initialize the InterestingTextList to include Scripture (prevent a crash trying later)
}
}
bool fFocusBox = TryHideFocusBoxAndUninstall();
ICmObject objRoot = m_objRepo.GetObject(m_hvoRoot);
using (var dlg = new InterlinearExportDialog(m_mediator, m_propertyTable, objRoot, Vc))
{
dlg.ShowDialog(this);
}
if (fFocusBox)
{
CreateFocusBox();
}
return true; // we handled this
}
/// <summary>
/// Hides the sandbox and removes it from the controls.
/// </summary>
/// <returns>true, if it could hide the sandbox. false, if it was not installed.</returns>
internal virtual bool TryHideFocusBoxAndUninstall()
{
return false; // by default it never exists.
}
/// <summary>
/// Placeholder for a routine which creates the focus box in InterlinDocForAnalysis.
/// </summary>
internal virtual void CreateFocusBox()
{
}
/// <summary>
///
/// </summary>
protected virtual void MakeVc()
{
throw new NotImplementedException();
}
protected void EnsureVc()
{
if (Vc == null)
MakeVc();
}
#region ISelectOccurrence
/// <summary>
/// This base version is used by 'read-only' tabs that need to select an
/// occurrence in IText from the analysis occurrence. Override for Sandbox-type selections.
/// </summary>
/// <param name="point"></param>
public virtual void SelectOccurrence(AnalysisOccurrence point)
{
if (point == null)
return;
Debug.Assert(point.HasWordform,
"Given annotation type should have wordform but was " + point + ".");
// The following will select the occurrence, ... I hope!
// Scroll to selection into view
var sel = SelectOccurrenceInIText(point);
if (sel == null)
return;
//sel.Install();
m_rootb.Activate(VwSelectionState.vssEnabled);
// Don't steal the focus from another window. See FWR-1795.
if (!Focused && ParentForm == Form.ActiveForm)
{
if (CanFocus)
Focus();
else
{
// For some reason as we switch to a tab containing this it isn't visible
// at the point where this is called. And that suppresses Focus().
// Arrange to get focus when we can.
VisibleChanged += FocusWhenVisible;
}
}
ScrollSelectionIntoView(sel, VwScrollSelOpts.kssoTop);
Update();
}
protected void FocusWhenVisible(object sender, EventArgs e)
{
if (CanFocus)
{
// It's possible that a focus box has been set up since we added this event handler.
// If so we prefer to focus that.
// But don't steal the focus from another window. See FWR-1795.
if (ParentForm == Form.ActiveForm)
{
var focusBox = (from Control c in Controls where c is FocusBoxController select c).FirstOrDefault();
if (focusBox != null)
focusBox.Focus();
else
Focus();
}
VisibleChanged -= FocusWhenVisible;
}
}
/// <summary>
/// Selects the specified AnalysisOccurrence in the interlinear text.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
protected internal IVwSelection SelectOccurrenceInIText(AnalysisOccurrence point)
{
Debug.Assert(point != null);
Debug.Assert(m_hvoRoot != 0);
var rgvsli = new SelLevInfo[3];
rgvsli[0].ihvo = point.Index; // 0 specifies where wf is in segment.
rgvsli[0].tag = SegmentTags.kflidAnalyses;
rgvsli[1].ihvo = point.Segment.IndexInOwner; // 1 specifies where segment is in para
rgvsli[1].tag = StTxtParaTags.kflidSegments;
rgvsli[2].ihvo = point.Segment.Paragraph.IndexInOwner; // 2 specifies were para is in IStText.
rgvsli[2].tag = StTextTags.kflidParagraphs;
return MakeWordformSelection(rgvsli);
}
/// <summary>
/// Get overridden for subclasses needing a Sandbox.
/// </summary>
/// <param name="rgvsli"></param>
/// <returns></returns>
protected virtual IVwSelection MakeWordformSelection(SelLevInfo[] rgvsli)
{
// top prop is atomic, leave index 0. Specifies displaying the contents of the Text.
IVwSelection sel;
try
{
// InterlinPrintChild and InterlinTaggingChild have no Sandbox,
// so they need a "real" interlinear text selection.
sel = RootBox.MakeTextSelInObj(0, rgvsli.Length, rgvsli, 0, null,
false, false, false, true, true);
}
catch (Exception e)
{
Debug.WriteLine(e.StackTrace);
return null;
}
return sel;
}
#endregion
internal virtual AnalysisOccurrence OccurrenceContainingSelection()
{
if (m_rootb == null)
return null;
// This works fine for non-Sandbox panes,
// Sandbox panes' selection may be in the Sandbox.
var sel = m_rootb.Selection;
return sel == null ? null : GetAnalysisFromSelection(sel);
}
protected AnalysisOccurrence GetAnalysisFromSelection(IVwSelection sel)
{
AnalysisOccurrence result = null;
var cvsli = sel.CLevels(false);
cvsli--; // CLevels includes the string property itself, but AllTextSelInfo doesn't need it.
// Out variables for AllTextSelInfo.
int ihvoRoot;
int tagTextProp;
int cpropPrevious;
int ichAnchor;
int ichEnd;
int ws;
bool fAssocPrev;
int ihvoEnd;
ITsTextProps ttpBogus;
// Main array of information retrieved from sel.
var rgvsli = SelLevInfo.AllTextSelInfo(sel, cvsli,
out ihvoRoot, out tagTextProp, out cpropPrevious, out ichAnchor, out ichEnd,
out ws, out fAssocPrev, out ihvoEnd, out ttpBogus);
if (rgvsli.Length > 1)
{
// Need to loop backwards until we get down to index 1 or index produces a valid Segment.
var i = rgvsli.Length - 1;
ISegment seg = null;
for (; i > 0; i--)
{
// get the container for whatever is selected at this level.
ICmObject container;
if (!m_objRepo.TryGetObject(rgvsli[i].hvo, out container))
return null; // may fail, e.g., trying to get bookmark for text just deleted.
seg = container as ISegment;
if (seg != null)
break;
}
if (seg != null && i > 0) // This checks the case where there is no Segment in the selection at all
{
// Make a new AnalysisOccurrence
var selObject = m_objRepo.GetObject(rgvsli[i-1].hvo);
if (selObject is IAnalysis)
{
var indexInContainer = rgvsli[i-1].ihvo;
result = new AnalysisOccurrence(seg, indexInContainer);
}
if (result == null || !result.IsValid)
result = new AnalysisOccurrence(seg, 0);
}
else
{
// TODO: other possibilities?!
Debug.Assert(false, "Reached 'other' situation in OccurrenceContainingSelection().");
}
}
return result;
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
m_contextButton = new BlueCircleButton();
m_contextButton.ForeColor = BackColor;
m_contextButton.BackColor = BackColor;
m_contextButton.Click += m_contextButton_Click;
}
protected override void OnMouseDown(MouseEventArgs e)
{
RemoveContextButtonIfPresent();
var ilineChoice = -1;
var sel = GrabMousePtSelectionToTest(e);
if (UserClickedOnLabels(sel, out ilineChoice))
{
SetContextButtonPosition(sel, ilineChoice);
}
if (e.Button == MouseButtons.Right)
{
ilineChoice = GetIndexOfLineChoice(sel);
if (ilineChoice < 0)
{
base.OnMouseDown(e);
return;
}
ShowContextMenuIfNotClosing(new Point(e.X, e.Y), ilineChoice);
}
base.OnMouseDown(e);
}
private void ShowContextMenuIfNotClosing(Point menuLocation, int ilineChoice)
{
// LT-4622 Make Configure Interlinear more accessible
// User clicked on interlinear labels, so I need to
// make a context menu and show it, if I'm not just closing one!
// This time test seems to be the only way to find out whether this click closed the last one.
if (DateTime.Now.Ticks - m_ticksWhenContextMenuClosed > 50000) // 5ms!
{
m_labelContextMenu = MakeContextMenu(ilineChoice);
m_labelContextMenu.Closed += m_labelContextMenu_Closed;
m_labelContextMenu.Show(this, menuLocation.X, menuLocation.Y);
}
}
private void SetContextButtonPosition(IVwSelection sel, int ilineChoice)
{
Debug.Assert(sel != null || !sel.IsValid, "No selection!");
//sel.GrowToWord();
Rect rcPrimary;
Rectangle rcSrcRoot;
using (new HoldGraphics(this))
{
Rect rcSec;
bool fSplit, fEndBeforeAnchor;
Rectangle rcDstRoot;
GetCoordRects(out rcSrcRoot, out rcDstRoot);
sel.Location(m_graphicsManager.VwGraphics, rcSrcRoot, rcDstRoot, out rcPrimary,
out rcSec, out fSplit, out fEndBeforeAnchor);
}
CalculateHorizContextButtonPosition(rcPrimary, rcSrcRoot);
m_iLineChoice = ilineChoice;
if (!Controls.Contains(m_contextButton))
Controls.Add(m_contextButton);
}
private void CalculateHorizContextButtonPosition(Rect rcPrimary, Rect rcSrcRoot)
{
// Enhance GJM: Not perfect for RTL script, but I can't figure out how to
// do it right just now.
var horizPosition = TextIsRightToLeft ? rcPrimary.left : rcSrcRoot.left;
m_contextButton.Location = new Point(horizPosition, rcPrimary.top);
}
protected bool TextIsRightToLeft
{
get
{
var rootWs = RootStText.MainWritingSystem;
var wsEngine = Cache.LanguageWritingSystemFactoryAccessor.get_EngineOrNull(rootWs);
return wsEngine != null && wsEngine.RightToLeftScript;
}
}
internal void RemoveContextButtonIfPresent()
{
m_iLineChoice = -1;
if (Controls.Contains(m_contextButton))
{
Controls.Remove(m_contextButton);
}
}
protected bool UserClickedOnLabels(IVwSelection selTest, out int ilineChoice)
{
ilineChoice = GetIndexOfLineChoice(selTest);
return ilineChoice > -1;
}
/// <summary>
/// Takes a mouse click point and makes an invisible selection for testing.
/// Exceptions caused by selection problems are caught, but not dealt with.
/// In case of an exception, the selection returned will be null
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
protected IVwSelection GrabMousePtSelectionToTest(MouseEventArgs e)
{
IVwSelection selTest = null;
try
{
Point pt;
Rectangle rcSrcRoot;
Rectangle rcDstRoot;
using (new HoldGraphics(this))
{
pt = PixelToView(new Point(e.X, e.Y));
GetCoordRects(out rcSrcRoot, out rcDstRoot);
}
// Make an invisible selection to see if we are in editable text.
selTest = m_rootb.MakeSelAt(pt.X, pt.Y, rcSrcRoot, rcDstRoot, false);
}
catch
{
}
return selTest;
}
protected int GetIndexOfLineChoice(IVwSelection selTest)
{
var helper = SelectionHelper.Create(selTest, this);
if (helper?.SelProps == null)
return -1;
var props = helper.SelProps;
int dummyvar;
return props.GetIntPropValues((int)FwTextPropType.ktptBulNumStartAt, out dummyvar);
}
#region Label Context Menu stuff
void m_contextButton_Click(object sender, EventArgs e)
{
Debug.Assert(m_iLineChoice > -1, "Why isn't this variable set?");
if (m_iLineChoice > -1)
{
ShowContextMenuIfNotClosing(((Control)sender).Location, m_iLineChoice);
}
}
protected virtual ContextMenuStrip MakeContextMenu(int ilineChoice)
{
var menu = new ContextMenuStrip();
bool isRibbonMenu = Vc.ToString() == "RibbonVc";
// Menu items:
// 1) Hide [name of clicked line]
// 2) Add Writing System > (submenu of other wss for this line)
// 3) Move Up
// 4) Move Down
// (separator)
// 5) Add Line > (submenu of currently hidden lines)
// 6) Configure Interlinear...
if (Vc != null && Vc.LineChoices != null && !isRibbonMenu) // just to be safe; shouldn't happen
{
var curLineChoices = Vc.LineChoices.Clone() as InterlinLineChoices;
if (curLineChoices == null)
return menu;
// 1) Hide [name of clicked line]
if (curLineChoices.OkToRemove(ilineChoice))
AddHideLineMenuItem(menu, curLineChoices, ilineChoice);
// 2) Add Writing System > (submenu of other wss for this line)
var addWsSubMenu = new ToolStripMenuItem(ITextStrings.ksAddWS);
AddAdditionalWsMenuItem(addWsSubMenu, curLineChoices, ilineChoice);
if (addWsSubMenu.DropDownItems.Count > 0)
menu.Items.Add(addWsSubMenu);
// 3) Move Up
if (curLineChoices.OkToMoveUp(ilineChoice))
AddMoveUpMenuItem(menu, ilineChoice);
// 4) Move Down
if (curLineChoices.OkToMoveDown(ilineChoice))
AddMoveDownMenuItem(menu, ilineChoice);
// Add menu separator here
menu.Items.Add(new ToolStripSeparator());
// 5) Add Line > (submenu of currently hidden lines)
var addLineSubMenu = new ToolStripMenuItem(ITextStrings.ksAddLine);
AddNewLineMenuItem(addLineSubMenu, curLineChoices);
if (addLineSubMenu.DropDownItems.Count > 0)
menu.Items.Add(addLineSubMenu);
}
// 6) Last, but not least, add a link to the Configure Interlinear dialog
var configLink = new ToolStripMenuItem(ITextStrings.ksConfigureLinkText);
configLink.Click += new EventHandler(configLink_Click); // TODO: Figure out how to pass more parameters
menu.Items.Add(configLink);
return menu;
}
private void AddHideLineMenuItem(ContextMenuStrip menu,
InterlinLineChoices curLineChoices, int ilineChoice)
{
var lineLabel = GetAppropriateLineLabel(curLineChoices, ilineChoice);
var hideItem = new ToolStripMenuItem(String.Format(ITextStrings.ksHideLine, lineLabel));
hideItem.Click += new EventHandler(hideItem_Click);
hideItem.Tag = ilineChoice;
menu.Items.Add(hideItem);
}
private string GetAppropriateLineLabel(InterlinLineChoices curLineChoices, int ilineChoice)
{
var curSpec = curLineChoices.EnabledLineSpecs[ilineChoice];
var result = curLineChoices.LabelFor(curSpec.Flid);
if (curLineChoices.EnabledRepetitionsOfFlid(curSpec.Flid) > 1)
result += "(" + curSpec.WsLabel(Cache).Text + ")";
return result;
}
private void AddAdditionalWsMenuItem(ToolStripMenuItem addSubMenu,
InterlinLineChoices curLineChoices, int ilineChoice)
{
var curSpec = curLineChoices.EnabledLineSpecs[ilineChoice];
// Do not add other writing systems for customs.
var mdc = (IFwMetaDataCacheManaged)m_cache.MetaDataCacheAccessor;
if (mdc.FieldExists(curSpec.Flid) && mdc.IsCustom(curSpec.Flid))
return;
var choices = GetWsComboItems(curSpec);
var curFlidDisplayedWss = curLineChoices.OtherEnabledWritingSystemsForFlid(curSpec.Flid, 0);
var curRealWs = GetRealWsFromSpec(curSpec);
if (!curFlidDisplayedWss.Contains(curRealWs))
curFlidDisplayedWss.Add(curRealWs);
var lgWsAcc = Cache.LanguageWritingSystemFactoryAccessor;
foreach (var item in choices)
{
var itemRealWs = lgWsAcc.GetWsFromStr(item.Id);
// Skip 'Magic' wss and ones that are already displayed
if (itemRealWs == 0 || curFlidDisplayedWss.Contains(itemRealWs))
continue;
var menuItem = new AddWritingSystemMenuItem(curSpec.Flid, itemRealWs);
menuItem.Text = item.ToString();
menuItem.Click += new EventHandler(addWsToFlidItem_Click);
addSubMenu.DropDownItems.Add(menuItem);
}
}
private IEnumerable<WsComboItem> GetWsComboItems(InterlinLineSpec curSpec)
{
using (var dummyCombobox = new ComboBox())
{
var dummyCachedBoxes = new Dictionary<ColumnConfigureDialog.WsComboContent, ComboBox.ObjectCollection>();
var comboObjects = ConfigureInterlinDialog.WsComboItemsInternal(
Cache, dummyCombobox, dummyCachedBoxes, curSpec.ComboContent);
var choices = new WsComboItem[comboObjects.Count];
comboObjects.CopyTo(choices, 0);
return choices;
}
}
private int GetRealWsFromSpec(InterlinLineSpec spec)
{
if (!spec.IsMagicWritingSystem)
{
return spec.WritingSystem;
}
// special case, the only few we support so far (and only for a few fields).
if (spec.WritingSystem == WritingSystemServices.kwsFirstAnal)
return Cache.LangProject.DefaultAnalysisWritingSystem.Handle;
if (spec.WritingSystem == WritingSystemServices.kwsVernInParagraph)
return Cache.LangProject.DefaultVernacularWritingSystem.Handle; // REVIEW (Hasso) 2018.01: this is frequently the case, but not always
int ws = -50;
try
{
ws = WritingSystemServices.InterpretWsLabel(Cache, spec.WsLabel(Cache).Text, null, 0, 0, null);
}
catch
{
Debug.Assert(ws != -50, "InterpretWsLabel was not able to interpret the Ws Label. The most likely cause for this is that a magic ws was passed in.");
}
return ws;
}
private void AddMoveUpMenuItem(ContextMenuStrip menu, int ilineChoice)
{
var moveUpItem = new ToolStripMenuItem(ITextStrings.ksMoveUp) { Tag = ilineChoice };
moveUpItem.Click += moveUpItem_Click;
menu.Items.Add(moveUpItem);
}
private void AddMoveDownMenuItem(ContextMenuStrip menu, int ilineChoice)
{
var moveDownItem = new ToolStripMenuItem(ITextStrings.ksMoveDown) { Tag = ilineChoice };
moveDownItem.Click += moveDownItem_Click;
menu.Items.Add(moveDownItem);
}
private void AddNewLineMenuItem(ToolStripMenuItem addLineSubMenu, InterlinLineChoices curLineChoices)
{
// Add menu options to add lines of flids that are in default list, but don't currently appear.
var unusedSpecs = GetUnusedSpecs(curLineChoices);
foreach (var specToAdd in unusedSpecs)
{
var menuItem = new AddLineMenuItem(specToAdd.Flid) { Text = specToAdd.ToString() };
menuItem.Click += addLineItem_Click;
addLineSubMenu.DropDownItems.Add(menuItem);
}
}
private static IEnumerable<LineOption> GetUnusedSpecs(InterlinLineChoices curLineChoices)
{
var allOptions = curLineChoices.LineOptions();
var optionsUsed = curLineChoices.EnabledItemsWithFlids(
allOptions.Select(lineOption => lineOption.Flid).ToArray());
return allOptions.Where(option => !optionsUsed.Any(
spec => spec.Flid == option.Flid)).ToList();
}
#region Menu Event Handlers
private void hideItem_Click(object sender, EventArgs e)
{
var ilineToHide = (int) (((ToolStripMenuItem) sender).Tag);
var newLineChoices = Vc.LineChoices.Clone() as InterlinLineChoices;
if (newLineChoices != null)
{
newLineChoices.Remove(newLineChoices.EnabledLineSpecs[ilineToHide]);
UpdateForNewLineChoices(newLineChoices);
}
RemoveContextButtonIfPresent(); // it will still have a spurious choice to hide the line we just hid; clicking may crash.
}
private void addWsToFlidItem_Click(object sender, EventArgs e)
{
var menuItem = sender as AddWritingSystemMenuItem;
if (menuItem == null)
return; // Impossible?
var flid = menuItem.Flid;
var wsToAdd = menuItem.Ws;
var newLineChoices = Vc.LineChoices.Clone() as InterlinLineChoices;
if (newLineChoices != null)
{
newLineChoices.Add(flid, wsToAdd);
UpdateForNewLineChoices(newLineChoices);
}
}
private void moveUpItem_Click(object sender, EventArgs e)
{
var ilineToHide = (int)(((ToolStripMenuItem) sender).Tag);
var newLineChoices = Vc.LineChoices.Clone() as InterlinLineChoices;
if (newLineChoices != null)
{
newLineChoices.MoveUp(ilineToHide);
UpdateForNewLineChoices(newLineChoices);
}
}
private void moveDownItem_Click(object sender, EventArgs e)
{
var ilineToHide = (int)(((ToolStripMenuItem) sender).Tag);
var newLineChoices = Vc.LineChoices.Clone() as InterlinLineChoices;
if (newLineChoices != null)
{
newLineChoices.MoveDown(ilineToHide);
UpdateForNewLineChoices(newLineChoices);
}
}
private void addLineItem_Click(object sender, EventArgs e)
{
var menuItem = sender as AddLineMenuItem;
if (menuItem == null)
return; // Impossible?
var flid = menuItem.Flid;
var newLineChoices = Vc.LineChoices.Clone() as InterlinLineChoices;
var mdc = (IFwMetaDataCacheManaged)m_cache.MetaDataCacheAccessor;
// Some virtual Ids such as -61 and 103 create standard items. so add those.
if (newLineChoices != null && (mdc.FieldExists(flid) || (flid <= ComplexConcPatternVc.kfragFeatureLine)))
{
newLineChoices.Add(flid, 0, true);
UpdateForNewLineChoices(newLineChoices);
}
}
private void configLink_Click(object sender, EventArgs e)
{
OnConfigureInterlinear(null/*, this is InterlinRibbon*/);
}
private void m_labelContextMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
m_ticksWhenContextMenuClosed = DateTime.Now.Ticks;
}
#endregion
#endregion
/// <summary>
/// True if we will be doing editing (display sandbox, restrict field order choices, etc.).
/// </summary>
public bool ForEditing { get; set; }
/// <summary>
/// The property table key storing InterlinLineChoices used by our display.
/// Parent controls (e.g. InterlinMaster) should pass in their own property
/// to configure for contexts it knows about.
/// </summary>
private string ConfigPropName { get; set; }
/// <summary>
/// The old property table key storing InterlinLineChoices used by our display.
/// </summary>
private static string OldConfigPropName { get; set; }
/// <summary>
/// </summary>
/// <param name="lineConfigPropName">the key used to store/restore line configuration settings.</param>
/// <param name="oldLineConfigPropName">the old key used to restore line configuration settings.</param>
/// <param name="mode"></param>
/// <returns></returns>
public InterlinLineChoices SetupLineChoices(string lineConfigPropName, string oldLineConfigPropName, InterlinLineChoices.InterlinMode mode)
{
ConfigPropName = lineConfigPropName;
OldConfigPropName = oldLineConfigPropName;
InterlinLineChoices lineChoices;
if (!TryRestoreLineChoices(out lineChoices))
{
if (ForEditing)
{
lineChoices = EditableInterlinLineChoices.DefaultChoices(m_cache.LangProject,
WritingSystemServices.kwsVernInParagraph, WritingSystemServices.kwsAnal);
lineChoices.Mode = mode;
if (mode == InterlinLineChoices.InterlinMode.Gloss ||
mode == InterlinLineChoices.InterlinMode.GlossAddWordsToLexicon)
lineChoices.SetStandardGlossState();
else
lineChoices.SetStandardState();
}
else
{
lineChoices = InterlinLineChoices.DefaultChoices(m_cache.LangProject,
WritingSystemServices.kwsVernInParagraph, WritingSystemServices.kwsAnal, mode);
}
}
else if (ForEditing)
{
// just in case this hasn't been set for restored lines
lineChoices.Mode = mode;
}
LineChoices = lineChoices;
return LineChoices;
}
/// <summary>
/// This is for setting m_vc.LineChoices even before we have a valid vc.
/// </summary>
protected InterlinLineChoices LineChoices { get; set; }
/// <summary>
/// Tries to restore the LineChoices saved in the ConfigPropName property in the property table.
/// </summary>
/// <param name="lineChoices"></param>
/// <returns></returns>
internal bool TryRestoreLineChoices(out InterlinLineChoices lineChoices)
{
lineChoices = null;
var persist = m_propertyTable.GetStringProperty(ConfigPropName, null, PropertyTable.SettingsGroup.LocalSettings);
if (persist == null)
persist = m_propertyTable.GetStringProperty(OldConfigPropName, null, PropertyTable.SettingsGroup.LocalSettings);
if (persist != null)
{
// Intentionally never pass OldConfigPropName into Restore to prevent corrupting it's old value with the new format.
lineChoices = InterlinLineChoices.Restore(persist, m_cache.LanguageWritingSystemFactoryAccessor,
m_cache.LangProject, WritingSystemServices.kwsVernInParagraph, m_cache.DefaultAnalWs, InterlinLineChoices.InterlinMode.Analyze, m_propertyTable, ConfigPropName);
}
return persist != null && lineChoices != null;
}
/// <summary>
/// Launch the Configure interlinear dialog and deal with the results
/// </summary>
/// <param name="argument"></param>
public bool OnConfigureInterlinear(object argument)
{
using (var dlg = new ConfigureInterlinDialog(m_mediator, m_propertyTable, this.m_cache, this.m_propertyTable.GetValue<IHelpTopicProvider>("HelpTopicProvider"),
this.Vc.LineChoices.Clone() as InterlinLineChoices))
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
UpdateForNewLineChoices(dlg.Choices);
}
return true; // We handled this
}
}
/// <summary>
/// Persist the new line choices and
/// Reconstruct the document based on the given newChoices for interlinear lines.
/// </summary>
/// <param name="newChoices"></param>
internal virtual void UpdateForNewLineChoices(InterlinLineChoices newChoices)
{
Vc.LineChoices = newChoices;
LineChoices = newChoices;
PersistAndDisplayChangedLineChoices();
}
internal void PersistAndDisplayChangedLineChoices()
{
m_propertyTable.SetProperty(ConfigPropName,
Vc.LineChoices.Persist(m_cache.LanguageWritingSystemFactoryAccessor),
PropertyTable.SettingsGroup.LocalSettings,
true);
UpdateDisplayForNewLineChoices();
}
/// <summary>
/// Do whatever is necessary to display new line choices.
/// </summary>
private void UpdateDisplayForNewLineChoices()
{
if (m_rootb == null)
return;
m_rootb.Reconstruct();
}
/// <summary>
/// delegate for determining whether a paragraph should be updated according to occurrences based upon
/// the given wordforms.
/// </summary>
/// <param name="para"></param>
/// <param name="wordforms"></param>
/// <returns></returns>
internal delegate bool UpdateGuessesCondition(IStTxtPara para, HashSet<IWfiWordform> wordforms);
/// <summary>
/// Update any necessary guesses when the specified wordforms change.
/// </summary>
/// <param name="wordforms"></param>
internal virtual void UpdateGuesses(HashSet<IWfiWordform> wordforms)
{
UpdateGuesses(wordforms, true);
}
private void UpdateGuesses(HashSet<IWfiWordform> wordforms, bool fUpdateDisplayWhereNeeded)
{
// now update the guesses for the paragraphs.
var pdut = new ParaDataUpdateTracker(Vc.GuessServices, Vc.GuessCache, this);
if (wordforms != null)
// The user may have changed the analyses for wordforms. (LT-21814)
foreach (var wordform in wordforms)
pdut.NoteChangedWordform(wordform.Hvo);
foreach (IStTxtPara para in RootStText.ParagraphsOS)
pdut.LoadAnalysisData(para, wordforms);
if (fUpdateDisplayWhereNeeded)
{
// now update the display with the affected annotations.
foreach (var changed in pdut.ChangedAnnotations)
UpdateDisplayForOccurrence(changed);
}
}
/// <summary>
/// Update all the guesses in the interlinear doc.
/// </summary>
internal virtual void UpdateGuessData()
{
UpdateGuesses(null, false);
}
protected void UpdateDisplayForOccurrence(AnalysisOccurrence occurrence)
{
if (occurrence == null)
return;
// Simluate replacing the wordform in the relevant segment with itself. This lets the VC Display method run again, this
// time possibly getting a different answer about whether hvoAnnotation is the current annotation, or about the
// size of the Sandbox.
m_rootb.PropChanged(occurrence.Segment.Hvo, SegmentTags.kflidAnalyses, occurrence.Index, 1, 1);
}
internal InterlinMaster GetMaster()
{
for (Control parentControl = Parent; parentControl != null; parentControl = parentControl.Parent)
{
var master = parentControl as InterlinMaster;
if (master != null)
return master;
}
return null;
}
#region implemention of IChangeRootObject
public virtual void SetRoot(int hvo)
{
EnsureVc();
if (LineChoices != null)
Vc.LineChoices = LineChoices;
SetRootInternal(hvo);
AddDecorator();
RemoveContextButtonIfPresent(); // Don't want to keep the context button for a different text!
}
/// <summary>
/// Returns the rootbox of this object, or null if not applicable
/// </summary>
/// <returns></returns>
public IVwRootBox GetRootBox()
{
return RootBox;
}
#endregion
/// <summary>
/// Allows InterlinTaggingChild to add a DomainDataByFlid decorator to the rootbox.
/// </summary>
protected virtual void AddDecorator()
{
}
protected virtual void SetRootInternal(int hvo)
{
// since we are rebuilding the display, reset our sandbox mask.
m_hvoRoot = hvo;
if (hvo != 0)
{
RootStText = Cache.ServiceLocator.GetInstance<IStTextRepository>().GetObject(hvo);