예제 #1
0
        private void textArea_Drop(object sender, DragEventArgs e)
        {
            try
            {
                DragDropEffects effect = GetEffect(e);
                e.Effects = effect;
                if (effect != DragDropEffects.None)
                {
                    int start = textArea.Caret.Offset;
                    if (mode == SelectionMode.Drag && textArea.Selection.Contains(start))
                    {
                        Debug.WriteLine("Drop: did not drop: drop target is inside selection");
                        e.Effects = DragDropEffects.None;
                    }
                    else
                    {
                        Debug.WriteLine("Drop: insert at " + start);

                        var pastingEventArgs = new DataObjectPastingEventArgs(e.Data, true, DataFormats.UnicodeText);
                        textArea.RaiseEvent(pastingEventArgs);
                        if (pastingEventArgs.CommandCancelled)
                        {
                            return;
                        }

                        string text = EditingCommandHandler.GetTextToPaste(pastingEventArgs, textArea);
                        if (text == null)
                        {
                            return;
                        }
                        bool rectangular = pastingEventArgs.DataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                        // Mark the undo group with the currentDragDescriptor, if the drag
                        // is originating from the same control. This allows combining
                        // the undo groups when text is moved.
                        textArea.Document.UndoStack.StartUndoGroup(this.currentDragDescriptor);
                        try
                        {
                            if (rectangular && RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, true))
                            {
                            }
                            else
                            {
                                textArea.Document.Insert(start, text);
                                textArea.Selection = Selection.Create(textArea, start, start + text.Length);
                            }
                        }
                        finally
                        {
                            textArea.Document.UndoStack.EndUndoGroup();
                        }
                    }
                    e.Handled = true;
                }
            }
            catch (Exception ex)
            {
                OnDragException(ex);
            }
        }
예제 #2
0
        const string LineSelectedType = "MSDEVLineSelect";          // This is the type VS 2003 and 2005 use for flagging a whole line copy

        public static bool ConfirmDataFormat(TextArea textArea, DataObject dataObject, string format)
        {
            var e = new DataObjectSettingDataEventArgs(dataObject, format);

            textArea.RaiseEvent(e);
            return(!e.CommandCancelled);
        }
예제 #3
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);
        }
예제 #4
0
        private static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try
                {
                    dataObject = Clipboard.GetDataObject();
                }
                catch (ExternalException)
                {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }

                var pastingEventArgs = new DataObjectPastingEventArgs(dataObject, false, DataFormats.UnicodeText);
                textArea.RaiseEvent(pastingEventArgs);
                if (pastingEventArgs.CommandCancelled)
                {
                    return;
                }

                string text = GetTextToPaste(pastingEventArgs, textArea);

                if (!string.IsNullOrEmpty(text))
                {
                    dataObject = pastingEventArgs.DataObject;
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #5
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);
        }
        void textArea_Drop(object sender, DragEventArgs e)
        {
            try {
                DragDropEffects effect = GetEffect(e);
                e.Effects = effect;
                if (effect != DragDropEffects.None)
                {
                    string text = e.Data.GetData(DataFormats.UnicodeText, true) as string;
                    if (text != null)
                    {
                        int start = textArea.Caret.Offset;
                        if (mode == SelectionMode.Drag && textArea.Selection.Contains(start))
                        {
                            Debug.WriteLine("Drop: did not drop: drop target is inside selection");
                            e.Effects = DragDropEffects.None;
                        }
                        else
                        {
                            Debug.WriteLine("Drop: insert at " + start);

                            bool rectangular = e.Data.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                            string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                            text = TextUtilities.NormalizeNewLines(text, newLine);

                            string pasteFormat;
                            // fill the suggested DataFormat used for the paste action:
                            if (rectangular)
                            {
                                pasteFormat = RectangleSelection.RectangularSelectionDataType;
                            }
                            else
                            {
                                pasteFormat = DataFormats.UnicodeText;
                            }

                            var pastingEventArgs = new DataObjectPastingEventArgs(e.Data, true, pasteFormat);
                            textArea.RaiseEvent(pastingEventArgs);
                            if (pastingEventArgs.CommandCancelled)
                            {
                                return;
                            }

                            // DataObject.PastingEvent handlers might have changed the format to apply.
                            rectangular = pastingEventArgs.FormatToApply == RectangleSelection.RectangularSelectionDataType;

                            // Mark the undo group with the currentDragDescriptor, if the drag
                            // is originating from the same control. This allows combining
                            // the undo groups when text is moved.
                            textArea.Document.UndoStack.StartUndoGroup(this.currentDragDescriptor);
                            try {
                                if (rectangular && RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, true))
                                {
                                }
                                else
                                {
                                    textArea.Document.Insert(start, text);
                                    textArea.Selection = Selection.Create(textArea, start, start + text.Length);
                                }
                            } finally {
                                textArea.Document.UndoStack.EndUndoGroup();
                            }
                        }
                        e.Handled = true;
                    }
                }
            } catch (Exception ex) {
                OnDragException(ex);
            }
        }
예제 #7
0
		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;
		}
