Hey! I’m using the C/C++ API of GTK4, and I’m trying to create a simple GtkDropDown that displays a list of strings (this list is implemented through a GtkStringList model), and that allows “multiselection”. The dropdown works, but, I’m unable to select multiple items in the list at the same time. Only single selection works.
I tried to use GtkMultiSelection to fix this, but it is not working. Only single selection works still. I’ve isolated the problem in a minimal GTK4 application to make a reproducible example. If you compile and run the example below, you will see a dropdown that allows only single selection. So my question is how can I change this minimal example to create a dropdown that allows multiple selections?
#include "gtk/gtk.h"
GtkStringList*
create_stars_items_list() {
GtkStringList* list = gtk_string_list_new(
(const char* const[]){"0", "1", "2", "3", "4", "5", NULL}
);
return list;
}
static void
setup_stars_listitem_cb(GtkListItemFactory* factory, GtkListItem* list_item) {
GtkWidget* widget = gtk_label_new("");
gtk_list_item_set_child(list_item, widget);
}
static void
bind_stars_listitem_cb(GtkListItemFactory* factory, GtkListItem* list_item) {
GtkWidget* widget = gtk_list_item_get_child(list_item);
GtkStringObject* string = (GtkStringObject*)gtk_list_item_get_item(list_item);
gtk_label_set_text(GTK_LABEL(widget), gtk_string_object_get_string(string));
}
static void
activate(GtkApplication* app, gpointer user_data) {
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_window_present (GTK_WINDOW (window));
GtkStringList* stars_list = create_stars_items_list();
GtkMultiSelection* stars_selection = gtk_multi_selection_new(G_LIST_MODEL(stars_list));
GtkListItemFactory* stars_factory = gtk_signal_list_item_factory_new();
g_signal_connect (stars_factory, "setup", G_CALLBACK(setup_stars_listitem_cb), NULL);
g_signal_connect (stars_factory, "bind", G_CALLBACK(bind_stars_listitem_cb), NULL);
GtkWidget* stars_dropdown = gtk_drop_down_new(G_LIST_MODEL(stars_selection), NULL);
gtk_window_set_child(GTK_WINDOW(window), stars_dropdown);
}
int main(int argc, char** argv) {
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}