示例#1
0
        /// <summary>
        /// Copy worker.
        /// </summary>
        internal static void Copy(TextEditor This, bool userInitiated)
        {
            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                // Note: _CreateDataObject raises a public event which might throw a recoverable exception.
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, /*isDragDrop:*/ false);

                if (dataObject != null)
                {
                    try
                    {
                        // The copy command was not terminated by application
                        // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException)
                        when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                        {
                            // Clipboard is failed to set the data object.
                            return;
                        }
                }
            }

            // Do not clear springload formatting
        }
        internal static bool _DoPaste(TextEditor This, IDataObject dataObject, bool isDragDrop)
        {
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return(false);
            }
            Invariant.Assert(dataObject != null);
            bool   result        = false;
            string formatToApply = TextEditorCopyPaste.GetPasteApplyFormat(This, dataObject);
            DataObjectPastingEventArgs dataObjectPastingEventArgs;

            try
            {
                dataObjectPastingEventArgs = new DataObjectPastingEventArgs(dataObject, isDragDrop, formatToApply);
            }
            catch (ArgumentException)
            {
                return(result);
            }
            This.UiScope.RaiseEvent(dataObjectPastingEventArgs);
            if (!dataObjectPastingEventArgs.CommandCancelled)
            {
                IDataObject dataObject2 = dataObjectPastingEventArgs.DataObject;
                formatToApply = dataObjectPastingEventArgs.FormatToApply;
                result        = TextEditorCopyPaste.PasteContentData(This, dataObject, dataObject2, formatToApply);
            }
            return(result);
        }
        // Token: 0x06003864 RID: 14436 RVA: 0x000FC5A8 File Offset: 0x000FA7A8
        private static bool PasteXaml(TextEditor This, string pasteXaml)
        {
            bool result;

            if (pasteXaml.Length == 0)
            {
                result = false;
            }
            else
            {
                try
                {
                    bool        useRestrictiveXamlReader = !Clipboard.UseLegacyDangerousClipboardDeserializationMode();
                    object      obj         = XamlReader.Load(new XmlTextReader(new StringReader(pasteXaml)), useRestrictiveXamlReader);
                    TextElement textElement = obj as TextElement;
                    result = (textElement != null && TextEditorCopyPaste.PasteTextElement(This, textElement));
                }
                catch (XamlParseException ex)
                {
                    Invariant.Assert(ex != null);
                    result = false;
                }
            }
            return(result);
        }
