[gtk/stringlist] fixup! Add GtkStringList as public api
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/stringlist] fixup! Add GtkStringList as public api
- Date: Tue, 23 Jun 2020 01:56:16 +0000 (UTC)
commit 08a9a9c7d99e70cfc9431d6cad2139d95d7cb32c
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Jun 22 21:53:36 2020 -0400
fixup! Add GtkStringList as public api
Add more stringlist api
docs/reference/gtk/gtk4-sections.txt | 4 ++
gtk/gtkstringlist.c | 122 ++++++++++++++++++++++++++++++++---
gtk/gtkstringlist.h | 20 +++++-
3 files changed, 134 insertions(+), 12 deletions(-)
---
diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt
index fc131ec9fa..015cfe623d 100644
--- a/docs/reference/gtk/gtk4-sections.txt
+++ b/docs/reference/gtk/gtk4-sections.txt
@@ -7560,6 +7560,10 @@ gtk_drop_down_get_type
<TITLE>GtkStringList</TITLE>
GtkStringList
gtk_string_list_new
+gtk_string_list_new_from_strv
+gtk_string_list_insert
+gtk_string_list_remove
+gtk_string_list_get_string
GtkStringObject
gtk_string_object_get_string
</SECTION>
diff --git a/gtk/gtkstringlist.c b/gtk/gtkstringlist.c
index 3105a78859..ea6fea0c5b 100644
--- a/gtk/gtkstringlist.c
+++ b/gtk/gtkstringlist.c
@@ -393,7 +393,7 @@ gtk_string_list_init (GtkStringList *self)
}
/**
- * gtk_string_list_new:
+ * gtk_string_list_new_from_strv:
* @strings: (allow-none): The strings to put in the model
* @length: length @strings, or -1 if @strings i %NULL-terminated
*
@@ -402,20 +402,122 @@ gtk_string_list_init (GtkStringList *self)
* Returns: a new #GtkStringList
**/
GtkStringList *
-gtk_string_list_new (const char * const *strings,
- int length)
+gtk_string_list_new_from_strv (const char **strings)
{
- GtkStringList *list;
+ 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_new:
+ * @string1: first string to add
+ * @...: a %NULL-terminated list fo strings, starting with @string1
+ *
+ * Creates a new #GtkStringList with the given strings.
+ *
+ * Returns: a new #GtkStringList
+ */
+GtkStringList *
+gtk_string_list_new (const char *string1,
+ ...)
+{
+ GtkStringList *self;
+ const char *s;
+ va_list args;
- list = g_object_new (GTK_TYPE_STRING_LIST, NULL);
+ self = g_object_new (GTK_TYPE_STRING_LIST, NULL);
- if (strings)
+ va_start (args, string1);
+ s = string1;
+ while (s)
{
- guint i;
+ g_sequence_append (self->items, gtk_string_object_new (s));
+ s = va_arg (args, const char *);
+ }
+ va_end (args);
+
+ return self;
+}
+
+/**
+ * gtk_string_list_insert:
+ * @self: a #GtkStringList
+ * @position: the position at which to insert @string
+ * @string: the string to insert
+ *
+ * Inserts @string into @self at @position.
+ */
+void
+gtk_string_list_insert (GtkStringList *self,
+ guint position,
+ const char *string)
+{
+ GSequenceIter *iter;
+
+ iter = g_sequence_get_iter_at_pos (self->items, position);
+ g_sequence_insert_before (iter, gtk_string_object_new (string));
+
+ g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
+}
- for (i = 0; (length == -1 || i < length) && strings[i]; i++)
- g_sequence_append (list->items, gtk_string_object_new (strings[i]));
+/**
+ * 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;
+
+ 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;
+
+ 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 list;
+ return obj->string;
+ }
}
diff --git a/gtk/gtkstringlist.h b/gtk/gtkstringlist.h
index 0c2a2fb917..2aafd5d812 100644
--- a/gtk/gtkstringlist.h
+++ b/gtk/gtkstringlist.h
@@ -45,8 +45,24 @@ 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 * const *strings,
- int length);
+GtkStringList * gtk_string_list_new (const char *string1,
+ ...) G_GNUC_NULL_TERMINATED;
+
+GDK_AVAILABLE_IN_ALL
+GtkStringList * gtk_string_list_new_from_strv (const char **strings);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_string_list_insert (GtkStringList *self,
+ guint position,
+ const char *string);
+
+GDK_AVAILABLE_IN_ALL
+void gtk_string_list_remove (GtkStringList *self,
+ guint position);
+
+GDK_AVAILABLE_IN_ALL
+const char * gtk_string_list_get_string (GtkStringList *self,
+ guint position);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]