Skip to main content

pango/
attr_list.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::mem;
4
5use glib::translate::*;
6
7use crate::{AttrIterator, AttrList, Attribute, ffi};
8
9impl AttrList {
10    /// Insert the given attribute into the [`AttrList`][crate::AttrList].
11    ///
12    /// It will replace any attributes of the same type
13    /// on that segment and be merged with any adjoining
14    /// attributes that are identical.
15    ///
16    /// This function is slower than [`insert()`][Self::insert()]
17    /// for creating an attribute list in order (potentially
18    /// much slower for large lists). However,
19    /// [`insert()`][Self::insert()] is not suitable for
20    /// continually changing a set of attributes since it
21    /// never removes or combines existing attributes.
22    /// ## `attr`
23    /// the attribute to insert
24    #[doc(alias = "pango_attr_list_change")]
25    pub fn change(&mut self, attr: impl Into<Attribute>) {
26        unsafe {
27            let attr = attr.into();
28            ffi::pango_attr_list_change(self.to_glib_none().0, attr.to_glib_none().0);
29            mem::forget(attr); //As attr transferred fully
30        }
31    }
32
33    #[cfg(feature = "v1_46")]
34    #[cfg_attr(docsrs, doc(cfg(feature = "v1_46")))]
35    #[doc(alias = "pango_attr_list_equal")]
36    fn equal(&self, other_list: &AttrList) -> bool {
37        unsafe {
38            from_glib(ffi::pango_attr_list_equal(
39                self.to_glib_none().0,
40                other_list.to_glib_none().0,
41            ))
42        }
43    }
44
45    /// Given a [`AttrList`][crate::AttrList] and callback function, removes
46    /// any elements of @self for which @func returns [`true`] and
47    /// inserts them into a new list.
48    /// ## `func`
49    /// callback function;
50    ///   returns [`true`] if an attribute should be filtered out
51    ///
52    /// # Returns
53    ///
54    /// the new
55    ///   [`AttrList`][crate::AttrList] or [`None`] if no attributes of the
56    ///   given types were found
57    #[doc(alias = "pango_attr_list_filter")]
58    #[must_use]
59    pub fn filter<P: FnMut(&Attribute) -> bool>(&mut self, func: P) -> Option<AttrList> {
60        let mut func_data: P = func;
61        unsafe extern "C" fn func_func<P: FnMut(&Attribute) -> bool>(
62            attribute: *mut ffi::PangoAttribute,
63            user_data: glib::ffi::gpointer,
64        ) -> glib::ffi::gboolean {
65            unsafe {
66                let attribute = from_glib_borrow(attribute);
67                let callback = user_data as *mut P;
68                (*callback)(&attribute).into_glib()
69            }
70        }
71        let func = Some(func_func::<P> as _);
72        let super_callback0: &mut P = &mut func_data;
73        unsafe {
74            from_glib_full(ffi::pango_attr_list_filter(
75                self.to_glib_none().0,
76                func,
77                super_callback0 as *mut _ as *mut _,
78            ))
79        }
80    }
81
82    /// Insert the given attribute into the [`AttrList`][crate::AttrList].
83    ///
84    /// It will be inserted after all other attributes with a
85    /// matching @start_index.
86    /// ## `attr`
87    /// the attribute to insert
88    #[doc(alias = "pango_attr_list_insert")]
89    pub fn insert(&mut self, attr: impl Into<Attribute>) {
90        unsafe {
91            let attr = attr.into();
92            ffi::pango_attr_list_insert(self.to_glib_none().0, attr.to_glib_none().0);
93            mem::forget(attr); //As attr transferred fully
94        }
95    }
96
97    /// Insert the given attribute into the [`AttrList`][crate::AttrList].
98    ///
99    /// It will be inserted before all other attributes with a
100    /// matching @start_index.
101    /// ## `attr`
102    /// the attribute to insert
103    #[doc(alias = "pango_attr_list_insert_before")]
104    pub fn insert_before(&mut self, attr: impl Into<Attribute>) {
105        unsafe {
106            let attr = attr.into();
107            ffi::pango_attr_list_insert_before(self.to_glib_none().0, attr.to_glib_none().0);
108            mem::forget(attr); //As attr transferred fully
109        }
110    }
111
112    /// Create a iterator initialized to the beginning of the list.
113    ///
114    /// @self must not be modified until this iterator is freed.
115    ///
116    /// # Returns
117    ///
118    /// the newly allocated
119    ///   [`AttrIterator`][crate::AttrIterator], which should be freed with
120    ///   `Pango::AttrIterator::destroy()`
121    #[doc(alias = "pango_attr_list_get_iterator")]
122    #[doc(alias = "get_iterator")]
123    pub fn iterator(&self) -> AttrIterator<'_> {
124        unsafe { from_glib_full(ffi::pango_attr_list_get_iterator(self.to_glib_none().0)) }
125    }
126
127    /// This function opens up a hole in @self, fills it
128    /// in with attributes from the left, and then merges
129    /// @other on top of the hole.
130    ///
131    /// This operation is equivalent to stretching every attribute
132    /// that applies at position @pos in @self by an amount @len,
133    /// and then calling [`change()`][Self::change()] with a copy
134    /// of each attribute in @other in sequence (offset in position
135    /// by @pos, and limited in length to @len).
136    ///
137    /// This operation proves useful for, for instance, inserting
138    /// a pre-edit string in the middle of an edit buffer.
139    ///
140    /// For backwards compatibility, the function behaves differently
141    /// when @len is 0. In this case, the attributes from @other are
142    /// not imited to @len, and are just overlayed on top of @self.
143    ///
144    /// This mode is useful for merging two lists of attributes together.
145    /// ## `other`
146    /// another [`AttrList`][crate::AttrList]
147    /// ## `pos`
148    /// the position in @self at which to insert @other
149    /// ## `len`
150    /// the length of the spliced segment. (Note that this
151    ///   must be specified since the attributes in @other may only
152    ///   be present at some subsection of this range)
153    #[doc(alias = "pango_attr_list_splice")]
154    pub fn splice(&mut self, other: &AttrList, pos: i32, len: i32) {
155        unsafe {
156            ffi::pango_attr_list_splice(self.to_glib_none().0, other.to_glib_none().0, pos, len);
157        }
158    }
159
160    /// Update indices of attributes in @self for a change in the
161    /// text they refer to.
162    ///
163    /// The change that this function applies is removing @remove
164    /// bytes at position @pos and inserting @add bytes instead.
165    ///
166    /// Attributes that fall entirely in the (@pos, @pos + @remove)
167    /// range are removed.
168    ///
169    /// Attributes that start or end inside the (@pos, @pos + @remove)
170    /// range are shortened to reflect the removal.
171    ///
172    /// Attributes start and end positions are updated if they are
173    /// behind @pos + @remove.
174    /// ## `pos`
175    /// the position of the change
176    /// ## `remove`
177    /// the number of removed bytes
178    /// ## `add`
179    /// the number of added bytes
180    #[cfg(feature = "v1_44")]
181    #[cfg_attr(docsrs, doc(cfg(feature = "v1_44")))]
182    #[doc(alias = "pango_attr_list_update")]
183    pub fn update(&mut self, pos: i32, remove: i32, add: i32) {
184        unsafe {
185            ffi::pango_attr_list_update(self.to_glib_none().0, pos, remove, add);
186        }
187    }
188}
189
190#[cfg(feature = "v1_46")]
191#[cfg_attr(docsrs, doc(cfg(feature = "v1_46")))]
192impl PartialEq for AttrList {
193    #[inline]
194    fn eq(&self, other: &Self) -> bool {
195        self.equal(other)
196    }
197}
198
199#[cfg(feature = "v1_46")]
200#[cfg_attr(docsrs, doc(cfg(feature = "v1_46")))]
201impl Eq for AttrList {}
202
203#[cfg(feature = "v1_50")]
204#[cfg_attr(docsrs, doc(cfg(feature = "v1_50")))]
205impl std::str::FromStr for AttrList {
206    type Err = glib::BoolError;
207
208    fn from_str(s: &str) -> Result<Self, Self::Err> {
209        Self::from_string(s)
210    }
211}