示例#4
0
        // Token: 0x0600386A RID: 14442 RVA: 0x000FC8DC File Offset: 0x000FAADC
        internal static void OnDragEnter(object sender, DragEventArgs e)
        {
            e.Handled = true;
            TextEditor textEditor = TextEditor._GetTextEditor(sender);

            if (textEditor == null)
            {
                e.Effects = DragDropEffects.None;
                return;
            }
            if (!textEditor._IsEnabled || textEditor.TextView == null || textEditor.TextView.RenderScope == null)
            {
                e.Effects = DragDropEffects.None;
                return;
            }
            if (e.Data == null)
            {
                e.Effects = DragDropEffects.None;
                return;
            }
            if (TextEditorCopyPaste.GetPasteApplyFormat(textEditor, e.Data) == string.Empty)
            {
                e.Effects = DragDropEffects.None;
                return;
            }
            TextEditorTyping._FlushPendingInputItems(textEditor);
            if (!textEditor.TextView.Validate(e.GetPosition(textEditor.TextView.RenderScope)))
            {
                e.Effects = DragDropEffects.None;
                return;
            }
            textEditor._dragDropProcess.TargetOnDragEnter(e);
        }
        // Token: 0x0600385A RID: 14426 RVA: 0x000FC080 File Offset: 0x000FA280
        private static void OnQueryStatusPaste(object target, CanExecuteRoutedEventArgs args)
        {
            TextEditor textEditor = TextEditor._GetTextEditor(target);

            if (textEditor == null || !textEditor._IsEnabled || textEditor.IsReadOnly)
            {
                return;
            }
            args.Handled = true;
            try
            {
                if (SecurityHelper.CallerHasAllClipboardPermission())
                {
                    string pasteApplyFormat = TextEditorCopyPaste.GetPasteApplyFormat(textEditor, Clipboard.GetDataObject());
                    args.CanExecute = (pasteApplyFormat.Length > 0);
                }
                else
                {
                    args.CanExecute = Clipboard.IsClipboardPopulated();
                }
            }
            catch (ExternalException)
            {
                args.CanExecute = false;
            }
        }
            internal bool SourceOnMouseMove(Point mouseMovePoint)
            {
                // Not allow the initiating DragDrop operation without the unmanaged code permission.
                // We chose to use this over clipboard because this was causing issues in LocalIntranet
                // which has similar restrictions as internet but has clipboard permission
                if (!_dragStarted || !SecurityHelper.CheckUnmanagedCodePermission())
                {
                    return(false); // false means that drag is not involved at all - selection extension should continue
                }

                // Check the mouse drag to start DragDrop operation.
                if (!InitialThresholdCrossed(mouseMovePoint))
                {
                    return(true); // true means that drag is in progress, even though not yet started - so selection should not extend
                }

                ITextSelection selection = _textEditor.Selection;

                // NOTE: This calls OnMouseMove recursively;
                // but because UiScope.IsMouseCaptured is false already,
                // we'll return with no actions
                // This is the first move in drag-drop gesture.
                // Execure the whole drag-drop ssequence: returns after the drop
                _dragStarted = false;

                // Execute OLE drag-drop process (synchronousely)
                // ----------------------------------------------

                // Set the original text range to delete it with DragDropEffects.Move effect.
                _dragSourceTextRange = new TextRange(selection.Start, selection.End);

                // Prepare data object (including side effects from application customization)

                // Note: _CreateDataObject raises a public event which might throw a recoverable exception.
                IDataObject dataObject = TextEditorCopyPaste._CreateDataObject(_textEditor, /*isDragDrop:*/ true);

                if (dataObject != null) // null would mean that application cancelled the command
                {
                    //
                    SourceDoDragDrop(selection, dataObject);

                    // Release mouse capture, because DoDragDrop is taking
                    // a mouse resposibility from now on.
                    // ReleaseMouseCapture shouldn't call before calling DoDragDroop
                    // that cause the generating WM_MOUSELEAVE message by system
                    // (xxxCapture xxxCancelMouseMoverTracking) that appear MouseLeave
                    // event during DragDrop event.
                    _textEditor.UiScope.ReleaseMouseCapture();

                    return(true); // true means that drag is in progress. Selection should not extend.
                }
                else
                {
                    // The DragDrop process has been terminated by application custom code
                    //
                    return(false);
                }
            }
示例#7
0
            // Token: 0x060085BF RID: 34239 RVA: 0x0024AB9C File Offset: 0x00248D9C
            internal void TargetOnDrop(DragEventArgs e)
            {
                if (!this.AllowDragDrop(e))
                {
                    return;
                }
                ITextSelection selection = this._textEditor.Selection;

                Invariant.Assert(selection != null);
                if (e.Data == null || e.AllowedEffects == DragDropEffects.None)
                {
                    e.Effects = DragDropEffects.None;
                    return;
                }
                if ((e.KeyStates & DragDropKeyStates.ControlKey) != DragDropKeyStates.None)
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else if (e.Effects != DragDropEffects.Copy)
                {
                    e.Effects = DragDropEffects.Move;
                }
                if (!this._textEditor.TextView.Validate(e.GetPosition(this._textEditor.TextView.RenderScope)))
                {
                    e.Effects = DragDropEffects.None;
                    return;
                }
                ITextPointer dropPosition = this.GetDropPosition(this._textEditor.TextView.RenderScope, e.GetPosition(this._textEditor.TextView.RenderScope));

                if (dropPosition != null)
                {
                    if (this._dragSourceTextRange != null && this._dragSourceTextRange.Start.TextContainer == selection.Start.TextContainer && !selection.IsEmpty && this.IsSelectionContainsDropPosition(selection, dropPosition))
                    {
                        selection.SetCaretToPosition(dropPosition, LogicalDirection.Backward, false, true);
                        e.Effects = DragDropEffects.None;
                        e.Handled = true;
                    }
                    else
                    {
                        using (selection.DeclareChangeBlock())
                        {
                            if ((e.Effects & DragDropEffects.Move) != DragDropEffects.None && this._dragSourceTextRange != null && this._dragSourceTextRange.Start.TextContainer == selection.Start.TextContainer)
                            {
                                this._dragSourceTextRange.Text = string.Empty;
                            }
                            selection.SetCaretToPosition(dropPosition, LogicalDirection.Backward, false, true);
                            e.Handled = TextEditorCopyPaste._DoPaste(this._textEditor, e.Data, true);
                        }
                    }
                    if (e.Handled)
                    {
                        this.Win32SetForegroundWindow();
                        this._textEditor.UiScope.Focus();
                        return;
                    }
                    e.Effects = DragDropEffects.None;
                }
            }
