[gtk/popover-list-lockup] Add a testcase for #3725




commit 38b3da92399e73d630a70698c33afa6b205f5adc
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Apr 9 15:19:53 2021 -0400

    Add a testcase for #3725

 tests/meson.build    |   1 +
 tests/testpopover2.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)
---
diff --git a/tests/meson.build b/tests/meson.build
index c0e5f836b0..f0110ac0aa 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,5 +1,6 @@
 gtk_tests = [
   # testname, optional extra sources
+  ['testpopover2'],
   ['testpopup'],
   ['testupload'],
   ['testtransform'],
diff --git a/tests/testpopover2.c b/tests/testpopover2.c
new file mode 100644
index 0000000000..80fb1fce26
--- /dev/null
+++ b/tests/testpopover2.c
@@ -0,0 +1,118 @@
+#include <gtk/gtk.h>
+
+static void
+remove_cb (GtkButton *button,
+           GtkListItem *item)
+{
+  GListModel *model;
+  guint position;
+
+  model = (GListModel *)g_object_get_data (G_OBJECT (button), "model");
+
+  position = gtk_list_item_get_position (item);
+
+  gtk_string_list_remove (GTK_STRING_LIST (model), position);
+}
+
+static void
+setup_item (GtkSignalListItemFactory *factory,
+            GtkListItem              *list_item,
+            GListModel               *model)
+{
+  GtkWidget *child;
+  GtkWidget *label;
+  GtkWidget *button;
+
+  label = gtk_label_new ("");
+  button = gtk_button_new_with_label ("X");
+
+  child = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+  gtk_box_append (GTK_BOX (child), label);
+  gtk_box_append (GTK_BOX (child), button);
+
+  gtk_list_item_set_child (list_item, child);
+}
+
+static void
+bind_item (GtkSignalListItemFactory *factory,
+           GtkListItem              *list_item,
+           GListModel               *model)
+{
+  gpointer item;
+  const char *string;
+  GtkWidget *box;
+  GtkWidget *label;
+  GtkWidget *button;
+
+  item = gtk_list_item_get_item (list_item);
+  string = gtk_string_object_get_string (GTK_STRING_OBJECT (item));
+
+  box = gtk_list_item_get_child (list_item);
+  label = gtk_widget_get_first_child (box);
+  button = gtk_widget_get_last_child (box);
+
+  gtk_label_set_label (GTK_LABEL (label), string);
+
+  g_object_set_data (G_OBJECT (button), "model", model);
+  g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), list_item);
+}
+
+int
+main (int argc, char *argv[])
+{
+  GtkWidget *window;
+  GtkWidget *menu_button;
+  GtkWidget *popover;
+  GtkWidget *box;
+  GtkWidget *close_button;
+  GtkWidget *list;
+  GtkSelectionModel *model;
+  GtkStringList *strings;
+  GtkListItemFactory *factory;
+  const char * data[] = {
+    "one",
+    "two",
+    NULL
+  };
+
+  gtk_init ();
+
+  window = gtk_window_new ();
+
+  menu_button = gtk_menu_button_new ();
+  gtk_widget_set_halign (menu_button, GTK_ALIGN_END);
+  gtk_widget_set_valign (menu_button, GTK_ALIGN_START);
+
+  popover = gtk_popover_new ();
+
+  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+
+  close_button = gtk_button_new_with_label ("Close");
+  g_signal_connect_swapped (close_button, "clicked", G_CALLBACK (gtk_menu_button_popdown), menu_button);
+
+  strings = gtk_string_list_new (data);
+  model = GTK_SELECTION_MODEL (gtk_no_selection_new (G_LIST_MODEL (strings)));
+  factory = gtk_signal_list_item_factory_new ();
+
+  g_signal_connect (factory, "setup", G_CALLBACK (setup_item), strings);
+  g_signal_connect (factory, "bind", G_CALLBACK (bind_item), strings);
+
+  list = gtk_list_view_new (model, factory);
+
+  gtk_box_append (GTK_BOX (box), list);
+
+  gtk_box_append (GTK_BOX (box), close_button);
+
+  gtk_popover_set_child (GTK_POPOVER (popover), box);
+
+  gtk_menu_button_set_popover (GTK_MENU_BUTTON (menu_button), popover);
+
+  gtk_window_set_child (GTK_WINDOW (window), menu_button);
+
+  gtk_window_present (GTK_WINDOW (window));
+
+  while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
+    g_main_context_iteration (NULL, TRUE);
+
+  return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]