stringlist: Add a construct-only strings property

This enables creating string lists in ui files
without using custom markup.

Related: #5350
This commit is contained in:
Matthias Clasen 2022-11-20 14:06:55 -05:00
parent 8f24072354
commit f0aefe2f7f
2 changed files with 71 additions and 7 deletions

View 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: */

View 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);