stringlist: Add a construct-only strings property
authorMatthias Clasen <mclasen@redhat.com>
Sun, 20 Nov 2022 19:06:55 +0000 (14:06 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 20 Nov 2022 19:07:45 +0000 (14:07 -0500)
This enables creating string lists in ui files
without using custom markup.

Related: #5350

gtk/gtkstringlist.c
testsuite/gtk/stringlist.c

index 092840b693d864aa4fb41de4b0bed6cf63dadcc7..39eac696ab3a1d8a432dff7672c2be56cceca706 100644 (file)
@@ -56,6 +56,8 @@
  * ```
  */
 
+/* {{{ GtkStringObject */
+
 /**
  * GtkStringObject:
  *
@@ -183,6 +185,9 @@ gtk_string_object_get_string (GtkStringObject *self)
   return self->string;
 }
 
+/* }}} */
+/* {{{ List model implementation */
+
 struct _GtkStringList
 {
   GObject parent_instance;
@@ -229,6 +234,9 @@ gtk_string_list_model_init (GListModelInterface *iface)
   iface->get_item = gtk_string_list_get_item;
 }
 
+/* }}} */
+/* {{{ Buildable implementation */
+
 typedef struct
 {
   GtkBuilder    *builder;
@@ -394,6 +402,13 @@ gtk_string_list_buildable_init (GtkBuildableIface *iface)
   iface->custom_finished = gtk_string_list_buildable_custom_finished;
 }
 
+/* }}} */
+/* {{{ GObject implementation */
+
+enum {
+  PROP_STRINGS = 1
+};
+
 G_DEFINE_TYPE_WITH_CODE (GtkStringList, gtk_string_list, G_TYPE_OBJECT,
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                 gtk_string_list_buildable_init)
@@ -410,12 +425,39 @@ gtk_string_list_dispose (GObject *object)
   G_OBJECT_CLASS (gtk_string_list_parent_class)->dispose (object);
 }
 
+static void
+gtk_string_list_set_property (GObject      *object,
+                              unsigned int  prop_id,
+                              const GValue *value,
+                              GParamSpec   *pspec)
+{
+  GtkStringList *self = GTK_STRING_LIST (object);
+
+  switch (prop_id)
+    {
+    case PROP_STRINGS:
+      gtk_string_list_splice (self, 0, 0,
+                              (const char * const *) g_value_get_boxed (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
 static void
 gtk_string_list_class_init (GtkStringListClass *class)
 {
   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
 
   gobject_class->dispose = gtk_string_list_dispose;
+  gobject_class->set_property = gtk_string_list_set_property;
+
+  g_object_class_install_property (gobject_class, PROP_STRINGS,
+      g_param_spec_boxed ("strings", NULL, NULL,
+                          G_TYPE_STRV,
+                          G_PARAM_WRITABLE|G_PARAM_STATIC_STRINGS|G_PARAM_CONSTRUCT_ONLY));
 }
 
 static void
@@ -424,6 +466,9 @@ gtk_string_list_init (GtkStringList *self)
   objects_init (&self->items);
 }
 
+/* }}} */
+/* {{{ Public API */
+
 /**
  * gtk_string_list_new:
  * @strings: (array zero-terminated=1) (nullable): The strings to put in the model
@@ -435,13 +480,9 @@ gtk_string_list_init (GtkStringList *self)
 GtkStringList *
 gtk_string_list_new (const char * const *strings)
 {
-  GtkStringList *self;
-
-  self = g_object_new (GTK_TYPE_STRING_LIST, NULL);
-
-  gtk_string_list_splice (self, 0, 0, strings);
-
-  return self;
+  return g_object_new (GTK_TYPE_STRING_LIST,
+                       "strings", strings,
+                       NULL);
 }
 
 /**
@@ -583,3 +624,6 @@ gtk_string_list_get_string (GtkStringList *self,
 
   return objects_get (&self->items, position)->string;
 }
+
+/* }}} */
+/* vim:set foldmethod=marker expandtab: */
index cf163b88454a940ac691497cc533ee3c3c03d77d..26b4b6eb8a3bf6c141d499aa008a38d3a082714b 100644 (file)
@@ -170,6 +170,25 @@ test_create_builder (void)
   g_object_unref (builder);
 }
 
+static void
+test_create_builder2 (void)
+{
+  const char *ui =
+"<interface>"
+"  <object class=\"GtkStringList\" id=\"list\">"
+"    <property name=\"strings\">a\nb\nc</property>"
+"  </object>"
+"</interface>";
+  GtkBuilder *builder;
+  GtkStringList *list;
+
+  builder = gtk_builder_new_from_string (ui, -1);
+  list = GTK_STRING_LIST (gtk_builder_get_object (builder, "list"));
+  assert_model (list, "a b c");
+
+  g_object_unref (builder);
+}
+
 static void
 test_get_string (void)
 {
@@ -253,6 +272,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/stringlist/create/empty", test_create_empty);
   g_test_add_func ("/stringlist/create/strv", test_create_strv);
   g_test_add_func ("/stringlist/create/builder", test_create_builder);
+  g_test_add_func ("/stringlist/create/builder2", test_create_builder2);
   g_test_add_func ("/stringlist/get_string", test_get_string);
   g_test_add_func ("/stringlist/splice", test_splice);
   g_test_add_func ("/stringlist/add_remove", test_add_remove);