Exemplo n.º 1
0
        private void Copy()
        {
            DataObject dataObject = new DataObject();

            dataObject.SetData(this.providersListBox.SelectedItem);

            DataObjectSettingDataEventArgs settingDataEventArgs = new DataObjectSettingDataEventArgs(dataObject, this.providersListBox.SelectedItem.GetType().FullName);

            this.RaiseEvent(settingDataEventArgs);

            if (settingDataEventArgs.CommandCancelled)
            {
                return;
            }

            DataObjectCopyingEventArgs copyingEventArgs = new DataObjectCopyingEventArgs(dataObject, false);

            this.RaiseEvent(copyingEventArgs);

            if (copyingEventArgs.CommandCancelled)
            {
                return;
            }

            Clipboard.SetDataObject(dataObject, true);
        }
Exemplo n.º 2
0
        static bool CopySelectedText(TextArea textArea)
        {
            var data             = textArea.Selection.CreateDataObject(textArea);
            var copyingEventArgs = new DataObjectCopyingEventArgs(data, false);

            textArea.RaiseEvent(copyingEventArgs);
            if (copyingEventArgs.CommandCancelled)
            {
                return(false);
            }

            try {
                Clipboard.SetDataObject(data, true);
            } catch (ExternalException) {
                // Apparently this exception sometimes happens randomly.
                // The MS controls just ignore it, so we'll do the same.
                return(false);
            }

            string text = textArea.Selection.GetText();

            text = TextUtilities.NormalizeNewLines(text, Environment.NewLine);
            textArea.OnTextCopied(new TextEventArgs(text));
            return(true);
        }
Exemplo n.º 3
0
 private void CopyingHandler(object Sender, DataObjectCopyingEventArgs Args)
 {
     if (Args.IsDragDrop)
     {
         Args.CancelCommand();
     }
 }
Exemplo n.º 4
0
        protected void OnCopy(object o, DataObjectCopyingEventArgs e)
        {
            string clipboard = "";

            for (TextPointer p = Selection.Start, next = null;
                 p != null && p.CompareTo(Selection.End) < 0;
                 p = next)
            {
                next = p.GetNextInsertionPosition(LogicalDirection.Forward);
                if (next == null)
                {
                    break;
                }

                //var word = new TextRange(p, next);
                //Console.WriteLine($"Word '{word.Text}' Inline {word.Start.Parent.GetType()}");
                //Console.WriteLine($" ... p {p.Parent.GetType()}");

                var t = new TextRange(p, next);
                clipboard += t.Start.Parent is EmojiInline ? (t.Start.Parent as EmojiInline).Text
                                                           : t.Text;
            }

            Clipboard.SetText(clipboard);
            e.Handled = true;
            e.CancelCommand();
        }
Exemplo n.º 5
0
 private static void NoDragCopy(object sender, DataObjectCopyingEventArgs e)
 {
     if (e.IsDragDrop)
     {
         e.CancelCommand();
     }
 }
Exemplo n.º 6
0
        private void OnTextCopy(object sender, DataObjectCopyingEventArgs e)
        {
            e.CancelCommand();
            if (FlowDocumentPageViewer.Selection != null)
            {
                using (var ms = new MemoryStream())
                {
                    TextPointer potStart = FlowDocumentPageViewer.Selection.Start;
                    TextPointer potEnd   = FlowDocumentPageViewer.Selection.End;
                    TextRange   range    = new TextRange(potStart, potEnd);
                    range.Save(ms, DataFormats.Xaml, true);

                    ms.Position = 0;
                    var sr         = new StreamReader(ms);
                    var xamlString = sr.ReadToEnd();

                    var dto = new DataObject();
                    dto.SetText(ConvertXamlToRtf(xamlString), TextDataFormat.Rtf);
                    string unformattedText = range.Text.Replace("\n", Environment.NewLine);
                    dto.SetText(unformattedText, TextDataFormat.UnicodeText);

                    Clipboard.Clear();
                    Clipboard.SetDataObject(dto);
                }
            }
        }
