diff options
author | Alexander Mikhaylenko <alexm@gnome.org> | 2020-12-14 18:53:00 +0500 |
---|---|---|
committer | Alexander Mikhaylenko <alexm@gnome.org> | 2020-12-14 18:53:00 +0500 |
commit | 29c49e085dd3075c1f05766a52ebaa31db2918c6 (patch) | |
tree | 88b629fb70d603207a6b010701b5f0f474e69d7b | |
parent | e2afb59a0b84b13c59216379b10e08c43398d82f (diff) | |
download | gtk+-29c49e085dd3075c1f05766a52ebaa31db2918c6.tar.gz |
tests: Add a nested scrolling test
-rw-r--r-- | tests/meson.build | 1 | ||||
-rw-r--r-- | tests/testnestedscrolling.c | 122 |
2 files changed, 123 insertions, 0 deletions
diff --git a/tests/meson.build b/tests/meson.build index 51b2311a80..87b18f4916 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -59,6 +59,7 @@ gtk_tests = [ ['testlockbutton'], ['testmenubutton'], ['testmountoperation'], + ['testnestedscrolling'], ['testnotebookdnd'], ['testnouiprint'], ['testoverlay'], diff --git a/tests/testnestedscrolling.c b/tests/testnestedscrolling.c new file mode 100644 index 0000000000..baf8c076a1 --- /dev/null +++ b/tests/testnestedscrolling.c @@ -0,0 +1,122 @@ +#include <gtk/gtk.h> + +/* This is the function that creates the #GListModel that we need. + * GTK list widgets need a #GListModel to display, as models support change + * notifications. + * Unfortunately various older APIs do not provide list models, so we create + * our own. + */ +static GListModel * +create_application_list (void) +{ + GListStore *store; + GList *apps, *l; + + /* We use a #GListStore here, which is a simple array-like list implementation + * for manual management. + * List models need to know what type of data they provide, so we need to + * provide the type here. As we want to do a list of applications, #GAppInfo + * is the object we provide. + */ + store = g_list_store_new (G_TYPE_APP_INFO); + + apps = g_app_info_get_all (); + + for (l = apps; l; l = l->next) + g_list_store_append (store, l->data); + + g_list_free_full (apps, g_object_unref); + + return G_LIST_MODEL (store); +} + +static void +setup_list_item (GtkSignalListItemFactory *factory, + GtkListItem *list_item) +{ + GtkWidget *label = gtk_label_new (NULL); + + gtk_widget_set_margin_top (label, 6); + gtk_widget_set_margin_bottom (label, 6); + + gtk_list_item_set_child (list_item, label); +} + +static void +bind_list_item (GtkSignalListItemFactory *factory, + GtkListItem *list_item) +{ + GtkWidget *label; + gpointer item; + const char *s; + + item = gtk_list_item_get_item (list_item); + + if (item) + s = g_app_info_get_name (G_APP_INFO (item)); + else + s = NULL; + + label = gtk_list_item_get_child (list_item); + gtk_label_set_text (GTK_LABEL (label), s); +} + +static void +on_application_activate (GtkApplication *app) +{ + GtkWidget *window, *swindow, *box; + gint i = 0; + + window = gtk_application_window_new (app); + gtk_window_set_default_size (GTK_WINDOW (window), 800, 600); + + swindow = gtk_scrolled_window_new (); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER); + gtk_window_set_child (GTK_WINDOW (window), swindow); + + box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); + gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (swindow), box); + + for (i = 0; i < 20; i++) + { + GtkWidget *list, *separator; + GtkSelectionModel *model; + GtkListItemFactory *factory; + + if (i > 0) + { + separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL); + gtk_box_append (GTK_BOX (box), separator); + } + + swindow = gtk_scrolled_window_new (); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); + gtk_box_append (GTK_BOX (box), swindow); + + model = GTK_SELECTION_MODEL (gtk_single_selection_new (create_application_list ())); + factory = gtk_signal_list_item_factory_new (); + g_signal_connect (factory, "setup", G_CALLBACK (setup_list_item), NULL); + g_signal_connect (factory, "bind", G_CALLBACK (bind_list_item), NULL); + + list = gtk_list_view_new (model, factory); + gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (swindow), list); + } + + gtk_window_present (GTK_WINDOW (window)); +} + +int +main (int argc, char *argv[]) +{ + GtkApplication *application = gtk_application_new ("org.gtk.test.nestedscrolling", + G_APPLICATION_FLAGS_NONE); + int result; + + g_signal_connect (application, "activate", + G_CALLBACK (on_application_activate), NULL); + + result = g_application_run (G_APPLICATION (application), argc, argv); + g_object_unref (application); + + return result; +} |