示例#8
0
        /// <summary>
        /// Cut worker.
        /// </summary>
        internal static void Cut(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                // Fail silently if the app explicitly denies clipboard access.
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                }
                catch (SecurityException)
                {
                    return;
                }
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                // Copy content onto the clipboard

                // Note: _CreateDataObject raises a public event which might throw a recoverable exception.
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, /*isDragDrop:*/ false);

                if (dataObject != null)
                {
                    try
                    {
                        // The copy command was not terminated by application
                        // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException)
                        when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                        {
                            // Clipboard is failed to set the data object.
                            return;
                        }

                    // Delete selected content
                    using (This.Selection.DeclareChangeBlock())
                    {
                        // Forget previously suggested horizontal position
                        TextEditorSelection._ClearSuggestedX(This);
                        This.Selection.Text = String.Empty;

                        // Clear springload formatting
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                }
            }
        }
        // Token: 0x0600385B RID: 14427 RVA: 0x000FC0FC File Offset: 0x000FA2FC
        private static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextEditor textEditor = TextEditor._GetTextEditor(target);

            if (textEditor == null || !textEditor._IsEnabled || textEditor.IsReadOnly)
            {
                return;
            }
            TextEditorCopyPaste.Paste(textEditor);
        }
        private static void OnCopy(object target, ExecutedRoutedEventArgs args)
        {
            TextEditor textEditor = TextEditor._GetTextEditor(target);

            if (textEditor == null || !textEditor._IsEnabled)
            {
                return;
            }
            if (textEditor.UiScope is PasswordBox)
            {
                return;
            }
            TextEditorCopyPaste.Copy(textEditor, args.UserInitiated);
        }
示例#11
0
        /// <summary>
        /// Copy worker.
        /// </summary>
        internal static void Copy(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                // Fail silently if the app explicitly denies clipboard access.
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            else if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                // Fail silently if we don't have clipboard permission.
                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                // Note: _CreateDataObject raises a public event which might throw a recoverable exception.
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, /*isDragDrop:*/ false);

                if (dataObject != null)
                {
                    try
                    {
                        // The copy command was not terminated by application
                        // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException)
                        when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                        {
                            // Clipboard is failed to set the data object.
                            return;
                        }
                }
            }

            // Do not clear springload formatting
        }
        internal static void Cut(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                    goto IL_1E;
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }
IL_1E:
            TextEditorTyping._FlushPendingInputItems(This);
            TextEditorTyping._BreakTypingSequence(This);
            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, false);
                if (dataObject != null)
                {
                    try
                    {
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException obj) when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                    {
                        return;
                    }
                    using (This.Selection.DeclareChangeBlock())
                    {
                        TextEditorSelection._ClearSuggestedX(This);
                        This.Selection.Text = string.Empty;
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                }
            }
        }
        internal static void Paste(TextEditor This)
        {
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }
            if (This.Selection.IsTableCellRange)
            {
                return;
            }
            TextEditorTyping._FlushPendingInputItems(This);
            TextEditorTyping._BreakTypingSequence(This);
            IDataObject dataObject;

            try
            {
                dataObject = Clipboard.GetDataObject();
            }
            catch (ExternalException)
            {
                dataObject = null;
            }
            bool coversEntireContent = This.Selection.CoversEntireContent;

            if (dataObject != null)
            {
                using (This.Selection.DeclareChangeBlock())
                {
                    TextEditorSelection._ClearSuggestedX(This);
                    if (TextEditorCopyPaste._DoPaste(This, dataObject, false))
                    {
                        This.Selection.SetCaretToPosition(This.Selection.End, LogicalDirection.Backward, false, true);
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                }
            }
            if (coversEntireContent)
            {
                This.Selection.ValidateLayout();
            }
        }
        /// <summary>
        /// An event reporting that the drag enter during drag-and-drop operation.
        /// </summary>
        internal static void OnDragEnter(object sender, DragEventArgs e)
        {
            // Consider event handled
            e.Handled = true;

            TextEditor This = TextEditor._GetTextEditor(sender);

            if (This == null)
            {
                e.Effects = DragDropEffects.None;
                return;
            }

            // Ignore the event if the editor has been detached from its scope
            if (!This._IsEnabled || This.TextView == null || This.TextView.RenderScope == null)
            {
                e.Effects = DragDropEffects.None;
                return;
            }

            // If there's no supported data available, don't allow the drag-and-drop.
            if (e.Data == null)
            {
                e.Effects = DragDropEffects.None;
                return;
            }

            // Ignore the event if there isn't the dropable(pasteable) data format
            if (TextEditorCopyPaste.GetPasteApplyFormat(This, e.Data) == string.Empty)
            {
                e.Effects = DragDropEffects.None;
                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            if (!This.TextView.Validate(e.GetPosition(This.TextView.RenderScope)))
            {
                e.Effects = DragDropEffects.None;
                return;
            }

            This._dragDropProcess.TargetOnDragEnter(e);
        }
        internal static void Copy(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                    goto IL_1B;
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }
IL_1B:
            TextEditorTyping._FlushPendingInputItems(This);
            TextEditorTyping._BreakTypingSequence(This);
            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, false);
                if (dataObject != null)
                {
                    try
                    {
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException obj) when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                    {
                    }
                }
            }
        }