Exemplo n.º 7
0
        private void OnExecutedCopy(object sender, ExecutedRoutedEventArgs e)
        {
            string format = DataFormats.UnicodeText;

            //string text = string.Join (Environment.NewLine, (from searchItem in this.SelectedItems.Cast<EditorSearchItem> () select searchItem.ToString ()).ToArray ()); ** Uncomment when we have C# 3.0 support **
            string     text       = string.Join(Environment.NewLine, Enumerable.ToArray(Enumerable.Select <EditorSearchItem, string> (Enumerable.Cast <EditorSearchItem> (this.SelectedItems), delegate(EditorSearchItem searchItem) { return(searchItem.ToString()); })));
            DataObject dataObject = new DataObject();

            dataObject.SetData(format, text);

            DataObjectSettingDataEventArgs settingDataEventArgs = new DataObjectSettingDataEventArgs(dataObject, format);

            this.RaiseEvent(settingDataEventArgs);

            if (settingDataEventArgs.CommandCancelled)
            {
                return;
            }

            DataObjectCopyingEventArgs copyingEventArgs = new DataObjectCopyingEventArgs(dataObject, false);

            this.RaiseEvent(copyingEventArgs);

            if (copyingEventArgs.CommandCancelled)
            {
                return;
            }

            Clipboard.SetDataObject(dataObject, true);
        }
 private void OnTextBoxCancelDrag(object sender, DataObjectCopyingEventArgs e)
 {
     if (e.IsDragDrop)
     {
         e.CancelCommand();
     }
 }
Exemplo n.º 9
0
        public static void GenerateClipBoardData(DataObjectCopyingEventArgs e, TextSelection selection)
        {
            string str;

            using (MemoryStream stream = new MemoryStream())
            {
                TextRange range = new TextRange(selection.Start, selection.End);
                range.ClearAllProperties();
                range.Save(stream, DataFormats.Xaml, true);
                stream.Flush();
                stream.Position = 0L;
                using (StreamReader reader = new StreamReader(stream))
                {
                    str = reader.ReadToEnd();
                }
            }
            if (!string.IsNullOrEmpty(str))
            {
                string str2 = ReplaceControls.ReplaceGUIWithClipboardControl(str, selection.Start, selection.End);
                if (!string.IsNullOrEmpty(str2) && (str2 != str))
                {
                    e.DataObject.SetData(BamaDataFormat, str2);
                }
            }
        }
Exemplo n.º 10
0
        protected void OnCopy(object o, DataObjectCopyingEventArgs e)
        {
            string clipboard = "";

            for (TextPointer p = Selection.Start, next = null;
                 p != null && p.CompareTo(Selection.End) < 0;
                 p = next)
            {
                next = p.GetNextInsertionPosition(LogicalDirection.Forward);
                if (next == null)
                {
                    break;
                }

                var emoji = (next.Parent as Run)?.PreviousInline as EmojiInline;
                if (emoji == null && next.Parent != p.Parent)
                {
                    emoji = (p.Parent as Run)?.NextInline as EmojiInline;
                }
                if (emoji != null && (p.Parent as Run).PreviousInline != emoji)
                {
                    clipboard += emoji?.Text;
                }
                else
                {
                    clipboard += new TextRange(p, next).Text;
                }
            }

            Clipboard.SetText(clipboard);
            e.Handled = true;
            e.CancelCommand();
        }
