pango/auto/attr_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, Attribute};
6use glib::translate::*;
7
8glib::wrapper! {
9 /// A [`AttrList`][crate::AttrList] represents a list of attributes that apply to a section
10 /// of text.
11 ///
12 /// The attributes in a [`AttrList`][crate::AttrList] are, in general, allowed to overlap in
13 /// an arbitrary fashion. However, if the attributes are manipulated only through
14 /// [`change()`][Self::change()], the overlap between properties will meet
15 /// stricter criteria.
16 ///
17 /// Since the [`AttrList`][crate::AttrList] structure is stored as a linear list, it is not
18 /// suitable for storing attributes for large amounts of text. In general, you
19 /// should not use a single [`AttrList`][crate::AttrList] for more than one paragraph of text.
20 #[derive(Debug)]
21 pub struct AttrList(Shared<ffi::PangoAttrList>);
22
23 match fn {
24 ref => |ptr| ffi::pango_attr_list_ref(ptr),
25 unref => |ptr| ffi::pango_attr_list_unref(ptr),
26 type_ => || ffi::pango_attr_list_get_type(),
27 }
28}
29
30impl AttrList {
31 /// Create a new empty attribute list with a reference
32 /// count of one.
33 ///
34 /// # Returns
35 ///
36 /// the newly allocated
37 /// [`AttrList`][crate::AttrList], which should be freed with
38 /// `Pango::AttrList::unref()`
39 #[doc(alias = "pango_attr_list_new")]
40 pub fn new() -> AttrList {
41 unsafe { from_glib_full(ffi::pango_attr_list_new()) }
42 }
43
44 #[doc(alias = "pango_attr_list_copy")]
45 #[must_use]
46 pub fn copy(&self) -> Option<AttrList> {
47 unsafe { from_glib_full(ffi::pango_attr_list_copy(self.to_glib_none().0)) }
48 }
49
50 /// Given a [`AttrList`][crate::AttrList] and callback function, removes
51 /// any elements of @self for which @func returns [`true`] and
52 /// inserts them into a new list.
53 /// ## `func`
54 /// callback function;
55 /// returns [`true`] if an attribute should be filtered out
56 ///
57 /// # Returns
58 ///
59 /// the new
60 /// [`AttrList`][crate::AttrList] or [`None`] if no attributes of the
61 /// given types were found
62 #[doc(alias = "pango_attr_list_filter")]
63 #[must_use]
64 pub fn filter<P: FnMut(&Attribute) -> bool>(&self, func: P) -> Option<AttrList> {
65 let mut func_data: P = func;
66 unsafe extern "C" fn func_func<P: FnMut(&Attribute) -> bool>(
67 attribute: *mut ffi::PangoAttribute,
68 user_data: glib::ffi::gpointer,
69 ) -> glib::ffi::gboolean {
70 let attribute = from_glib_borrow(attribute);
71 let callback = user_data as *mut P;
72 (*callback)(&attribute).into_glib()
73 }
74 let func = Some(func_func::<P> as _);
75 let super_callback0: &mut P = &mut func_data;
76 unsafe {
77 from_glib_full(ffi::pango_attr_list_filter(
78 self.to_glib_none().0,
79 func,
80 super_callback0 as *mut _ as *mut _,
81 ))
82 }
83 }
84
85 /// Gets a list of all attributes in @self.
86 ///
87 /// # Returns
88 ///
89 ///
90 /// a list of all attributes in @self. To free this value,
91 /// call `Pango::Attribute::destroy()` on each value and
92 /// g_slist_free() on the list.
93 #[cfg(feature = "v1_44")]
94 #[cfg_attr(docsrs, doc(cfg(feature = "v1_44")))]
95 #[doc(alias = "pango_attr_list_get_attributes")]
96 #[doc(alias = "get_attributes")]
97 pub fn attributes(&self) -> Vec<Attribute> {
98 unsafe {
99 FromGlibPtrContainer::from_glib_full(ffi::pango_attr_list_get_attributes(
100 self.to_glib_none().0,
101 ))
102 }
103 }
104
105 /// This function opens up a hole in @self, fills it
106 /// in with attributes from the left, and then merges
107 /// @other on top of the hole.
108 ///
109 /// This operation is equivalent to stretching every attribute
110 /// that applies at position @pos in @self by an amount @len,
111 /// and then calling [`change()`][Self::change()] with a copy
112 /// of each attribute in @other in sequence (offset in position
113 /// by @pos, and limited in length to @len).
114 ///
115 /// This operation proves useful for, for instance, inserting
116 /// a pre-edit string in the middle of an edit buffer.
117 ///
118 /// For backwards compatibility, the function behaves differently
119 /// when @len is 0. In this case, the attributes from @other are
120 /// not imited to @len, and are just overlayed on top of @self.
121 ///
122 /// This mode is useful for merging two lists of attributes together.
123 /// ## `other`
124 /// another [`AttrList`][crate::AttrList]
125 /// ## `pos`
126 /// the position in @self at which to insert @other
127 /// ## `len`
128 /// the length of the spliced segment. (Note that this
129 /// must be specified since the attributes in @other may only
130 /// be present at some subsection of this range)
131 #[doc(alias = "pango_attr_list_splice")]
132 pub fn splice(&self, other: &AttrList, pos: i32, len: i32) {
133 unsafe {
134 ffi::pango_attr_list_splice(self.to_glib_none().0, other.to_glib_none().0, pos, len);
135 }
136 }
137
138 /// Serializes a [`AttrList`][crate::AttrList] to a string.
139 ///
140 /// In the resulting string, serialized attributes are separated by newlines or commas.
141 /// Individual attributes are serialized to a string of the form
142 ///
143 /// [START END] TYPE VALUE
144 ///
145 /// Where START and END are the indices (with -1 being accepted in place
146 /// of MAXUINT), TYPE is the nickname of the attribute value type, e.g.
147 /// _weight_ or _stretch_, and the value is serialized according to its type:
148 ///
149 /// Optionally, START and END can be omitted to indicate unlimited extent.
150 ///
151 /// - enum values as nick or numeric value
152 /// - boolean values as _true_ or _false_
153 /// - integers and floats as numbers
154 /// - strings as string, optionally quoted
155 /// - font features as quoted string
156 /// - PangoLanguage as string
157 /// - PangoFontDescription as serialized by [`FontDescription::to_str()`][crate::FontDescription::to_str()], quoted
158 /// - PangoColor as serialized by [`Color::to_str()`][crate::Color::to_str()]
159 ///
160 /// Examples:
161 ///
162 /// 0 10 foreground red, 5 15 weight bold, 0 200 font-desc "Sans 10"
163 ///
164 /// 0 -1 weight 700
165 /// 0 100 family Times
166 ///
167 /// weight bold
168 ///
169 /// To parse the returned value, use [`from_string()`][Self::from_string()].
170 ///
171 /// Note that shape attributes can not be serialized.
172 ///
173 /// # Returns
174 ///
175 /// a newly allocated string
176 #[cfg(feature = "v1_50")]
177 #[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
178 #[doc(alias = "pango_attr_list_to_string")]
179 #[doc(alias = "to_string")]
180 pub fn to_str(&self) -> glib::GString {
181 unsafe { from_glib_full(ffi::pango_attr_list_to_string(self.to_glib_none().0)) }
182 }
183
184 /// Update indices of attributes in @self for a change in the
185 /// text they refer to.
186 ///
187 /// The change that this function applies is removing @remove
188 /// bytes at position @pos and inserting @add bytes instead.
189 ///
190 /// Attributes that fall entirely in the (@pos, @pos + @remove)
191 /// range are removed.
192 ///
193 /// Attributes that start or end inside the (@pos, @pos + @remove)
194 /// range are shortened to reflect the removal.
195 ///
196 /// Attributes start and end positions are updated if they are
197 /// behind @pos + @remove.
198 /// ## `pos`
199 /// the position of the change
200 /// ## `remove`
201 /// the number of removed bytes
202 /// ## `add`
203 /// the number of added bytes
204 #[cfg(feature = "v1_44")]
205 #[cfg_attr(docsrs, doc(cfg(feature = "v1_44")))]
206 #[doc(alias = "pango_attr_list_update")]
207 pub fn update(&self, pos: i32, remove: i32, add: i32) {
208 unsafe {
209 ffi::pango_attr_list_update(self.to_glib_none().0, pos, remove, add);
210 }
211 }
212
213 /// Deserializes a [`AttrList`][crate::AttrList] from a string.
214 ///
215 /// This is the counterpart to [`to_str()`][Self::to_str()].
216 /// See that functions for details about the format.
217 /// ## `text`
218 /// a string
219 ///
220 /// # Returns
221 ///
222 /// a new [`AttrList`][crate::AttrList]
223 #[cfg(feature = "v1_50")]
224 #[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
225 #[doc(alias = "pango_attr_list_from_string")]
226 pub fn from_string(text: &str) -> Result<AttrList, glib::BoolError> {
227 unsafe {
228 Option::<_>::from_glib_full(ffi::pango_attr_list_from_string(text.to_glib_none().0))
229 .ok_or_else(|| glib::bool_error!("Can't parse AttrList"))
230 }
231 }
232}
233
234impl Default for AttrList {
235 fn default() -> Self {
236 Self::new()
237 }
238}
239
240#[cfg(feature = "v1_50")]
241#[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
242impl std::fmt::Display for AttrList {
243 #[inline]
244 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
245 f.write_str(&self.to_str())
246 }
247}