summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2020-06-23 20:43:49 +0000
committerMatthias Clasen <mclasen@redhat.com>2020-06-23 20:43:49 +0000
commit2cfeb1048c1090b7ce99638bd19feaf41246a8f2 (patch)
treed4e9c0dd6de3cfa388e7dd45ca61ef82ce927df0
parentfff981b220e4c91f9d7bac99996da63fdc58ecfd (diff)
parente8210d5865bca6f2738bb0a68962963f19e76e37 (diff)
downloadgtk+-2cfeb1048c1090b7ce99638bd19feaf41246a8f2.tar.gz
Merge branch 'stringlist' into 'master'
add a public stringlist api See merge request GNOME/gtk!2134
-rw-r--r--docs/reference/gtk/gtk4-docs.xml3
-rw-r--r--docs/reference/gtk/gtk4-sections.txt14
-rw-r--r--docs/reference/gtk/gtk4.types.in2
-rw-r--r--gtk/gtk.h1
-rw-r--r--gtk/gtkdropdown.c350
-rw-r--r--gtk/gtkstringlist.c620
-rw-r--r--gtk/gtkstringlist.h75
-rw-r--r--gtk/meson.build1
-rw-r--r--gtk/ui/gtkprintunixdialog.ui30
-rw-r--r--testsuite/gtk/meson.build1
-rw-r--r--testsuite/gtk/stringlist.c249
11 files changed, 1000 insertions, 346 deletions
diff --git a/docs/reference/gtk/gtk4-docs.xml b/docs/reference/gtk/gtk4-docs.xml
index c0ad7bf691..4c57d0c60a 100644
--- a/docs/reference/gtk/gtk4-docs.xml
+++ b/docs/reference/gtk/gtk4-docs.xml
@@ -74,8 +74,9 @@
<xi:include href="xml/gtkmultiselection.xml" />
<xi:include href="xml/gtkpropertyselection.xml" />
</section>
- <xi:include href="xml/gtkdirectorylist.xml" />
<xi:include href="xml/gtkbookmarklist.xml" />
+ <xi:include href="xml/gtkdirectorylist.xml" />
+ <xi:include href="xml/gtkstringlist.xml" />
</chapter>
<chapter id="ListContainers">
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index 59df2f46e8..163a964973 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -7554,3 +7554,17 @@ GTK_TYPE_DROP_DOWN
<SUBSECTION Private>
gtk_drop_down_get_type
</SECTION>
+
+<SECTION>
+<FILE>gtkstringlist</FILE>
+<TITLE>GtkStringList</TITLE>
+GtkStringList
+gtk_string_list_new
+gtk_string_list_append
+gtk_string_list_take
+gtk_string_list_remove
+gtk_string_list_splice
+gtk_string_list_get_string
+GtkStringObject
+gtk_string_object_get_string
+</SECTION>
diff --git a/docs/reference/gtk/gtk4.types.in b/docs/reference/gtk/gtk4.types.in
index 332bb508d7..ac13151dca 100644
--- a/docs/reference/gtk/gtk4.types.in
+++ b/docs/reference/gtk/gtk4.types.in
@@ -210,6 +210,8 @@ gtk_stack_sidebar_get_type
gtk_stack_switcher_get_type
gtk_statusbar_get_type
gtk_string_filter_get_type
+gtk_string_list_get_type
+gtk_string_object_get_type
gtk_string_sorter_get_type
gtk_switch_get_type
gtk_level_bar_get_type
diff --git a/gtk/gtk.h b/gtk/gtk.h
index b7600866f9..0ab53bfd33 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -243,6 +243,7 @@
#include <gtk/gtkstackswitcher.h>
#include <gtk/gtkstatusbar.h>
#include <gtk/gtkstringfilter.h>
+#include <gtk/gtkstringlist.h>
#include <gtk/gtkstringsorter.h>
#include <gtk/gtkstylecontext.h>
#include <gtk/gtkstyleprovider.h>
diff --git a/gtk/gtkdropdown.c b/gtk/gtkdropdown.c
index ef151e7203..8430c1450b 100644
--- a/gtk/gtkdropdown.c
+++ b/gtk/gtkdropdown.c
@@ -44,6 +44,7 @@
#include "gtklistitem.h"
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
+#include "gtkstringlist.h"
/**
* SECTION:gtkdropdown
@@ -69,26 +70,6 @@
* useful if the list of options is long. To enable the search entry,
* use gtk_drop_down_set_enable_search().
*
- * # GtkDropDown as GtkBuildable
- *
- * The GtkDropDown implementation of the GtkBuildable interface supports
- * adding items directly using the <items> element and specifying <item>
- * elements for each item. Using <items> is equivalent to calling
- * gtk_drop_down_set_from_strings(). Each <item> element supports
- * the regular translation attributes “translatable”, “context”
- * and “comments”.
- *
- * Here is a UI definition fragment specifying GtkDropDown items:
- * |[
- * <object class="GtkDropDown">
- * <items>
- * <item translatable="yes">Factory</item>
- * <item translatable="yes">Home</item>
- * <item translatable="yes">Subway</item>
- * </items>
- * </object>
- * ]|
- *
* * # CSS nodes
*
* GtkDropDown has a single CSS node with name dropdown,
@@ -138,13 +119,7 @@ enum
N_PROPS
};
-static void gtk_drop_down_buildable_interface_init (GtkBuildableIface *iface);
-
-static GtkBuildableIface *buildable_parent_iface = NULL;
-
-G_DEFINE_TYPE_WITH_CODE (GtkDropDown, gtk_drop_down, GTK_TYPE_WIDGET,
- G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
- gtk_drop_down_buildable_interface_init))
+G_DEFINE_TYPE (GtkDropDown, gtk_drop_down, GTK_TYPE_WIDGET)
static GParamSpec *properties[N_PROPS] = { NULL, };
@@ -577,20 +552,26 @@ bind_item (GtkSignalListItemFactory *factory,
GtkWidget *label;
GValue value = G_VALUE_INIT;
- if (self->expression == NULL)
- {
- g_critical ("Either GtkDropDown::factory or GtkDropDown::expression must be set");
- return;
- }
-
item = gtk_list_item_get_item (list_item);
label = gtk_list_item_get_child (list_item);
- if (gtk_expression_evaluate (self->expression, item, &value))
+ if (self->expression &&
+ gtk_expression_evaluate (self->expression, item, &value))
{
gtk_label_set_label (GTK_LABEL (label), g_value_get_string (&value));
g_value_unset (&value);
}
+ else if (GTK_IS_STRING_OBJECT (item))
+ {
+ const char *string;
+
+ string = gtk_string_object_get_string (GTK_STRING_OBJECT (item));
+ gtk_label_set_label (GTK_LABEL (label), string);
+ }
+ else
+ {
+ g_critical ("Either GtkDropDown:factory or GtkDropDown:expression must be set");
+ }
}
static void
@@ -943,118 +924,6 @@ gtk_drop_down_get_expression (GtkDropDown *self)
return self->expression;
}
-
-#define GTK_TYPE_DROP_DOWN_STRING_HOLDER (gtk_drop_down_string_holder_get_type ())
-G_DECLARE_FINAL_TYPE (GtkDropDownStringHolder, gtk_drop_down_string_holder, GTK, DROP_DOWN_STRING_HOLDER, GObject)
-
-struct _GtkDropDownStringHolder {
- GObject parent_instance;
- char *string;
-};
-
-enum {
- PROP_STRING = 1,
- PROP_NUM_PROPERTIES
-};
-
-G_DEFINE_TYPE (GtkDropDownStringHolder, gtk_drop_down_string_holder, G_TYPE_OBJECT);
-
-static void
-gtk_drop_down_string_holder_init (GtkDropDownStringHolder *holder)
-{
-}
-
-static void
-gtk_drop_down_string_holder_finalize (GObject *object)
-{
- GtkDropDownStringHolder *holder = GTK_DROP_DOWN_STRING_HOLDER (object);
-
- g_free (holder->string);
-
- G_OBJECT_CLASS (gtk_drop_down_string_holder_parent_class)->finalize (object);
-}
-
-static void
-gtk_drop_down_string_holder_set_property (GObject *object,
- guint property_id,
- const GValue *value,
- GParamSpec *pspec)
-{
- GtkDropDownStringHolder *holder = GTK_DROP_DOWN_STRING_HOLDER (object);
-
- switch (property_id)
- {
- case PROP_STRING:
- g_free (holder->string);
- holder->string = g_value_dup_string (value);
- break;
-
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
- break;
- }
-}
-
-static void
-gtk_drop_down_string_holder_get_property (GObject *object,
- guint property_id,
- GValue *value,
- GParamSpec *pspec)
-{
- GtkDropDownStringHolder *holder = GTK_DROP_DOWN_STRING_HOLDER (object);
-
- switch (property_id)
- {
- case PROP_STRING:
- g_value_set_string (value, holder->string);
- break;
-
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
- break;
- }
-}
-
-static void
-gtk_drop_down_string_holder_class_init (GtkDropDownStringHolderClass *class)
-{
- GObjectClass *object_class = G_OBJECT_CLASS (class);
- GParamSpec *pspec;
-
- object_class->finalize = gtk_drop_down_string_holder_finalize;
- object_class->set_property = gtk_drop_down_string_holder_set_property;
- object_class->get_property = gtk_drop_down_string_holder_get_property;
-
- pspec = g_param_spec_string ("string", "String", "String",
- NULL,
- G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
-
- g_object_class_install_property (object_class, PROP_STRING, pspec);
-
-}
-
-static GtkDropDownStringHolder *
-gtk_drop_down_string_holder_new (const char *string)
-{
- return g_object_new (GTK_TYPE_DROP_DOWN_STRING_HOLDER, "string", string, NULL);
-}
-
-static GListModel *
-gtk_drop_down_strings_model_new (const char *const *text)
-{
- GListStore *store;
- int i;
-
- store = g_list_store_new (GTK_TYPE_DROP_DOWN_STRING_HOLDER);
- for (i = 0; text[i]; i++)
- {
- GtkDropDownStringHolder *holder = gtk_drop_down_string_holder_new (text[i]);
- g_list_store_append (store, holder);
- g_object_unref (holder);
- }
- return G_LIST_MODEL (store);
-}
-
/**
* gtk_drop_down_set_from_strings:
* @self: a #GtkDropDown
@@ -1067,7 +936,6 @@ void
gtk_drop_down_set_from_strings (GtkDropDown *self,
const char *const *texts)
{
- GtkExpression *expression;
GListModel *model;
g_return_if_fail (GTK_IS_DROP_DOWN (self));
@@ -1075,193 +943,7 @@ gtk_drop_down_set_from_strings (GtkDropDown *self,
set_default_factory (self);
- expression = gtk_property_expression_new (GTK_TYPE_DROP_DOWN_STRING_HOLDER, NULL, "string");
- gtk_drop_down_set_expression (self, expression);
- gtk_expression_unref (expression);
-
- model = gtk_drop_down_strings_model_new (texts);
+ model = G_LIST_MODEL (gtk_string_list_new ((const char **)texts));
gtk_drop_down_set_model (self, model);
g_object_unref (model);
}
-
-typedef struct {
- GtkBuilder *builder;
- GObject *object;
- const gchar *domain;
-
- gchar *context;
- guint translatable : 1;
- guint is_text : 1;
-
- GString *string;
- GPtrArray *strings;
-} ItemParserData;
-
-static void
-item_start_element (GtkBuildableParseContext *context,
- const gchar *element_name,
- const gchar **names,
- const gchar **values,
- gpointer user_data,
- GError **error)
-{
- ItemParserData *data = (ItemParserData*)user_data;
-
- if (strcmp (element_name, "items") == 0)
- {
- if (!_gtk_builder_check_parent (data->builder, context, "object", error))
- return;
-
- if (!g_markup_collect_attributes (element_name, names, values, error,
- G_MARKUP_COLLECT_INVALID, NULL, NULL,
- G_MARKUP_COLLECT_INVALID))
- _gtk_builder_prefix_error (data->builder, context, error);
- }
- else if (strcmp (element_name, "item") == 0)
- {
- gboolean translatable = FALSE;
- const gchar *msg_context = NULL;
-
- if (!_gtk_builder_check_parent (data->builder, context, "items", error))
- return;
-
- if (!g_markup_collect_attributes (element_name, names, values, error,
- G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "translatable", &translatable,
- G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "comments", NULL,
- G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "context", &msg_context,
- G_MARKUP_COLLECT_INVALID))
- {
- _gtk_builder_prefix_error (data->builder, context, error);
- return;
- }
-
- data->is_text = TRUE;
- data->translatable = translatable;
- data->context = g_strdup (msg_context);
- }
- else
- {
- _gtk_builder_error_unhandled_tag (data->builder, context,
- "GtkDropDown", element_name,
- error);
- }
-}
-
-static void
-item_text (GtkBuildableParseContext *context,
- const gchar *text,
- gsize text_len,
- gpointer user_data,
- GError **error)
-{
- ItemParserData *data = (ItemParserData*)user_data;
-
- if (data->is_text)
- g_string_append_len (data->string, text, text_len);
-}
-
-static void
-item_end_element (GtkBuildableParseContext *context,
- const gchar *element_name,
- gpointer user_data,
- GError **error)
-{
- ItemParserData *data = (ItemParserData*)user_data;
-
- /* Append the translated strings */
- if (data->string->len)
- {
- if (data->translatable)
- {
- const gchar *translated;
-
- translated = _gtk_builder_parser_translate (data->domain,
- data->context,
- data->string->str);
- g_string_assign (data->string, translated);
- }
-
- g_ptr_array_add (data->strings, g_strdup (data->string->str));
- }
-
- data->translatable = FALSE;
- g_string_set_size (data->string, 0);
- g_clear_pointer (&data->context, g_free);
- data->is_text = FALSE;
-}
-
-static const GtkBuildableParser item_parser =
-{
- item_start_element,
- item_end_element,
- item_text
-};
-
-static gboolean
-gtk_drop_down_buildable_custom_tag_start (GtkBuildable *buildable,
- GtkBuilder *builder,
- GObject *child,
- const gchar *tagname,
- GtkBuildableParser *parser,
- gpointer *parser_data)
-{
- if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
- tagname, parser, parser_data))
- return TRUE;
-
- if (strcmp (tagname, "items") == 0)
- {
- ItemParserData *data;
-
- data = g_slice_new0 (ItemParserData);
- data->builder = g_object_ref (builder);
- data->object = g_object_ref (G_OBJECT (buildable));
- data->domain = gtk_builder_get_translation_domain (builder);
- data->string = g_string_new ("");
- data->strings = g_ptr_array_new_with_free_func (g_free);
-
- *parser = item_parser;
- *parser_data = data;
-
- return TRUE;
- }
-
- return FALSE;
-}
-
-static void
-gtk_drop_down_buildable_custom_finished (GtkBuildable *buildable,
- GtkBuilder *builder,
- GObject *child,
- const gchar *tagname,
- gpointer user_data)
-{
- ItemParserData *data;
-
- buildable_parent_iface->custom_finished (buildable, builder, child,
- tagname, user_data);
-
- if (strcmp (tagname, "items") == 0)
- {
- data = (ItemParserData*)user_data;
-
- g_ptr_array_add (data->strings, NULL);
-
- gtk_drop_down_set_from_strings (GTK_DROP_DOWN (data->object), (const char **)data->strings->pdata);
-
- g_object_unref (data->object);
- g_object_unref (data->builder);
- g_string_free (data->string, TRUE);
- g_ptr_array_unref (data->strings);
- g_slice_free (ItemParserData, data);
- }
-}
-
-static void
-gtk_drop_down_buildable_interface_init (GtkBuildableIface *iface)
-{
- buildable_parent_iface = g_type_interface_peek_parent (iface);
-
- iface->custom_tag_start = gtk_drop_down_buildable_custom_tag_start;
- iface->custom_finished = gtk_drop_down_buildable_custom_finished;
-}
diff --git a/gtk/gtkstringlist.c b/gtk/gtkstringlist.c
new file mode 100644
index 0000000000..d4cad21ad3
--- /dev/null
+++ b/gtk/gtkstringlist.c
@@ -0,0 +1,620 @@
+/*
+ * Copyright © 2020 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen@redhat.com>
+ */
+
+#include "config.h"
+
+#include "gtkstringlist.h"
+
+#include "gtkbuildable.h"
+#include "gtkbuilderprivate.h"
+#include "gtkintl.h"
+#include "gtkprivate.h"
+
+/**
+ * SECTION:gtkstringlist
+ * @title: GtkStringList
+ * @short_description: A list model for strings
+ * @see_also: #GListModel
+ *
+ * #GtkStringList is a list model that wraps an array of strings.
+ *
+ * The objects in the model have a "string" property.
+ *
+ * # GtkStringList as GtkBuildable
+ *
+ * The GtkStringList implementation of the GtkBuildable interface
+ * supports adding items directly using the <items> element and
+ * specifying <item> elements for each item. Each <item> element
+ * supports the regular translation attributes “translatable”,
+ * “context” and “comments”.
+ *
+ * Here is a UI definition fragment specifying a GtkStringList
+ * |[
+ * <object class="GtkStringList">
+ * <items>
+ * <item translatable="yes">Factory</item>
+ * <item translatable="yes">Home</item>
+ * <item translatable="yes">Subway</item>
+ * </items>
+ * </object>
+ * ]|
+
+ */
+
+struct _GtkStringObject
+{
+ GObject parent_instance;
+ char *string;
+};
+
+enum {
+ PROP_STRING = 1,
+ PROP_NUM_PROPERTIES
+};
+
+G_DEFINE_TYPE (GtkStringObject, gtk_string_object, G_TYPE_OBJECT);
+
+static void
+gtk_string_object_init (GtkStringObject *object)
+{
+}
+
+static void
+gtk_string_object_finalize (GObject *object)
+{
+ GtkStringObject *self = GTK_STRING_OBJECT (object);
+
+ g_free (self->string);
+
+ G_OBJECT_CLASS (gtk_string_object_parent_class)->finalize (object);
+}
+
+static void
+gtk_string_object_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GtkStringObject *self = GTK_STRING_OBJECT (object);
+
+ switch (property_id)
+ {
+ case PROP_STRING:
+ g_free (self->string);
+ self->string = g_value_dup_string (value);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_string_object_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GtkStringObject *self = GTK_STRING_OBJECT (object);
+
+ switch (property_id)
+ {
+ case PROP_STRING:
+ g_value_set_string (value, self->string);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+gtk_string_object_class_init (GtkStringObjectClass *class)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+ GParamSpec *pspec;
+
+ object_class->finalize = gtk_string_object_finalize;
+ object_class->set_property = gtk_string_object_set_property;
+ object_class->get_property = gtk_string_object_get_property;
+
+ pspec = g_param_spec_string ("string", "String", "String",
+ NULL,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS);
+
+ g_object_class_install_property (object_class, PROP_STRING, pspec);
+
+}
+
+static GtkStringObject *
+gtk_string_object_new (const char *string)
+{
+ return g_object_new (GTK_TYPE_STRING_OBJECT, "string", string, NULL);
+}
+
+static GtkStringObject *
+gtk_string_object_new_take (char *string)
+{
+ GtkStringObject *obj;
+
+ obj = g_object_new (GTK_TYPE_STRING_OBJECT, NULL);
+ obj->string = string;
+
+ return obj;
+}
+
+/**
+ * gtk_string_object_get_string:
+ * @self: a #GtkStringObject
+ *
+ * Returns the string contained in a #GtkStringObject.
+ *
+ * Returns: the string of @self
+ */
+const char *
+gtk_string_object_get_string (GtkStringObject *self)
+{
+ g_return_val_if_fail (GTK_IS_STRING_OBJECT (self), NULL);
+
+ return self->string;
+}
+
+struct _GtkStringList
+{
+ GObject parent_instance;
+
+ GSequence *items;
+};
+
+struct _GtkStringListClass
+{
+ GObjectClass parent_class;
+};
+
+static GType
+gtk_string_list_get_item_type (GListModel *list)
+{
+ return G_TYPE_OBJECT;
+}
+
+static guint
+gtk_string_list_get_n_items (GListModel *list)
+{
+ GtkStringList *self = GTK_STRING_LIST (list);
+
+ return g_sequence_get_length (self->items);
+}
+
+static gpointer
+gtk_string_list_get_item (GListModel *list,
+ guint position)
+{
+ GtkStringList *self = GTK_STRING_LIST (list);
+ GSequenceIter *iter;
+
+ iter = g_sequence_get_iter_at_pos (self->items, position);
+
+ if (g_sequence_iter_is_end (iter))
+ return NULL;
+ else
+ return g_object_ref (g_sequence_get (iter));
+}
+
+static void
+gtk_string_list_model_init (GListModelInterface *iface)
+{
+ iface->get_item_type = gtk_string_list_get_item_type;
+ iface->get_n_items = gtk_string_list_get_n_items;
+ iface->get_item = gtk_string_list_get_item;
+}
+
+typedef struct
+{
+ GtkBuilder *builder;
+ GtkStringList *list;
+ GString *string;
+ const gchar *domain;
+ gchar *context;
+
+ guint translatable : 1;
+ guint is_text : 1;
+} ItemParserData;
+
+static void
+item_start_element (GtkBuildableParseContext *context,
+ const gchar *element_name,
+ const gchar **names,
+ const gchar **values,
+ gpointer user_data,
+ GError **error)
+{
+ ItemParserData *data = (ItemParserData*)user_data;
+
+ if (strcmp (element_name, "items") == 0)
+ {
+ if (!_gtk_builder_check_parent (data->builder, context, "object", error))
+ return;
+
+ if (!g_markup_collect_attributes (element_name, names, values, error,
+ G_MARKUP_COLLECT_INVALID, NULL, NULL,
+ G_MARKUP_COLLECT_INVALID))
+ _gtk_builder_prefix_error (data->builder, context, error);
+ }
+ else if (strcmp (element_name, "item") == 0)
+ {
+ gboolean translatable = FALSE;
+ const gchar *msg_context = NULL;
+
+ if (!_gtk_builder_check_parent (data->builder, context, "items", error))
+ return;
+
+ if (!g_markup_collect_attributes (element_name, names, values, error,
+ G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "translatable", &translatable,
+ G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "comments", NULL,
+ G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "context", &msg_context,
+ G_MARKUP_COLLECT_INVALID))
+ {
+ _gtk_builder_prefix_error (data->builder, context, error);
+ return;
+ }
+
+ data->is_text = TRUE;
+ data->translatable = translatable;
+ data->context = g_strdup (msg_context);
+ }
+ else
+ {
+ _gtk_builder_error_unhandled_tag (data->builder, context,
+ "GtkStringList", element_name,
+ error);
+ }
+}
+
+static void
+item_text (GtkBuildableParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error)
+{
+ ItemParserData *data = (ItemParserData*)user_data;
+
+ if (data->is_text)
+ g_string_append_len (data->string, text, text_len);
+}
+
+static void
+item_end_element (GtkBuildableParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error)
+{
+ ItemParserData *data = (ItemParserData*)user_data;
+
+ /* Append the translated strings */
+ if (data->string->len)
+ {
+ if (data->translatable)
+ {
+ const char *translated;
+
+ translated = _gtk_builder_parser_translate (data->domain,
+ data->context,
+ data->string->str);
+ g_string_assign (data->string, translated);
+ }
+
+ g_sequence_append (data->list->items, gtk_string_object_new (data->string->str));
+ }
+
+ data->translatable = FALSE;
+ g_string_set_size (data->string, 0);
+ g_clear_pointer (&data->context, g_free);
+ data->is_text = FALSE;
+}
+
+static const GtkBuildableParser item_parser =
+{
+ item_start_element,
+ item_end_element,
+ item_text
+};
+
+static gboolean
+gtk_string_list_buildable_custom_tag_start (GtkBuildable *buildable,
+ GtkBuilder *builder,
+ GObject *child,
+ const gchar *tagname,
+ GtkBuildableParser *parser,
+ gpointer *parser_data)
+{
+ if (strcmp (tagname, "items") == 0)
+ {
+ ItemParserData *data;
+
+ data = g_slice_new0 (ItemParserData);
+ data->builder = g_object_ref (builder);
+ data->list = g_object_ref (GTK_STRING_LIST (buildable));
+ data->domain = gtk_builder_get_translation_domain (builder);
+ data->string = g_string_new ("");
+
+ *parser = item_parser;
+ *parser_data = data;
+
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static void
+gtk_string_list_buildable_custom_finished (GtkBuildable *buildable,
+ GtkBuilder *builder,
+ GObject *child,
+ const gchar *tagname,
+ gpointer user_data)
+{
+ if (strcmp (tagname, "items") == 0)
+ {
+ ItemParserData *data;
+
+ data = (ItemParserData*)user_data;
+ g_object_unref (data->list);
+ g_object_unref (data->builder);
+ g_string_free (data->string, TRUE);
+ g_slice_free (ItemParserData, data);
+ }
+}
+
+static void
+gtk_string_list_buildable_init (GtkBuildableIface *iface)
+{
+ iface->custom_tag_start = gtk_string_list_buildable_custom_tag_start;
+ iface->custom_finished = gtk_string_list_buildable_custom_finished;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GtkStringList, gtk_string_list, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
+ gtk_string_list_buildable_init)
+ G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
+ gtk_string_list_model_init))
+
+static void
+gtk_string_list_dispose (GObject *object)
+{
+ GtkStringList *self = GTK_STRING_LIST (object);
+
+ g_clear_pointer (&self->items, g_sequence_free);
+
+ G_OBJECT_CLASS (gtk_string_list_parent_class)->dispose (object);
+}
+
+static void
+gtk_string_list_class_init (GtkStringListClass *class)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+
+ gobject_class->dispose = gtk_string_list_dispose;
+}
+
+static void
+gtk_string_list_init (GtkStringList *self)
+{
+ self->items = g_sequence_new (g_object_unref);
+}
+
+/**
+ * gtk_string_list_new:
+ * @strings: (allow-none): The strings to put in the model
+ *
+ * Creates a new #GtkStringList with the given @strings.
+ *
+ * Returns: a new #GtkStringList
+ **/
+GtkStringList *
+gtk_string_list_new (const char **strings)
+{
+ GtkStringList *self;
+ guint i;
+
+ self = g_object_new (GTK_TYPE_STRING_LIST, NULL);
+
+ for (i = 0; strings[i]; i++)
+ g_sequence_append (self->items, gtk_string_object_new (strings[i]));
+
+ return self;
+}
+
+/**
+ * gtk_string_list_splice:
+ * @self: a #GtkStringList
+ * @position: the position at which to make the change
+ * @n_removals: the number of strings to remove
+ * @additions: (array length=n_additions): the strings to add
+ * @n_additions: the number of items to add
+ *
+ * Changes @self by removing @n_removals strings and adding @n_additions
+ * strings to it.
+ *
+ * This function is more efficient than gtk_string_list_insert() and
+ * gtk_string_list_remove(), because it only emits
+ * #GListModel::items-changed once for the change.
+ *
+ * This function takes a ref on each item in @additions.
+ *
+ * The parameters @position and @n_removals must be correct (ie:
+ * @position + @n_removals must be less than or equal to the length
+ * of the list at the time this function is called).
+ */
+void
+gtk_string_list_splice (GtkStringList *self,
+ guint position,
+ guint n_removals,
+ const char **additions,
+ guint n_additions)
+{
+ GSequenceIter *it;
+ guint n_items;
+
+ g_return_if_fail (GTK_IS_STRING_LIST (self));
+ g_return_if_fail (position + n_removals >= position); /* overflow */
+
+ n_items = g_sequence_get_length (self->items);
+ g_return_if_fail (position + n_removals <= n_items);
+
+ it = g_sequence_get_iter_at_pos (self->items, position);
+
+ if (n_removals)
+ {
+ GSequenceIter *end;
+
+ end = g_sequence_iter_move (it, n_removals);
+ g_sequence_remove_range (it, end);
+
+ it = end;
+ }
+
+ if (n_additions)
+ {
+ gint i;
+
+ for (i = 0; i < n_additions; i++)
+ {
+ g_sequence_insert_before (it, gtk_string_object_new (additions[i]));
+ }
+ }
+
+ g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, n_additions);
+}
+
+/**
+ * gtk_string_list_append:
+ * @self: a #GtkStringList
+ * @string: the string to insert
+ *
+ * Appends @string to @self.
+ *
+ * The @string will be copied. See gtk_string_list_take()
+ * for a way to avoid that.
+ */
+void
+gtk_string_list_append (GtkStringList *self,
+ const char *string)
+{
+ guint n_items;
+
+ g_return_if_fail (GTK_IS_STRING_LIST (self));
+
+ n_items = g_sequence_get_length (self->items);
+ g_sequence_append (self->items, gtk_string_object_new (string));
+
+ g_list_model_items_changed (G_LIST_MODEL (self), n_items, 0, 1);
+}
+
+/**
+ * gtk_string_list_take:
+ * @self: a #GtkStringList
+ * @string: the string to insert
+ *
+ * Adds @string to self at the end, and takes
+ * ownership of it.
+ *
+ * This variant of gtk_string_list_append() is
+ * convenient for formatting strings:
+ *
+ * |[
+ * gtk_string_list_take (self, g_strdup_print ("%d dollars", lots));
+ * ]|
+ */
+void
+gtk_string_list_take (GtkStringList *self,
+ char *string)
+{
+ guint n_items;
+
+ g_return_if_fail (GTK_IS_STRING_LIST (self));
+
+ n_items = g_sequence_get_length (self->items);
+ g_sequence_append (self->items, gtk_string_object_new_take (string));
+
+ g_list_model_items_changed (G_LIST_MODEL (self), n_items, 0, 1);
+}
+
+/**
+ * gtk_string_list_remove:
+ * @self: a #GtkStringList
+ * @position: the position of the string that is to be removed
+ *
+ * Removes the string at @position from @self. @position must
+ * be smaller than the current length of the list.
+ */
+void
+gtk_string_list_remove (GtkStringList *self,
+ guint position)
+{
+ GSequenceIter *iter;
+
+ g_return_if_fail (GTK_IS_STRING_LIST (self));
+
+ iter = g_sequence_get_iter_at_pos (self->items, position);
+ g_return_if_fail (!g_sequence_iter_is_end (iter));
+
+ g_sequence_remove (iter);
+
+ g_list_model_items_changed (G_LIST_MODEL (self), position, 1, 0);
+}
+
+/**
+ * gtk_string_list_get_string:
+ * @self: a #GtkStringList
+ * @position:
+ *
+ * Gets the string that is at @position in @self. @position
+ * must be smaller than the current length of the list.
+ *
+ * This function returns the const char *. To get the
+ * object wrapping it, use g_list_model_get_item().
+ *
+ * Returns: the string at the given position
+ */
+const char *
+gtk_string_list_get_string (GtkStringList *self,
+ guint position)
+{
+ GSequenceIter *iter;
+
+ g_return_val_if_fail (GTK_IS_STRING_LIST (self), NULL);
+
+ iter = g_sequence_get_iter_at_pos (self->items, position);
+
+ if (g_sequence_iter_is_end (iter))
+ {
+ return NULL;
+ }
+ else
+ {
+ GtkStringObject *obj = g_sequence_get (iter);
+
+ return obj->string;
+ }
+}
diff --git a/gtk/gtkstringlist.h b/gtk/gtkstringlist.h
new file mode 100644
index 0000000000..31c788312a
--- /dev/null
+++ b/gtk/gtkstringlist.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright © 2020 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen@redhat.com>
+ */
+
+#ifndef __GTK_STRING_LIST_H__
+#define __GTK_STRING_LIST_H__
+
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gio/gio.h>
+/* for GDK_AVAILABLE_IN_ALL */
+#include <gdk/gdk.h>
+
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_STRING_OBJECT (gtk_string_object_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkStringObject, gtk_string_object, GTK, STRING_OBJECT, GObject)
+
+GDK_AVAILABLE_IN_ALL
+const char * gtk_string_object_get_string (GtkStringObject *self);
+
+#define GTK_TYPE_STRING_LIST (gtk_string_list_get_type ())
+
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkStringList, gtk_string_list, GTK, STRING_LIST, GObject)
+
+GDK_AVAILABLE_IN_ALL
+GtkStringList * gtk_string_list_new (const char **strings);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_string_list_append (GtkStringList *self,
+ const char *string);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_string_list_take (GtkStringList *self,
+ char *string);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_string_list_remove (GtkStringList *self,
+ guint position);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_string_list_splice (GtkStringList *self,
+ guint position,
+ guint n_removals,
+ const char **additions,
+ guint n_additions);
+
+GDK_AVAILABLE_IN_ALL
+const char * gtk_string_list_get_string (GtkStringList *self,
+ guint position);
+
+G_END_DECLS
+
+#endif /* __GTK_STRING_LIST_H__ */
diff --git a/gtk/meson.build b/gtk/meson.build
index 83048902a7..ac11bf9cb2 100644
--- a/gtk/meson.build
+++ b/gtk/meson.build
@@ -380,6 +380,7 @@ gtk_public_sources = files([
'gtkstackswitcher.c',
'gtkstatusbar.c',
'gtkstringfilter.c',
+ 'gtkstringlist.c',
'gtkstringsorter.c',
'gtkstylecontext.c',
'gtkstyleprovider.c',
diff --git a/gtk/ui/gtkprintunixdialog.ui b/gtk/ui/gtkprintunixdialog.ui
index 2910e526ea..6d086c9da8 100644
--- a/gtk/ui/gtkprintunixdialog.ui
+++ b/gtk/ui/gtkprintunixdialog.ui
@@ -511,11 +511,15 @@
<object class="GtkDropDown" id="page_set_combo">
<property name="valign">baseline</property>
<property name="selected">0</property>
- <items>
- <item translatable="yes">All sheets</item>
- <item translatable="yes">Even sheets</item>
- <item translatable="yes">Odd sheets</item>
- </items>
+ <property name="model">
+ <object class="GtkStringList">
+ <items>
+ <item translatable="yes">All sheets</item>
+ <item translatable="yes">Even sheets</item>
+ <item translatable="yes">Odd sheets</item>
+ </items>
+ </object>
+ </property>
<layout>
<property name="left-attach">1</property>
<property name="top-attach">3</property>
@@ -695,12 +699,16 @@
<property name="sensitive">0</property>
<property name="selected">0</property>
<property name="valign">baseline</property>
- <items>
- <item translatable="yes">Portrait</item>
- <item translatable="yes">Landscape</item>
- <item translatable="yes">Reverse portrait</item>
- <item translatable="yes">Reverse landscape</item>
- </items>
+ <property name="model">
+ <object class="GtkStringList">
+ <items>
+ <item translatable="yes">Portrait</item>
+ <item translatable="yes">Landscape</item>
+ <item translatable="yes">Reverse portrait</item>
+ <item translatable="yes">Reverse landscape</item>
+ </items>
+ </object>
+ </property>
<layout>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
diff --git a/testsuite/gtk/meson.build b/testsuite/gtk/meson.build
index 5946f7cfda..9859b11209 100644
--- a/testsuite/gtk/meson.build
+++ b/testsuite/gtk/meson.build
@@ -58,6 +58,7 @@ tests = [
['sorter'],
['sortlistmodel'],
['spinbutton'],
+ ['stringlist'],
['templates'],
['textbuffer'],
['textiter'],
diff --git a/testsuite/gtk/stringlist.c b/testsuite/gtk/stringlist.c
new file mode 100644
index 0000000000..5b6256f93f
--- /dev/null
+++ b/testsuite/gtk/stringlist.c
@@ -0,0 +1,249 @@
+/* GtkStringList tests
+ *
+ * Copyright (C) 2020, Red Hat, Inc.
+ * Authors: Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtk/gtk.h>
+
+static GQuark changes_quark;
+
+static char *
+model_to_string (GListModel *model)
+{
+ GtkStringList *self = GTK_STRING_LIST (model);
+ GString *string = g_string_new (NULL);
+ guint i;
+
+ for (i = 0; i < g_list_model_get_n_items (model); i++)
+ {
+ if (i > 0)
+ g_string_append (string, " ");
+ g_string_append_printf (string, "%s", gtk_string_list_get_string (self, i));
+ }
+
+ return g_string_free (string, FALSE);
+}
+
+#define assert_model(model, expected) G_STMT_START{ \
+ char *s = model_to_string (G_LIST_MODEL (model)); \
+ if (!g_str_equal (s, expected)) \
+ g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+ #model " == " #expected, s, "==", expected); \
+ g_free (s); \
+}G_STMT_END
+
+#define assert_changes(model, expected) G_STMT_START{ \
+ GString *changes = g_object_get_qdata (G_OBJECT (model), changes_quark); \
+ if (!g_str_equal (changes->str, expected)) \
+ g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
+ #model " == " #expected, changes->str, "==", expected); \
+ g_string_set_size (changes, 0); \
+}G_STMT_END
+
+static void
+items_changed (GListModel *model,
+ guint position,
+ guint removed,
+ guint added,
+ GString *changes)
+{
+ g_assert (removed != 0 || added != 0);
+
+ if (changes->len)
+ g_string_append (changes, ", ");
+
+ if (removed == 1 && added == 0)
+ {
+ g_string_append_printf (changes, "-%u", position);
+ }
+ else if (removed == 0 && added == 1)
+ {
+ g_string_append_printf (changes, "+%u", position);
+ }
+ else
+ {
+ g_string_append_printf (changes, "%u", position);
+ if (removed > 0)
+ g_string_append_printf (changes, "-%u", removed);
+ if (added > 0)
+ g_string_append_printf (changes, "+%u", added);
+ }
+}
+
+static void
+free_changes (gpointer data)
+{
+ GString *changes = data;
+
+ /* all changes must have been checked via assert_changes() before */
+ g_assert_cmpstr (changes->str, ==, "");
+
+ g_string_free (changes, TRUE);
+}
+
+static GtkStringList *
+new_model (const char **strings)
+{
+ GtkStringList *result;
+ GString *changes;
+
+ result = gtk_string_list_new (strings);
+ changes = g_string_new ("");
+ g_object_set_qdata_full (G_OBJECT(result), changes_quark, changes, free_changes);
+ g_signal_connect (result, "items-changed", G_CALLBACK (items_changed), changes);
+
+ return result;
+}
+
+static void
+test_create_empty (void)
+{
+ GtkStringList *list;
+
+ list = new_model ((const char *[]){ NULL });
+
+ assert_model (list, "");
+ assert_changes (list, "");
+
+ g_object_unref (list);
+}
+
+static void
+test_create_strv (void)
+{
+ GtkStringList *list;
+
+ list = new_model ((const char *[]){ "a", "b", "c", NULL });
+
+ assert_model (list, "a b c");
+ assert_changes (list, "");
+
+ g_object_unref (list);
+}
+
+static void
+test_create_builder (void)
+{
+ const char *ui =
+"<interface>"
+" <object class=\"GtkStringList\" id=\"list\">"
+" <items>"
+" <item translatable=\"yes\" context=\"ctx\" comments=\"none\">a</item>"
+" <item>b</item>"
+" <item>c</item>"
+" </items>"
+" </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 (list);
+}
+
+static void
+test_get_string (void)
+{
+ GtkStringList *list;
+
+ list = new_model ((const char *[]){ "a", "b", "c", NULL });
+
+ assert_model (list, "a b c");
+
+ g_assert_cmpstr (gtk_string_list_get_string (list, 0), ==, "a");
+ g_assert_cmpstr (gtk_string_list_get_string (list, 1), ==, "b");
+ g_assert_cmpstr (gtk_string_list_get_string (list, 2), ==, "c");
+ g_assert_null (gtk_string_list_get_string (list, 3));
+
+ g_object_unref (list);
+}
+
+static void
+test_splice (void)
+{
+ GtkStringList *list;
+
+ list = new_model ((const char *[]){ "a", "b", "c", "d", "e", NULL });
+
+ assert_model (list, "a b c d e");
+
+ gtk_string_list_splice (list, 2, 2, (const char *[]){ "x", "y", "z" }, 3);
+
+ assert_model (list, "a b x y z e");
+ assert_changes (list, "2-2+3");
+
+ g_object_unref (list);
+}
+
+static void
+test_add_remove (void)
+{
+ GtkStringList *list;
+
+ list = new_model ((const char *[]){ "a", "b", "c", "d", "e", NULL });
+
+ assert_model (list, "a b c d e");
+
+ gtk_string_list_remove (list, 2);
+
+ assert_model (list, "a b d e");
+ assert_changes (list, "-2");
+
+ gtk_string_list_append (list, "x");
+
+ assert_model (list, "a b d e x");
+ assert_changes (list, "+4");
+
+ g_object_unref (list);
+}
+
+static void
+test_take (void)
+{
+ GtkStringList *list;
+
+ list = new_model ((const char *[]){ NULL });
+
+ assert_model (list, "");
+
+ gtk_string_list_take (list, g_strdup_printf ("%d dollars", (int)1e6));
+ assert_model (list, "1000000 dollars");
+ assert_changes (list, "+0");
+
+ g_object_unref (list);
+}
+
+int
+main (int argc, char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ changes_quark = g_quark_from_static_string ("What did I see? Can I believe what I saw?");
+
+ 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/get_string", test_get_string);
+ g_test_add_func ("/stringlist/splice", test_splice);
+ g_test_add_func ("/stringlist/add_remove", test_add_remove);
+ g_test_add_func ("/stringlist/take", test_take);
+
+ return g_test_run ();
+}