Exemplo n.º 11
0
 /// <summary>
 /// Delegate called to disable the capability of dragging selected text of the text box.
 /// </summary>
 /// <param name="pSender">The modified text box.</param>
 /// <param name="pEventArgs">The event arguments.</param>
 private void DisableDragCopy(object pSender, DataObjectCopyingEventArgs pEventArgs)
 {
     if (pEventArgs.IsDragDrop)
     {
         pEventArgs.CancelCommand();
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Handles Copy event, because text contains images and we need to substitute that
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnCopy(object sender, DataObjectCopyingEventArgs e)
        {
            if (!_emojisExist)
            {
                return;
            }

            string copied = e.DataObject.GetData(DataFormats.UnicodeText) as string;

            // Full message
            if (copied.Length == _trueLength)
            {
                Clipboard.SetText(_msg, TextDataFormat.UnicodeText);
            }
            else
            {
                int start  = GetSelectionStart();
                int length = GetSelectionLength();

                string toCopy = _msg.Substring(start, length);

                Clipboard.SetText(toCopy, TextDataFormat.UnicodeText);
            }

            e.Handled = true;
            e.CancelCommand();
        }
Exemplo n.º 13
0
 private void CopyCommand(object sender, DataObjectCopyingEventArgs args)
 {
     if (!string.IsNullOrEmpty(Selection.Text))
     {
         args.DataObject.SetData(typeof(string), Selection.Text);
     }
     args.Handled = true;
 }
Exemplo n.º 14
0
        void OnCopy(object sender, DataObjectCopyingEventArgs e)
        {
            var offset = ConvertGlobalOffsetToLocalOffset(SelectionStart, out int index);

            if (ItemsSource.Count == 0 ||
                _infos[index]._readOnlyLength > -1 &&
                (!_infos[index]._canBeSpecified ||
                 offset < _infos[index]._readOnlyLength))
            {
                e.CancelCommand();
            }
        }
Exemplo n.º 15
0
        private static bool CopyWholeLine(TextArea textArea, DocumentLine line)
        {
            ISegment wholeLine = new SimpleSegment(line.Offset, line.TotalLength);
            string   text      = textArea.Document.GetText(wholeLine);

            // Ensure we use the appropriate newline sequence for the OS
            text = TextUtilities.NormalizeNewLines(text, Environment.NewLine);
            DataObject data = new DataObject();

            if (ConfirmDataFormat(textArea, data, DataFormats.UnicodeText))
            {
                data.SetText(text);
            }

            // Also copy text in HTML format to clipboard - good for pasting text into Word
            // or to the SharpDevelop forums.
            if (ConfirmDataFormat(textArea, data, DataFormats.Html))
            {
                IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter;
                HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, wholeLine, new HtmlOptions(textArea.Options)));
            }

            if (ConfirmDataFormat(textArea, data, LineSelectedType))
            {
                MemoryStream lineSelected = new MemoryStream(1);
                lineSelected.WriteByte(1);
                data.SetData(LineSelectedType, lineSelected, false);
            }

            var copyingEventArgs = new DataObjectCopyingEventArgs(data, false);

            textArea.RaiseEvent(copyingEventArgs);
            if (copyingEventArgs.CommandCancelled)
            {
                return(false);
            }

            try
            {
                Clipboard.SetDataObject(data, true);
            }
            catch (ExternalException)
            {
                // Apparently this exception sometimes happens randomly.
                // The MS controls just ignore it, so we'll do the same.
                return(false);
            }
            textArea.OnTextCopied(new TextEventArgs(text));
            return(true);
        }
Exemplo n.º 16
0
        private void NoDragCopy(object sender, DataObjectCopyingEventArgs e)
        {
            if (e.IsDragDrop)
            {
                dragStart = Selection.Start;
                dragEnd   = Selection.End;

                e.CancelCommand();

                Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.Normal, new EventHandler(DoDragDrop),
                    this, new EventArgs());
            }
        }
        private void CopyingHandler(object sender, DataObjectCopyingEventArgs e)
        {
            // Das DataObject kann hier nicht neu zugewiesen werden, da es keine SET Methode gibt
            // aber die Daten des vorhandenen DataObjects können abgeändert werden
            string   str = (string)e.DataObject.GetData(DataFormats.UnicodeText);
            copyMode cm  = GetCurrCopyMode();

            if (cm == copyMode.ascii7Bit)
            {
                e.DataObject.SetData(DataFormats.Text, Get7bitASCIIbyUnicodeCharacterSetRange(str), true);
                e.DataObject.SetData(DataFormats.UnicodeText, Get7bitASCIIbyUnicodeCharacterSetRange(str), true);
            }
            else if (cm == copyMode.unicodeCharacterSetRange)
            {
                e.DataObject.SetData(DataFormats.UnicodeText, str, true);
            }
            e.DataObject.SetData("PETSCII", GetPETSCIIByteArrayByUnicodeString(str, false, true, false));
        }
