1use crate::{ffi, SnippetChunk, SnippetContext};
7use glib::{
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "GtkSourceSnippet")]
16 pub struct Snippet(Object<ffi::GtkSourceSnippet, ffi::GtkSourceSnippetClass>);
17
18 match fn {
19 type_ => || ffi::gtk_source_snippet_get_type(),
20 }
21}
22
23impl Snippet {
24 #[doc(alias = "gtk_source_snippet_new")]
25 pub fn new(trigger: Option<&str>, language_id: Option<&str>) -> Snippet {
26 assert_initialized_main_thread!();
27 unsafe {
28 from_glib_full(ffi::gtk_source_snippet_new(
29 trigger.to_glib_none().0,
30 language_id.to_glib_none().0,
31 ))
32 }
33 }
34
35 #[cfg(feature = "v5_6")]
36 #[cfg_attr(docsrs, doc(cfg(feature = "v5_6")))]
37 #[doc(alias = "gtk_source_snippet_new_parsed")]
38 pub fn new_parsed(text: &str) -> Result<Snippet, glib::Error> {
39 assert_initialized_main_thread!();
40 unsafe {
41 let mut error = std::ptr::null_mut();
42 let ret = ffi::gtk_source_snippet_new_parsed(text.to_glib_none().0, &mut error);
43 if error.is_null() {
44 Ok(from_glib_full(ret))
45 } else {
46 Err(from_glib_full(error))
47 }
48 }
49 }
50
51 pub fn builder() -> SnippetBuilder {
56 SnippetBuilder::new()
57 }
58
59 #[doc(alias = "gtk_source_snippet_add_chunk")]
60 pub fn add_chunk(&self, chunk: &SnippetChunk) {
61 unsafe {
62 ffi::gtk_source_snippet_add_chunk(self.to_glib_none().0, chunk.to_glib_none().0);
63 }
64 }
65
66 #[doc(alias = "gtk_source_snippet_copy")]
67 #[must_use]
68 pub fn copy(&self) -> Snippet {
69 unsafe { from_glib_full(ffi::gtk_source_snippet_copy(self.to_glib_none().0)) }
70 }
71
72 #[doc(alias = "gtk_source_snippet_get_context")]
73 #[doc(alias = "get_context")]
74 pub fn context(&self) -> Option<SnippetContext> {
75 unsafe { from_glib_none(ffi::gtk_source_snippet_get_context(self.to_glib_none().0)) }
76 }
77
78 #[doc(alias = "gtk_source_snippet_get_description")]
79 #[doc(alias = "get_description")]
80 pub fn description(&self) -> glib::GString {
81 unsafe {
82 from_glib_none(ffi::gtk_source_snippet_get_description(
83 self.to_glib_none().0,
84 ))
85 }
86 }
87
88 #[doc(alias = "gtk_source_snippet_get_focus_position")]
89 #[doc(alias = "get_focus_position")]
90 #[doc(alias = "focus-position")]
91 pub fn focus_position(&self) -> i32 {
92 unsafe { ffi::gtk_source_snippet_get_focus_position(self.to_glib_none().0) }
93 }
94
95 #[doc(alias = "gtk_source_snippet_get_language_id")]
96 #[doc(alias = "get_language_id")]
97 #[doc(alias = "language-id")]
98 pub fn language_id(&self) -> glib::GString {
99 unsafe {
100 from_glib_none(ffi::gtk_source_snippet_get_language_id(
101 self.to_glib_none().0,
102 ))
103 }
104 }
105
106 #[doc(alias = "gtk_source_snippet_get_n_chunks")]
107 #[doc(alias = "get_n_chunks")]
108 pub fn n_chunks(&self) -> u32 {
109 unsafe { ffi::gtk_source_snippet_get_n_chunks(self.to_glib_none().0) }
110 }
111
112 #[doc(alias = "gtk_source_snippet_get_name")]
113 #[doc(alias = "get_name")]
114 pub fn name(&self) -> glib::GString {
115 unsafe { from_glib_none(ffi::gtk_source_snippet_get_name(self.to_glib_none().0)) }
116 }
117
118 #[doc(alias = "gtk_source_snippet_get_nth_chunk")]
119 #[doc(alias = "get_nth_chunk")]
120 pub fn nth_chunk(&self, nth: u32) -> SnippetChunk {
121 unsafe {
122 from_glib_none(ffi::gtk_source_snippet_get_nth_chunk(
123 self.to_glib_none().0,
124 nth,
125 ))
126 }
127 }
128
129 #[doc(alias = "gtk_source_snippet_get_trigger")]
130 #[doc(alias = "get_trigger")]
131 pub fn trigger(&self) -> Option<glib::GString> {
132 unsafe { from_glib_none(ffi::gtk_source_snippet_get_trigger(self.to_glib_none().0)) }
133 }
134
135 #[doc(alias = "gtk_source_snippet_set_description")]
136 #[doc(alias = "description")]
137 pub fn set_description(&self, description: &str) {
138 unsafe {
139 ffi::gtk_source_snippet_set_description(
140 self.to_glib_none().0,
141 description.to_glib_none().0,
142 );
143 }
144 }
145
146 #[doc(alias = "gtk_source_snippet_set_language_id")]
147 #[doc(alias = "language-id")]
148 pub fn set_language_id(&self, language_id: &str) {
149 unsafe {
150 ffi::gtk_source_snippet_set_language_id(
151 self.to_glib_none().0,
152 language_id.to_glib_none().0,
153 );
154 }
155 }
156
157 #[doc(alias = "gtk_source_snippet_set_name")]
158 #[doc(alias = "name")]
159 pub fn set_name(&self, name: &str) {
160 unsafe {
161 ffi::gtk_source_snippet_set_name(self.to_glib_none().0, name.to_glib_none().0);
162 }
163 }
164
165 #[doc(alias = "gtk_source_snippet_set_trigger")]
166 #[doc(alias = "trigger")]
167 pub fn set_trigger(&self, trigger: &str) {
168 unsafe {
169 ffi::gtk_source_snippet_set_trigger(self.to_glib_none().0, trigger.to_glib_none().0);
170 }
171 }
172
173 pub fn buffer(&self) -> Option<gtk::TextBuffer> {
174 ObjectExt::property(self, "buffer")
175 }
176
177 #[doc(alias = "buffer")]
178 pub fn connect_buffer_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
179 unsafe extern "C" fn notify_buffer_trampoline<F: Fn(&Snippet) + 'static>(
180 this: *mut ffi::GtkSourceSnippet,
181 _param_spec: glib::ffi::gpointer,
182 f: glib::ffi::gpointer,
183 ) {
184 let f: &F = &*(f as *const F);
185 f(&from_glib_borrow(this))
186 }
187 unsafe {
188 let f: Box_<F> = Box_::new(f);
189 connect_raw(
190 self.as_ptr() as *mut _,
191 c"notify::buffer".as_ptr() as *const _,
192 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
193 notify_buffer_trampoline::<F> as *const (),
194 )),
195 Box_::into_raw(f),
196 )
197 }
198 }
199
200 #[doc(alias = "description")]
201 pub fn connect_description_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
202 unsafe extern "C" fn notify_description_trampoline<F: Fn(&Snippet) + 'static>(
203 this: *mut ffi::GtkSourceSnippet,
204 _param_spec: glib::ffi::gpointer,
205 f: glib::ffi::gpointer,
206 ) {
207 let f: &F = &*(f as *const F);
208 f(&from_glib_borrow(this))
209 }
210 unsafe {
211 let f: Box_<F> = Box_::new(f);
212 connect_raw(
213 self.as_ptr() as *mut _,
214 c"notify::description".as_ptr() as *const _,
215 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
216 notify_description_trampoline::<F> as *const (),
217 )),
218 Box_::into_raw(f),
219 )
220 }
221 }
222
223 #[doc(alias = "focus-position")]
224 pub fn connect_focus_position_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
225 unsafe extern "C" fn notify_focus_position_trampoline<F: Fn(&Snippet) + 'static>(
226 this: *mut ffi::GtkSourceSnippet,
227 _param_spec: glib::ffi::gpointer,
228 f: glib::ffi::gpointer,
229 ) {
230 let f: &F = &*(f as *const F);
231 f(&from_glib_borrow(this))
232 }
233 unsafe {
234 let f: Box_<F> = Box_::new(f);
235 connect_raw(
236 self.as_ptr() as *mut _,
237 c"notify::focus-position".as_ptr() as *const _,
238 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
239 notify_focus_position_trampoline::<F> as *const (),
240 )),
241 Box_::into_raw(f),
242 )
243 }
244 }
245
246 #[doc(alias = "language-id")]
247 pub fn connect_language_id_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
248 unsafe extern "C" fn notify_language_id_trampoline<F: Fn(&Snippet) + 'static>(
249 this: *mut ffi::GtkSourceSnippet,
250 _param_spec: glib::ffi::gpointer,
251 f: glib::ffi::gpointer,
252 ) {
253 let f: &F = &*(f as *const F);
254 f(&from_glib_borrow(this))
255 }
256 unsafe {
257 let f: Box_<F> = Box_::new(f);
258 connect_raw(
259 self.as_ptr() as *mut _,
260 c"notify::language-id".as_ptr() as *const _,
261 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
262 notify_language_id_trampoline::<F> as *const (),
263 )),
264 Box_::into_raw(f),
265 )
266 }
267 }
268
269 #[doc(alias = "name")]
270 pub fn connect_name_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
271 unsafe extern "C" fn notify_name_trampoline<F: Fn(&Snippet) + 'static>(
272 this: *mut ffi::GtkSourceSnippet,
273 _param_spec: glib::ffi::gpointer,
274 f: glib::ffi::gpointer,
275 ) {
276 let f: &F = &*(f as *const F);
277 f(&from_glib_borrow(this))
278 }
279 unsafe {
280 let f: Box_<F> = Box_::new(f);
281 connect_raw(
282 self.as_ptr() as *mut _,
283 c"notify::name".as_ptr() as *const _,
284 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
285 notify_name_trampoline::<F> as *const (),
286 )),
287 Box_::into_raw(f),
288 )
289 }
290 }
291
292 #[doc(alias = "trigger")]
293 pub fn connect_trigger_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
294 unsafe extern "C" fn notify_trigger_trampoline<F: Fn(&Snippet) + 'static>(
295 this: *mut ffi::GtkSourceSnippet,
296 _param_spec: glib::ffi::gpointer,
297 f: glib::ffi::gpointer,
298 ) {
299 let f: &F = &*(f as *const F);
300 f(&from_glib_borrow(this))
301 }
302 unsafe {
303 let f: Box_<F> = Box_::new(f);
304 connect_raw(
305 self.as_ptr() as *mut _,
306 c"notify::trigger".as_ptr() as *const _,
307 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
308 notify_trigger_trampoline::<F> as *const (),
309 )),
310 Box_::into_raw(f),
311 )
312 }
313 }
314}
315
316impl Default for Snippet {
317 fn default() -> Self {
318 glib::object::Object::new::<Self>()
319 }
320}
321
322impl std::fmt::Display for Snippet {
323 #[inline]
324 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
325 f.write_str(&self.name())
326 }
327}
328
329#[must_use = "The builder must be built to be used"]
334pub struct SnippetBuilder {
335 builder: glib::object::ObjectBuilder<'static, Snippet>,
336}
337
338impl SnippetBuilder {
339 fn new() -> Self {
340 Self {
341 builder: glib::object::Object::builder(),
342 }
343 }
344
345 pub fn description(self, description: impl Into<glib::GString>) -> Self {
346 Self {
347 builder: self.builder.property("description", description.into()),
348 }
349 }
350
351 pub fn language_id(self, language_id: impl Into<glib::GString>) -> Self {
352 Self {
353 builder: self.builder.property("language-id", language_id.into()),
354 }
355 }
356
357 pub fn name(self, name: impl Into<glib::GString>) -> Self {
358 Self {
359 builder: self.builder.property("name", name.into()),
360 }
361 }
362
363 pub fn trigger(self, trigger: impl Into<glib::GString>) -> Self {
364 Self {
365 builder: self.builder.property("trigger", trigger.into()),
366 }
367 }
368
369 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
372 pub fn build(self) -> Snippet {
373 assert_initialized_main_thread!();
374 self.builder.build()
375 }
376}