#!/usr/bin/python3

from gi.repository import Gdk
from gi.repository import Gtk

Gtk.init()

WANTED_DATA = "wanted_data"

class CustomStore(Gtk.ListStore):
    def __init__(self, *args):
        Gtk.ListStore.__init__(self, *args)

    def do_drag_data_get(self, path, selection_data):
        selection_data.set(
            Gdk.Atom.intern("something/maybe", False), 0,
            bytes(WANTED_DATA, encoding="UTF8"))

        the_data = str(selection_data.get_data(), "UTF-8")
        assert(the_data == WANTED_DATA)

        print("(selection_data is %s) -- Data '%s' == WANTED_DATA '%s'" % (selection_data,
                                                                          the_data, WANTED_DATA))

        return True


storemodel = Gtk.ListStore(str)
for i in range(4):
    storemodel.append(["(ListStore) " + ("%s" % i) * 4])

def _dragDataReceivedCb(treeview, drag_context, x, y, selection_data, unused_info, timestamp):
    the_data = str(selection_data.get_data(), "UTF-8")
    print("(selection_data is %s, Actually got data: '%s'" % (selection_data, the_data))
    assert(the_data == WANTED_DATA)

namecell = Gtk.CellRendererText()
namecol = Gtk.TreeViewColumn("NAME")
namecol.pack_start(namecell, True)
namecol.add_attribute(namecell, "text", 0)
treeview = Gtk.TreeView(model=storemodel)
treeview.append_column(namecol)

treeview.connect("drag-data-received", _dragDataReceivedCb)
EFFECT_TARGET_ENTRY = Gtk.TargetEntry.new("myapp/dragtype", 0, 1001)
treeview.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [EFFECT_TARGET_ENTRY], Gdk.DragAction.COPY)
treeview.enable_model_drag_dest([EFFECT_TARGET_ENTRY], Gdk.DragAction.COPY)


win = Gtk.Window()
win.add(treeview)
win.show_all()


win.connect("delete-event", Gtk.main_quit)
Gtk.main()