Exemplo n.º 18
0
 private void OnCopy(object sender, DataObjectCopyingEventArgs e)
 {
     if (e.DataObject != null)
     {
         UpdateDocument();
         var range = new TextRange(Selection.Start, Selection.End);
         using (var ms = new MemoryStream())
         {
             range.Save(ms, DataFormats.Xaml, true);
             ms.Position = 0;
             using (var reader = new StreamReader(ms, Encoding.UTF8))
             {
                 var xaml = reader.ReadToEnd();
                 e.DataObject.SetData(DataFormats.Xaml, xaml);
             }
         }
         e.Handled = true;
     }
 }
Exemplo n.º 19
0
        private void OnCopy(object o, DataObjectCopyingEventArgs e)
        {
            var clipboard = "";

            for (TextPointer p = Selection.Start, next; p != null && p.CompareTo(Selection.End) < 0; p = next)
            {
                next = p.GetNextInsertionPosition(LogicalDirection.Forward);
                if (next == null)
                {
                    break;
                }

                var textRange = new TextRange(p, next);
                clipboard += textRange.Start.Parent is EmojiSpan span ? span.Text : textRange.Text;
            }

            ClipboardHelper.SetText(clipboard);
            e.Handled = true;
            e.CancelCommand();
        }
Exemplo n.º 20
0
        /// <summary>
        /// Creates DataObject for Copy and Drag operations
        /// </summary>
        internal static DataObject _CreateDataObject(TextEditor This, bool isDragDrop)
        {
            DataObject dataObject;

            // Create the data object for drag and drop.
            //  We could provide more extensibility here -
            // by allowing application to create its own DataObject.
            // Without it our extensibility looks inconsistent:
            // the interface IDataObject suggests that you can
            // create your own implementation of it, but you
            // really cannot, because there is no way of
            // using it in our TextEditor.Copy/Drag.
            dataObject = new DataObject();

            // Get plain text and copy it into the data object.
            string textString = This.Selection.Text;

            if (textString != String.Empty)
            {
                // Copy plain text into data object.
                // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Text))
                {
                    dataObject.SetData(DataFormats.Text, textString, false);
                }

                // Copy unicode text into data object.
                // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.UnicodeText))
                {
                    dataObject.SetData(DataFormats.UnicodeText, textString, false);
                }
            }

            // Get the rtf and xaml text and then copy it into the data object after confirm data format.
            // We do this only if our content is rich
            if (This.AcceptsRichContent)
            {
                Stream wpfContainerMemory = null;
                // null wpfContainerMemory on entry means that container is optional
                // and will be not created when there is no images in the range.

                // Create in-memory wpf package, and serialize the content of selection into it
                string xamlTextWithImages = WpfPayload.SaveRange(This.Selection, ref wpfContainerMemory, /*useFlowDocumentAsRoot:*/ false);

                if (xamlTextWithImages.Length > 0)
                {
                    // ConfirmDataFormatSetting raises a public event - could throw recoverable exception.
                    if (wpfContainerMemory != null && ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.XamlPackage))
                    {
                        dataObject.SetData(DataFormats.XamlPackage, wpfContainerMemory);
                    }

                    // ConfirmDataFormatSetting raises a public event - could throw recoverable exception.
                    if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Rtf))
                    {
                        // Convert xaml to rtf text to set rtf data into data object.
                        string rtfText = ConvertXamlToRtf(xamlTextWithImages, wpfContainerMemory);

                        if (rtfText != String.Empty)
                        {
                            dataObject.SetData(DataFormats.Rtf, rtfText, true);
                        }
                    }

                    // Add a CF_BITMAP if we have only one image selected.
                    Image image = This.Selection.GetUIElementSelected() as Image;
                    if (image != null && image.Source is System.Windows.Media.Imaging.BitmapSource)
                    {
                        dataObject.SetImage((System.Windows.Media.Imaging.BitmapSource)image.Source);
                    }
                }

                // Xaml format is availabe both in Full Trust and in Partial Trust
                // Need to re-serialize xaml to avoid image references within a container:
                StringWriter  stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter xmlWriter    = new XmlTextWriter(stringWriter);
                TextRangeSerialization.WriteXaml(xmlWriter, This.Selection, /*useFlowDocumentAsRoot:*/ false, /*wpfPayload:*/ null);
                string xamlText = stringWriter.ToString();
                //  Use WpfPayload.SaveRangeAsXaml method to produce correct image.Source properties.

                if (xamlText.Length > 0)
                {
                    // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                    if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Xaml))
                    {
                        // Place Xaml data onto the dataobject using safe setter
                        dataObject.SetData(DataFormats.Xaml, xamlText, false);
                    }
                }
            }

            // Notify application about our data object preparation completion
            DataObjectCopyingEventArgs dataObjectCopyingEventArgs = new DataObjectCopyingEventArgs(dataObject, /*isDragDrop:*/ isDragDrop);

            This.UiScope.RaiseEvent(dataObjectCopyingEventArgs);
            if (dataObjectCopyingEventArgs.CommandCancelled)
            {
                dataObject = null;
            }

            return(dataObject);
        }
