Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions Src/Common/SimpleRootSite/EditingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using SIL.Reporting;
using SIL.LCModel.Utils;
using SIL.Windows.Forms.Keyboarding;
using SIL.LCModel;

namespace SIL.FieldWorks.Common.RootSites
{
Expand Down Expand Up @@ -3426,6 +3427,18 @@ public string GetClipboardAsString()
/// <returns>True if the paste succeeded, false otherwise</returns>
/// ------------------------------------------------------------------------------------
public virtual bool PasteClipboard()
{
return PasteClipboard(null);
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// Paste data from the clipboard into the view. Caller is reponsible to make UOW.
/// </summary>
/// <param name="cache">The LcmCache.</param>
/// <returns>True if the paste succeeded, false otherwise</returns>
/// ------------------------------------------------------------------------------------
public virtual bool PasteClipboard(LcmCache cache)
{
CheckDisposed();
// Do nothing if command is not enabled. Needed for Ctrl-V keypress.
Expand All @@ -3450,23 +3463,25 @@ public virtual bool PasteClipboard()
ChangeStyleForPaste(vwsel, ref vttp);

ITsString tss = GetTextFromClipboard(vwsel, vwsel.CanFormatChar, vttp[0]);
return PasteCore(tss);
return PasteCore(tss, cache);
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// Core logic for Paste command (without modifying the clipboard, to support testing).
/// </summary>
/// <param name="tss">The TsString to paste.</param>
/// <param name="cache">The LcmCache.</param>
/// <returns>True if that paste succeeded, false otherwise</returns>
/// ------------------------------------------------------------------------------------
public bool PasteCore(ITsString tss)
public bool PasteCore(ITsString tss, LcmCache cache)
{
IVwSelection vwsel = EditedRootBox.Selection;
try
{
if (tss != null)
{
tss = ReplaceExternalWritingSystems(tss, vwsel, cache);
// At this point, we may need to override internal formatting values
// for certain target rootsites. We do this with an event handler that the
// rootsite can register for its editing helper. (See LT-1445.)
Expand Down Expand Up @@ -3507,6 +3522,58 @@ public bool PasteCore(ITsString tss)
return true;
}

private ITsString ReplaceExternalWritingSystems(ITsString tss, IVwSelection vwsel, LcmCache cache)
{
if (cache == null)
// Can't tell whether writing systems are external.
return tss;

// Check for external writing systems.
bool hasExternalWritingSystem = false;
for (int i = 0; i < tss.RunCount; i++)
{
int ws = tss.get_WritingSystem(i);
if (IsExternalWritingSystem(ws, cache))
{
hasExternalWritingSystem = true;
break;
}
}
if (!hasExternalWritingSystem)
return tss;

// Replace external writing systems with selection's writing system.
ITsString selTss;
vwsel.GetSelectionString(out selTss, string.Empty);
ITsStrBldr stringBldr = TsStringUtils.MakeStrBldr();
TsRunInfo runInfo;
for (int irun = 0; irun < tss.RunCount; irun++)
{
int ttv, ws;
ITsTextProps props = tss.FetchRunInfo(irun, out runInfo);
ws = props.GetIntPropValues((int)FwTextPropType.ktptWs, out ttv);
if (IsExternalWritingSystem(ws, cache))
{
ws = selTss.get_WritingSystemAt(0);
ITsPropsBldr propsBldr = props.GetBldr();
propsBldr.SetIntPropValues((int)FwTextPropType.ktptWs, ttv, ws);
props = propsBldr.GetTextProps();
}
stringBldr.Replace(runInfo.ichMin, runInfo.ichMin, tss.get_RunText(irun), props);
}
return stringBldr.GetString();
}

private bool IsExternalWritingSystem(int ws, LcmCache cache)
{
foreach (CoreWritingSystemDefinition writingSystem in cache.LanguageProject.AllWritingSystems)
{
if (writingSystem.Handle == ws)
return false;
}
return true;
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// Gets the text from clipboard.
Expand Down
2 changes: 1 addition & 1 deletion Src/xWorks/FwXWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public bool OnEditPaste(object arg)
Cache.ServiceLocator.GetInstance<IActionHandler>(), stUndo, stRedo))
using (new DataUpdateMonitor(this, "EditPaste"))
{
if (m_viewHelper.ActiveView.EditingHelper.PasteClipboard())
if (m_viewHelper.ActiveView.EditingHelper.PasteClipboard(Cache))
undoHelper.RollBack = false;
}
return true;
Expand Down
Loading