I'm still stuck on this problem, so I created a test program. It evinces
the same behavior. I suppose that there is a difference between my
programs and the one that Francois wrote, but I'm not seeing it. In the
test program, DnD works fine when I allow the treeview to initiate it
automatically (set MANUAL to False). When I set MANUAL to True, it is no
longer possible to drag a row to a new location (it is not possible to
release the drag). Any help appreciated.
--
Jeffrey Barish
# DND works when I allow GTK to initiate it automatically, but not when I
# initiate it manually. Set the MANUAL global to select the mode.
import pygtk
import gtk
MANUAL = True
numbers = ('zero', 'one', 'two', 'three', 'four', 'five', 'six')
data = [(str(i), d) for i, d in enumerate(numbers)]
class TreeViewDnDTest:
def __init__(self):
self.drag_context = None
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("DND Test")
self.window.set_size_request(200, 300)
self.window.connect('delete-event', self.on_delete_event)
self.button = gtk.Button('Text on the button')
self.liststore = gtk.ListStore(str, str)
for item in data:
self.liststore.append(item)
self.treeview = gtk.TreeView(self.liststore)
self.treeview.connect('drag-data-received', self.on_drag_data_received)
if MANUAL:
self.treeview.connect('button-press-event', self.on_treeview_button_press_event)
self.treeview.connect('motion-notify-event', self.on_treeview_motion_notify_event)
self.treeview.connect('button-release-event', self.on_button_release_event)
renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn('Int', renderer, text=0)
self.treeview.append_column(column)
column = gtk.TreeViewColumn('Str', renderer, text=1)
self.treeview.append_column(column)
self.button.drag_source_set(gtk.gdk.BUTTON1_MASK,
[('tree_model_row_from_button', gtk.TARGET_SAME_APP, 0)],
gtk.gdk.ACTION_MOVE)
if not MANUAL:
self.treeview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK,
[('tree_model_row', gtk.TARGET_SAME_WIDGET, 1)],
gtk.gdk.ACTION_MOVE)
self.treeview.enable_model_drag_dest(
[('tree_model_row', 0, 1), ('tree_model_row_from_button', 0, 0)],
gtk.gdk.ACTION_DEFAULT)
self.vbox = gtk.VBox()
self.vbox.pack_start(self.button, False)
self.vbox.pack_start(self.treeview, True)
self.window.add(self.vbox)
self.window.show_all()
def on_delete_event(self, widget, event, data=None):
gtk.main_quit()
return False
def on_drag_data_received(self, treeview, context, x, y, selection,
info, etime):
source_widget = context.get_source_widget()
if source_widget == treeview:
model, source_iter = treeview.get_selection().get_selected()
source_row = model[source_iter]
elif source_widget == self.button:
model = treeview.get_model()
source_row = ('9', 'newrow')
drop_info = treeview.get_dest_row_at_pos(x, y)
if drop_info:
path, position = drop_info
iter = model.get_iter(path)
if (position == gtk.TREE_VIEW_DROP_BEFORE
or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE):
model.insert_before(iter, source_row)
else:
model.insert_after(iter, source_row)
else:
model.append(source_row)
if context.action == gtk.gdk.ACTION_MOVE:
context.finish(True, True, etime)
if MANUAL:
def on_treeview_button_press_event(self, treeview, event):
if event.button == 1:
self.press_start_x = int(event.x)
self.press_start_y = int(event.y)
self.press_start_event = event
def on_treeview_motion_notify_event(self, treeview, event):
if event.state == gtk.gdk.BUTTON1_MASK:
row = self.treeview.get_dest_row_at_pos(int(event.x), int(event.y))
if row:
treepath, drop_pos = row
threshold = treeview.drag_check_threshold(
self.press_start_x, self.press_start_y, int(event.x), int(event.y))
if self.drag_context is None and threshold:
targets = [('tree_model_row', gtk.TARGET_SAME_WIDGET, 1)]
actions = gtk.gdk.ACTION_MOVE
button = gtk.gdk.BUTTON1_MASK
self.drag_context = treeview.drag_begin(targets, actions, button,
self.press_start_event)
def on_button_release_event(self, treeview, event):
print "received button release event"
if __name__ == '__main__':