Skip to main content

gsk4/auto/
render_replay.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::{RenderNode, ffi};
6use glib::{prelude::*, translate::*};
7use std::boxed::Box as Box_;
8
9glib::wrapper! {
10    /// bounds);
11    ///     }
12    ///   else
13    ///     {
14    ///       result = gsk_render_replay_default (replay, node);
15    ///     }
16    ///
17    ///   return result;
18    /// }
19    ///
20    /// int
21    /// main (int argc, char *argv[])
22    /// {
23    ///   GFile *file;
24    ///   GBytes *bytes;
25    ///   GskRenderNode *result, *node;
26    ///   GskRenderReplay *replay;
27    ///
28    ///   gtk_init ();
29    ///
30    ///   if (argc != 3)
31    ///     {
32    ///       g_print ("usage: `s` INFILE OUTFILE\n", argv[0]);
33    ///       return 0;
34    ///     }
35    ///
36    ///   file = g_file_new_for_commandline_arg (argv[1]);
37    ///   bytes = g_file_load_bytes (file, NULL, NULL, NULL);
38    ///   g_object_unref (file);
39    ///   if (bytes == NULL)
40    ///     return 1;
41    ///
42    ///   node = gsk_render_node_deserialize (bytes, NULL, NULL);
43    ///   g_bytes_unref (bytes);
44    ///   if (node == NULL)
45    ///     return 1;
46    ///
47    ///   replay = gsk_render_replay_new ();
48    ///   gsk_render_replay_set_node_filter (replay, redact_nodes, NULL, NULL);
49    ///   result = gsk_render_replay_filter_node (replay, node);
50    ///   gsk_render_replay_free (replay);
51    ///
52    ///   if (!gsk_render_node_write_to_file (result, argv[2], NULL))
53    ///     return 1;
54    ///
55    ///   gsk_render_node_unref (result);
56    ///   gsk_render_node_unref (node);
57    ///
58    ///   return 0;
59    /// }
60    /// ```text
61    ///
62    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
63    pub struct RenderReplay(Boxed<ffi::GskRenderReplay>);
64
65    match fn {
66        copy => |ptr| glib::gobject_ffi::g_boxed_copy(ffi::gsk_render_replay_get_type(), ptr as *mut _) as *mut ffi::GskRenderReplay,
67        free => |ptr| glib::gobject_ffi::g_boxed_free(ffi::gsk_render_replay_get_type(), ptr as *mut _),
68        type_ => || ffi::gsk_render_replay_get_type(),
69    }
70}
71
72impl RenderReplay {
73    /// Creates a new replay object to replay nodes.
74    ///
75    /// # Returns
76    ///
77    /// A new replay object to replay nodes
78    #[doc(alias = "gsk_render_replay_new")]
79    pub fn new() -> RenderReplay {
80        assert_initialized_main_thread!();
81        unsafe { from_glib_full(ffi::gsk_render_replay_new()) }
82    }
83
84    /// Replays the node using the default method.
85    ///
86    /// The default method calls [`filter_node()`][Self::filter_node()]
87    /// on all its child nodes and the filter functions for all its
88    /// properties. If none of them are changed, it returns the passed
89    /// in node. Otherwise it constructs a new node with the changed
90    /// children and properties.
91    ///
92    /// It may not be possible to construct a new node when any of the
93    /// callbacks return NULL. In that case, this function will return
94    /// NULL, too.
95    /// ## `node`
96    /// the node to replay
97    ///
98    /// # Returns
99    ///
100    /// The replayed node
101    #[doc(alias = "gsk_render_replay_default")]
102    #[allow(clippy::should_implement_trait)]
103    pub fn default(&mut self, node: impl AsRef<RenderNode>) -> Option<RenderNode> {
104        unsafe {
105            from_glib_full(ffi::gsk_render_replay_default(
106                self.to_glib_none_mut().0,
107                node.as_ref().to_glib_none().0,
108            ))
109        }
110    }
111
112    /// Filters a font using the current filter function.
113    /// ## `font`
114    /// The font to filter
115    ///
116    /// # Returns
117    ///
118    /// the filtered font
119    #[doc(alias = "gsk_render_replay_filter_font")]
120    pub fn filter_font(&mut self, font: &impl IsA<pango::Font>) -> pango::Font {
121        unsafe {
122            from_glib_full(ffi::gsk_render_replay_filter_font(
123                self.to_glib_none_mut().0,
124                font.as_ref().to_glib_none().0,
125            ))
126        }
127    }
128
129    /// Replays a node using the replay's filter function.
130    ///
131    /// After the replay the node may be unchanged, or it may be
132    /// removed, which will result in [`None`] being returned.
133    ///
134    /// If no filter node is set, [`default()`][Self::default()] is
135    /// called instead.
136    /// ## `node`
137    /// the node to replay
138    ///
139    /// # Returns
140    ///
141    /// The replayed node
142    #[doc(alias = "gsk_render_replay_filter_node")]
143    pub fn filter_node(&mut self, node: impl AsRef<RenderNode>) -> Option<RenderNode> {
144        unsafe {
145            from_glib_full(ffi::gsk_render_replay_filter_node(
146                self.to_glib_none_mut().0,
147                node.as_ref().to_glib_none().0,
148            ))
149        }
150    }
151
152    /// Filters a texture using the current filter function.
153    /// ## `texture`
154    /// The texture to filter
155    ///
156    /// # Returns
157    ///
158    /// the filtered texture
159    #[doc(alias = "gsk_render_replay_filter_texture")]
160    pub fn filter_texture(&mut self, texture: &impl IsA<gdk::Texture>) -> gdk::Texture {
161        unsafe {
162            from_glib_full(ffi::gsk_render_replay_filter_texture(
163                self.to_glib_none_mut().0,
164                texture.as_ref().to_glib_none().0,
165            ))
166        }
167    }
168
169    /// Sets a filter function to be called by [`default()`][Self::default()]
170    /// for nodes that contain fonts.
171    ///
172    /// You can call `GskRenderReplay::filter_font()` to filter
173    /// a font yourself.
174    /// ## `filter`
175    ///
176    ///   the font filter function
177    #[doc(alias = "gsk_render_replay_set_font_filter")]
178    pub fn set_font_filter(
179        &mut self,
180        filter: Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>,
181    ) {
182        let filter_data: Box_<
183            Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>,
184        > = Box_::new(filter);
185        unsafe extern "C" fn filter_func(
186            replay: *mut ffi::GskRenderReplay,
187            font: *mut pango::ffi::PangoFont,
188            user_data: glib::ffi::gpointer,
189        ) -> *mut pango::ffi::PangoFont {
190            unsafe {
191                let replay = from_glib_borrow(replay);
192                let font = from_glib_borrow(font);
193                let callback = &*(user_data
194                    as *mut Option<
195                        Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>,
196                    >);
197                if let Some(ref callback) = *callback {
198                    callback(&replay, &font)
199                } else {
200                    panic!("cannot get closure...")
201                }
202                .to_glib_full()
203            }
204        }
205        let filter = if filter_data.is_some() {
206            Some(filter_func as _)
207        } else {
208            None
209        };
210        unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
211            unsafe {
212                let _callback = Box_::from_raw(
213                    data as *mut Option<
214                        Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>,
215                    >,
216                );
217            }
218        }
219        let destroy_call3 = Some(user_destroy_func as _);
220        let super_callback0: Box_<
221            Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>,
222        > = filter_data;
223        unsafe {
224            ffi::gsk_render_replay_set_font_filter(
225                self.to_glib_none_mut().0,
226                filter,
227                Box_::into_raw(super_callback0) as *mut _,
228                destroy_call3,
229            );
230        }
231    }
232
233    /// Sets the function to use as a node filter.
234    ///
235    /// This is the most complex function to use for replaying nodes.
236    /// It can either:
237    ///
238    /// * keep the node and just return it unchanged
239    ///
240    /// * create a replacement node and return that
241    ///
242    /// * discard the node by returning `NULL`
243    ///
244    /// * call [`default()`][Self::default()] to have the default handler
245    ///   run for this node, which calls your function on its children
246    /// ## `filter`
247    ///
248    ///   the function to call to replay nodes
249    #[doc(alias = "gsk_render_replay_set_node_filter")]
250    pub fn set_node_filter(
251        &mut self,
252        filter: Option<Box_<dyn Fn(&RenderReplay, &RenderNode) -> Option<RenderNode> + 'static>>,
253    ) {
254        let filter_data: Box_<
255            Option<Box_<dyn Fn(&RenderReplay, &RenderNode) -> Option<RenderNode> + 'static>>,
256        > = Box_::new(filter);
257        unsafe extern "C" fn filter_func(
258            replay: *mut ffi::GskRenderReplay,
259            node: *mut ffi::GskRenderNode,
260            user_data: glib::ffi::gpointer,
261        ) -> *mut ffi::GskRenderNode {
262            unsafe {
263                let replay = from_glib_borrow(replay);
264                let node = from_glib_borrow(node);
265                let callback = &*(user_data
266                    as *mut Option<
267                        Box_<dyn Fn(&RenderReplay, &RenderNode) -> Option<RenderNode> + 'static>,
268                    >);
269                if let Some(ref callback) = *callback {
270                    callback(&replay, &node)
271                } else {
272                    panic!("cannot get closure...")
273                }
274                .to_glib_full()
275            }
276        }
277        let filter = if filter_data.is_some() {
278            Some(filter_func as _)
279        } else {
280            None
281        };
282        unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
283            unsafe {
284                let _callback = Box_::from_raw(
285                    data as *mut Option<
286                        Box_<dyn Fn(&RenderReplay, &RenderNode) -> Option<RenderNode> + 'static>,
287                    >,
288                );
289            }
290        }
291        let destroy_call3 = Some(user_destroy_func as _);
292        let super_callback0: Box_<
293            Option<Box_<dyn Fn(&RenderReplay, &RenderNode) -> Option<RenderNode> + 'static>>,
294        > = filter_data;
295        unsafe {
296            ffi::gsk_render_replay_set_node_filter(
297                self.to_glib_none_mut().0,
298                filter,
299                Box_::into_raw(super_callback0) as *mut _,
300                destroy_call3,
301            );
302        }
303    }
304
305    /// Sets a filter function to be called by [`default()`][Self::default()]
306    /// for nodes that contain textures.
307    ///
308    /// You can call `GskRenderReplay::filter_texture()` to filter
309    /// a texture yourself.
310    /// ## `filter`
311    ///
312    ///   the texture filter function
313    #[doc(alias = "gsk_render_replay_set_texture_filter")]
314    pub fn set_texture_filter(
315        &mut self,
316        filter: Option<Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>>,
317    ) {
318        let filter_data: Box_<
319            Option<Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>>,
320        > = Box_::new(filter);
321        unsafe extern "C" fn filter_func(
322            replay: *mut ffi::GskRenderReplay,
323            texture: *mut gdk::ffi::GdkTexture,
324            user_data: glib::ffi::gpointer,
325        ) -> *mut gdk::ffi::GdkTexture {
326            unsafe {
327                let replay = from_glib_borrow(replay);
328                let texture = from_glib_borrow(texture);
329                let callback = &*(user_data
330                    as *mut Option<
331                        Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>,
332                    >);
333                if let Some(ref callback) = *callback {
334                    callback(&replay, &texture)
335                } else {
336                    panic!("cannot get closure...")
337                }
338                .to_glib_full()
339            }
340        }
341        let filter = if filter_data.is_some() {
342            Some(filter_func as _)
343        } else {
344            None
345        };
346        unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
347            unsafe {
348                let _callback = Box_::from_raw(
349                    data as *mut Option<
350                        Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>,
351                    >,
352                );
353            }
354        }
355        let destroy_call3 = Some(user_destroy_func as _);
356        let super_callback0: Box_<
357            Option<Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>>,
358        > = filter_data;
359        unsafe {
360            ffi::gsk_render_replay_set_texture_filter(
361                self.to_glib_none_mut().0,
362                filter,
363                Box_::into_raw(super_callback0) as *mut _,
364                destroy_call3,
365            );
366        }
367    }
368}
369
370#[cfg(feature = "v4_22")]
371#[cfg_attr(docsrs, doc(cfg(feature = "v4_22")))]
372impl Default for RenderReplay {
373    fn default() -> Self {
374        Self::new()
375    }
376}