Skip to main content

gtk4/
list_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::{ListBox, ListBoxRow, Ordering, ffi};
8
9impl ListBox {
10    #[doc(alias = "gtk_list_box_bind_model")]
11    #[doc(alias = "bind_model")]
12    pub fn unbind_model(&self) {
13        unsafe {
14            ffi::gtk_list_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_list_box_set_filter_func")]
25    #[doc(alias = "set_filter_func")]
26    pub fn unset_filter_func(&self) {
27        unsafe {
28            ffi::gtk_list_box_set_filter_func(self.to_glib_none().0, None, ptr::null_mut(), None)
29        }
30    }
31
32    #[doc(alias = "gtk_list_box_set_header_func")]
33    #[doc(alias = "set_header_func")]
34    pub fn unset_header_func(&self) {
35        unsafe {
36            ffi::gtk_list_box_set_header_func(self.to_glib_none().0, None, ptr::null_mut(), None)
37        }
38    }
39
40    /// Sets a sort function.
41    ///
42    /// By setting a sort function on the @self one can dynamically reorder
43    /// the rows of the list, based on the contents of the rows.
44    ///
45    /// The @sort_func will be called for each row after the call, and will
46    /// continue to be called each time a row changes (via
47    /// [`ListBoxRowExt::changed()`][crate::prelude::ListBoxRowExt::changed()]) and when [`invalidate_sort()`][Self::invalidate_sort()]
48    /// is called.
49    ///
50    /// Note that using a sort function is incompatible with using a model
51    /// (see [`bind_model()`][Self::bind_model()]).
52    /// ## `sort_func`
53    /// the sort function
54    #[doc(alias = "gtk_list_box_set_sort_func")]
55    pub fn set_sort_func<P: Fn(&ListBoxRow, &ListBoxRow) -> Ordering + 'static>(
56        &self,
57        sort_func: P,
58    ) {
59        let sort_func_data: Box_<P> = Box_::new(sort_func);
60        unsafe extern "C" fn sort_func_func<
61            P: Fn(&ListBoxRow, &ListBoxRow) -> Ordering + 'static,
62        >(
63            row1: *mut ffi::GtkListBoxRow,
64            row2: *mut ffi::GtkListBoxRow,
65            user_data: glib::ffi::gpointer,
66        ) -> libc::c_int {
67            unsafe {
68                let row1 = from_glib_borrow(row1);
69                let row2 = from_glib_borrow(row2);
70                let callback: &P = &*(user_data as *mut _);
71                let res = (*callback)(&row1, &row2);
72                res.into_glib()
73            }
74        }
75        let sort_func = Some(sort_func_func::<P> as _);
76        unsafe extern "C" fn destroy_func<P: Fn(&ListBoxRow, &ListBoxRow) -> Ordering + 'static>(
77            data: glib::ffi::gpointer,
78        ) {
79            unsafe {
80                let _callback: Box_<P> = Box_::from_raw(data as *mut _);
81            }
82        }
83        let destroy_call3 = Some(destroy_func::<P> as _);
84        let super_callback0: Box_<P> = sort_func_data;
85        unsafe {
86            ffi::gtk_list_box_set_sort_func(
87                self.to_glib_none().0,
88                sort_func,
89                Box_::into_raw(super_callback0) as *mut _,
90                destroy_call3,
91            );
92        }
93    }
94
95    #[doc(alias = "gtk_list_box_set_sort_func")]
96    #[doc(alias = "set_sort_func")]
97    pub fn unset_sort_func(&self) {
98        unsafe {
99            ffi::gtk_list_box_set_sort_func(self.to_glib_none().0, None, ptr::null_mut(), None)
100        }
101    }
102}