gtk4/auto/string_list.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, Buildable};
6use glib::translate::*;
7
8glib::wrapper! {
9 /// [`StringList`][crate::StringList] is a list model that wraps an array of strings.
10 ///
11 /// The objects in the model are of type [`StringObject`][crate::StringObject] and have
12 /// a "string" property that can be used inside expressions.
13 ///
14 /// [`StringList`][crate::StringList] is well-suited for any place where you would
15 /// typically use a `char*[]`, but need a list model.
16 ///
17 /// ## GtkStringList as GtkBuildable
18 ///
19 /// The [`StringList`][crate::StringList] implementation of the [`Buildable`][crate::Buildable] interface
20 /// supports adding items directly using the `<items>` element and
21 /// specifying `<item>` elements for each item. Each `<item>` element
22 /// supports the regular translation attributes “translatable”,
23 /// “context” and “comments”.
24 ///
25 /// Here is a UI definition fragment specifying a [`StringList`][crate::StringList]
26 ///
27 /// ```xml
28 /// <object class="GtkStringList">
29 /// <items>
30 /// <item translatable="yes">Factory</item>
31 /// <item translatable="yes">Home</item>
32 /// <item translatable="yes">Subway</item>
33 /// </items>
34 /// </object>
35 /// ```
36 ///
37 /// ## Properties
38 ///
39 ///
40 /// #### `item-type`
41 /// The type of items. See [`ListModelExtManual::item_type()`][crate::gio::prelude::ListModelExtManual::item_type()].
42 ///
43 /// Readable
44 ///
45 ///
46 /// #### `n-items`
47 /// The number of items. See [`ListModelExtManual::n_items()`][crate::gio::prelude::ListModelExtManual::n_items()].
48 ///
49 /// Readable
50 ///
51 ///
52 /// #### `strings`
53 /// The strings in the model.
54 ///
55 /// Writeable | Construct Only
56 ///
57 /// # Implements
58 ///
59 /// [`trait@glib::ObjectExt`], [`trait@gio::prelude::ListModelExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt]
60 #[doc(alias = "GtkStringList")]
61 pub struct StringList(Object<ffi::GtkStringList, ffi::GtkStringListClass>) @implements gio::ListModel, Buildable;
62
63 match fn {
64 type_ => || ffi::gtk_string_list_get_type(),
65 }
66}
67
68impl StringList {
69 /// Creates a new [`StringList`][crate::StringList] with the given @strings.
70 /// ## `strings`
71 /// The strings to put in the model
72 ///
73 /// # Returns
74 ///
75 /// a new [`StringList`][crate::StringList]
76 #[doc(alias = "gtk_string_list_new")]
77 pub fn new(strings: &[&str]) -> StringList {
78 assert_initialized_main_thread!();
79 unsafe { from_glib_full(ffi::gtk_string_list_new(strings.to_glib_none().0)) }
80 }
81
82 /// Appends @string to @self.
83 ///
84 /// The @string will be copied. See
85 /// `Gtk::StringList::take()` for a way to avoid that.
86 /// ## `string`
87 /// the string to insert
88 #[doc(alias = "gtk_string_list_append")]
89 pub fn append(&self, string: &str) {
90 unsafe {
91 ffi::gtk_string_list_append(self.to_glib_none().0, string.to_glib_none().0);
92 }
93 }
94
95 /// Gets the position of the @string in @self.
96 ///
97 /// If @self does not contain @string item, `G_MAXUINT` is returned.
98 /// ## `string`
99 /// the string to find
100 ///
101 /// # Returns
102 ///
103 /// the position of the string
104 #[cfg(feature = "v4_18")]
105 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
106 #[doc(alias = "gtk_string_list_find")]
107 pub fn find(&self, string: &str) -> u32 {
108 unsafe { ffi::gtk_string_list_find(self.to_glib_none().0, string.to_glib_none().0) }
109 }
110
111 /// Gets the string that is at @position in @self.
112 ///
113 /// If @self does not contain @position items, [`None`] is returned.
114 ///
115 /// This function returns the const char *. To get the
116 /// object wrapping it, use g_list_model_get_item().
117 /// ## `position`
118 /// the position to get the string for
119 ///
120 /// # Returns
121 ///
122 /// the string at the given position
123 #[doc(alias = "gtk_string_list_get_string")]
124 #[doc(alias = "get_string")]
125 pub fn string(&self, position: u32) -> Option<glib::GString> {
126 unsafe {
127 from_glib_none(ffi::gtk_string_list_get_string(
128 self.to_glib_none().0,
129 position,
130 ))
131 }
132 }
133
134 /// Removes the string at @position from @self.
135 ///
136 /// @position must be smaller than the current
137 /// length of the list.
138 /// ## `position`
139 /// the position of the string that is to be removed
140 #[doc(alias = "gtk_string_list_remove")]
141 pub fn remove(&self, position: u32) {
142 unsafe {
143 ffi::gtk_string_list_remove(self.to_glib_none().0, position);
144 }
145 }
146
147 /// Changes @self by removing @n_removals strings and adding @additions
148 /// to it.
149 ///
150 /// This function is more efficient than [`append()`][Self::append()]
151 /// and [`remove()`][Self::remove()], because it only emits the
152 /// ::items-changed signal once for the change.
153 ///
154 /// This function copies the strings in @additions.
155 ///
156 /// The parameters @position and @n_removals must be correct (ie:
157 /// @position + @n_removals must be less than or equal to the length
158 /// of the list at the time this function is called).
159 /// ## `position`
160 /// the position at which to make the change
161 /// ## `n_removals`
162 /// the number of strings to remove
163 /// ## `additions`
164 /// The strings to add
165 #[doc(alias = "gtk_string_list_splice")]
166 pub fn splice(&self, position: u32, n_removals: u32, additions: &[&str]) {
167 unsafe {
168 ffi::gtk_string_list_splice(
169 self.to_glib_none().0,
170 position,
171 n_removals,
172 additions.to_glib_none().0,
173 );
174 }
175 }
176}