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
// Copyright 2013-2016, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use ffi;
use glib::object::IsA;
use glib::translate::*;
use glib::{Type, ToValue, Value};
use libc::c_int;
use TreeIter;
use TreeStore;
use TreeModel;

impl TreeStore {
    pub fn new(column_types: &[Type]) -> TreeStore {
        assert_initialized_main_thread!();
        unsafe {
            let mut column_types = column_types.iter().map(|t| t.to_glib()).collect::<Vec<_>>();
            from_glib_full(
                ffi::gtk_tree_store_newv(column_types.len() as c_int,
                    column_types.as_mut_ptr()))
        }
    }
}

pub trait TreeStoreExtManual {
    fn insert_with_values(&self, parent: Option<&TreeIter>, position: Option<u32>, columns: &[u32],
            values: &[&ToValue]) -> TreeIter;

    fn reorder(&self, parent: &TreeIter, new_order: &[u32]);

    fn set(&self, iter: &TreeIter, columns: &[u32], values: &[&ToValue]);

    fn set_value(&self, iter: &TreeIter, column: u32, value: &Value);
}

impl<O: IsA<TreeStore> + IsA<TreeModel>> TreeStoreExtManual for O {
    fn insert_with_values(&self, parent: Option<&TreeIter>, position: Option<u32>, columns: &[u32],
            values: &[&ToValue]) -> TreeIter {
        unsafe {
            assert!(position.unwrap_or(0) <= i32::max_value() as u32);
            assert!(columns.len() == values.len());
            let n_columns = ffi::gtk_tree_model_get_n_columns(self.to_glib_none().0) as u32;
            assert!(columns.len() <= n_columns as usize);
            for (&column, value) in columns.iter().zip(values.iter()) {
                let type_ = from_glib(
                    ffi::gtk_tree_model_get_column_type(self.to_glib_none().0, column as c_int));
                assert!(Value::type_transformable(value.to_value_type(), type_));
            }
            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),
                values.to_glib_none().0,
                columns.len() as c_int);
            iter
        }
    }

    fn reorder(&self, parent: &TreeIter, new_order: &[u32]) {
        unsafe {
            let count = ffi::gtk_tree_model_iter_n_children(self.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 `{}`, found `{}`.",
                          count,
                          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: `{:?}`",
                          count - 1,
                          new_order);
            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));
            }
        }
    }

    fn set(&self, iter: &TreeIter, columns: &[u32], values: &[&ToValue]) {
        unsafe {
            assert!(columns.len() == values.len());
            let n_columns = ffi::gtk_tree_model_get_n_columns(self.to_glib_none().0) as u32;
            assert!(columns.len() <= n_columns as usize);
            for (&column, value) in columns.iter().zip(values.iter()) {
                assert!(column < n_columns);
                let type_ = from_glib(
                    ffi::gtk_tree_model_get_column_type(self.to_glib_none().0, column as c_int));
                assert!(Value::type_transformable(value.to_value_type(), type_));
            }
            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),
                values.to_glib_none().0,
                columns.len() as c_int);
        }
    }

    fn set_value(&self, iter: &TreeIter, column: u32, value: &Value) {
        unsafe {
            let columns = ffi::gtk_tree_model_get_n_columns(self.to_glib_none().0);
            assert!(column < columns as u32);
            let type_ = from_glib(
                ffi::gtk_tree_model_get_column_type(self.to_glib_none().0, column as c_int));
            assert!(Value::type_transformable(value.type_(), 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));
        }
    }
}