1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::{TreeIter, TreeModel, TreeStore};
use glib::{translate::*, Cast, ToValue, Type, Value};
use libc::c_int;
impl TreeStore {
/// Creates a new tree store.
///
/// The tree store will have @n_columns, with each column using the
/// corresponding type passed to this function.
///
/// Note that only types derived from standard GObject fundamental types
/// are supported.
///
/// As an example:
///
/// **⚠️ The following code is in c ⚠️**
///
/// ```c
/// gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING, GDK_TYPE_TEXTURE);
/// ```
///
/// will create a new [`TreeStore`][crate::TreeStore] with three columns of type
/// `int`, `gchararray`, and [`gdk::Texture`][crate::gdk::Texture] respectively.
///
/// # Deprecated since 4.10
///
/// Use [`TreeListModel`][crate::TreeListModel] instead
///
/// # Returns
///
/// a new [`TreeStore`][crate::TreeStore]
#[doc(alias = "gtk_tree_store_newv")]
#[doc(alias = "gtk_tree_store_new")]
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub fn new(column_types: &[Type]) -> Self {
assert_initialized_main_thread!();
unsafe {
let mut column_types = column_types
.iter()
.map(|t| t.into_glib())
.collect::<Vec<_>>();
from_glib_full(ffi::gtk_tree_store_newv(
column_types.len() as c_int,
column_types.as_mut_ptr(),
))
}
}
/// A variant of gtk_tree_store_insert_with_values() which takes
/// the columns and values as two arrays, instead of varargs.
///
/// This function is mainly intended for language bindings.
///
/// # Deprecated since 4.10
///
/// Use [`TreeListModel`][crate::TreeListModel] instead
/// ## `parent`
/// A valid [`TreeIter`][crate::TreeIter]
/// ## `position`
/// position to insert the new row, or -1 for last
/// ## `columns`
/// an array of column numbers
/// ## `values`
/// an array of GValues
///
/// # Returns
///
///
/// ## `iter`
/// An unset [`TreeIter`][crate::TreeIter] to set the new row
#[doc(alias = "gtk_tree_store_insert_with_values")]
#[doc(alias = "gtk_tree_store_insert_with_valuesv")]
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub fn insert_with_values(
&self,
parent: Option<&TreeIter>,
position: Option<u32>,
columns_and_values: &[(u32, &dyn ToValue)],
) -> TreeIter {
unsafe {
assert!(
position.unwrap_or(0) <= i32::max_value() as u32,
"can't have more than {} rows",
i32::max_value()
);
let n_columns =
ffi::gtk_tree_model_get_n_columns(self.upcast_ref::<TreeModel>().to_glib_none().0)
as u32;
assert!(
columns_and_values.len() <= n_columns as usize,
"got values for {} columns but only {n_columns} columns exist",
columns_and_values.len(),
);
for (column, value) in columns_and_values {
assert!(
*column < n_columns,
"got column {column} which is higher than the number of columns {n_columns}",
);
let type_ = from_glib(ffi::gtk_tree_model_get_column_type(
self.upcast_ref::<TreeModel>().to_glib_none().0,
*column as c_int,
));
assert!(
Value::type_transformable(value.value_type(), type_),
"column {column} is of type {} but found value of type {type_}",
value.value_type()
);
}
let columns = columns_and_values
.iter()
.map(|(c, _)| *c)
.collect::<Vec<_>>();
let values = columns_and_values
.iter()
.map(|(_, v)| v.to_value())
.collect::<Vec<_>>();
let mut iter = TreeIter::uninitialized();
ffi::gtk_tree_store_insert_with_valuesv(
self.to_glib_none().0,
iter.to_glib_none_mut().0,
mut_override(parent.to_glib_none().0),
position.map_or(-1, |n| n as c_int),
mut_override(columns.as_ptr() as *const c_int),
mut_override(values.as_ptr() as *const glib::gobject_ffi::GValue),
columns.len() as c_int,
);
iter
}
}
/// Reorders the children of @parent in @self to follow the order
/// indicated by @new_order.
///
/// Note that this function only works with unsorted stores.
///
/// # Deprecated since 4.10
///
/// Use [`TreeListModel`][crate::TreeListModel] instead
/// ## `parent`
/// the parent of the children to re-order
/// ## `new_order`
/// an array of integers mapping the new position
/// of each child to its old position before the re-ordering,
/// i.e. `new_order[newpos] = oldpos`
#[doc(alias = "gtk_tree_store_reorder")]
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub fn reorder(&self, parent: &TreeIter, new_order: &[u32]) {
unsafe {
let count = ffi::gtk_tree_model_iter_n_children(
self.upcast_ref::<TreeModel>().to_glib_none().0,
mut_override(parent.to_glib_none().0),
);
let safe_count = count as usize == new_order.len();
debug_assert!(
safe_count,
"Incorrect `new_order` slice length. Expected `{count}`, found `{}`.",
new_order.len()
);
let safe_values = new_order.iter().max().map_or(true, |&max| {
let max = max as i32;
max >= 0 && max < count
});
debug_assert!(
safe_values,
"Some `new_order` slice values are out of range. Maximum safe value: \
`{}`. The slice contents: `{new_order:?}`",
count - 1,
);
if safe_count && safe_values {
ffi::gtk_tree_store_reorder(
self.to_glib_none().0,
mut_override(parent.to_glib_none().0),
mut_override(new_order.as_ptr() as *const c_int),
);
}
}
}
/// Sets the value of one or more cells in the row referenced by @iter.
///
/// The variable argument list should contain integer column numbers,
/// each column number followed by the value to be set.
///
/// The list is terminated by a value of `-1`.
///
/// For example, to set column 0 with type `G_TYPE_STRING` to “Foo”, you would
/// write
///
/// **⚠️ The following code is in c ⚠️**
///
/// ```c
/// gtk_tree_store_set (store, iter, 0, "Foo", -1);
/// ```
///
/// The value will be referenced by the store if it is a `G_TYPE_OBJECT`, and it
/// will be copied if it is a `G_TYPE_STRING` or `G_TYPE_BOXED`.
///
/// # Deprecated since 4.10
///
/// Use [`TreeListModel`][crate::TreeListModel] instead
/// ## `iter`
/// A valid [`TreeIter`][crate::TreeIter] for the row being modified
#[doc(alias = "gtk_tree_store_set")]
#[doc(alias = "gtk_tree_store_set_valuesv")]
#[doc(alias = "gtk_tree_store_set_valist")]
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub fn set(&self, iter: &TreeIter, columns_and_values: &[(u32, &dyn ToValue)]) {
unsafe {
let n_columns =
ffi::gtk_tree_model_get_n_columns(self.upcast_ref::<TreeModel>().to_glib_none().0)
as u32;
assert!(
columns_and_values.len() <= n_columns as usize,
"got values for {} columns but only {n_columns} columns exist",
columns_and_values.len(),
);
for (column, value) in columns_and_values {
assert!(
*column < n_columns,
"got column {column} which is higher than the number of columns {n_columns}",
);
let type_ = from_glib(ffi::gtk_tree_model_get_column_type(
self.upcast_ref::<TreeModel>().to_glib_none().0,
*column as c_int,
));
assert!(
Value::type_transformable(value.value_type(), type_),
"column {column} is of type {type_} but found value of type {}",
value.value_type()
);
}
let columns = columns_and_values
.iter()
.map(|(c, _)| *c)
.collect::<Vec<_>>();
let values = columns_and_values
.iter()
.map(|(_, v)| v.to_value())
.collect::<Vec<_>>();
ffi::gtk_tree_store_set_valuesv(
self.to_glib_none().0,
mut_override(iter.to_glib_none().0),
mut_override(columns.as_ptr() as *const c_int),
mut_override(values.as_ptr() as *const glib::gobject_ffi::GValue),
columns.len() as c_int,
);
}
}
/// Sets the type of the columns in a tree store.
///
/// This function is meant primarily for types that inherit from
/// [`TreeStore`][crate::TreeStore], and should only be used when constructing a new
/// [`TreeStore`][crate::TreeStore].
///
/// This functions cannot be called after a row has been added,
/// or a method on the [`TreeModel`][crate::TreeModel] interface is called on the
/// tree store.
///
/// # Deprecated since 4.10
///
/// Use [`TreeListModel`][crate::TreeListModel] instead
/// ## `types`
/// An array of `GType` types, one for each column
#[doc(alias = "gtk_tree_store_set_column_types")]
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub fn set_column_types(&self, types: &[glib::Type]) {
unsafe {
let types_ptr: Vec<glib::ffi::GType> = types.iter().map(|t| t.into_glib()).collect();
ffi::gtk_tree_store_set_column_types(
self.to_glib_none().0,
types.len() as i32,
mut_override(types_ptr.as_ptr()),
)
}
}
/// Sets the data in the cell specified by @iter and @column.
///
/// The type of @value must be convertible to the type of the
/// column.
///
/// # Deprecated since 4.10
///
/// Use [`TreeListModel`][crate::TreeListModel] instead
/// ## `iter`
/// A valid [`TreeIter`][crate::TreeIter] for the row being modified
/// ## `column`
/// column number to modify
/// ## `value`
/// new value for the cell
#[doc(alias = "gtk_tree_store_set_value")]
#[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
#[allow(deprecated)]
pub fn set_value(&self, iter: &TreeIter, column: u32, value: &Value) {
unsafe {
let columns =
ffi::gtk_tree_model_get_n_columns(self.upcast_ref::<TreeModel>().to_glib_none().0)
as u32;
assert!(
column < columns,
"got column {column} which is higher than the number of columns {columns}",
);
let type_ = from_glib(ffi::gtk_tree_model_get_column_type(
self.upcast_ref::<TreeModel>().to_glib_none().0,
column as c_int,
));
assert!(
Value::type_transformable(value.type_(), type_),
"column {column} is of type {type_} but found value of type {}",
value.type_()
);
ffi::gtk_tree_store_set_value(
self.to_glib_none().0,
mut_override(iter.to_glib_none().0),
column as c_int,
mut_override(value.to_glib_none().0),
);
}
}
}