gtk4/auto/
text_tag_table.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, TextTag};
6use glib::{
7    object::ObjectType as _,
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// The collection of tags in a [`TextBuffer`][crate::TextBuffer]
16    ///
17    /// You may wish to begin by reading the
18    /// [text widget conceptual overview](section-text-widget.html),
19    /// which gives an overview of all the objects and data types
20    /// related to the text widget and how they work together.
21    ///
22    /// # GtkTextTagTables as GtkBuildable
23    ///
24    /// The [`TextTagTable`][crate::TextTagTable] implementation of the [`Buildable`][crate::Buildable] interface
25    /// supports adding tags by specifying “tag” as the “type” attribute
26    /// of a `<child>` element.
27    ///
28    /// An example of a UI definition fragment specifying tags:
29    /// ```xml
30    /// <object class="GtkTextTagTable">
31    ///  <child type="tag">
32    ///    <object class="GtkTextTag"/>
33    ///  </child>
34    /// </object>
35    /// ```
36    ///
37    /// ## Signals
38    ///
39    ///
40    /// #### `tag-added`
41    ///  Emitted every time a new tag is added in the [`TextTagTable`][crate::TextTagTable].
42    ///
43    ///
44    ///
45    ///
46    /// #### `tag-changed`
47    ///  Emitted every time a tag in the [`TextTagTable`][crate::TextTagTable] changes.
48    ///
49    ///
50    ///
51    ///
52    /// #### `tag-removed`
53    ///  Emitted every time a tag is removed from the [`TextTagTable`][crate::TextTagTable].
54    ///
55    /// The @tag is still valid by the time the signal is emitted, but
56    /// it is not associated with a tag table any more.
57    ///
58    ///
59    ///
60    /// # Implements
61    ///
62    /// [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt]
63    #[doc(alias = "GtkTextTagTable")]
64    pub struct TextTagTable(Object<ffi::GtkTextTagTable>) @implements Buildable;
65
66    match fn {
67        type_ => || ffi::gtk_text_tag_table_get_type(),
68    }
69}
70
71impl TextTagTable {
72    /// Creates a new [`TextTagTable`][crate::TextTagTable].
73    ///
74    /// The table contains no tags by default.
75    ///
76    /// # Returns
77    ///
78    /// a new [`TextTagTable`][crate::TextTagTable]
79    #[doc(alias = "gtk_text_tag_table_new")]
80    pub fn new() -> TextTagTable {
81        assert_initialized_main_thread!();
82        unsafe { from_glib_full(ffi::gtk_text_tag_table_new()) }
83    }
84
85    /// Add a tag to the table.
86    ///
87    /// The tag is assigned the highest priority in the table.
88    ///
89    /// @tag must not be in a tag table already, and may not have
90    /// the same name as an already-added tag.
91    /// ## `tag`
92    /// a [`TextTag`][crate::TextTag]
93    ///
94    /// # Returns
95    ///
96    /// [`true`] on success.
97    #[doc(alias = "gtk_text_tag_table_add")]
98    pub fn add(&self, tag: &impl IsA<TextTag>) -> bool {
99        unsafe {
100            from_glib(ffi::gtk_text_tag_table_add(
101                self.to_glib_none().0,
102                tag.as_ref().to_glib_none().0,
103            ))
104        }
105    }
106
107    /// Calls @func on each tag in @self, with user data @data.
108    ///
109    /// Note that the table may not be modified while iterating
110    /// over it (you can’t add/remove tags).
111    /// ## `func`
112    /// a function to call on each tag
113    #[doc(alias = "gtk_text_tag_table_foreach")]
114    pub fn foreach<P: FnMut(&TextTag)>(&self, func: P) {
115        let mut func_data: P = func;
116        unsafe extern "C" fn func_func<P: FnMut(&TextTag)>(
117            tag: *mut ffi::GtkTextTag,
118            data: glib::ffi::gpointer,
119        ) {
120            let tag = from_glib_borrow(tag);
121            let callback = data as *mut P;
122            (*callback)(&tag)
123        }
124        let func = Some(func_func::<P> as _);
125        let super_callback0: &mut P = &mut func_data;
126        unsafe {
127            ffi::gtk_text_tag_table_foreach(
128                self.to_glib_none().0,
129                func,
130                super_callback0 as *mut _ as *mut _,
131            );
132        }
133    }
134
135    /// Returns the size of the table (number of tags)
136    ///
137    /// # Returns
138    ///
139    /// number of tags in @self
140    #[doc(alias = "gtk_text_tag_table_get_size")]
141    #[doc(alias = "get_size")]
142    pub fn size(&self) -> i32 {
143        unsafe { ffi::gtk_text_tag_table_get_size(self.to_glib_none().0) }
144    }
145
146    /// Look up a named tag.
147    /// ## `name`
148    /// name of a tag
149    ///
150    /// # Returns
151    ///
152    /// The tag
153    #[doc(alias = "gtk_text_tag_table_lookup")]
154    pub fn lookup(&self, name: &str) -> Option<TextTag> {
155        unsafe {
156            from_glib_none(ffi::gtk_text_tag_table_lookup(
157                self.to_glib_none().0,
158                name.to_glib_none().0,
159            ))
160        }
161    }
162
163    /// Remove a tag from the table.
164    ///
165    /// If a [`TextBuffer`][crate::TextBuffer] has @self as its tag table, the tag is
166    /// removed from the buffer. The table’s reference to the tag is
167    /// removed, so the tag will end up destroyed if you don’t have
168    /// a reference to it.
169    /// ## `tag`
170    /// a [`TextTag`][crate::TextTag]
171    #[doc(alias = "gtk_text_tag_table_remove")]
172    pub fn remove(&self, tag: &impl IsA<TextTag>) {
173        unsafe {
174            ffi::gtk_text_tag_table_remove(self.to_glib_none().0, tag.as_ref().to_glib_none().0);
175        }
176    }
177
178    /// Emitted every time a new tag is added in the [`TextTagTable`][crate::TextTagTable].
179    /// ## `tag`
180    /// the added tag.
181    #[doc(alias = "tag-added")]
182    pub fn connect_tag_added<F: Fn(&Self, &TextTag) + 'static>(&self, f: F) -> SignalHandlerId {
183        unsafe extern "C" fn tag_added_trampoline<F: Fn(&TextTagTable, &TextTag) + 'static>(
184            this: *mut ffi::GtkTextTagTable,
185            tag: *mut ffi::GtkTextTag,
186            f: glib::ffi::gpointer,
187        ) {
188            let f: &F = &*(f as *const F);
189            f(&from_glib_borrow(this), &from_glib_borrow(tag))
190        }
191        unsafe {
192            let f: Box_<F> = Box_::new(f);
193            connect_raw(
194                self.as_ptr() as *mut _,
195                b"tag-added\0".as_ptr() as *const _,
196                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
197                    tag_added_trampoline::<F> as *const (),
198                )),
199                Box_::into_raw(f),
200            )
201        }
202    }
203
204    /// Emitted every time a tag in the [`TextTagTable`][crate::TextTagTable] changes.
205    /// ## `tag`
206    /// the changed tag.
207    /// ## `size_changed`
208    /// whether the change affects the [`TextView`][crate::TextView] layout.
209    #[doc(alias = "tag-changed")]
210    pub fn connect_tag_changed<F: Fn(&Self, &TextTag, bool) + 'static>(
211        &self,
212        f: F,
213    ) -> SignalHandlerId {
214        unsafe extern "C" fn tag_changed_trampoline<
215            F: Fn(&TextTagTable, &TextTag, bool) + 'static,
216        >(
217            this: *mut ffi::GtkTextTagTable,
218            tag: *mut ffi::GtkTextTag,
219            size_changed: glib::ffi::gboolean,
220            f: glib::ffi::gpointer,
221        ) {
222            let f: &F = &*(f as *const F);
223            f(
224                &from_glib_borrow(this),
225                &from_glib_borrow(tag),
226                from_glib(size_changed),
227            )
228        }
229        unsafe {
230            let f: Box_<F> = Box_::new(f);
231            connect_raw(
232                self.as_ptr() as *mut _,
233                b"tag-changed\0".as_ptr() as *const _,
234                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
235                    tag_changed_trampoline::<F> as *const (),
236                )),
237                Box_::into_raw(f),
238            )
239        }
240    }
241
242    /// Emitted every time a tag is removed from the [`TextTagTable`][crate::TextTagTable].
243    ///
244    /// The @tag is still valid by the time the signal is emitted, but
245    /// it is not associated with a tag table any more.
246    /// ## `tag`
247    /// the removed tag.
248    #[doc(alias = "tag-removed")]
249    pub fn connect_tag_removed<F: Fn(&Self, &TextTag) + 'static>(&self, f: F) -> SignalHandlerId {
250        unsafe extern "C" fn tag_removed_trampoline<F: Fn(&TextTagTable, &TextTag) + 'static>(
251            this: *mut ffi::GtkTextTagTable,
252            tag: *mut ffi::GtkTextTag,
253            f: glib::ffi::gpointer,
254        ) {
255            let f: &F = &*(f as *const F);
256            f(&from_glib_borrow(this), &from_glib_borrow(tag))
257        }
258        unsafe {
259            let f: Box_<F> = Box_::new(f);
260            connect_raw(
261                self.as_ptr() as *mut _,
262                b"tag-removed\0".as_ptr() as *const _,
263                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
264                    tag_removed_trampoline::<F> as *const (),
265                )),
266                Box_::into_raw(f),
267            )
268        }
269    }
270}
271
272impl Default for TextTagTable {
273    fn default() -> Self {
274        Self::new()
275    }
276}