Get row of ColumnView when preparing drag and drop

I want to implement drag&drop from a ColumnView (with multiselect). I know how to get currently selected rows, but D&D should work when none is selected too. In such case I’d like to pick up the clicked (eventually hovered) row, but I see no way to do it. Do you have any pointers?

Current code:

    let tree_model = gtk4::TreeListModel::new(store, false, false, move |item| {
        add_to_tree_on_expand(item, &data_clone)
    });

    let selection = gtk4::MultiSelection::new(Some(tree_model));
    let view_left = create_view_left(data.clone());
    let view_right = create_view_right(data.clone());

    let list_view = gtk4::ColumnView::new(Some(selection.clone()));
    list_view.append_column(&ColumnViewColumn::new(None, Some(view_left)));
    list_view.append_column(&ColumnViewColumn::new(None, Some(view_right)));
    list_view.set_reorderable(false);

    let dnd_controller = gtk4::DragSource::new();
    dnd_controller.connect_prepare(move |a, b, c| {
        if selection.selection().size() == 0 {
            // do smth here
        } else {
            for i in 0..selection.selection().size() {
                let selected_idx = selection.selection().nth(i as u32);
                let row = selection
                    .item(selected_idx)
                    .unwrap()
                    .downcast::<gtk4::TreeListRow>()
                    .unwrap();
                let item = row.item().unwrap().downcast::<LibraryItemObj>().unwrap();
                debug!("{} -> {:?}", selected_idx, item.get_item());
            }
        }

        Some(ContentProvider::for_value(&"aaaaa".into()))
    });
    list_view.add_controller(dnd_controller);

Hi,

gtk doesn’t offer a way to get the model item from the row widget, so it’s something you have to implement yourself in bind/unbind.

In C you can use GObject.Object.set_data to link a reference of the item to the widget.

Then, in mouse events, get the row widget with a combination of Gtk.Widget.pick and Gtk.Widget.get_ancestor, and retrieve the model item with GObject.Object.get_data .

1 Like

Good golly. That’s complicated. Do you have any sample code that you’ve done something like this? I think it might be useful to have a library of codes.

I talked to Matthias Clasen about this at GUADEC. It would be something that would be great to include in the platform itself since this would be useful for things like spreadsheets and the like.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.