gtk4/
flow_box.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_, ptr};
4
5use glib::translate::*;
6
7use crate::{ffi, FlowBox, FlowBoxChild, Ordering};
8
9impl FlowBox {
10    #[doc(alias = "gtk_flow_box_bind_model")]
11    #[doc(alias = "bind_model")]
12    pub fn unbind_model(&self) {
13        unsafe {
14            ffi::gtk_flow_box_bind_model(
15                self.to_glib_none().0,
16                ptr::null_mut(),
17                None,
18                ptr::null_mut(),
19                None,
20            )
21        }
22    }
23
24    #[doc(alias = "gtk_flow_box_set_filter_func")]
25    #[doc(alias = "set_filter_func")]
26    pub fn unset_filter_func(&self) {
27        unsafe {
28            ffi::gtk_flow_box_set_filter_func(self.to_glib_none().0, None, ptr::null_mut(), None)
29        }
30    }
31
32    #[doc(alias = "gtk_flow_box_set_sort_func")]
33    #[doc(alias = "set_sort_func")]
34    pub fn unset_sort_func(&self) {
35        unsafe {
36            ffi::gtk_flow_box_set_sort_func(self.to_glib_none().0, None, ptr::null_mut(), None)
37        }
38    }
39
40    /// By setting a sort function on the @self, one can dynamically
41    /// reorder the children of the box, based on the contents of
42    /// the children.
43    ///
44    /// The @sort_func will be called for each child after the call,
45    /// and will continue to be called each time a child changes (via
46    /// [`FlowBoxChildExt::changed()`][crate::prelude::FlowBoxChildExt::changed()]) and when
47    /// [`invalidate_sort()`][Self::invalidate_sort()] is called.
48    ///
49    /// Note that using a sort function is incompatible with using a model
50    /// (see [`bind_model()`][Self::bind_model()]).
51    /// ## `sort_func`
52    /// the sort function
53    #[doc(alias = "gtk_flow_box_set_sort_func")]
54    pub fn set_sort_func<P: Fn(&FlowBoxChild, &FlowBoxChild) -> Ordering + 'static>(
55        &self,
56        sort_func: P,
57    ) {
58        let sort_func_data: Box_<P> = Box_::new(sort_func);
59        unsafe extern "C" fn sort_func_func<
60            P: Fn(&FlowBoxChild, &FlowBoxChild) -> Ordering + 'static,
61        >(
62            child1: *mut ffi::GtkFlowBoxChild,
63            child2: *mut ffi::GtkFlowBoxChild,
64            user_data: glib::ffi::gpointer,
65        ) -> libc::c_int {
66            let child1 = from_glib_borrow(child1);
67            let child2 = from_glib_borrow(child2);
68            let callback: &P = &*(user_data as *mut _);
69            let res = (*callback)(&child1, &child2);
70            res.into_glib()
71        }
72        let sort_func = Some(sort_func_func::<P> as _);
73        unsafe extern "C" fn destroy_func<
74            P: Fn(&FlowBoxChild, &FlowBoxChild) -> Ordering + 'static,
75        >(
76            data: glib::ffi::gpointer,
77        ) {
78            let _callback: Box_<P> = Box_::from_raw(data as *mut _);
79        }
80        let destroy_call3 = Some(destroy_func::<P> as _);
81        let super_callback0: Box_<P> = sort_func_data;
82        unsafe {
83            ffi::gtk_flow_box_set_sort_func(
84                self.to_glib_none().0,
85                sort_func,
86                Box_::into_raw(super_callback0) as *mut _,
87                destroy_call3,
88            );
89        }
90    }
91}