[Gtk-sharp-list] When using Drag and Drop in a TreeView, how do I keep from dropping between rows?

Matthew Pirocchi matthew.pirocchi at gmail.com
Tue Feb 9 20:43:46 EST 2010


Thanks Michael, this wasn't as complicated to do as I feared. Here's
the code that I used--it won't work quite right if you have TreeViews
that are both drag sources and drag destinations, but it shouldn't be
that hard to tweak in that case.

[GLib.ConnectBefore]
void HandleTagViewDragMotion (object o, DragMotionArgs args)
{
	TreeView treeView = (TreeView) o;
	
	TreePath path;
	TreeViewDropPosition pos;
	if (!treeView.GetDestRowAtPos (args.X, args.Y, out path, out pos)) {
		args.RetVal = false;
		return;
	}
	
	if (pos == TreeViewDropPosition.Before)
		treeView.SetDragDestRow (path, TreeViewDropPosition.IntoOrBefore);
	else if (pos == TreeViewDropPosition.After)
		treeView.SetDragDestRow (path, TreeViewDropPosition.IntoOrAfter);
	
	Gdk.Drag.Status (args.Context, args.Context.SuggestedAction, args.Time);
	
	args.RetVal = true;
}

On Fri, Feb 5, 2010 at 3:18 PM, Michael Hutchinson
<m.j.hutchinson at gmail.com> wrote:
> On Fri, Feb 5, 2010 at 2:10 PM, Matthew Pirocchi
> <matthew.pirocchi at gmail.com> wrote:
>> I'm testing a window that looks something like this:
>>
>> http://i.imgur.com/3OaT6.png
>>
>> Dragging a Tag to a Card links the Tag to the Card. So does dragging a
>> Card to a Tag.
>>
>> It's meaningless to drop a tag between two cards, or a card between
>> two tags. I can ignore these outcomes in the Handle...DataReceived
>> function like this:
>>
>> if (dropPos != TreeViewDropPosition.IntoOrAfter &&
>>    dropPos != TreeViewDropPosition.IntoOrBefore)
>>    return;
>>
>> However, when dragging, the user still sees the option to insert:
>>
>> http://i.imgur.com/UU01L.png
>>
>> How do I prevent this from happening?
>
> I think i's possible but complicated, using the OnDragMotion and
> inserting your handler before the normal one (ConnectBefore), the on
> each event checking what's at the current location and possibly using
> args.RetVal=true to prevent the event propagating to the default drag
> motion handler.
>
> For an example see
> main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Components/ExtensibleTreeView.cs
>
> --
> Michael Hutchinson
> http://mjhutchinson.com
>


More information about the Gtk-sharp-list mailing list