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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#![allow(unused_variables)]

use crate::prelude::*;
use crate::{Actor, BoxLayout, ItemFactory, Model, Widget};
use glib::signal::SignalHandlerId;
use std::{cell::RefCell, fmt};

#[derive(Clone, Debug)]
pub struct AttributeData {
    pub name: String,
    pub col: usize,
}

#[derive(Clone, Debug)]
pub struct ListViewProps {
    pub model: Option<Model>,
    pub attributes: Vec<AttributeData>,
    pub item_type: glib::types::Type,
    pub factory: Option<ItemFactory>,
    pub filter_changed: u64,
    pub row_added: u64,
    pub row_changed: u64,
    pub row_removed: u64,
    pub sort_changed: u64,
    pub is_frozen: bool,
}

#[derive(Clone, Debug)]
pub struct ListView {
    props: RefCell<ListViewProps>,
    widget: Widget,
}

impl ListView {
    pub fn new() -> ListView {
        // assert_initialized_main_thread!();
        // unsafe { Actor::from_glib_none(ffi::listview_new()).unsafe_cast() }
        unimplemented!()
    }
}

impl Default for ListView {
    fn default() -> Self {
        Self::new()
    }
}

impl Object for ListView {}
impl Is<ListView> for ListView {}

impl AsRef<ListView> for ListView {
    fn as_ref(&self) -> &ListView {
        self
    }
}

impl Is<BoxLayout> for ListView {}

impl AsRef<BoxLayout> for ListView {
    fn as_ref(&self) -> &BoxLayout {
        // &self.widget
        unimplemented!()
    }
}

impl Is<Widget> for ListView {}

impl AsRef<Widget> for ListView {
    fn as_ref(&self) -> &Widget {
        &self.widget
    }
}

impl Is<Actor> for ListView {}

impl AsRef<Actor> for ListView {
    fn as_ref(&self) -> &Actor {
        let actor: &Actor = self.widget.as_ref();
        actor
    }
}