Exemplo n.º 21
0
 private void ConsoleTextBox_OnCopying(object sender, DataObjectCopyingEventArgs e)
 {
     e.CancelCommand();
 }
Exemplo n.º 22
0
 private void onCopyCut(object sender, DataObjectCopyingEventArgs e)
 {
 }
Exemplo n.º 23
0
        internal static DataObject _CreateDataObject(TextEditor This, bool isDragDrop)
        {
            DataObject dataObject;

            // Create the data object for drag and drop.
            //



            (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert();//BlessedAssert
            try
            {
                dataObject = new DataObject();
            }
            finally
            {
                UIPermission.RevertAssert();
            }

            // Get plain text and copy it into the data object.
            string textString = This.Selection.Text;

            if (textString != String.Empty)
            {
                // Copy plain text into data object.
                // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Text))
                {
                    CriticalSetDataWrapper(dataObject, DataFormats.Text, textString);
                }

                // Copy unicode text into data object.
                // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.UnicodeText))
                {
                    CriticalSetDataWrapper(dataObject, DataFormats.UnicodeText, textString);
                }
            }

            // Get the rtf and xaml text and then copy it into the data object after confirm data format.
            // We do this only if our content is rich
            if (This.AcceptsRichContent)
            {
                // This ensures that in the confines of partial trust RTF is not enabled.
                // We use unmanaged code permission over clipboard permission since
                // the latter is available in intranet zone and this is something that will
                // fail in intranet too.
                if (SecurityHelper.CheckUnmanagedCodePermission())
                {
                    // In FullTrust we allow all rich formats on the clipboard

                    Stream wpfContainerMemory = null;
                    // null wpfContainerMemory on entry means that container is optional
                    // and will be not created when there is no images in the range.

                    // Create in-memory wpf package, and serialize the content of selection into it
                    string xamlTextWithImages = WpfPayload.SaveRange(This.Selection, ref wpfContainerMemory, /*useFlowDocumentAsRoot:*/ false);

                    if (xamlTextWithImages.Length > 0)
                    {
                        // ConfirmDataFormatSetting raises a public event - could throw recoverable exception.
                        if (wpfContainerMemory != null && ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.XamlPackage))
                        {
                            dataObject.SetData(DataFormats.XamlPackage, wpfContainerMemory);
                        }

                        // ConfirmDataFormatSetting raises a public event - could throw recoverable exception.
                        if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Rtf))
                        {
                            // Convert xaml to rtf text to set rtf data into data object.
                            string rtfText = ConvertXamlToRtf(xamlTextWithImages, wpfContainerMemory);

                            if (rtfText != String.Empty)
                            {
                                dataObject.SetData(DataFormats.Rtf, rtfText, true);
                            }
                        }
                    }

                    // Add a CF_BITMAP if we have only one image selected.
                    Image image = This.Selection.GetUIElementSelected() as Image;
                    if (image != null && image.Source is System.Windows.Media.Imaging.BitmapSource)
                    {
                        dataObject.SetImage((System.Windows.Media.Imaging.BitmapSource)image.Source);
                    }
                }

                // Xaml format is availabe both in Full Trust and in Partial Trust
                // Need to re-serialize xaml to avoid image references within a container:
                StringWriter  stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter xmlWriter    = new XmlTextWriter(stringWriter);
                TextRangeSerialization.WriteXaml(xmlWriter, This.Selection, /*useFlowDocumentAsRoot:*/ false, /*wpfPayload:*/ null);
                string xamlText = stringWriter.ToString();
                //

                if (xamlText.Length > 0)
                {
                    // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                    if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Xaml))
                    {
                        // Place Xaml data onto the dataobject using safe setter
                        CriticalSetDataWrapper(dataObject, DataFormats.Xaml, xamlText);

                        // The dataobject itself must hold an information about permission set
                        // of the source appdomain. Set it there:

                        // Package permission set for the current appdomain
                        PermissionSet psCurrentAppDomain            = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin();
                        string        permissionSetCurrentAppDomain = psCurrentAppDomain.ToString();
                        CriticalSetDataWrapper(dataObject, DataFormats.ApplicationTrust, permissionSetCurrentAppDomain);
                    }
                }
            }

            // Notify application about our data object preparation completion
            DataObjectCopyingEventArgs dataObjectCopyingEventArgs = new DataObjectCopyingEventArgs(dataObject, /*isDragDrop:*/ isDragDrop);

            This.UiScope.RaiseEvent(dataObjectCopyingEventArgs);
            if (dataObjectCopyingEventArgs.CommandCancelled)
            {
                dataObject = null;
            }

            return(dataObject);
        }
        private static void OnPasteMultiple(object sender, ExecutedRoutedEventArgs args)
        {
            var textEditor = (TextEditor)sender;
            var textArea   = textEditor.TextArea;

            if (textArea?.Document == null)
            {
                return;
            }

            var completionWindow = new CompletionWindow(textArea);

            // ----- Create completion list.
            var completionList = completionWindow.CompletionList;
            var completionData = completionList.CompletionData;
            var stringBuilder  = new StringBuilder();

            foreach (var text in ClipboardRing.GetEntries())
            {
                // Replace special characters for display.
                stringBuilder.Clear();
                stringBuilder.Append(text);
                stringBuilder.Replace("\n", "\\n");
                stringBuilder.Replace("\r", "\\r");
                stringBuilder.Replace("\t", "\\t");

                // Use TextBlock for TextTrimming.
                var textBlock = new TextBlock
                {
                    Text         = stringBuilder.ToString(),
                    TextTrimming = TextTrimming.CharacterEllipsis,
                    MaxWidth     = 135
                };
                completionData.Add(new CompletionData(text, text, null, textBlock));
            }

            // ----- Show completion window.
            completionList.SelectedItem = completionData[0];
            completionWindow.Show();

            // ----- Handle InsertionRequested event.
            completionList.InsertionRequested += (s, e) =>
            {
                var ta   = completionWindow.TextArea;
                var cl   = completionWindow.CompletionList;
                var text = cl.SelectedItem.Text;

                // Copy text into clipboard.
                var data = new DataObject();
                if (EditingCommandHandler.ConfirmDataFormat(ta, data, DataFormats.UnicodeText))
                {
                    data.SetText(text);
                }

                var copyingEventArgs = new DataObjectCopyingEventArgs(data, false);
                ta.RaiseEvent(copyingEventArgs);
                if (copyingEventArgs.CommandCancelled)
                {
                    return;
                }

                try
                {
                    Clipboard.SetDataObject(data, true);
                }
                catch (ExternalException)
                {
                }

                ClipboardRing.Add(text);
            };
        }
        internal static DataObject _CreateDataObject(TextEditor This, bool isDragDrop)
        {
            DataObject dataObject;
            // Create the data object for drag and drop.
            // 






            (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert();//BlessedAssert
            try
            {
                dataObject = new DataObject();
            }
            finally
            {
                UIPermission.RevertAssert();
            }

            // Get plain text and copy it into the data object.
            string textString = This.Selection.Text;

            if (textString != String.Empty)
            {
                // Copy plain text into data object.
                // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Text))
                {
                    CriticalSetDataWrapper(dataObject,DataFormats.Text, textString);
                }

                // Copy unicode text into data object.
                // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.UnicodeText))
                {
                    CriticalSetDataWrapper(dataObject,DataFormats.UnicodeText, textString);
                }
            }

            // Get the rtf and xaml text and then copy it into the data object after confirm data format.
            // We do this only if our content is rich
            if (This.AcceptsRichContent)
            {
                // This ensures that in the confines of partial trust RTF is not enabled.
                // We use unmanaged code permission over clipboard permission since
                // the latter is available in intranet zone and this is something that will
                // fail in intranet too.
                if (SecurityHelper.CheckUnmanagedCodePermission())
                {
                    // In FullTrust we allow all rich formats on the clipboard

                    Stream wpfContainerMemory = null;
                    // null wpfContainerMemory on entry means that container is optional
                    // and will be not created when there is no images in the range.

                    // Create in-memory wpf package, and serialize the content of selection into it
                    string xamlTextWithImages = WpfPayload.SaveRange(This.Selection, ref wpfContainerMemory, /*useFlowDocumentAsRoot:*/false);

                    if (xamlTextWithImages.Length > 0)
                    {
                        // ConfirmDataFormatSetting raises a public event - could throw recoverable exception.
                        if (wpfContainerMemory != null && ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.XamlPackage))
                        {
                            dataObject.SetData(DataFormats.XamlPackage, wpfContainerMemory);
                        }

                        // ConfirmDataFormatSetting raises a public event - could throw recoverable exception.
                        if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Rtf))
                        {
                            // Convert xaml to rtf text to set rtf data into data object.
                            string rtfText = ConvertXamlToRtf(xamlTextWithImages, wpfContainerMemory);

                            if (rtfText != String.Empty)
                            {
                                dataObject.SetData(DataFormats.Rtf, rtfText, true);
                            }
                        }
                    }

                    // Add a CF_BITMAP if we have only one image selected.
                    Image image = This.Selection.GetUIElementSelected() as Image;
                    if (image != null && image.Source is System.Windows.Media.Imaging.BitmapSource)
                    {
                        dataObject.SetImage((System.Windows.Media.Imaging.BitmapSource)image.Source);
                    }
                }

                // Xaml format is availabe both in Full Trust and in Partial Trust
                // Need to re-serialize xaml to avoid image references within a container:
                StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
                TextRangeSerialization.WriteXaml(xmlWriter, This.Selection, /*useFlowDocumentAsRoot:*/false, /*wpfPayload:*/null);
                string xamlText = stringWriter.ToString();
                // 

                if (xamlText.Length > 0)
                {
                    // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception.
                    if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Xaml))
                    {
                        // Place Xaml data onto the dataobject using safe setter
                        CriticalSetDataWrapper(dataObject, DataFormats.Xaml, xamlText);

                        // The dataobject itself must hold an information about permission set
                        // of the source appdomain. Set it there:

                        // Package permission set for the current appdomain
                        PermissionSet psCurrentAppDomain = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin();
                        string permissionSetCurrentAppDomain = psCurrentAppDomain.ToString();
                        CriticalSetDataWrapper(dataObject, DataFormats.ApplicationTrust, permissionSetCurrentAppDomain);
                    }
                }
            }

            // Notify application about our data object preparation completion
            DataObjectCopyingEventArgs dataObjectCopyingEventArgs = new DataObjectCopyingEventArgs(dataObject, /*isDragDrop:*/isDragDrop);
            This.UiScope.RaiseEvent(dataObjectCopyingEventArgs);
            if (dataObjectCopyingEventArgs.CommandCancelled)
            {
                dataObject = null;
            }

            return dataObject;
        }
        void StartDrag()
        {
            // prevent nested StartDrag calls
            mode = SelectionMode.Drag;

            // mouse capture and Drag'n'Drop doesn't mix
            textArea.ReleaseMouseCapture();

            DataObject dataObject = textArea.Selection.CreateDataObject(textArea);

            DragDropEffects allowedEffects = DragDropEffects.All;
            var             deleteOnMove   = textArea.Selection.Segments.Select(s => new AnchorSegment(textArea.Document, s)).ToList();

            foreach (ISegment s in deleteOnMove)
            {
                ISegment[] result = textArea.GetDeletableSegments(s);
                if (result.Length != 1 || result[0].Offset != s.Offset || result[0].EndOffset != s.EndOffset)
                {
                    allowedEffects &= ~DragDropEffects.Move;
                }
            }

            var copyingEventArgs = new DataObjectCopyingEventArgs(dataObject, true);

            textArea.RaiseEvent(copyingEventArgs);
            if (copyingEventArgs.CommandCancelled)
            {
                return;
            }

            object dragDescriptor = new object();

            this.currentDragDescriptor = dragDescriptor;

            DragDropEffects resultEffect;

            using (textArea.AllowCaretOutsideSelection()) {
                var oldCaretPosition = textArea.Caret.Position;
                try {
                    Debug.WriteLine("DoDragDrop with allowedEffects=" + allowedEffects);
                    resultEffect = DragDrop.DoDragDrop(textArea, dataObject, allowedEffects);
                    Debug.WriteLine("DoDragDrop done, resultEffect=" + resultEffect);
                } catch (COMException ex) {
                    // ignore COM errors - don't crash on badly implemented drop targets
                    Debug.WriteLine("DoDragDrop failed: " + ex.ToString());
                    return;
                }
                if (resultEffect == DragDropEffects.None)
                {
                    // reset caret if drag was aborted
                    textArea.Caret.Position = oldCaretPosition;
                }
            }

            this.currentDragDescriptor = null;

            if (deleteOnMove != null && resultEffect == DragDropEffects.Move && (allowedEffects & DragDropEffects.Move) == DragDropEffects.Move)
            {
                bool draggedInsideSingleDocument = (dragDescriptor == textArea.Document.UndoStack.LastGroupDescriptor);
                if (draggedInsideSingleDocument)
                {
                    textArea.Document.UndoStack.StartContinuedUndoGroup(null);
                }
                textArea.Document.BeginUpdate();
                try {
                    foreach (ISegment s in deleteOnMove)
                    {
                        textArea.Document.Remove(s.Offset, s.Length);
                    }
                } finally {
                    textArea.Document.EndUpdate();
                    if (draggedInsideSingleDocument)
                    {
                        textArea.Document.UndoStack.EndUndoGroup();
                    }
                }
            }
        }
