4

I have a gtk.Treeview setup as a drag source:

self.drag_source_set(gtk.gdk.BUTTON1_MASK, targets, gtk.gdk.ACTION_COPY)

and it's TreeSelection is set to SELECT_MULTIPLE.

But everytime I try to drag a multi row selection, the cursor jumps to the current mouse position, resetting the selection to the current row. Even though the mouse is above one of the selected rows. It only works when I hold down the Shift or Ctrl button down.

What is going on?

Edit 1:

I have set up a bare-bones treeview to rule out any bugs in my code and it does the same thing.

Edit 2:

I found a code snippet that does what I want. It's from the quod libet sources, called MultiDragTreeView.

XORcist
  • 4,288
  • 24
  • 32
  • Can you give a link for that MultiDragTreeView? Is it in Python? – saeedgnu Mar 27 '11 at 12:23
  • Maybe this is that, but source link does not work: http://www.daa.com.au/pipermail/pygtk/2006-June/012440.html – saeedgnu Mar 27 '11 at 12:24
  • Oh, I found 2 classes: [this](http://www.koders.com/python/fidFF5C3F24E188371DD3A19E2BCF52A6C89BB5CA14.aspx?s=MultiDragTreeView#L26) and [this](http://www.koders.com/python/fidB8058C60799FF28A4C6AB95D011FA5590A7EE46F.aspx?s=MultiDragTreeView#L87) – saeedgnu Mar 27 '11 at 12:33

2 Answers2

2

I found a code snippet that does what I want. It's from the quod libet sources, called 'MultiDragTreeView'. Quoting the docstring:

"""TreeView with multirow drag support:
* Selections don't change until button-release-event...
* Unless they're a Shift/Ctrl modification, then they happen immediately
* Drag icons include 3 rows/2 plus a "and more" count"""
gpoo
  • 8,408
  • 3
  • 38
  • 53
XORcist
  • 4,288
  • 24
  • 32
  • 1
    The link no longer works, but I think this is where the code has moved to on GitHub: https://github.com/quodlibet/quodlibet/blob/release-3.9.1/quodlibet/quodlibet/qltk/views.py#L966 – Thomas K Jun 10 '17 at 10:54
  • 2
    Quodlibet is GPL licensed, but it looks like the original author of that code has also released it under the MIT license: https://kevinmehall.net/2010/pygtk_multi_select_drag_drop – Thomas K Jun 10 '17 at 11:02
0

I found a workaround to this, if item is selected, remove the item from selection and add control mask to the event.:

def on_iconview_button_press_event(widget, event):
    if event.type != Gdk.EventType.BUTTON_PRESS or event.button != 1:
        return
    if (event.state & Gdk.ModifierType.CONTROL_MASK):#do no changes if ctrl is pressed
        return
    path = widget.get_path_at_pos(event.x, event.y)
    if not path:
        return
    if widget.path_is_selected(path):
        widget.unselect_path(path)
        event.state |= Gdk.ModifierType.CONTROL_MASK
        return
iconview.connect('button-press-event', on_iconview_button_press_event)
Eudien
  • 1