2020-06-21 22:00:29 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
/**
|
2021-02-28 01:06:26 +00:00
|
|
|
* GtkStringList:
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* `GtkStringList` is a list model that wraps an array of strings.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
|
|
|
* The objects in the model have a "string" property.
|
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* `GtkStringList` is well-suited for any place where you would
|
2020-07-02 17:01:48 +00:00
|
|
|
* typically use a `char*[]`, but need a list model.
|
|
|
|
*
|
2020-06-21 22:00:29 +00:00
|
|
|
* # GtkStringList as GtkBuildable
|
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* The `GtkStringList` implementation of the `GtkBuildable` interface
|
2020-06-21 22:00:29 +00:00
|
|
|
* 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”.
|
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Here is a UI definition fragment specifying a `GtkStringList`
|
|
|
|
*
|
|
|
|
* ```xml
|
2020-06-21 22:00:29 +00:00
|
|
|
* <object class="GtkStringList">
|
|
|
|
* <items>
|
|
|
|
* <item translatable="yes">Factory</item>
|
|
|
|
* <item translatable="yes">Home</item>
|
|
|
|
* <item translatable="yes">Subway</item>
|
|
|
|
* </items>
|
|
|
|
* </object>
|
2021-02-28 01:06:26 +00:00
|
|
|
* ```
|
2020-06-21 22:00:29 +00:00
|
|
|
*/
|
|
|
|
|
2021-03-01 06:41:44 +00:00
|
|
|
/**
|
|
|
|
* GtkStringObject:
|
|
|
|
*
|
|
|
|
* `GtkStringObject` is the type of items in a `GtkStringList`.
|
|
|
|
*
|
|
|
|
* A `GtkStringObject` is a wrapper around a `const char*`; it has
|
|
|
|
* a [property@Gtk.StringObject:string] property.
|
|
|
|
*/
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
#define GDK_ARRAY_ELEMENT_TYPE GtkStringObject *
|
|
|
|
#define GDK_ARRAY_NAME objects
|
|
|
|
#define GDK_ARRAY_TYPE_NAME Objects
|
|
|
|
#define GDK_ARRAY_FREE_FUNC g_object_unref
|
|
|
|
#include "gdk/gdkarrayimpl.c"
|
|
|
|
|
2020-06-21 22:00:29 +00:00
|
|
|
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_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->get_property = gtk_string_object_get_property;
|
|
|
|
|
2021-02-28 01:06:26 +00:00
|
|
|
/**
|
|
|
|
* GtkStringObject:string: (attributes org.gtk.Property.get=gtk_string_object_get_string)
|
|
|
|
*
|
|
|
|
* The string.
|
|
|
|
*/
|
2022-05-11 12:19:39 +00:00
|
|
|
pspec = g_param_spec_string ("string", NULL, NULL,
|
2020-06-21 22:00:29 +00:00
|
|
|
NULL,
|
2020-06-29 22:35:25 +00:00
|
|
|
G_PARAM_READABLE |
|
2020-06-21 22:00:29 +00:00
|
|
|
G_PARAM_STATIC_STRINGS);
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_STRING, pspec);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-04 15:02:44 +00:00
|
|
|
/**
|
|
|
|
* gtk_string_object_new:
|
2020-07-07 11:03:44 +00:00
|
|
|
* @string: (not nullable): The string to wrap
|
2020-07-04 15:02:44 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Wraps a string in an object for use with `GListModel`.
|
2020-07-04 15:02:44 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Returns: a new `GtkStringObject`
|
|
|
|
*/
|
2020-07-04 15:02:44 +00:00
|
|
|
GtkStringObject *
|
2020-06-30 17:35:04 +00:00
|
|
|
gtk_string_object_new (const char *string)
|
|
|
|
{
|
|
|
|
return gtk_string_object_new_take (g_strdup (string));
|
|
|
|
}
|
|
|
|
|
2020-06-21 22:00:29 +00:00
|
|
|
/**
|
2021-02-28 01:06:26 +00:00
|
|
|
* gtk_string_object_get_string: (attributes org.gtk.Method.get_property=string)
|
|
|
|
* @self: a `GtkStringObject`
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Returns the string contained in a `GtkStringObject`.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
Objects items;
|
2020-06-21 22:00:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
return objects_get_size (&self->items);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gtk_string_list_get_item (GListModel *list,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
GtkStringList *self = GTK_STRING_LIST (list);
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
if (position >= objects_get_size (&self->items))
|
2020-06-21 22:00:29 +00:00
|
|
|
return NULL;
|
2020-07-03 23:13:39 +00:00
|
|
|
|
|
|
|
return g_object_ref (objects_get (&self->items, position));
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *domain;
|
|
|
|
char *context;
|
2020-06-21 22:00:29 +00:00
|
|
|
|
|
|
|
guint translatable : 1;
|
|
|
|
guint is_text : 1;
|
|
|
|
} ItemParserData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
item_start_element (GtkBuildableParseContext *context,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *element_name,
|
|
|
|
const char **names,
|
|
|
|
const char **values,
|
2020-06-21 22:00:29 +00:00
|
|
|
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;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *msg_context = NULL;
|
2020-06-21 22:00:29 +00:00
|
|
|
|
|
|
|
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,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *text,
|
2020-06-21 22:00:29 +00:00
|
|
|
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,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *element_name,
|
2020-06-21 22:00:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
gtk_string_list_append (data->list, data->string->str);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *tagname,
|
2020-06-21 22:00:29 +00:00
|
|
|
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,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *tagname,
|
2020-06-21 22:00:29 +00:00
|
|
|
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);
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
objects_clear (&self->items);
|
2020-06-21 22:00:29 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-07-03 23:13:39 +00:00
|
|
|
objects_init (&self->items);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_string_list_new:
|
2020-06-25 21:10:37 +00:00
|
|
|
* @strings: (array zero-terminated=1) (nullable): The strings to put in the model
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Creates a new `GtkStringList` with the given @strings.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Returns: a new `GtkStringList`
|
2020-06-25 21:10:37 +00:00
|
|
|
*/
|
2020-06-21 22:00:29 +00:00
|
|
|
GtkStringList *
|
2020-06-27 04:31:18 +00:00
|
|
|
gtk_string_list_new (const char * const *strings)
|
2020-06-21 22:00:29 +00:00
|
|
|
{
|
|
|
|
GtkStringList *self;
|
|
|
|
|
|
|
|
self = g_object_new (GTK_TYPE_STRING_LIST, NULL);
|
|
|
|
|
2020-06-30 21:31:02 +00:00
|
|
|
gtk_string_list_splice (self, 0, 0, strings);
|
2020-06-21 22:00:29 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_string_list_splice:
|
2021-02-28 01:06:26 +00:00
|
|
|
* @self: a `GtkStringList`
|
2020-06-21 22:00:29 +00:00
|
|
|
* @position: the position at which to make the change
|
|
|
|
* @n_removals: the number of strings to remove
|
2020-06-30 17:36:51 +00:00
|
|
|
* @additions: (array zero-terminated=1) (nullable): The strings to add
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2020-06-30 17:36:51 +00:00
|
|
|
* Changes @self by removing @n_removals strings and adding @additions
|
|
|
|
* to it.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* This function is more efficient than [method@Gtk.StringList.append]
|
|
|
|
* and [method@Gtk.StringList.remove], because it only emits the
|
|
|
|
* ::items-changed signal once for the change.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2020-08-03 22:41:36 +00:00
|
|
|
* This function copies the strings in @additions.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
|
|
|
* 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
|
2020-06-30 17:36:51 +00:00
|
|
|
gtk_string_list_splice (GtkStringList *self,
|
|
|
|
guint position,
|
|
|
|
guint n_removals,
|
|
|
|
const char * const *additions)
|
2020-06-21 22:00:29 +00:00
|
|
|
{
|
2020-07-03 23:13:39 +00:00
|
|
|
guint i, n_additions;
|
2020-06-21 22:00:29 +00:00
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_STRING_LIST (self));
|
|
|
|
g_return_if_fail (position + n_removals >= position); /* overflow */
|
2020-07-03 23:13:39 +00:00
|
|
|
g_return_if_fail (position + n_removals <= objects_get_size (&self->items));
|
2020-06-21 22:00:29 +00:00
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
if (additions)
|
|
|
|
n_additions = g_strv_length ((char **) additions);
|
|
|
|
else
|
|
|
|
n_additions = 0;
|
2020-06-21 22:00:29 +00:00
|
|
|
|
2020-12-23 19:04:29 +00:00
|
|
|
objects_splice (&self->items, position, n_removals, FALSE, NULL, n_additions);
|
2020-06-21 22:00:29 +00:00
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
for (i = 0; i < n_additions; i++)
|
2020-06-21 22:00:29 +00:00
|
|
|
{
|
2020-07-03 23:13:39 +00:00
|
|
|
*objects_index (&self->items, position + i) = gtk_string_object_new (additions[i]);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
if (n_removals || n_additions)
|
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, n_additions);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_string_list_append:
|
2021-02-28 01:06:26 +00:00
|
|
|
* @self: a `GtkStringList`
|
2020-06-21 22:00:29 +00:00
|
|
|
* @string: the string to insert
|
|
|
|
*
|
|
|
|
* Appends @string to @self.
|
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* The @string will be copied. See
|
|
|
|
* [method@Gtk.StringList.take] for a way to avoid that.
|
2020-06-21 22:00:29 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_string_list_append (GtkStringList *self,
|
|
|
|
const char *string)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_STRING_LIST (self));
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
objects_append (&self->items, gtk_string_object_new (string));
|
2020-06-21 22:00:29 +00:00
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), objects_get_size (&self->items) - 1, 0, 1);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_string_list_take:
|
2021-02-28 01:06:26 +00:00
|
|
|
* @self: a `GtkStringList`
|
2020-06-25 21:10:37 +00:00
|
|
|
* @string: (transfer full): the string to insert
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
|
|
|
* Adds @string to self at the end, and takes
|
|
|
|
* ownership of it.
|
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* This variant of [method@Gtk.StringList.append]
|
|
|
|
* is convenient for formatting strings:
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* ```c
|
2020-06-21 22:00:29 +00:00
|
|
|
* gtk_string_list_take (self, g_strdup_print ("%d dollars", lots));
|
2021-02-28 01:06:26 +00:00
|
|
|
* ```
|
2020-06-21 22:00:29 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_string_list_take (GtkStringList *self,
|
|
|
|
char *string)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_STRING_LIST (self));
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
objects_append (&self->items, gtk_string_object_new_take (string));
|
2020-06-21 22:00:29 +00:00
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
g_list_model_items_changed (G_LIST_MODEL (self), objects_get_size (&self->items) - 1, 0, 1);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_string_list_remove:
|
2021-02-28 01:06:26 +00:00
|
|
|
* @self: a `GtkStringList`
|
2020-06-21 22:00:29 +00:00
|
|
|
* @position: the position of the string that is to be removed
|
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Removes the string at @position from @self.
|
|
|
|
*
|
|
|
|
* @position must be smaller than the current
|
|
|
|
* length of the list.
|
2020-06-21 22:00:29 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gtk_string_list_remove (GtkStringList *self,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_STRING_LIST (self));
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
gtk_string_list_splice (self, position, 1, NULL);
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_string_list_get_string:
|
2021-02-28 01:06:26 +00:00
|
|
|
* @self: a `GtkStringList`
|
2020-06-28 16:41:57 +00:00
|
|
|
* @position: the position to get the string for
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
2021-02-28 01:06:26 +00:00
|
|
|
* Gets the string that is at @position in @self.
|
|
|
|
*
|
|
|
|
* If @self does not contain @position items, %NULL is returned.
|
2020-06-21 22:00:29 +00:00
|
|
|
*
|
|
|
|
* This function returns the const char *. To get the
|
|
|
|
* object wrapping it, use g_list_model_get_item().
|
|
|
|
*
|
2020-11-24 20:41:18 +00:00
|
|
|
* Returns: (nullable): the string at the given position
|
2020-06-21 22:00:29 +00:00
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
gtk_string_list_get_string (GtkStringList *self,
|
|
|
|
guint position)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GTK_IS_STRING_LIST (self), NULL);
|
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
if (position >= objects_get_size (&self->items))
|
|
|
|
return NULL;
|
2020-06-21 22:00:29 +00:00
|
|
|
|
2020-07-03 23:13:39 +00:00
|
|
|
return objects_get (&self->items, position)->string;
|
2020-06-21 22:00:29 +00:00
|
|
|
}
|