Exemplo n.º 27
0
 private void NoCopy(object sender, DataObjectCopyingEventArgs e)
 {
     e.CancelCommand();
 }
Exemplo n.º 28
0
        protected void OnCopy(object o, DataObjectCopyingEventArgs e)
        {
            /*string clipboard = "";
             *
             * for (TextPointer p = Selection.Start, next = null;
             *   p != null && p.CompareTo(Selection.End) < 0;
             *   p = next)
             * {
             *  next = p.GetNextInsertionPosition(LogicalDirection.Forward);
             *  if (next == null)
             *      break;
             *
             *  //var word = new TextRange(p, next);
             *  //Console.WriteLine("Word '{0}' Inline {1}", word.Text, word.Start.Parent is EmojiElement ? "Emoji" : "not Emoji");
             *  //Console.WriteLine(" ... p {0}", p.Parent is EmojiElement ? "Emoji" : p.Parent.GetType().ToString());
             *
             *  var t = new TextRange(p, next);
             *  clipboard += t.Start.Parent is EmojiInline ? (t.Start.Parent as EmojiInline).Text
             *                                             : t.Text;
             * }
             *
             * Clipboard.SetText(clipboard);*/


            StringBuilder stringBuilder = new StringBuilder();

            // Enumerating the whole document to match for the Selection!
            // Couldn't found an efficient solution to fix copying issue! :( -- arnobpl

            bool isStartFound = false;

            foreach (Block block in this.Document.Blocks)
            {
                if (block is Paragraph)
                {
                    foreach (Inline inline in ((Paragraph)block).Inlines)
                    {
                        if (!isStartFound)
                        {
                            if (this.Selection.Contains(inline.ContentStart))
                            {
                                isStartFound = true;
                                stringBuilder.Append(GetText(inline));
                            }
                        }
                        else
                        {
                            if (this.Selection.Contains(inline.ContentEnd))
                            {
                                stringBuilder.Append(GetText(inline));
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            Clipboard.SetText(stringBuilder.ToString());

            e.Handled = true;
            e.CancelCommand();
        }
Exemplo n.º 29
0
 private void Topic_OnCopy(object sender, DataObjectCopyingEventArgs e)
 {
     e.Handled = true;
     e.CancelCommand();
     Clipboard.SetText(txtTopic.SelectedText.ToAresColor());
 }
        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);
        }