Esempio n. 1
0
        /// <summary>
        /// Paste the content data(Text, Unicode, Xaml and Rtf) to the current text selection
        /// </summary>
        /// <param name="This"></param>
        /// <param name="dataObject">
        /// data object containing data to paste
        /// </param>
        /// <param name="dataObjectToApply">
        /// </param>
        /// <param name="formatToApply">
        /// </param>
        /// <returns>
        /// true if successful, false otherwise
        /// </returns>
        private static bool PasteContentData(TextEditor This, IDataObject dataObject, IDataObject dataObjectToApply, string formatToApply)
        {
            // CF_BITMAP - pasting a single image.
            if (formatToApply == DataFormats.Bitmap && dataObjectToApply is DataObject)
            {
                // We check unmanaged code instead of all clipboard because in paste
                // there is a high level assert for all clipboard in commandmanager.cs
                if (This.AcceptsRichContent && This.Selection is TextSelection)
                {
                    System.Windows.Media.Imaging.BitmapSource bitmapSource = GetPasteData(dataObjectToApply, DataFormats.Bitmap) as System.Windows.Media.Imaging.BitmapSource;

                    if (bitmapSource != null)
                    {
                        // Pack the image into a WPF container
                        MemoryStream packagedImage = WpfPayload.SaveImage(bitmapSource, WpfPayload.ImageBmpContentType);

                        // Place it onto a data object
                        dataObjectToApply = new DataObject();
                        formatToApply     = DataFormats.XamlPackage;
                        dataObjectToApply.SetData(DataFormats.XamlPackage, packagedImage);
                    }
                }
            }

            if (formatToApply == DataFormats.XamlPackage)
            {
                // We check unmanaged code instead of all clipboard because in paste
                // there is a high level assert for all clipboard in commandmanager.cs
                if (This.AcceptsRichContent && This.Selection is TextSelection)
                {
                    object pastedData = GetPasteData(dataObjectToApply, DataFormats.XamlPackage);

                    MemoryStream pastedMemoryStream = pastedData as MemoryStream;
                    if (pastedMemoryStream != null)
                    {
                        object element = WpfPayload.LoadElement(pastedMemoryStream);
                        if ((element is Section || element is Span) && PasteTextElement(This, (TextElement)element))
                        {
                            return(true);
                        }
                        else if (element is FrameworkElement)
                        {
                            ((TextSelection)This.Selection).InsertEmbeddedUIElement((FrameworkElement)element);
                            return(true);
                        }
                    }
                }

                // Fall to Xaml:
                dataObjectToApply = dataObject; // go back to source data object
                if (dataObjectToApply.GetDataPresent(DataFormats.Xaml))
                {
                    formatToApply = DataFormats.Xaml;
                }
                else if (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 pastedData = GetPasteData(dataObjectToApply, DataFormats.Xaml);

                    if (pastedData != null && PasteXaml(This, pastedData.ToString()))
                    {
                        return(true);
                    }
                }

                // Fall to Rtf:
                dataObjectToApply = dataObject; // go back to source data object
                if (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)
            {
                // This demand is present to explicitly disable RTF independant of any
                // asserts in the confines of partial trust
                // We check unmanaged code instead of all clipboard because in paste
                // there is a high level assert for all clipboard in commandmanager.cs
                if (This.AcceptsRichContent)
                {
                    object pastedData = GetPasteData(dataObjectToApply, DataFormats.Rtf);

                    // Convert rtf to xaml text to paste rtf data into the target.
                    if (pastedData != null)
                    {
                        MemoryStream memoryStream = ConvertRtfToXaml(pastedData.ToString());
                        if (memoryStream != null)
                        {
                            TextElement textElement = WpfPayload.LoadElement(memoryStream) as TextElement;
                            if ((textElement is Section || textElement is Span) && PasteTextElement(This, textElement))
                            {
                                return(true);
                            }
                        }
                    }
                }

                // Fall to plain text:
                dataObjectToApply = dataObject; // go back to source data object
                if (dataObjectToApply.GetDataPresent(DataFormats.UnicodeText))
                {
                    formatToApply = DataFormats.UnicodeText;
                }
                else if (dataObjectToApply.GetDataPresent(DataFormats.Text))
                {
                    formatToApply = DataFormats.Text;
                }
            }

            if (formatToApply == DataFormats.UnicodeText)
            {
                object pastedData = GetPasteData(dataObjectToApply, DataFormats.UnicodeText);
                if (pastedData == null)
                {
                    if (dataObjectToApply.GetDataPresent(DataFormats.Text))
                    {
                        formatToApply     = DataFormats.Text; // fall to plain text
                        dataObjectToApply = dataObject;       // go back to source data object
                    }
                }
                else
                {
                    // Dont attempt to recover if pasting Unicode text fails because our only fallback is mbcs text,
                    // which will either evaluate identically (at best) or
                    // produce a string with unexpected text (worse!) from WideCharToMultiByte conversion.
                    return(PastePlainText(This, pastedData.ToString()));
                }
            }

            if (formatToApply == DataFormats.Text)
            {
                object pastedData = GetPasteData(dataObjectToApply, DataFormats.Text);
                if (pastedData != null && PastePlainText(This, pastedData.ToString()))
                {
                    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);
 }