pub trait ListViewExt: 'static {
    /// add_attribute:
    /// @listview: An #ListView
    /// @attribute: Name of the attribute
    /// @column: Column number
    ///
    /// Adds an attribute mapping between the current model and the objects from the
    /// cell renderer.
    ///
    fn add_attribute(&self, attribute: &str, column: u32);

    /// freeze:
    /// @listview: An #ListView
    ///
    /// Freeze the view. This means that the view will not act on changes to the
    /// model until it is thawed. Call #thaw to thaw the view.
    ///
    fn freeze(&self);

    /// get_factory:
    /// @listview: A #ListView
    ///
    /// Gets the #ItemFactory used for creating new list items.
    ///
    /// Returns: (transfer none): A #ItemFactory.
    ///
    fn get_factory(&self) -> Option<ItemFactory>;

    /// get_item_type:
    /// @listview: An #ListView
    ///
    /// Get the item type currently being used to create items
    ///
    /// Returns: a #GType
    ///
    fn get_item_type(&self) -> glib::types::Type;

    /// get_model:
    /// @listview: An #ListView
    ///
    /// Get the model currently used by the #ListView
    ///
    /// Returns: (transfer none): the current #Model
    ///
    fn get_model(&self) -> Option<Model>;

    /// set_factory:
    /// @listview: A #ListView
    /// @factory: (allow-none): A #ItemFactory
    ///
    /// Sets @factory to be the factory used for creating new list items
    ///
    fn set_factory(&self, factory: Option<&ItemFactory>);

    /// set_item_type:
    /// @listview: An #ListView
    /// @item_type: A #GType
    ///
    /// Set the item type used to create items representing each row in the
    /// model
    ///
    fn set_item_type(&self, item_type: glib::types::Type);

    /// set_model:
    /// @listview: An #ListView
    /// @model: A #Model
    ///
    /// Set the model used by the #ListView
    ///
    fn set_model<P: Is<Model>>(&self, model: &P);

    /// thaw:
    /// @listview: An #ListView
    ///
    /// Thaw the view. This means that the view will now act on changes to the
    /// model.
    ///
    fn thaw(&self);

    fn connect_property_factory_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_item_type_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    fn connect_property_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: Is<ListView>> ListViewExt for O {
    /// add_attribute:
    /// @listview: An #ListView
    /// @attribute: Name of the attribute
    /// @column: Column number
    ///
    /// Adds an attribute mapping between the current model and the objects from the
    /// cell renderer.
    ///
    fn add_attribute(&self, attribute: &str, column: u32) {
        let listview = self.as_ref();

        // let prop = g_new (AttributeData, 1);
        // prop.name = g_strdup (_attribute);
        // prop.col = column;

        // listview.attributes.push(prop);
        // model_changed_cb(listview.model, listview);
    }

    /// freeze:
    /// @listview: An #ListView
    ///
    /// Freeze the view. This means that the view will not act on changes to the
    /// model until it is thawed. Call #thaw to thaw the view.
    ///
    fn freeze(&self) {
        let listview = self.as_ref();
        // listview.is_frozen = true;
    }

    /// get_factory:
    /// @listview: A #ListView
    ///
    /// Gets the #ItemFactory used for creating new list items.
    ///
    /// Returns: (transfer none): A #ItemFactory.
    ///
    fn get_factory(&self) -> Option<ItemFactory> {
        let listview = self.as_ref();
        let props = listview.props.borrow();

        props.factory.clone()
    }

    /// get_item_type:
    /// @listview: An #ListView
    ///
    /// Get the item type currently being used to create items
    ///
    /// Returns: a #GType
    ///
    fn get_item_type(&self) -> glib::types::Type {
        let listview = self.as_ref();
        let props = listview.props.borrow();

        props.item_type
    }

    /// get_model:
    /// @listview: An #ListView
    ///
    /// Get the model currently used by the #ListView
    ///
    /// Returns: (transfer none): the current #Model
    ///
    fn get_model(&self) -> Option<Model> {
        let listview = self.as_ref();
        let props = listview.props.borrow();

        props.model.clone()
    }

    /// set_factory:
    /// @listview: A #ListView
    /// @factory: (allow-none): A #ItemFactory
    ///
    /// Sets @factory to be the factory used for creating new list items
    ///
    fn set_factory(&self, factory: Option<&ItemFactory>) {
        let listview = self.as_ref();
        let mut props = listview.props.borrow_mut();

        // if listview.factory == factory {
        //     return;
        // }

        if props.factory.is_some() {
            // g_object_unref(listview.factory);
            props.factory = None;
        }

        if factory.is_some() {
            // props.factory = g_object_ref(factory);
        }

        // g_object_notify(G_OBJECT(listview), "factory");
    }

    /// set_item_type:
    /// @listview: An #ListView
    /// @item_type: A #GType
    ///
    /// Set the item type used to create items representing each row in the
    /// model
    ///
    fn set_item_type(&self, item_type: glib::types::Type) {
        let listview = self.as_ref();
        let mut props = listview.props.borrow_mut();

        props.item_type = item_type;

        // // update the view
        // model_changed_cb(listview.model, listview);
    }

    /// set_model:
    /// @listview: An #ListView
    /// @model: A #Model
    ///
    /// Set the model used by the #ListView
    ///
    fn set_model<P: Is<Model>>(&self, model: &P) {
        let listview = self.as_ref();
        let mut props = listview.props.borrow_mut();

        if props.model.is_some() {
            // g_signal_handlers_disconnect_by_func(listview.model,
            //                                     (GCallback) model_changed_cb,
            //                                     listview);
            // g_signal_handlers_disconnect_by_func(listview.model,
            //                                     (GCallback) row_changed_cb,
            //                                     listview);
            // g_signal_handlers_disconnect_by_func(listview.model,
            //                                     (GCallback) row_removed_cb,
            //                                     listview);
            // g_object_unref(listview.model);

            props.model = None;
        }

        // if model {
        //     g_return_if_fail(CLUTTER_IS_MODEL(model));

        //     props.model = g_object_ref (model);

        //     props.filter_changed = g_signal_connect(listview.model,
        //                                         "filter-changed",
        //                                         G_CALLBACK(model_changed_cb),
        //                                         listview);

        //     props.row_added = g_signal_connec (listview.model,
        //                                     "row-added",
        //                                     G_CALLBAC (row_changed_cb),
        //                                     listview);

        //     props.row_changed = g_signal_connect (listview.model,
        //                                         "row-changed",
        //                                         G_CALLBACK(row_changed_cb),
        //                                         listview);

        //     // model_changed_cb (called from row_changed_cb) expect the row to already
        //     // have been removed, thus we need to use _after
        //     props.row_removed = g_signal_connect_after(listview.model,
        //                                             "row-removed",
        //                                             G_CALLBACK(row_removed_cb),
        //                                             listview);

        //     props.sort_changed = g_signal_connect(listview.model,
        //                                         "sort-changed",
        //                                         G_CALLBACK(model_changed_cb),
        //                                         listview);

        //     // Only do this inside this block, setting the model to None should have
        //     // the effect of preserving the view; just disconnect the handlers
        //     model_changed_cb(listview.model, listview);
        // }
    }

    /// thaw:
    /// @listview: An #ListView
    ///
    /// Thaw the view. This means that the view will now act on changes to the
    /// model.
    ///
    fn thaw(&self) {
        let listview = self.as_ref();
        let mut props = listview.props.borrow_mut();

        props.is_frozen = false;

        // // Repopulate
        // model_changed_cb(listview.model, listview);
    }

    fn connect_property_factory_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        // unsafe extern "C" fn notify_factory_trampoline<P, F: Fn(&P) + 'static>(
        //     this: *mut ffi::ListView,
        //     _param_spec: glib_sys::gpointer,
        //     f: glib_sys::gpointer,
        // ) where
        //     P: Is<ListView>,
        // {
        //     let f: &F = &*(f as *const F);
        //     f(&ListView::from_glib_borrow(this).unsafe_cast_ref())
        // }
        // unsafe {
        //     let f: Box_<F> = Box_::new(f);
        //     connect_raw(
        //         self.as_ptr() as *mut _,
        //         b"notify::factory\0".as_ptr() as *const _,
        //         Some(transmute::<_, unsafe extern "C" fn()>(
        //             notify_factory_trampoline::<Self, F> as *const (),
        //         )),
        //         Box_::into_raw(f),
        //     )
        // }
        unimplemented!()
    }

    fn connect_property_item_type_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        // unsafe extern "C" fn notify_item_type_trampoline<P, F: Fn(&P) + 'static>(
        //     this: *mut ffi::ListView,
        //     _param_spec: glib_sys::gpointer,
        //     f: glib_sys::gpointer,
        // ) where
        //     P: Is<ListView>,
        // {
        //     let f: &F = &*(f as *const F);
        //     f(&ListView::from_glib_borrow(this).unsafe_cast_ref())
        // }
        // unsafe {
        //     let f: Box_<F> = Box_::new(f);
        //     connect_raw(
        //         self.as_ptr() as *mut _,
        //         b"notify::item-type\0".as_ptr() as *const _,
        //         Some(transmute::<_, unsafe extern "C" fn()>(
        //             notify_item_type_trampoline::<Self, F> as *const (),
        //         )),
        //         Box_::into_raw(f),
        //     )
        // }
        unimplemented!()
    }

    fn connect_property_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        // unsafe extern "C" fn notify_model_trampoline<P, F: Fn(&P) + 'static>(
        //     this: *mut ffi::ListView,
        //     _param_spec: glib_sys::gpointer,
        //     f: glib_sys::gpointer,
        // ) where
        //     P: Is<ListView>,
        // {
        //     let f: &F = &*(f as *const F);
        //     f(&ListView::from_glib_borrow(this).unsafe_cast_ref())
        // }
        // unsafe {
        //     let f: Box_<F> = Box_::new(f);
        //     connect_raw(
        //         self.as_ptr() as *mut _,
        //         b"notify::model\0".as_ptr() as *const _,
        //         Some(transmute::<_, unsafe extern "C" fn()>(
        //             notify_model_trampoline::<Self, F> as *const (),
        //         )),
        //         Box_::into_raw(f),
        //     )
        // }
        unimplemented!()
    }
}

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