gtk4/text_buffer.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{boxed::Box as Box_, mem::transmute, slice, str};
4
5use glib::{
6 signal::{connect_raw, SignalHandlerId},
7 translate::*,
8};
9use libc::{c_char, c_int};
10
11use crate::{ffi, prelude::*, TextBuffer, TextIter, TextTag};
12
13#[cfg(feature = "v4_16")]
14use crate::TextBufferNotifyFlags;
15
16// rustdoc-stripper-ignore-next
17/// Trait containing manually implemented methods of
18/// [`TextBuffer`](crate::TextBuffer).
19pub trait TextBufferExtManual: IsA<TextBuffer> + 'static {
20 // rustdoc-stripper-ignore-next
21 /// # Panics
22 ///
23 /// If the properties don't exists or are not writeable.
24 // rustdoc-stripper-ignore-next-stop
25 /// Creates a tag and adds it to the tag table for @self.
26 ///
27 /// Equivalent to calling [`TextTag::new()`][crate::TextTag::new()] and then adding the
28 /// tag to the buffer’s tag table. The returned tag is owned by
29 /// the buffer’s tag table, so the ref count will be equal to one.
30 ///
31 /// If @tag_name is [`None`], the tag is anonymous.
32 ///
33 /// If @tag_name is non-[`None`], a tag called @tag_name must not already
34 /// exist in the tag table for this buffer.
35 ///
36 /// The @first_property_name argument and subsequent arguments are a list
37 /// of properties to set on the tag, as with g_object_set().
38 /// ## `tag_name`
39 /// name of the new tag
40 /// ## `first_property_name`
41 /// name of first property to set
42 ///
43 /// # Returns
44 ///
45 /// a new tag
46 #[doc(alias = "gtk_text_buffer_create_tag")]
47 fn create_tag(
48 &self,
49 tag_name: Option<&str>,
50 properties: &[(&str, &dyn ToValue)],
51 ) -> Option<TextTag> {
52 let tag = TextTag::new(tag_name);
53 tag.set_properties(properties);
54 if self.as_ref().tag_table().add(&tag) {
55 Some(tag)
56 } else {
57 None
58 }
59 }
60
61 /// Inserts @text into @self at @iter, applying the list of tags to
62 /// the newly-inserted text.
63 ///
64 /// The last tag specified must be [`None`] to terminate the list.
65 /// Equivalent to calling [`TextBufferExt::insert()`][crate::prelude::TextBufferExt::insert()],
66 /// then [`TextBufferExt::apply_tag()`][crate::prelude::TextBufferExt::apply_tag()] on the inserted text;
67 /// this is just a convenience function.
68 /// ## `iter`
69 /// an iterator in @self
70 /// ## `text`
71 /// UTF-8 text
72 /// ## `len`
73 /// length of @text, or -1
74 /// ## `first_tag`
75 /// first tag to apply to @text
76 #[doc(alias = "gtk_text_buffer_insert_with_tags")]
77 fn insert_with_tags(&self, iter: &mut TextIter, text: &str, tags: &[&TextTag]) {
78 let start_offset = iter.offset();
79 self.as_ref().insert(iter, text);
80 let start_iter = self.as_ref().iter_at_offset(start_offset);
81 tags.iter().for_each(|tag| {
82 self.as_ref().apply_tag(&(*tag).clone(), &start_iter, iter);
83 });
84 }
85
86 /// Inserts @text into @self at @iter, applying the list of tags to
87 /// the newly-inserted text.
88 ///
89 /// Same as [`insert_with_tags()`][Self::insert_with_tags()], but allows you
90 /// to pass in tag names instead of tag objects.
91 /// ## `iter`
92 /// position in @self
93 /// ## `text`
94 /// UTF-8 text
95 /// ## `len`
96 /// length of @text, or -1
97 /// ## `first_tag_name`
98 /// name of a tag to apply to @text
99 #[doc(alias = "gtk_text_buffer_insert_with_tags_by_name")]
100 fn insert_with_tags_by_name(&self, iter: &mut TextIter, text: &str, tags_names: &[&str]) {
101 let start_offset = iter.offset();
102 self.as_ref().insert(iter, text);
103 let start_iter = self.as_ref().iter_at_offset(start_offset);
104 let tag_table = self.as_ref().tag_table();
105 tags_names.iter().for_each(|tag_name| {
106 if let Some(tag) = tag_table.lookup(tag_name) {
107 self.as_ref().apply_tag(&tag, &start_iter, iter);
108 } else {
109 glib::g_warning!("TextBuffer", "No tag with name {}!", tag_name);
110 }
111 });
112 }
113
114 /// Emitted to insert text in a [`TextBuffer`][crate::TextBuffer].
115 ///
116 /// Insertion actually occurs in the default handler.
117 ///
118 /// Note that if your handler runs before the default handler
119 /// it must not invalidate the @location iter (or has to
120 /// revalidate it). The default signal handler revalidates
121 /// it to point to the end of the inserted text.
122 ///
123 /// See also: [`TextBufferExt::insert()`][crate::prelude::TextBufferExt::insert()],
124 /// [`TextBufferExt::insert_range()`][crate::prelude::TextBufferExt::insert_range()].
125 /// ## `location`
126 /// position to insert @text in @textbuffer
127 /// ## `text`
128 /// the UTF-8 text to be inserted
129 /// ## `len`
130 /// length of the inserted text in bytes
131 fn connect_insert_text<F: Fn(&Self, &mut TextIter, &str) + 'static>(
132 &self,
133 f: F,
134 ) -> SignalHandlerId {
135 unsafe {
136 unsafe extern "C" fn insert_text_trampoline<
137 T,
138 F: Fn(&T, &mut TextIter, &str) + 'static,
139 >(
140 this: *mut ffi::GtkTextBuffer,
141 location: *mut ffi::GtkTextIter,
142 text: *mut c_char,
143 len: c_int,
144 f: glib::ffi::gpointer,
145 ) where
146 T: IsA<TextBuffer>,
147 {
148 let mut location_copy = from_glib_none(location);
149 let f: &F = &*(f as *const F);
150 let text = if len <= 0 {
151 &[]
152 } else {
153 slice::from_raw_parts(text as *const u8, len as usize)
154 };
155
156 f(
157 TextBuffer::from_glib_borrow(this).unsafe_cast_ref(),
158 &mut location_copy,
159 str::from_utf8(text).unwrap(),
160 )
161 }
162 let f: Box_<F> = Box_::new(f);
163 connect_raw(
164 self.to_glib_none().0 as *mut _,
165 c"insert-text".as_ptr() as *mut _,
166 Some(transmute::<usize, unsafe extern "C" fn()>(
167 insert_text_trampoline::<Self, F> as usize,
168 )),
169 Box_::into_raw(f),
170 )
171 }
172 }
173
174 /// Adds a `callback::Gtk::TextBufferCommitNotify to be called when a change
175 /// is to be made to the [type@Gtk.TextBuffer].
176 ///
177 /// Functions are explicitly forbidden from making changes to the
178 /// [type@Gtk.TextBuffer] from this callback. It is intended for tracking
179 /// changes to the buffer only.
180 ///
181 /// It may be advantageous to use `callback::Gtk::TextBufferCommitNotify over
182 /// connecting to the [`insert-text`][struct@crate::TextBuffer#insert-text] or
183 /// [`delete-range`][struct@crate::TextBuffer#delete-range] signals to avoid ordering issues with
184 /// other signal handlers which may further modify the [type@Gtk.TextBuffer].
185 /// ## `flags`
186 /// which notifications should be dispatched to @callback
187 /// ## `commit_notify`
188 /// a
189 /// `callback::Gtk::TextBufferCommitNotify to call for commit notifications
190 ///
191 /// # Returns
192 ///
193 /// a handler id which may be used to remove the commit notify
194 /// callback using [`TextBufferExt::remove_commit_notify()`][crate::prelude::TextBufferExt::remove_commit_notify()].
195 #[cfg(feature = "v4_16")]
196 #[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
197 #[doc(alias = "gtk_text_buffer_add_commit_notify")]
198 fn add_commit_notify<P: Fn(&TextBuffer, TextBufferNotifyFlags, u32, u32) + 'static>(
199 &self,
200 flags: TextBufferNotifyFlags,
201 commit_notify: P,
202 ) -> u32 {
203 let commit_notify_data: Box_<P> = Box_::new(commit_notify);
204 unsafe extern "C" fn commit_notify_func<
205 P: Fn(&TextBuffer, TextBufferNotifyFlags, u32, u32) + 'static,
206 >(
207 buffer: *mut ffi::GtkTextBuffer,
208 flags: ffi::GtkTextBufferNotifyFlags,
209 position: std::ffi::c_uint,
210 length: std::ffi::c_uint,
211 user_data: glib::ffi::gpointer,
212 ) {
213 let buffer = from_glib_borrow(buffer);
214 let flags = from_glib(flags);
215 let callback = &*(user_data as *mut P);
216 (*callback)(&buffer, flags, position, length)
217 }
218 let commit_notify = Some(commit_notify_func::<P> as _);
219 unsafe extern "C" fn destroy_func<
220 P: Fn(&TextBuffer, TextBufferNotifyFlags, u32, u32) + 'static,
221 >(
222 data: glib::ffi::gpointer,
223 ) {
224 let _callback = Box_::from_raw(data as *mut P);
225 }
226 let destroy_call4 = Some(destroy_func::<P> as _);
227 let super_callback0: Box_<P> = commit_notify_data;
228 unsafe {
229 ffi::gtk_text_buffer_add_commit_notify(
230 self.as_ref().to_glib_none().0,
231 flags.into_glib(),
232 commit_notify,
233 Box_::into_raw(super_callback0) as *mut _,
234 destroy_call4,
235 )
236 }
237 }
238}
239
240impl<O: IsA<TextBuffer>> TextBufferExtManual for O {}
241
242impl std::fmt::Write for TextBuffer {
243 fn write_str(&mut self, s: &str) -> std::fmt::Result {
244 let mut iter = self.end_iter();
245 self.insert(&mut iter, s);
246 Ok(())
247 }
248}