示例#16
0
            internal bool SourceOnMouseMove(Point mouseMovePoint)
            {
                if (!this._dragStarted || !SecurityHelper.CheckUnmanagedCodePermission())
                {
                    return(false);
                }
                if (!this.InitialThresholdCrossed(mouseMovePoint))
                {
                    return(true);
                }
                ITextSelection selection = this._textEditor.Selection;

                this._dragStarted         = false;
                this._dragSourceTextRange = new TextRange(selection.Start, selection.End);
                IDataObject dataObject = TextEditorCopyPaste._CreateDataObject(this._textEditor, true);

                if (dataObject != null)
                {
                    this.SourceDoDragDrop(selection, dataObject);
                    this._textEditor.UiScope.ReleaseMouseCapture();
                    return(true);
                }
                return(false);
            }
 private static bool PasteContentData(TextEditor This, IDataObject dataObject, IDataObject dataObjectToApply, string formatToApply)
 {
     if (formatToApply == DataFormats.Bitmap && dataObjectToApply is DataObject && This.AcceptsRichContent && This.Selection is TextSelection && SecurityHelper.CheckUnmanagedCodePermission())
     {
         BitmapSource bitmapSource = TextEditorCopyPaste.GetPasteData(dataObjectToApply, DataFormats.Bitmap) as BitmapSource;
         if (bitmapSource != null)
         {
             MemoryStream data = WpfPayload.SaveImage(bitmapSource, "image/bmp");
             dataObjectToApply = new DataObject();
             formatToApply     = DataFormats.XamlPackage;
             dataObjectToApply.SetData(DataFormats.XamlPackage, data);
         }
     }
     if (formatToApply == DataFormats.XamlPackage)
     {
         if (This.AcceptsRichContent && This.Selection is TextSelection && SecurityHelper.CheckUnmanagedCodePermission())
         {
             object       pasteData    = TextEditorCopyPaste.GetPasteData(dataObjectToApply, DataFormats.XamlPackage);
             MemoryStream memoryStream = pasteData as MemoryStream;
             if (memoryStream != null)
             {
                 object obj = WpfPayload.LoadElement(memoryStream);
                 if ((obj is Section || obj is Span) && TextEditorCopyPaste.PasteTextElement(This, (TextElement)obj))
                 {
                     return(true);
                 }
                 if (obj is FrameworkElement)
                 {
                     ((TextSelection)This.Selection).InsertEmbeddedUIElement((FrameworkElement)obj);
                     return(true);
                 }
             }
         }
         dataObjectToApply = dataObject;
         if (dataObjectToApply.GetDataPresent(DataFormats.Xaml))
         {
             formatToApply = DataFormats.Xaml;
         }
         else if (SecurityHelper.CheckUnmanagedCodePermission() && dataObjectToApply.GetDataPresent(DataFormats.Rtf))
         {
             formatToApply = DataFormats.Rtf;
         }
         else if (dataObjectToApply.GetDataPresent(DataFormats.UnicodeText))
         {
             formatToApply = DataFormats.UnicodeText;
         }
         else if (dataObjectToApply.GetDataPresent(DataFormats.Text))
         {
             formatToApply = DataFormats.Text;
         }
     }
     if (formatToApply == DataFormats.Xaml)
     {
         if (This.AcceptsRichContent && This.Selection is TextSelection)
         {
             object pasteData2 = TextEditorCopyPaste.GetPasteData(dataObjectToApply, DataFormats.Xaml);
             if (pasteData2 != null && TextEditorCopyPaste.PasteXaml(This, pasteData2.ToString()))
             {
                 return(true);
             }
         }
         dataObjectToApply = dataObject;
         if (SecurityHelper.CheckUnmanagedCodePermission() && dataObjectToApply.GetDataPresent(DataFormats.Rtf))
         {
             formatToApply = DataFormats.Rtf;
         }
         else if (dataObjectToApply.GetDataPresent(DataFormats.UnicodeText))
         {
             formatToApply = DataFormats.UnicodeText;
         }
         else if (dataObjectToApply.GetDataPresent(DataFormats.Text))
         {
             formatToApply = DataFormats.Text;
         }
     }
     if (formatToApply == DataFormats.Rtf)
     {
         if (This.AcceptsRichContent && SecurityHelper.CheckUnmanagedCodePermission())
         {
             object pasteData3 = TextEditorCopyPaste.GetPasteData(dataObjectToApply, DataFormats.Rtf);
             if (pasteData3 != null)
             {
                 MemoryStream memoryStream2 = TextEditorCopyPaste.ConvertRtfToXaml(pasteData3.ToString());
                 if (memoryStream2 != null)
                 {
                     TextElement textElement = WpfPayload.LoadElement(memoryStream2) as TextElement;
                     if ((textElement is Section || textElement is Span) && TextEditorCopyPaste.PasteTextElement(This, textElement))
                     {
                         return(true);
                     }
                 }
             }
         }
         dataObjectToApply = dataObject;
         if (dataObjectToApply.GetDataPresent(DataFormats.UnicodeText))
         {
             formatToApply = DataFormats.UnicodeText;
         }
         else if (dataObjectToApply.GetDataPresent(DataFormats.Text))
         {
             formatToApply = DataFormats.Text;
         }
     }
     if (formatToApply == DataFormats.UnicodeText)
     {
         object pasteData4 = TextEditorCopyPaste.GetPasteData(dataObjectToApply, DataFormats.UnicodeText);
         if (pasteData4 != null)
         {
             return(TextEditorCopyPaste.PastePlainText(This, pasteData4.ToString()));
         }
         if (dataObjectToApply.GetDataPresent(DataFormats.Text))
         {
             formatToApply     = DataFormats.Text;
             dataObjectToApply = dataObject;
         }
     }
     if (formatToApply == DataFormats.Text)
     {
         object pasteData5 = TextEditorCopyPaste.GetPasteData(dataObjectToApply, DataFormats.Text);
         if (pasteData5 != null && TextEditorCopyPaste.PastePlainText(This, pasteData5.ToString()))
         {
             return(true);
         }
     }
     return(false);
 }
            /// <summary>
            /// Called from an event reporting that the drop happened.
            /// </summary>
            internal void TargetOnDrop(DragEventArgs e)
            {
                //

                if (!AllowDragDrop(e))
                {
                    return;
                }

                ITextSelection selection = _textEditor.Selection;

                Invariant.Assert(selection != null);

                if (e.Data == null || e.AllowedEffects == DragDropEffects.None)
                {
                    e.Effects = DragDropEffects.None;
                    return;
                }

                if ((int)(e.KeyStates & DragDropKeyStates.ControlKey) != 0)
                {
                    e.Effects = DragDropEffects.Copy;
                }
                else if (e.Effects != DragDropEffects.Copy)
                {
                    e.Effects = DragDropEffects.Move;
                }

                // Force a layout update on the content so the GetTextPositionFromPoint
                // call following can succeed.
                if (!_textEditor.TextView.Validate(e.GetPosition(_textEditor.TextView.RenderScope)))
                {
                    e.Effects = DragDropEffects.None;
                    return;
                }

                // Get the text position from the text target point.
                ITextPointer dropPosition = GetDropPosition(_textEditor.TextView.RenderScope as Visual, e.GetPosition(_textEditor.TextView.RenderScope));

                if (dropPosition != null)
                {
                    if (_dragSourceTextRange != null && _dragSourceTextRange.Start.TextContainer == selection.Start.TextContainer &&
                        !selection.IsEmpty && IsSelectionContainsDropPosition(selection, dropPosition))
                    {
                        // When we drop inside of selected area, we
                        // should not select dropped content,
                        // otherwise it looks for end user as if
                        // nothing happened.

                        // Set caret to this position.
                        selection.SetCaretToPosition(dropPosition, LogicalDirection.Backward, /*allowStopAtLineEnd:*/ false, /*allowStopNearSpace:*/ true);

                        // Indicate the resulting effect of an action
                        // Note that dropResult may stay equal to DragDropResult.Drop
                        e.Effects = DragDropEffects.None;

                        // Mark the event as handled
                        e.Handled = true;
                    }
                    else
                    {
                        using (selection.DeclareChangeBlock())
                        {
                            // For MaxLength filter work correctly in case
                            // when we dragdrop within the same TextContainer,
                            // we need to delete dragged content first -
                            // before dropping when filtering will occur.
                            // Note, that this will duplicate operation on
                            // source side, but it will be void deletion action
                            if ((e.Effects & DragDropEffects.Move) != 0 && //
                                _dragSourceTextRange != null && _dragSourceTextRange.Start.TextContainer == selection.Start.TextContainer)
                            {
                                _dragSourceTextRange.Text = String.Empty;
                            }

                            // When we drop outside of selection,
                            // we should ignore current selection and
                            // move ip into dropping point.
                            selection.SetCaretToPosition(dropPosition, LogicalDirection.Backward, /*allowStopAtLineEnd:*/ false, /*allowStopNearSpace:*/ true);

                            // _DoPaste raises a public event -- could raise recoverable exception.
                            e.Handled = TextEditorCopyPaste._DoPaste(_textEditor, e.Data, /*isDragDrop:*/ true);
                            //
                        }
                    }

                    if (e.Handled)
                    {
                        // Set the drop target as the foreground window.
                        Win32SetForegroundWindow();

                        // Set the focus into the drop target.
                        _textEditor.UiScope.Focus();
                    }
                    else
                    {
                        // When a target did not handle a drop event, we must
                        // prevent from deleting a content on source end -
                        // otherwise we'll have data loss
                        e.Effects = DragDropEffects.None;
                    }
                }
            }
        internal static DataObject _CreateDataObject(TextEditor This, bool isDragDrop)
        {
            new UIPermission(UIPermissionClipboard.AllClipboard).Assert();
            DataObject dataObject;

            try
            {
                dataObject = new DataObject();
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            string text = This.Selection.Text;

            if (text != string.Empty)
            {
                if (TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Text))
                {
                    TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.Text, text);
                }
                if (TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.UnicodeText))
                {
                    TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.UnicodeText, text);
                }
            }
            if (This.AcceptsRichContent)
            {
                if (SecurityHelper.CheckUnmanagedCodePermission())
                {
                    Stream stream = null;
                    string text2  = WpfPayload.SaveRange(This.Selection, ref stream, false);
                    if (text2.Length > 0)
                    {
                        if (stream != null && TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.XamlPackage))
                        {
                            dataObject.SetData(DataFormats.XamlPackage, stream);
                        }
                        if (TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Rtf))
                        {
                            string text3 = TextEditorCopyPaste.ConvertXamlToRtf(text2, stream);
                            if (text3 != string.Empty)
                            {
                                dataObject.SetData(DataFormats.Rtf, text3, true);
                            }
                        }
                    }
                    Image image = This.Selection.GetUIElementSelected() as Image;
                    if (image != null && image.Source is BitmapSource)
                    {
                        dataObject.SetImage((BitmapSource)image.Source);
                    }
                }
                StringWriter  stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter xmlWriter    = new XmlTextWriter(stringWriter);
                TextRangeSerialization.WriteXaml(xmlWriter, This.Selection, false, null);
                string text4 = stringWriter.ToString();
                if (text4.Length > 0 && TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Xaml))
                {
                    TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.Xaml, text4);
                    PermissionSet permissionSet = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin();
                    string        content       = permissionSet.ToString();
                    TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.ApplicationTrust, content);
                }
            }
            DataObjectCopyingEventArgs dataObjectCopyingEventArgs = new DataObjectCopyingEventArgs(dataObject, isDragDrop);

            This.UiScope.RaiseEvent(dataObjectCopyingEventArgs);
            if (dataObjectCopyingEventArgs.CommandCancelled)
            {
                dataObject = null;
            }
            return(dataObject);
        }
