2222using SIL . Reporting ;
2323using SIL . LCModel . Utils ;
2424using SIL . Windows . Forms . Keyboarding ;
25+ using SIL . LCModel ;
2526
2627namespace SIL . FieldWorks . Common . RootSites
2728{
@@ -3426,6 +3427,18 @@ public string GetClipboardAsString()
34263427 /// <returns>True if the paste succeeded, false otherwise</returns>
34273428 /// ------------------------------------------------------------------------------------
34283429 public virtual bool PasteClipboard ( )
3430+ {
3431+ return PasteClipboard ( null ) ;
3432+ }
3433+
3434+ /// ------------------------------------------------------------------------------------
3435+ /// <summary>
3436+ /// Paste data from the clipboard into the view. Caller is reponsible to make UOW.
3437+ /// </summary>
3438+ /// <param name="cache">The LcmCache.</param>
3439+ /// <returns>True if the paste succeeded, false otherwise</returns>
3440+ /// ------------------------------------------------------------------------------------
3441+ public virtual bool PasteClipboard ( LcmCache cache )
34293442 {
34303443 CheckDisposed ( ) ;
34313444 // Do nothing if command is not enabled. Needed for Ctrl-V keypress.
@@ -3450,23 +3463,25 @@ public virtual bool PasteClipboard()
34503463 ChangeStyleForPaste ( vwsel , ref vttp ) ;
34513464
34523465 ITsString tss = GetTextFromClipboard ( vwsel , vwsel . CanFormatChar , vttp [ 0 ] ) ;
3453- return PasteCore ( tss ) ;
3466+ return PasteCore ( tss , cache ) ;
34543467 }
34553468
34563469 /// ------------------------------------------------------------------------------------
34573470 /// <summary>
34583471 /// Core logic for Paste command (without modifying the clipboard, to support testing).
34593472 /// </summary>
34603473 /// <param name="tss">The TsString to paste.</param>
3474+ /// <param name="cache">The LcmCache.</param>
34613475 /// <returns>True if that paste succeeded, false otherwise</returns>
34623476 /// ------------------------------------------------------------------------------------
3463- public bool PasteCore ( ITsString tss )
3477+ public bool PasteCore ( ITsString tss , LcmCache cache )
34643478 {
34653479 IVwSelection vwsel = EditedRootBox . Selection ;
34663480 try
34673481 {
34683482 if ( tss != null )
34693483 {
3484+ tss = ReplaceExternalWritingSystems ( tss , vwsel , cache ) ;
34703485 // At this point, we may need to override internal formatting values
34713486 // for certain target rootsites. We do this with an event handler that the
34723487 // rootsite can register for its editing helper. (See LT-1445.)
@@ -3507,6 +3522,58 @@ public bool PasteCore(ITsString tss)
35073522 return true ;
35083523 }
35093524
3525+ private ITsString ReplaceExternalWritingSystems ( ITsString tss , IVwSelection vwsel , LcmCache cache )
3526+ {
3527+ if ( cache == null )
3528+ // Can't tell whether writing systems are external.
3529+ return tss ;
3530+
3531+ // Check for external writing systems.
3532+ bool hasExternalWritingSystem = false ;
3533+ for ( int i = 0 ; i < tss . RunCount ; i ++ )
3534+ {
3535+ int ws = tss . get_WritingSystem ( i ) ;
3536+ if ( IsExternalWritingSystem ( ws , cache ) )
3537+ {
3538+ hasExternalWritingSystem = true ;
3539+ break ;
3540+ }
3541+ }
3542+ if ( ! hasExternalWritingSystem )
3543+ return tss ;
3544+
3545+ // Replace external writing systems with selection's writing system.
3546+ ITsString selTss ;
3547+ vwsel . GetSelectionString ( out selTss , string . Empty ) ;
3548+ ITsStrBldr stringBldr = TsStringUtils . MakeStrBldr ( ) ;
3549+ TsRunInfo runInfo ;
3550+ for ( int irun = 0 ; irun < tss . RunCount ; irun ++ )
3551+ {
3552+ int ttv , ws ;
3553+ ITsTextProps props = tss . FetchRunInfo ( irun , out runInfo ) ;
3554+ ws = props . GetIntPropValues ( ( int ) FwTextPropType . ktptWs , out ttv ) ;
3555+ if ( IsExternalWritingSystem ( ws , cache ) )
3556+ {
3557+ ws = selTss . get_WritingSystemAt ( 0 ) ;
3558+ ITsPropsBldr propsBldr = props . GetBldr ( ) ;
3559+ propsBldr . SetIntPropValues ( ( int ) FwTextPropType . ktptWs , ttv , ws ) ;
3560+ props = propsBldr . GetTextProps ( ) ;
3561+ }
3562+ stringBldr . Replace ( runInfo . ichMin , runInfo . ichMin , tss . get_RunText ( irun ) , props ) ;
3563+ }
3564+ return stringBldr . GetString ( ) ;
3565+ }
3566+
3567+ private bool IsExternalWritingSystem ( int ws , LcmCache cache )
3568+ {
3569+ foreach ( CoreWritingSystemDefinition writingSystem in cache . LanguageProject . AllWritingSystems )
3570+ {
3571+ if ( writingSystem . Handle == ws )
3572+ return false ;
3573+ }
3574+ return true ;
3575+ }
3576+
35103577 /// ------------------------------------------------------------------------------------
35113578 /// <summary>
35123579 /// Gets the text from clipboard.
0 commit comments