Ejemplo n.º 1
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();
                    }
                }
            }
        }