gtk4/
tree_view_column.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::{ffi, prelude::*, CellRenderer, TreeViewColumn};
6
7impl TreeViewColumn {
8    /// Creates a new [`TreeViewColumn`][crate::TreeViewColumn] with a number of default values.
9    /// This is equivalent to calling gtk_tree_view_column_set_title(),
10    /// gtk_tree_view_column_pack_start(), and
11    /// gtk_tree_view_column_set_attributes() on the newly created [`TreeViewColumn`][crate::TreeViewColumn].
12    ///
13    /// Here’s a simple example:
14    ///
15    /// **⚠️ The following code is in c ⚠️**
16    ///
17    /// ```c
18    ///  enum { TEXT_COLUMN, COLOR_COLUMN, N_COLUMNS };
19    ///  // ...
20    ///  {
21    ///    GtkTreeViewColumn *column;
22    ///    GtkCellRenderer   *renderer = gtk_cell_renderer_text_new ();
23    ///
24    ///    column = gtk_tree_view_column_new_with_attributes ("Title",
25    ///                                                       renderer,
26    ///                                                       "text", TEXT_COLUMN,
27    ///                                                       "foreground", COLOR_COLUMN,
28    ///                                                       NULL);
29    ///  }
30    /// ```
31    ///
32    /// # Deprecated since 4.10
33    ///
34    /// Use GtkColumnView instead
35    /// ## `title`
36    /// The title to set the header to
37    /// ## `cell`
38    /// The [`CellRenderer`][crate::CellRenderer]
39    ///
40    /// # Returns
41    ///
42    /// A newly created [`TreeViewColumn`][crate::TreeViewColumn].
43    #[doc(alias = "gtk_tree_view_column_new_with_attributes")]
44    #[doc(alias = "new_with_attributes")]
45    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
46    #[allow(deprecated)]
47    pub fn with_attributes(
48        title: &str,
49        cell_renderer: &impl IsA<CellRenderer>,
50        attributes: &[(&str, i32)],
51    ) -> Self {
52        assert_initialized_main_thread!();
53        let tree_view_column = TreeViewColumn::new();
54        tree_view_column.set_title(title);
55        tree_view_column.pack_start(cell_renderer, true);
56        tree_view_column.set_attributes(cell_renderer, attributes);
57
58        tree_view_column
59    }
60
61    /// Sets the attributes in the list as the attributes of @self.
62    ///
63    /// The attributes should be in attribute/column order, as in
64    /// gtk_tree_view_column_add_attribute(). All existing attributes
65    /// are removed, and replaced with the new attributes.
66    ///
67    /// # Deprecated since 4.10
68    ///
69    /// Use GtkColumnView instead
70    /// ## `cell_renderer`
71    /// the [`CellRenderer`][crate::CellRenderer] we’re setting the attributes of
72    #[doc(alias = "gtk_tree_view_column_set_attributes")]
73    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
74    #[allow(deprecated)]
75    pub fn set_attributes(
76        &self,
77        cell_renderer: &impl IsA<CellRenderer>,
78        attributes: &[(&str, i32)],
79    ) {
80        self.clear_attributes(cell_renderer);
81        attributes.iter().for_each(|(attribute, column)| {
82            self.add_attribute(cell_renderer, attribute, *column);
83        });
84    }
85
86    #[doc(alias = "gtk_tree_view_column_set_cell_data_func")]
87    #[doc(alias = "set_cell_data_func")]
88    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
89    #[allow(deprecated)]
90    pub fn unset_cell_data_func(&self, cell_renderer: &impl IsA<CellRenderer>) {
91        unsafe {
92            ffi::gtk_tree_view_column_set_cell_data_func(
93                self.to_glib_none().0,
94                cell_renderer.as_ref().to_glib_none().0,
95                None,
96                std::ptr::null_mut(),
97                None,
98            );
99        }
100    }
101}