How can I perform drag and drop of text into the editor?

SyntaxEditor for WPF Forum

Posted 14 years ago by Craig - Varigence, Inc.
Version: 10.2.0531
Avatar
I have a scenario where I want to drag a TreeViewItem from a WPF TreeView into the SyntaxEditor, so that the TreeViewItem's header text is inserted into the SyntaxEditor's text.

I have some code together but I was wondering the following:

1. Is there any function built into the SyntaxEditor for performing drag and drop on it? Perhaps something that accepts the DragEventArgs parameter, and lets the editor figure out where to add the data?

2. If there isn't such a method, is there a way to convert mouse coordinates to caret coordinates so I can control where the text is inserted by moving the SyntaxEditor's caret to where my mouse pointer is?

3. Is there a special insertion function I can use which inserts text at the editor's caret position and/or a mouse position?

4. Are there any drag and drop samples that I may have missed or discussion about this in the documentation? I looked myself and didn't see anything but maybe I missed something.

Thanks,

-Craig

Comments (4)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

We don't have drag/drop handling in just yet, but it is on our TODO list. I'll mark this post down with our TODO item for it.

We do have "location" (mouse location) to offset conversion methods. See the "SyntaxEditor Editor View Features / Locations, Offsets, and Positions" documentation topic for details on that.

Our document model supports many features for programmatic text changes in which you could pass an offset range to update, or alternatively you could replace the selected text in an IEditorView with the IEditorView.SelectedText property.

Sorry there aren't any samples on drag/drop yet until we get it built-in. There are still a number of feature areas we need to get implemented like this and we are knocking them out one by one. But the top customer request at the moment is the .NET Languages Add-on port.


Actipro Software Support

Posted 14 years ago by Craig - Varigence, Inc.
Avatar
Two more questions:

1. How can I move the syntax editor caret, so it follows my mouse cursor, as I perform a drag over? I've tried changing the Caret's Position, but it doesn't seem to update the caret location in the UI. Is there something else I need to do?

2. I came up with this approach for handling a drop in the syntax editor. Does this look reasonable? Are there any better approaches?


            var syntaxEditor = sender as SyntaxEditor;
            if (syntaxEditor != null)
            {
                var syntaxEditorRelativePosition = GetMousePosition(syntaxEditor);
                var syntaxEditorPosition = syntaxEditor.ActiveView.TransformToTextArea(syntaxEditorRelativePosition.GetValueOrDefault());
                var textPosition = syntaxEditor.ActiveView.LocationToPosition(syntaxEditorPosition, LocationToPositionAlgorithm.BestFit);
                syntaxEditor.Caret.Position = textPosition;
                var offset = syntaxEditor.Document.CurrentSnapshot.PositionToOffset(textPosition);

                if (performDrop)
                {
                    var attributeValue = " " + draggedObject.GetAttribute("Value");
                    syntaxEditor.Document.InsertText(TextChangeTypes.Custom, offset, attributeValue);
                }

                e.Effects = DragDropEffects.Move;
            }

Thanks again,

-Craig

[Modified at 10/15/2010 02:27 PM]
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Craig,

The caret should move if you set its Position unless its rendering is suspended, possibly by drag methods preventing updating? It's hard hard to say without debugging it.

For your code, you may want to get the mouse position relative to the view's VisualElement property, which is the EditorView control. Then do your TranslateToTextArea off that.

Alternatively, look into our hit testing features (described in the docs in "SyntaxEditor Editor View Features / Hit Testing")
) as that will give you the offset the mouse is over and is easier than rolling code like this.


Actipro Software Support

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Just FYI... per this blog post, drag and drop has been implemented for the WPF SyntaxEditor in its upcoming 2011.2 version:
http://blog.actiprosoftware.com/post/2011/08/24/SyntaxEditor-Drag-and-Drop.aspx


Actipro Software Support

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.