How to add keyboard expand/collapse to a multiselection GtkListView

I have the event handler setup but how do i find the current item in the GtkListView widget with GtkMultiSelection model and how do i toggle the collapse/expanded state?

Ok, it was easy once you find out. I answer myself in the hope that Google/Bing will help others with the same question.

My design mistake was to catch the keystrokes on the ListView level, but you have to do deeper down the widget tree.

in the item_factory_setup function do 

    GtkEventController* event_controller = gtk_event_controller_key_new();
    g_signal_connect(event_controller, "key-pressed", G_CALLBACK(cb_on_expander_key_pressed), list_item);
    gtk_widget_add_controller(expander, event_controller);


and then use the callback:

static gboolean cb_on_expander_key_pressed (GtkEventControllerKey* controller, guint keyval, guint keycode, GdkModifierType state, GtkListItem* list_item)  {
    if (keyval == GDK_KEY_Left) {
        GtkWidget* expander = gtk_list_item_get_child(list_item);
        gtk_widget_activate_action(expander, "listitem.collapse", nullptr);
        return TRUE;
    } else if (keyval == GDK_KEY_Right) {
        GtkWidget* expander = gtk_list_item_get_child(list_item);
        gtk_widget_activate_action(expander, "listitem.expand", nullptr);
        return TRUE;
    } else {
        return FALSE;
    }
}

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