예제 #8
0
		const string LineSelectedType = "MSDEVLineSelect";  // This is the type VS 2003 and 2005 use for flagging a whole line copy
		
		public static bool ConfirmDataFormat(TextArea textArea, DataObject dataObject, string format)
		{
			var e = new DataObjectSettingDataEventArgs(dataObject, format);
			textArea.RaiseEvent(e);
			return !e.CommandCancelled;
		}
예제 #9
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.
			}
			
			string text = textArea.Selection.GetText();
			text = TextUtilities.NormalizeNewLines(text, Environment.NewLine);
			textArea.OnTextCopied(new TextEventArgs(text));
			return true;
		}
예제 #10
0
        static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try {
                    dataObject = Clipboard.GetDataObject();
                } catch (ExternalException) {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }
                Debug.WriteLine(dataObject.GetData(DataFormats.Html) as string);

                // convert text back to correct newlines for this document
                string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                string text;
                try {
                    text = (string)dataObject.GetData(DataFormats.UnicodeText);
                    text = TextUtilities.NormalizeNewLines(text, newLine);
                } catch (OutOfMemoryException) {
                    return;
                }

                if (!string.IsNullOrEmpty(text))
                {
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                    string pasteFormat;
                    // fill the suggested DataFormat used for the paste action:
                    if (fullLine)
                    {
                        pasteFormat = LineSelectedType;
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        pasteFormat = RectangleSelection.RectangularSelectionDataType;
                    }
                    else
                    {
                        pasteFormat = DataFormats.UnicodeText;
                    }

                    var pastingEventArgs = new DataObjectPastingEventArgs(dataObject, false, pasteFormat);
                    textArea.RaiseEvent(pastingEventArgs);
                    if (pastingEventArgs.CommandCancelled)
                    {
                        return;
                    }

                    // DataObject.PastingEvent handlers might have changed the format to apply.
                    pasteFormat = pastingEventArgs.FormatToApply;

                    fullLine    = pasteFormat == LineSelectedType;
                    rectangular = pasteFormat == RectangleSelection.RectangularSelectionDataType;

                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }
예제 #11
0
        static void OnPaste(object target, ExecutedRoutedEventArgs args)
        {
            TextArea textArea = GetTextArea(target);

            if (textArea != null && textArea.Document != null)
            {
                IDataObject dataObject;
                try {
                    dataObject = Clipboard.GetDataObject();
                } catch (ExternalException) {
                    return;
                }
                if (dataObject == null)
                {
                    return;
                }

                var pastingEventArgs = new DataObjectPastingEventArgs(dataObject, false, DataFormats.UnicodeText);
                textArea.RaiseEvent(pastingEventArgs);
                if (pastingEventArgs.CommandCancelled)
                {
                    return;
                }

                dataObject = pastingEventArgs.DataObject;
                if (dataObject == null)
                {
                    return;
                }

                // convert text back to correct newlines for this document
                string newLine = TextUtilities.GetNewLineFromDocument(textArea.Document, textArea.Caret.Line);
                string text;
                try {
                    // Try retrieving the text as one of:
                    //  - the FormatToApply
                    //  - UnicodeText
                    //  - Text
                    // (but don't try the same format twice)
                    if (pastingEventArgs.FormatToApply != null && dataObject.GetDataPresent(pastingEventArgs.FormatToApply))
                    {
                        text = (string)dataObject.GetData(pastingEventArgs.FormatToApply);
                    }
                    else if (pastingEventArgs.FormatToApply != DataFormats.UnicodeText && dataObject.GetDataPresent(DataFormats.UnicodeText))
                    {
                        text = (string)dataObject.GetData(DataFormats.UnicodeText);
                    }
                    else if (pastingEventArgs.FormatToApply != DataFormats.Text && dataObject.GetDataPresent(DataFormats.Text))
                    {
                        text = (string)dataObject.GetData(DataFormats.Text);
                    }
                    else
                    {
                        return;                         // no text data format
                    }
                    text = TextUtilities.NormalizeNewLines(text, newLine);
                } catch (OutOfMemoryException) {
                    // may happen when trying to paste a huge string
                    return;
                }

                if (!string.IsNullOrEmpty(text))
                {
                    bool fullLine    = textArea.Options.CutCopyWholeLine && dataObject.GetDataPresent(LineSelectedType);
                    bool rectangular = dataObject.GetDataPresent(RectangleSelection.RectangularSelectionDataType);

                    if (fullLine)
                    {
                        DocumentLine currentLine = textArea.Document.GetLineByNumber(textArea.Caret.Line);
                        if (textArea.ReadOnlySectionProvider.CanInsert(currentLine.Offset))
                        {
                            textArea.Document.Insert(currentLine.Offset, text);
                        }
                    }
                    else if (rectangular && textArea.Selection.IsEmpty && !(textArea.Selection is RectangleSelection))
                    {
                        if (!RectangleSelection.PerformRectangularPaste(textArea, textArea.Caret.Position, text, false))
                        {
                            textArea.ReplaceSelectionWithText(text);
                        }
                    }
                    else
                    {
                        textArea.ReplaceSelectionWithText(text);
                    }
                }
                textArea.Caret.BringCaretToView();
                args.Handled = true;
            }
        }