Ejemplo n.º 1
0
        const string LineSelectedType = "MSDEVLineSelect";  // This is the type VS 2003 and 2005 use for flagging a whole line copy

        static void 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(text);

            // Also copy text in HTML format to clipboard - good for pasting text into Word
            // or to the SharpDevelop forums.
            IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter;

            HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, wholeLine, new HtmlOptions(textArea.Options)));

            MemoryStream lineSelected = new MemoryStream(1);

            lineSelected.WriteByte(1);
            data.SetData(LineSelectedType, lineSelected, 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;
            }
            textArea.OnTextCopied(new TextEventArgs(text));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a data object containing the selection's text.
        /// </summary>
        public virtual DataObject CreateDataObject(TextArea textArea)
        {
            DataObject data = new DataObject();

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

            // Enable drag/drop to Word, Notepad++ and others
            if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.UnicodeText))
            {
                data.SetText(text);
            }

            // Enable drag/drop to SciTe:
            // We cannot use SetText, thus we need to use typeof(string).FullName as data format.
            // new DataObject(object) calls SetData(object), which in turn calls SetData(Type, data),
            // which then uses Type.FullName as format.
            // We immitate that behavior here as well:
            if (EditingCommandHandler.ConfirmDataFormat(textArea, data, typeof(string).FullName))
            {
                data.SetData(typeof(string).FullName, text);
            }

            // Also copy text in HTML format to clipboard - good for pasting text into Word
            // or to the SharpDevelop forums.
            if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.Html))
            {
                HtmlClipboard.SetHtml(data, CreateHtmlFragment(new HtmlOptions(textArea.Options)));
            }
            return(data);
        }
Ejemplo n.º 3
0
        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            if (_edtXPath.SelectionLength > 0)
            {
                string text = _edtXPath.SelectedText;
                Clipboard.SetText(text);
            }
            else
            {
                DataObject dobj = new DataObject();
                dobj.SetText(_edtXPath.Text);

                try
                {
                    IHighlighter highlighter = new DocumentHighlighter(
                        _edtXPath.Document, _edtXPath.SyntaxHighlighting.MainRuleSet);
                    string html = HtmlClipboard.CreateHtmlFragment(
                        _edtXPath.Document, highlighter, null, new HtmlOptions());

                    HtmlClipboard.SetHtml(dobj, html);
                }
                catch
                {
                    //ignore
                }

                Clipboard.SetDataObject(dobj);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a HTML fragment for the selected text.
        /// </summary>
        public string CreateHtmlFragment(TextArea textArea, HtmlOptions options)
        {
            if (textArea == null)
            {
                throw new ArgumentNullException("textArea");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            IHighlighter  highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter;
            StringBuilder html        = new StringBuilder();
            bool          first       = true;

            foreach (ISegment selectedSegment in this.Segments)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    html.AppendLine("<br>");
                }
                html.Append(HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, selectedSegment, options));
            }
            return(html.ToString());
        }
Ejemplo n.º 5
0
		static void CopySelectedText(TextArea textArea)
		{
			string text = textArea.Selection.GetText(textArea.Document);
			// Ensure we use the appropriate newline sequence for the OS
			DataObject data = new DataObject(NewLineFinder.NormalizeNewLines(text, Environment.NewLine));
			// Also copy text in HTML format to clipboard - good for pasting text into Word
			// or to the SharpDevelop forums.
			HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragmentForSelection(textArea, new HtmlOptions(textArea.Options)));
			Clipboard.SetDataObject(data, true);
		}
Ejemplo n.º 6
0
		/// <summary>
		/// Creates a data object containing the selection's text.
		/// </summary>
		public virtual DataObject CreateDataObject(TextArea textArea)
		{
			string text = GetText(textArea.Document);
			// Ensure we use the appropriate newline sequence for the OS
			DataObject data = new DataObject(TextUtilities.NormalizeNewLines(text, Environment.NewLine));
			// we cannot use DataObject.SetText - then we cannot drag to SciTe
			// (but dragging to Word works in both cases)
			
			// Also copy text in HTML format to clipboard - good for pasting text into Word
			// or to the SharpDevelop forums.
			HtmlClipboard.SetHtml(data, CreateHtmlFragment(textArea, new HtmlOptions(textArea.Options)));
			return data;
		}
Ejemplo n.º 7
0
		const string LineSelectedType = "MSDEVLineSelect";  // This is the type VS 2003 and 2005 use for flagging a whole line copy
		
		static void 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
			DataObject data = new DataObject(NewLineFinder.NormalizeNewLines(text, Environment.NewLine));
			
			// Also copy text in HTML format to clipboard - good for pasting text into Word
			// or to the SharpDevelop forums.
			DocumentHighlighter highlighter = textArea.GetService(typeof(DocumentHighlighter)) as DocumentHighlighter;
			HtmlClipboard.SetHtml(data, HtmlClipboard.CreateHtmlFragment(textArea.Document, highlighter, wholeLine, new HtmlOptions(textArea.Options)));
			
			MemoryStream lineSelected = new MemoryStream(1);
			lineSelected.WriteByte(1);
			data.SetData(LineSelectedType, lineSelected, false);
			
			Clipboard.SetDataObject(data, true);
		}
Ejemplo n.º 8
0
        protected override void Execute(EditorFrame ef)
        {
            TextEditor textEditor = ef.XmlEditor;

            if (textEditor == null)
            {
                throw new Exception("No XmlEditor");
            }

            SaveFileDialog dlgSaveFile = new SaveFileDialog();

            dlgSaveFile.Filter = "Html Files|*.html|All Files|*.*";
            dlgSaveFile.Title  = "Select an Html file to save";
            if (string.IsNullOrWhiteSpace(ef.XSDocument.Filename))
            {
                dlgSaveFile.FileName = "undefined.html";
            }
            else
            {
                dlgSaveFile.FileName = ef.XSDocument.Filename + ".html";
            }

            if (dlgSaveFile.ShowDialog() == true)
            {
                try
                {
                    IHighlighter highlighter = new DocumentHighlighter(
                        textEditor.Document, textEditor.SyntaxHighlighting.MainRuleSet);
                    string html = HtmlClipboard.CreateHtmlFragment(
                        textEditor.Document, highlighter, null, new HtmlOptions());

                    File.WriteAllText(dlgSaveFile.FileName, html);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(Application.Current.MainWindow, ex.Message, "Error", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
            }
        }
Ejemplo n.º 9
0
        void StartDrag()
        {
            // prevent nested StartDrag calls
            mode = SelectionMode.Drag;

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

            string text = textArea.Selection.GetText(textArea.Document);

            text = NewLineFinder.NormalizeNewLines(text, Environment.NewLine);
            DataObject dataObject = new DataObject(text);
            // we cannot use DataObject.SetText - then we cannot drag to SciTe
            // (but dragging to Word works in both cases)

            // also copy as HTML - adds syntax highlighting when dragging to Word
            string htmlFragment = HtmlClipboard.CreateHtmlFragmentForSelection(textArea, new HtmlOptions(textArea.Options));

            HtmlClipboard.SetHtml(dataObject, htmlFragment);

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

            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)
            {
                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();
                    }
                }
            }
        }
Ejemplo n.º 10
0
        public string GetHTMLString()
        {
            IHighlighter highlighter = new DocumentHighlighter(TextEditor.Document, TextEditor.SyntaxHighlighting);

            return(HtmlClipboard.CreateHtmlFragment(TextEditor.Document, highlighter, null, new HtmlOptions()));
        }