1use gio_sys;
6#[cfg(any(feature = "v2_44", feature = "dox"))]
7use glib;
8use glib::object::Cast;
9use glib::object::IsA;
10use glib::translate::*;
11use glib::StaticType;
12use glib::ToValue;
13use std::fmt;
14use ListModel;
15
16glib_wrapper! {
17 pub struct ListStore(Object<gio_sys::GListStore, gio_sys::GListStoreClass, ListStoreClass>) @implements ListModel;
18
19 match fn {
20 get_type => || gio_sys::g_list_store_get_type(),
21 }
22}
23
24impl ListStore {
25 #[cfg(any(feature = "v2_44", feature = "dox"))]
26 pub fn new(item_type: glib::types::Type) -> ListStore {
27 unsafe { from_glib_full(gio_sys::g_list_store_new(item_type.to_glib())) }
28 }
29}
30
31pub struct ListStoreBuilder {
32 #[cfg(any(feature = "v2_44", feature = "dox"))]
33 item_type: Option<glib::types::Type>,
34}
35
36impl ListStoreBuilder {
37 pub fn new() -> Self {
38 Self {
39 #[cfg(any(feature = "v2_44", feature = "dox"))]
40 item_type: None,
41 }
42 }
43
44 pub fn build(self) -> ListStore {
45 let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
46 #[cfg(any(feature = "v2_44", feature = "dox"))]
47 {
48 if let Some(ref item_type) = self.item_type {
49 properties.push(("item-type", item_type));
50 }
51 }
52 glib::Object::new(ListStore::static_type(), &properties)
53 .expect("object new")
54 .downcast()
55 .expect("downcast")
56 }
57
58 #[cfg(any(feature = "v2_44", feature = "dox"))]
59 pub fn item_type(mut self, item_type: glib::types::Type) -> Self {
60 self.item_type = Some(item_type);
61 self
62 }
63}
64
65pub const NONE_LIST_STORE: Option<&ListStore> = None;
66
67pub trait ListStoreExt: 'static {
68 #[cfg(any(feature = "v2_44", feature = "dox"))]
69 fn append<P: IsA<glib::Object>>(&self, item: &P);
70
71 #[cfg(any(feature = "v2_44", feature = "dox"))]
72 fn insert<P: IsA<glib::Object>>(&self, position: u32, item: &P);
73
74 #[cfg(any(feature = "v2_44", feature = "dox"))]
78 fn remove(&self, position: u32);
79
80 #[cfg(any(feature = "v2_44", feature = "dox"))]
81 fn remove_all(&self);
82
83 #[cfg(any(feature = "v2_44", feature = "dox"))]
87 fn splice(&self, position: u32, n_removals: u32, additions: &[glib::Object]);
88}
89
90impl<O: IsA<ListStore>> ListStoreExt for O {
91 #[cfg(any(feature = "v2_44", feature = "dox"))]
92 fn append<P: IsA<glib::Object>>(&self, item: &P) {
93 unsafe {
94 gio_sys::g_list_store_append(
95 self.as_ref().to_glib_none().0,
96 item.as_ref().to_glib_none().0,
97 );
98 }
99 }
100
101 #[cfg(any(feature = "v2_44", feature = "dox"))]
102 fn insert<P: IsA<glib::Object>>(&self, position: u32, item: &P) {
103 unsafe {
104 gio_sys::g_list_store_insert(
105 self.as_ref().to_glib_none().0,
106 position,
107 item.as_ref().to_glib_none().0,
108 );
109 }
110 }
111
112 #[cfg(any(feature = "v2_44", feature = "dox"))]
118 fn remove(&self, position: u32) {
119 unsafe {
120 gio_sys::g_list_store_remove(self.as_ref().to_glib_none().0, position);
121 }
122 }
123
124 #[cfg(any(feature = "v2_44", feature = "dox"))]
125 fn remove_all(&self) {
126 unsafe {
127 gio_sys::g_list_store_remove_all(self.as_ref().to_glib_none().0);
128 }
129 }
130
131 #[cfg(any(feature = "v2_44", feature = "dox"))]
137 fn splice(&self, position: u32, n_removals: u32, additions: &[glib::Object]) {
138 let n_additions = additions.len() as u32;
139 unsafe {
140 gio_sys::g_list_store_splice(
141 self.as_ref().to_glib_none().0,
142 position,
143 n_removals,
144 additions.to_glib_none().0,
145 n_additions,
146 );
147 }
148 }
149}
150
151impl fmt::Display for ListStore {
152 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
153 write!(f, "ListStore")
154 }
155}