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::{Attribute, ffi};
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            unsafe {
71                let attribute = from_glib_borrow(attribute);
72                let callback = user_data as *mut P;
73                (*callback)(&attribute).into_glib()
74            }
75        }
76        let func = Some(func_func::<P> as _);
77        let super_callback0: &mut P = &mut func_data;
78        unsafe {
79            from_glib_full(ffi::pango_attr_list_filter(
80                self.to_glib_none().0,
81                func,
82                super_callback0 as *mut _ as *mut _,
83            ))
84        }
85    }
86
87    /// Gets a list of all attributes in @self.
88    ///
89    /// # Returns
90    ///
91    ///
92    ///   a list of all attributes in @self. To free this value,
93    ///   call `Pango::Attribute::destroy()` on each value and
94    ///   g_slist_free() on the list.
95    #[cfg(feature = "v1_44")]
96    #[cfg_attr(docsrs, doc(cfg(feature = "v1_44")))]
97    #[doc(alias = "pango_attr_list_get_attributes")]
98    #[doc(alias = "get_attributes")]
99    pub fn attributes(&self) -> Vec<Attribute> {
100        unsafe {
101            FromGlibPtrContainer::from_glib_full(ffi::pango_attr_list_get_attributes(
102                self.to_glib_none().0,
103            ))
104        }
105    }
106
107    /// This function opens up a hole in @self, fills it
108    /// in with attributes from the left, and then merges
109    /// @other on top of the hole.
110    ///
111    /// This operation is equivalent to stretching every attribute
112    /// that applies at position @pos in @self by an amount @len,
113    /// and then calling [`change()`][Self::change()] with a copy
114    /// of each attribute in @other in sequence (offset in position
115    /// by @pos, and limited in length to @len).
116    ///
117    /// This operation proves useful for, for instance, inserting
118    /// a pre-edit string in the middle of an edit buffer.
119    ///
120    /// For backwards compatibility, the function behaves differently
121    /// when @len is 0. In this case, the attributes from @other are
122    /// not imited to @len, and are just overlayed on top of @self.
123    ///
124    /// This mode is useful for merging two lists of attributes together.
125    /// ## `other`
126    /// another [`AttrList`][crate::AttrList]
127    /// ## `pos`
128    /// the position in @self at which to insert @other
129    /// ## `len`
130    /// the length of the spliced segment. (Note that this
131    ///   must be specified since the attributes in @other may only
132    ///   be present at some subsection of this range)
133    #[doc(alias = "pango_attr_list_splice")]
134    pub fn splice(&self, other: &AttrList, pos: i32, len: i32) {
135        unsafe {
136            ffi::pango_attr_list_splice(self.to_glib_none().0, other.to_glib_none().0, pos, len);
137        }
138    }
139
140    /// Serializes a [`AttrList`][crate::AttrList] to a string.
141    ///
142    /// In the resulting string, serialized attributes are separated by newlines or commas.
143    /// Individual attributes are serialized to a string of the form
144    ///
145    ///     [START END] TYPE VALUE
146    ///
147    /// Where START and END are the indices (with -1 being accepted in place
148    /// of MAXUINT), TYPE is the nickname of the attribute value type, e.g.
149    /// _weight_ or _stretch_, and the value is serialized according to its type:
150    ///
151    /// Optionally, START and END can be omitted to indicate unlimited extent.
152    ///
153    /// - enum values as nick or numeric value
154    /// - boolean values as _true_ or _false_
155    /// - integers and floats as numbers
156    /// - strings as string, optionally quoted
157    /// - font features as quoted string
158    /// - PangoLanguage as string
159    /// - PangoFontDescription as serialized by [`FontDescription::to_str()`][crate::FontDescription::to_str()], quoted
160    /// - PangoColor as serialized by [`Color::to_str()`][crate::Color::to_str()]
161    ///
162    /// Examples:
163    ///
164    ///     0 10 foreground red, 5 15 weight bold, 0 200 font-desc "Sans 10"
165    ///
166    ///     0 -1 weight 700
167    ///     0 100 family Times
168    ///
169    ///     weight bold
170    ///
171    /// To parse the returned value, use [`from_string()`][Self::from_string()].
172    ///
173    /// Note that shape attributes can not be serialized.
174    ///
175    /// # Returns
176    ///
177    /// a newly allocated string
178    #[cfg(feature = "v1_50")]
179    #[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
180    #[doc(alias = "pango_attr_list_to_string")]
181    #[doc(alias = "to_string")]
182    pub fn to_str(&self) -> glib::GString {
183        unsafe { from_glib_full(ffi::pango_attr_list_to_string(self.to_glib_none().0)) }
184    }
185
186    /// Update indices of attributes in @self for a change in the
187    /// text they refer to.
188    ///
189    /// The change that this function applies is removing @remove
190    /// bytes at position @pos and inserting @add bytes instead.
191    ///
192    /// Attributes that fall entirely in the (@pos, @pos + @remove)
193    /// range are removed.
194    ///
195    /// Attributes that start or end inside the (@pos, @pos + @remove)
196    /// range are shortened to reflect the removal.
197    ///
198    /// Attributes start and end positions are updated if they are
199    /// behind @pos + @remove.
200    /// ## `pos`
201    /// the position of the change
202    /// ## `remove`
203    /// the number of removed bytes
204    /// ## `add`
205    /// the number of added bytes
206    #[cfg(feature = "v1_44")]
207    #[cfg_attr(docsrs, doc(cfg(feature = "v1_44")))]
208    #[doc(alias = "pango_attr_list_update")]
209    pub fn update(&self, pos: i32, remove: i32, add: i32) {
210        unsafe {
211            ffi::pango_attr_list_update(self.to_glib_none().0, pos, remove, add);
212        }
213    }
214
215    /// Deserializes a [`AttrList`][crate::AttrList] from a string.
216    ///
217    /// This is the counterpart to [`to_str()`][Self::to_str()].
218    /// See that functions for details about the format.
219    /// ## `text`
220    /// a string
221    ///
222    /// # Returns
223    ///
224    /// a new [`AttrList`][crate::AttrList]
225    #[cfg(feature = "v1_50")]
226    #[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
227    #[doc(alias = "pango_attr_list_from_string")]
228    pub fn from_string(text: &str) -> Result<AttrList, glib::BoolError> {
229        unsafe {
230            Option::<_>::from_glib_full(ffi::pango_attr_list_from_string(text.to_glib_none().0))
231                .ok_or_else(|| glib::bool_error!("Can't parse AttrList"))
232        }
233    }
234}
235
236impl Default for AttrList {
237    fn default() -> Self {
238        Self::new()
239    }
240}
241
242#[cfg(feature = "v1_50")]
243#[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
244impl std::fmt::Display for AttrList {
245    #[inline]
246    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
247        f.write_str(&self.to_str())
248    }
249}