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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use gio_sys;
#[cfg(any(feature = "v2_44", feature = "dox"))]
use glib;
#[cfg(any(feature = "v2_44", feature = "dox"))]
use glib::object::Cast;
#[cfg(any(feature = "v2_44", feature = "dox"))]
use glib::object::IsA;
use glib::translate::*;
#[cfg(any(feature = "v2_44", feature = "dox"))]
use glib::StaticType;
#[cfg(any(feature = "v2_44", feature = "dox"))]
use glib::ToValue;
use std::fmt;
use ListModel;

glib_wrapper! {
    /// `ListStore` is a simple implementation of `ListModel` that stores all
    /// items in memory.
    ///
    /// It provides insertions, deletions, and lookups in logarithmic time
    /// with a fast path for the common case of iterating the list linearly.
    ///
    /// Feature: `v2_44`
    ///
    /// # Implements
    ///
    /// [`ListStoreExt`](trait.ListStoreExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html), [`ListModelExt`](trait.ListModelExt.html), [`ListStoreExtManual`](prelude/trait.ListStoreExtManual.html)
    pub struct ListStore(Object<gio_sys::GListStore, gio_sys::GListStoreClass, ListStoreClass>) @implements ListModel;

    match fn {
        get_type => || gio_sys::g_list_store_get_type(),
    }
}

impl ListStore {
    /// Creates a new `ListStore` with items of type `item_type`. `item_type`
    /// must be a subclass of `gobject::Object`.
    ///
    /// Feature: `v2_44`
    ///
    /// ## `item_type`
    /// the `glib::Type` of items in the list
    ///
    /// # Returns
    ///
    /// a new `ListStore`
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    pub fn new(item_type: glib::types::Type) -> ListStore {
        unsafe { from_glib_full(gio_sys::g_list_store_new(item_type.to_glib())) }
    }
}

#[derive(Clone, Default)]
pub struct ListStoreBuilder {
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    item_type: Option<glib::types::Type>,
}

impl ListStoreBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn build(self) -> ListStore {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        #[cfg(any(feature = "v2_44", feature = "dox"))]
        {
            if let Some(ref item_type) = self.item_type {
                properties.push(("item-type", item_type));
            }
        }
        glib::Object::new(ListStore::static_type(), &properties)
            .expect("object new")
            .downcast()
            .expect("downcast")
    }

    #[cfg(any(feature = "v2_44", feature = "dox"))]
    pub fn item_type(mut self, item_type: glib::types::Type) -> Self {
        self.item_type = Some(item_type);
        self
    }
}

pub const NONE_LIST_STORE: Option<&ListStore> = None;

/// Trait containing all `ListStore` methods.
///
/// Feature: `v2_44`
///
/// # Implementors
///
/// [`ListStore`](struct.ListStore.html)
pub trait ListStoreExt: 'static {
    /// Appends `item` to `self`. `item` must be of type `ListStore:item-type`.
    ///
    /// This function takes a ref on `item`.
    ///
    /// Use `ListStoreExt::splice` to append multiple items at the same time
    /// efficiently.
    ///
    /// Feature: `v2_44`
    ///
    /// ## `item`
    /// the new item
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn append<P: IsA<glib::Object>>(&self, item: &P);

    /// Inserts `item` into `self` at `position`. `item` must be of type
    /// `ListStore:item-type` or derived from it. `position` must be smaller
    /// than the length of the list, or equal to it to append.
    ///
    /// This function takes a ref on `item`.
    ///
    /// Use `ListStoreExt::splice` to insert multiple items at the same time
    /// efficiently.
    ///
    /// Feature: `v2_44`
    ///
    /// ## `position`
    /// the position at which to insert the new item
    /// ## `item`
    /// the new item
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn insert<P: IsA<glib::Object>>(&self, position: u32, item: &P);

    /// Removes the item from `self` that is at `position`. `position` must be
    /// smaller than the current length of the list.
    ///
    /// Use `ListStoreExt::splice` to remove multiple items at the same time
    /// efficiently.
    ///
    /// Feature: `v2_44`
    ///
    /// ## `position`
    /// the position of the item that is to be removed
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn remove(&self, position: u32);

    /// Removes all items from `self`.
    ///
    /// Feature: `v2_44`
    ///
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn remove_all(&self);

    /// Changes `self` by removing `n_removals` items and adding `n_additions`
    /// items to it. `additions` must contain `n_additions` items of type
    /// `ListStore:item-type`. `None` is not permitted.
    ///
    /// This function is more efficient than `ListStoreExt::insert` and
    /// `ListStoreExt::remove`, because it only emits
    /// `ListModel::items-changed` once for the change.
    ///
    /// This function takes a ref on each item in `additions`.
    ///
    /// The parameters `position` and `n_removals` must be correct (ie:
    /// `position` + `n_removals` must be less than or equal to the length of
    /// the list at the time this function is called).
    ///
    /// Feature: `v2_44`
    ///
    /// ## `position`
    /// the position at which to make the change
    /// ## `n_removals`
    /// the number of items to remove
    /// ## `additions`
    /// the items to add
    /// ## `n_additions`
    /// the number of items to add
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn splice(&self, position: u32, n_removals: u32, additions: &[glib::Object]);
}

impl<O: IsA<ListStore>> ListStoreExt for O {
    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn append<P: IsA<glib::Object>>(&self, item: &P) {
        unsafe {
            gio_sys::g_list_store_append(
                self.as_ref().to_glib_none().0,
                item.as_ref().to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn insert<P: IsA<glib::Object>>(&self, position: u32, item: &P) {
        unsafe {
            gio_sys::g_list_store_insert(
                self.as_ref().to_glib_none().0,
                position,
                item.as_ref().to_glib_none().0,
            );
        }
    }

    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn remove(&self, position: u32) {
        unsafe {
            gio_sys::g_list_store_remove(self.as_ref().to_glib_none().0, position);
        }
    }

    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn remove_all(&self) {
        unsafe {
            gio_sys::g_list_store_remove_all(self.as_ref().to_glib_none().0);
        }
    }

    #[cfg(any(feature = "v2_44", feature = "dox"))]
    fn splice(&self, position: u32, n_removals: u32, additions: &[glib::Object]) {
        let n_additions = additions.len() as u32;
        unsafe {
            gio_sys::g_list_store_splice(
                self.as_ref().to_glib_none().0,
                position,
                n_removals,
                additions.to_glib_none().0,
                n_additions,
            );
        }
    }
}

impl fmt::Display for ListStore {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "ListStore")
    }
}