示例#20
0
        /// <summary>
        /// Paste worker.
        /// </summary>
        internal static void Paste(TextEditor This)
        {
            if (This.Selection.IsTableCellRange)
            {
                //  We do not support clipboard for table selection so far
                // Word behavior: When source range is text segment then this segment is pasted
                // into each table cell overriding its current content.
                // If source range is table range then it is repeated on target -
                // cell by cell in circular manner.
                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            // Get DataObject from the Clipboard
            IDataObject dataObject;

            try
            {
                dataObject = Clipboard.GetDataObject();
            }
            catch (ExternalException)
            {
                // Clipboard is failed to get the data object.
                // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                dataObject = null;
                //  must re-throw ???
            }

            bool forceLayoutUpdate = This.Selection.CoversEntireContent;

            if (dataObject != null)
            {
                using (This.Selection.DeclareChangeBlock())
                {
                    // Forget previously suggested horizontal position
                    TextEditorSelection._ClearSuggestedX(This);

                    // _DoPaste raises a public event -- could raise recoverable exception.
                    if (TextEditorCopyPaste._DoPaste(This, dataObject, /*isDragDrop:*/ false))
                    {
                        // Collapse selection to the end
                        // Use backward direction to stay oriented towards pasted content
                        This.Selection.SetCaretToPosition(This.Selection.End, LogicalDirection.Backward, /*allowStopAtLineEnd:*/ false, /*allowStopNearSpace:*/ true);

                        // Clear springload formatting
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                } // PUBLIC EVENT RAISED HERE AS CHANGEBLOCK CLOSES!
            }

            // If we replaced the entire document content, background layout will
            // kick in.  Force it to complete now.
            if (forceLayoutUpdate)
            {
                This.Selection.ValidateLayout();
            }
        }
示例#21
0
        internal static void Paste(TextEditor This)
        {
            // Don't try anything if the caller doesn't have the rights to read from the clipboard...
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }

            if (This.Selection.IsTableCellRange)
            {
                //



                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            // Get DataObject from the Clipboard
            IDataObject dataObject;

            try
            {
                dataObject = Clipboard.GetDataObject();
            }
            catch (ExternalException)
            {
                // Clipboard is failed to get the data object.
                // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                dataObject = null;
                //
            }

            bool forceLayoutUpdate = This.Selection.CoversEntireContent;

            if (dataObject != null)
            {
                using (This.Selection.DeclareChangeBlock())
                {
                    // Forget previously suggested horizontal position
                    TextEditorSelection._ClearSuggestedX(This);

                    // _DoPaste raises a public event -- could raise recoverable exception.
                    if (TextEditorCopyPaste._DoPaste(This, dataObject, /*isDragDrop:*/ false))
                    {
                        // Collapse selection to the end
                        // Use backward direction to stay oriented towards pasted content
                        This.Selection.SetCaretToPosition(This.Selection.End, LogicalDirection.Backward, /*allowStopAtLineEnd:*/ false, /*allowStopNearSpace:*/ true);

                        // Clear springload formatting
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                } // PUBLIC EVENT RAISED HERE AS CHANGEBLOCK CLOSES!
            }

            // If we replaced the entire document content, background layout will
            // kick in.  Force it to complete now.
            if (forceLayoutUpdate)
            {
                This.Selection.ValidateLayout();
            }
        }