Thomas Mills Hinkle wrote:

On Mon, 28 Jun 2004 12:03:57 -0400 Thomas Mills Hinkle
<[EMAIL PROTECTED]> wrote:



Actually, if anyone can point me to an example that uses dragndrop to
reorganize rows, I'd be appreciative. One reason I'm hesitant to work
up some example code is that my current code is quite hackish -- I'd
love to see an example of how to do this more elegantly!



Okay, I've gone ahead and put together some examples (based on the tutorial's treeView dnd example) including both my up&down buttons and drag&drop. The broken example uses the move_up and move_down calls and is buggy on my current system, exhibiting the symptoms I described earlier with the up&down buttons and segfaulting when I drag rows internally.

Both programs work fine for me with PyGTK CVS and GTK+ 2.4.0.

The working example, which uses my own code to move the rows, works
smoothly on my system -- correctly allowing buttons and dnd to reorder
rows and handling dnd from other apps.

I still don't know the proper way to differentiate an internal drag from
an external one. A gander at my code will show the hack I've used -- I
define my own dnd type ('EXAMPLE_INTERNAL') and look for it when I
receive the drop signal. This clearly isn't the right solution (in
addition to its inelegance, it also will fail to do the right thing with
dnd between multiple instances of the example application). Can someone
tell me the right way to do this?


This is exactly the method (with a different target) that GTK+ uses to implement reorderable rows using set_reorderable() however you need to set the gtk.TARGET_SAME_WIDGET flag to limit the target to the same widget. Also you need to rearrange the source target order so that EXAMPLE_INTERNAL is matched first if available then text/plain and STRING:

self.treeview.enable_model_drag_source(gtk.gdk.BUTTON1_MASK,
[
('EXAMPLE_INTERNAL',gtk.TARGET_SAME_WIDGET,0),
('text/plain', 0, 1),
('STRING',0,2)
],
gtk.gdk.ACTION_COPY)


Likewise for the dest target order:

self.treeview.enable_model_drag_dest([
('EXAMPLE_INTERNAL',gtk.TARGET_SAME_WIDGET,0),
('text/plain', 0, 1),
('STRING',0,2)],
gtk.gdk.ACTION_COPY)


Then in drag_data_received_data()

Use the selection.target to differentiate the drag sources:

   def drag_data_received_data(self, treeview, context, x, y, selection,
                               info, etime):
       mod = treeview.get_model()
       data = selection.data
       drop_info = treeview.get_dest_row_at_pos(x, y)
       if str(selection.target) == 'EXAMPLE_INTERNAL':
           mod,moving_iter=treeview.get_selection().get_selected()
       else: moving_iter=False
    ....

Also your drag_data_get_data() method should set the selection data like:

       selection.set(selection.target, 8, data)

This should allow reordering internal rows while still allowing external drag and drop which is what I'm guessing you want to do.

John

_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to