-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathFocusBoxController.cs
More file actions
597 lines (529 loc) · 18.7 KB
/
FocusBoxController.cs
File metadata and controls
597 lines (529 loc) · 18.7 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
// Copyright (c) 2015 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.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using System.Xml;
using SIL.LCModel.Core.KernelInterfaces;
using SIL.FieldWorks.Common.ViewsInterfaces;
using SIL.FieldWorks.Common.RootSites;
using SIL.LCModel;
using SIL.LCModel.DomainServices;
using SIL.LCModel.Infrastructure;
using SIL.FieldWorks.Common.FwUtils;
using SIL.FieldWorks.FdoUi;
using SIL.LCModel.Utils;
using XCore;
namespace SIL.FieldWorks.IText
{
public partial class FocusBoxController : UserControl, ISelectOccurrence, SimpleRootSite.ISuppressDefaultKeyboardOnKillFocus
{
internal IAnalysisControlInternal m_sandbox;
protected Mediator m_mediator;
protected PropertyTable m_propertyTable;
private IVwStylesheet m_stylesheet;
protected InterlinLineChoices m_lineChoices;
/// <summary>
/// currently only valid after SelectOccurrence has a valid occurrence.
/// </summary>
internal LcmCache Cache { get; set; }
public FocusBoxController()
{
this.Visible = false;
InitializeComponent();
btnLinkNextWord.GotFocus += HandleFocusWrongButton;
btnMenu.GotFocus += HandleFocusWrongButton;
}
internal bool IsDirty
{
get { return m_sandbox.IsDirty; }
}
// There is no logical reason for other buttons ever to get the focus. But .NET helpfully focuses the link words button
// as we hide the focus box. And in some other circumstance, which I can't even figure out, it focuses the menu button.
// I can't figure out how to prevent it, but it's better for the confirm
// changes button to have it instead, since that's the button that is supposed to have the same function
// as Enter, so if .NET activates some button because the user presses Enter while it has focus,
// it had better be that one. See FWR-3399 and FWR-3453.
void HandleFocusWrongButton(object sender, EventArgs e)
{
btnConfirmChanges.Focus();
}
public void UpdateLineChoices(InterlinLineChoices choices)
{
m_lineChoices = choices;
// Under certain circumstances this can get called when sandbox is null (LT-11468)
if (m_sandbox != null)
m_sandbox.UpdateLineChoices(choices);
}
public FocusBoxController(Mediator mediator, PropertyTable propertyTable, IVwStylesheet stylesheet, InterlinLineChoices lineChoices)
: this()
{
m_mediator = mediator;
m_propertyTable = propertyTable;
m_stylesheet = stylesheet;
m_lineChoices = lineChoices;
if (m_mediator != null)
SetToolTips();
}
private void SetToolTips()
{
toolTip.SetToolTip(btnBreakPhrase,
AppendShortcutToToolTip(m_mediator.CommandSet["CmdBreakPhrase"] as Command));
toolTip.SetToolTip(btnConfirmChanges,
AppendShortcutToToolTip(m_mediator.CommandSet["CmdApproveAndMoveNext"] as Command));
toolTip.SetToolTip(btnLinkNextWord,
AppendShortcutToToolTip(m_mediator.CommandSet["CmdMakePhrase"] as Command));
toolTip.SetToolTip(btnUndoChanges,
AppendShortcutToToolTip(m_mediator.CommandSet["CmdUndo"] as Command));
toolTip.SetToolTip(btnConfirmChangesForWholeText,
AppendShortcutToToolTip(m_mediator.CommandSet["CmdApproveForWholeTextAndMoveNext"] as Command));
}
internal InterlinDocForAnalysis InterlinDoc
{
get
{
if (Parent != null && Parent is InterlinDocForAnalysis)
return Parent as InterlinDocForAnalysis;
else
return null;
}
}
internal AnalysisOccurrence SelectedOccurrence { get; set; }
internal AnalysisTree InitialAnalysis { get; set; }
#region Sandbox
internal IAnalysisControlInternal InterlinWordControl
{
get { return m_sandbox; }
set { m_sandbox = value; }
}
#region Sandbox setup
/// <summary>
/// Change root of Sandbox or create it; Lay it out and figure its size;
/// tell m_vc the size.
/// </summary>
/// <returns></returns>
private void ChangeOrCreateSandbox(AnalysisOccurrence selected)
{
this.SuspendLayout();
panelSandbox.SuspendLayout();
if (InterlinDoc != null)
{
InterlinDoc.RecordGuessIfNotKnown(selected);
}
int color = (int)CmObjectUi.RGB(DefaultBackColor);
//if this sandbox is presenting a wordform with multiple possible analyses then set the
//bg color indicator
if (selected.Analysis.Wordform != null &&
SandboxBase.GetHasMultipleRelevantAnalyses(selected.Analysis.Wordform, IsParsingDevMode()))
{
color = InterlinVc.MultipleApprovedGuessColor;
}
if (m_sandbox == null)
{
m_sandbox = CreateNewSandbox(selected);
m_sandbox.MultipleAnalysisColor = color;
}
else
{
//set the color before switching so that the color is correct when DisplayWordForm is called
m_sandbox.MultipleAnalysisColor = color;
m_sandbox.SwitchWord(selected);
}
UpdateButtonState();
// add the sandbox plus some padding.
panelSandbox.ResumeLayout();
this.ResumeLayout();
SetSandboxSize();
}
internal bool IsParsingDevMode()
{
if (InterlinDoc?.GetMaster() == null)
return false;
return InterlinDoc.GetMaster().IsParsingDevMode();
}
internal virtual IAnalysisControlInternal CreateNewSandbox(AnalysisOccurrence selected)
{
Sandbox sandbox = new Sandbox(selected.Analysis.Cache, m_mediator, m_propertyTable, m_stylesheet,
m_lineChoices, selected, this);
sandbox.SizeToContent = true; // Layout will ignore size.
//sandbox.Mediator = Mediator;
sandbox.StyleSheet = m_stylesheet;
panelSandbox.Controls.Add(sandbox); // Makes it real and may give it a root box.
// Note: adding sandbox to Controls doesn't always MakeRoot(), because OnHandleCreated happens
// only when the parent control is Visible.
if (sandbox.RootBox == null)
sandbox.MakeRoot();
AdjustControlsForRightToLeftWritingSystem(sandbox);
// this is needed for the Undo button.
sandbox.SandboxChangedEvent += m_sandbox_SandboxChangedEvent;
return sandbox as IAnalysisControlInternal;
}
// Set the size of the sandbox on the VC...if it exists yet.
void SetSandboxSize()
{
// Make the focus box the size it really needs to be for the current object.
// This will adjust its size, but we already know we're trying to do that,
// so we don't want the notification...it attempts work we're already in
// the middle of and may confuse things or produce flashing.
AdjustSizeAndLocationForControls(true);
}
/// <summary>
/// NOTE: currently needs to get called after sandbox.MakeRoot, since that's when RightToLeftWritingSystem is valid.
/// </summary>
/// <param name="sandbox"></param>
private void AdjustControlsForRightToLeftWritingSystem(Sandbox sandbox)
{
if (sandbox.RightToLeftWritingSystem && btnConfirmChanges.Location.X != 0)
{
panelSandbox.Anchor = AnchorStyles.Right | AnchorStyles.Top;
// make buttons RightToLeft oriented.
btnConfirmChanges.Anchor = AnchorStyles.Left;
btnConfirmChanges.Location = new Point(0, btnConfirmChanges.Location.Y);
btnConfirmChangesForWholeText.Anchor = AnchorStyles.Left;
btnConfirmChangesForWholeText.Location =
new Point(btnConfirmChanges.Width, btnConfirmChangesForWholeText.Location.Y);
btnUndoChanges.Anchor = AnchorStyles.Left;
btnUndoChanges.Location = new Point(
btnConfirmChanges.Width + btnConfirmChangesForWholeText.Width, btnUndoChanges.Location.Y);
btnBreakPhrase.Anchor = AnchorStyles.Left;
btnBreakPhrase.Location = new Point(
btnConfirmChanges.Width + btnConfirmChangesForWholeText.Width + btnUndoChanges.Width, btnBreakPhrase.Location.Y);
btnMenu.Anchor = AnchorStyles.Right;
btnMenu.Location = new Point(panelControlBar.Width - btnMenu.Width, btnMenu.Location.Y);
}
}
protected override void OnLayout(LayoutEventArgs e)
{
base.OnLayout(e);
AdjustSizeAndLocationForControls(this.Visible);
}
bool m_fAdjustingSize = false;
internal void AdjustSizeAndLocationForControls(bool fAdjustOverallSize)
{
if (m_fAdjustingSize)
return;
m_fAdjustingSize = true;
try
{
if (m_sandbox != null && m_sandbox.RightToLeftWritingSystem && m_sandbox is UserControl)
{
UserControl sandbox = m_sandbox as UserControl;
if (panelSandbox.Width != sandbox.Width)
panelSandbox.Width = sandbox.Width;
if (sandbox.Location.X != panelSandbox.Width - sandbox.Width)
sandbox.Location = new Point(panelSandbox.Width - sandbox.Width, sandbox.Location.Y);
}
if (m_sandbox != null && fAdjustOverallSize)
{
panelControlBar.Width = panelSandbox.Width; // if greater than min width.
// move control panel to bottom of sandbox panel.
panelControlBar.Location = new Point(panelControlBar.Location.X, panelSandbox.Height - 1);
// move side bar to right of sandbox panel.
if (m_sandbox.RightToLeftWritingSystem)
{
panelSidebar.Location = new Point(0, panelSidebar.Location.Y);
panelControlBar.Location = new Point(panelSidebar.Width, panelControlBar.Location.Y);
panelSandbox.Location = new Point(panelSidebar.Width, panelSandbox.Location.Y);
}
else
{
panelSidebar.Location = new Point(panelSandbox.Width, panelSidebar.Location.Y);
}
this.Size = new Size(panelSidebar.Width + Math.Max(panelSandbox.Width, panelControlBar.Width),
panelControlBar.Height + panelSandbox.Height);
}
}
finally
{
Focus();
m_fAdjustingSize = false;
}
}
#endregion Sandbox setup
/// <summary>
/// turn focus over to sandbox
/// </summary>
/// <param name="e"></param>
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
FocusSandbox();
}
/// <summary>
/// Call this rather than "FocusBox.Focus()" because we don't want focus passing through this
/// to the sandbox if we can help it. One problem can be that as the main view loses focus it
/// resets the default keyboard.
/// </summary>
internal void FocusSandbox()
{
if (m_sandbox != null && m_sandbox is UserControl)
(m_sandbox as UserControl).Focus();
}
#endregion
#region InterlinDoc interface
#region ISelectOccurrence Members
public virtual void SelectOccurrence(AnalysisOccurrence selected)
{
SelectedOccurrence = selected;
InitialAnalysis = new AnalysisTree();
if (SelectedOccurrence != null)
{
if (Cache == null)
Cache = SelectedOccurrence.Analysis.Cache;
InitialAnalysis.Analysis = SelectedOccurrence.Analysis;
}
ChangeOrCreateSandbox(selected);
}
#endregion
internal bool MakeDefaultSelection(object parameter)
{
if (IsDisposed)
return false; // result is not currently used, not sure what it should be.
if (InterlinWordControl != null)
{
InterlinWordControl.MakeDefaultSelection();
}
else
{
// It wasn't ready yet. Try once more later.
m_mediator.IdleQueue.Add(IdleQueuePriority.Medium, _ =>
{
InterlinWordControl?.MakeDefaultSelection();
return true;
});
}
return true;
}
#endregion InterlinDoc interface
void m_sandbox_SandboxChangedEvent(object sender, SandboxChangedEventArgs e)
{
UpdateButtonState_Undo();
}
internal bool ShowLinkWordsIcon
{
get
{
return HaveValidOccurrence() && SelectedOccurrence.CanMakePhraseWithNextWord();
}
}
private bool HaveValidOccurrence()
{
return SelectedOccurrence !=null && SelectedOccurrence.IsValid;
}
internal bool ShowBreakPhraseIcon
{
get
{
return HaveValidOccurrence() && SelectedOccurrence.CanBreakPhrase();
}
}
private void UpdateButtonState()
{
// only update button state when we're fully installed.
if (InterlinDoc == null || !InterlinDoc.IsFocusBoxInstalled)
return;
// we're fully installed, so update the buttons.
btnLinkNextWord.Visible = btnLinkNextWord.Enabled = ShowLinkWordsIcon;
btnBreakPhrase.Visible = btnBreakPhrase.Enabled = ShowBreakPhraseIcon;
UpdateButtonState_Undo();
// LT-11406: Somehow JoinWords (and BreakPhrase) leaves the selection elsewhere,
// this should make it select the default location.
m_mediator.IdleQueue.Add(IdleQueuePriority.Medium, MakeDefaultSelection);
}
private void UpdateButtonState_Undo()
{
bool shouldEnable = InterlinWordControl != null && InterlinWordControl.HasChanged;
btnUndoChanges.Visible = btnUndoChanges.Enabled = shouldEnable;
}
private void btnLinkNextWord_Click(object sender, EventArgs e)
{
OnJoinWords(m_mediator.CommandSet["CmdMakePhrase"] as Command);
}
/// <summary>
/// Note: Assume we are in the OnDisplayShowLinkWords is true context.
/// </summary>
public bool OnJoinWords(object arg)
{
var cmd = (ICommandUndoRedoText)arg;
UndoableUnitOfWorkHelper.Do(cmd.UndoText, cmd.RedoText, Cache.ActionHandlerAccessor,
() =>
{
SelectedOccurrence.MakePhraseWithNextWord();
if (InterlinDoc != null)
{
// Joining words renumbers the occurrences.
// We need to clear the analysis cache to avoid problems (cf LT-21965).
InterlinDoc.ResetAnalysisCache();
InterlinDoc.RecordGuessIfNotKnown(SelectedOccurrence);
}
});
InterlinWordControl.SwitchWord(SelectedOccurrence);
UpdateButtonState();
return true;
}
/// <summary>
/// split the current occurrence into occurrences for each word in the phrase-wordform.
/// (if it IsPhrase)
/// </summary>
public void OnBreakPhrase(object arg)
{
// (LT-8069) in some odd circumstances, the break phrase icon lingers on the tool bar menu when it should
// have disappeared. If we're in that state, just return.
if (!ShowBreakPhraseIcon)
return;
var cmd = (ICommandUndoRedoText)arg;
UndoableUnitOfWorkHelper.Do(cmd.UndoText, cmd.RedoText, Cache.ActionHandlerAccessor,
() => SelectedOccurrence.BreakPhrase());
if (InterlinDoc != null)
{
// Breaking phrases renumbers the occurrences.
// We need to clear the analysis cache to avoid problems.
InterlinDoc.ResetAnalysisCache();
}
InterlinWordControl.SwitchWord(SelectedOccurrence);
UpdateButtonState();
}
private void btnBreakPhrase_Click(object sender, EventArgs e)
{
OnBreakPhrase(m_mediator.CommandSet["CmdBreakPhrase"] as Command);
}
private void btnUndoChanges_Click(object sender, EventArgs e)
{
// LT-14001 the only time we don't want the sandbox changed event to fire
// and update the undo button state is when we're actually processing
// the undo! Other changes to the sandbox need to update the undo button state.
var sandbox = m_sandbox as SandboxBase;
if (sandbox != null)
sandbox.SandboxChangedEvent -= m_sandbox_SandboxChangedEvent;
InterlinWordControl.Undo();
if (sandbox != null)
sandbox.SandboxChangedEvent += m_sandbox_SandboxChangedEvent;
UpdateButtonState();
}
private void btnConfirmChanges_Click(object sender, EventArgs e)
{
if (this is FocusBoxControllerForDisplay)
(this as FocusBoxControllerForDisplay).OnApproveAndMoveNext();
}
private void btnConfirmChangesForWholeText_Click(object sender, EventArgs e)
{
ApproveGuessOrChangesForWholeTextAndMoveNext(m_mediator.CommandSet["CmdApproveForWholeTextAndMoveNext"] as Command);
}
private void btnMenu_Click(object sender, EventArgs e)
{
XWindow window = m_propertyTable.GetValue<XWindow>("window");
window.ShowContextMenu("mnuFocusBox",
btnMenu.PointToScreen(new Point(btnMenu.Width / 2, btnMenu.Height / 2)),
null,
null);
}
private string ShortcutText(Keys shortcut)
{
string shortcutText = "";
if (shortcut != Keys.None)
{
shortcutText = TypeDescriptor.GetConverter(typeof(Keys)).ConvertToString(null, CultureInfo.InvariantCulture, shortcut);
}
return shortcutText;
}
private string AppendShortcutToToolTip(Command command)
{
string shortcutText = ShortcutText(command.Shortcut);
if (command.Id == "CmdApproveAndMoveNext" && shortcutText.IndexOf('+') > 0)
{
// alter this one, since there can be two key combinations that should work for it (Control-key is not always necessary).
shortcutText = shortcutText.Insert(0, "(");
shortcutText = shortcutText.Insert(shortcutText.IndexOf('+') + 1, ")");
}
string tooltip = command.ToolTip;
return AppendShortcutToToolTip(tooltip, shortcutText);
}
private string AppendShortcutToToolTip(string toolTip, string shortcut)
{
if (String.IsNullOrEmpty(shortcut))
return toolTip;
return String.Format("{0} ({1})", toolTip, shortcut);
}
// consider updating the button state in another way
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
UpdateButtonState();
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
UpdateButtonState();
}
private void toolTip_Popup(object sender, PopupEventArgs e)
{
// for some reason, tool tips for sub controls only work
// when the parent control has a tool tip.
// we only want to show tool tips for the buttons.
if (!(e.AssociatedControl is Button))
{
// we don't want to actually show this tool tip.
e.Cancel = true;
}
}
}
/// <summary>
/// implements everything needed by the FocuxBoxControl
/// </summary>
internal interface IAnalysisControlInternal
{
bool RightToLeftWritingSystem { get; }
bool HasChanged { get; }
void Undo();
void SwitchWord(AnalysisOccurrence selected);
void MakeDefaultSelection();
bool ShouldSave(bool fSaveGuess);
AnalysisTree GetRealAnalysis(bool fSaveGuess, out IWfiAnalysis obsoleteAna);
int GetLineOfCurrentSelection();
bool SelectOnOrBeyondLine(int startLine, int increment);
void UpdateLineChoices(InterlinLineChoices choices);
int MultipleAnalysisColor { set; }
bool IsDirty { get; }
}
/// <summary>
/// Implements IxCoreColleague for use in displaying and responding to user actions.
/// TODO: move mediator related code here. but what to do about Sandbox?
/// </summary>
public partial class FocusBoxControllerForDisplay : FocusBoxController, IxCoreColleague
{
public FocusBoxControllerForDisplay(Mediator mediator, PropertyTable propertyTable, IVwStylesheet stylesheet, InterlinLineChoices lineChoices, bool rightToLeft)
: base(mediator, propertyTable, stylesheet, lineChoices)
{
m_fRightToLeft = rightToLeft;
}
#region IxCoreColleague Members
public IxCoreColleague[] GetMessageTargets()
{
if (InterlinWordControl is IxCoreColleague)
return new[] {(IxCoreColleague)InterlinWordControl, this };
return new IxCoreColleague[] { this };
}
public void Init(Mediator mediator, PropertyTable propertyTable, XmlNode configurationParameters)
{
// Do nothing.
}
/// <summary>
/// Should not be called if disposed.
/// </summary>
public bool ShouldNotCall
{
get { return IsDisposed; }
}
/// <summary>
/// Mediator message handling Priority
/// </summary>
public int Priority
{
get { return (int)ColleaguePriority.Low; }
}
#endregion
}
}