[Gtk-sharp-list] Listening for DragDataReceived in a TreeView when the Model is a TreeModelSort

Matthew Pirocchi matthew.pirocchi at gmail.com
Sun Dec 19 17:11:55 EST 2010


I have a TreeView whose Model is a TreeModelSort with a ListStore as its
child. I would like this TreeView to listen to DragDataReceived. However,
when I drag data to it, I get this warning:

(TestDragDrop:4706): Gtk-WARNING **: You must override the default
'drag_drop' handler on GtkTreeView when using models that don't support the
GtkTreeDragDest interface and enabling drag-and-drop. The simplest way to do
this is to connect to 'drag_drop' and call g_signal_stop_emission_by_name()
in your signal handler to prevent the default handler from running. Look at
the source code for the default handler in gtktreeview.c to get an idea what
your handler should do. (gtktreeview.c is in the GTK source code.) If you're
using GTK from a language other than C, there may be a more natural way to
override default handlers, e.g. via derivation.

I'm a bit lost on how to do thisin Gtk#. The message says "If you're using
GTK from a language other than C, there may be a more natural way to
override default handlers, e.g. via derivation." Can someone point me to an
example of what this would look like? What exactly am I overriding? There is
no DragDrop method, and overriding OnDragDataReceived doesn't seem to work.
Alternatively, I could manually sort the ListStore, but that seems like a
really bad idea (especially since there doesn't seem to be any easy way to
swap elements).

Here is an example TreeView for reference:


using System;
using System.Linq;
using System.Text;
using Gtk;

namespace TestDragDrop
{
public class DestTreeView : TreeView
{
public DestTreeView ()
{
ListStore store = new ListStore (typeof (string));
TreeModelSort sort = new TreeModelSort (store) { DefaultSortFunc =
BarSortFunc };
Model = sort;
 TreeViewColumn column = new TreeViewColumn { Title = "Bar" };
CellRendererText cell = new CellRendererText ();
column.PackStart (cell, true);
column.SetCellDataFunc (cell, RenderBar);
AppendColumn (column);
 EnableModelDragDest (Constants.Targets, Gdk.DragAction.Link);
DragDataReceived += HandleDragDataReceived;
 store.AppendValues ("bar1");
store.AppendValues ("bar2");
}

string GetValueAtPos (int x, int y)
{
TreePath path;
TreeViewDropPosition pos;
if (!GetDestRowAtPos (x, y, out path, out pos))
return "";
 TreeIter iter;
if (!Model.GetIter (out iter, path))
return "";
 return (string) Model.GetValue (iter, 0);
}

void HandleDragDataReceived (object o, DragDataReceivedArgs args)
{
string text = System.Text.Encoding.UTF8.GetString (args.SelectionData.Data);
string val = GetValueAtPos (args.X, args.Y);
 Console.WriteLine ("Data: '{0}'\nRow: '{1}'", text, val);
}

void RenderBar (TreeViewColumn column, CellRenderer renderer, TreeModel
model, TreeIter iter)
{
((CellRendererText) renderer).Text = (string) model.GetValue (iter, 0);
}

int BarSortFunc (TreeModel model, TreeIter iter1, TreeIter iter2)
{
string bar1 = (string) model.GetValue (iter1, 0);
string bar2 = (string) model.GetValue (iter2, 0);
 return bar1.CompareTo (bar2);
}
}
}


Thanks,
- Matthew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20101219/d2f054d6/attachment.html 


More information about the Gtk-sharp-list mailing list