gtk-rs/gtk

is it possible create custom cell renderers?

emmanueltouzery opened this issue · 10 comments

the process to create custom cell renderers is gtk is through inheritance.. I was searching for examples of custom cell renderers in gtk-rs and couldn't find any?

I need a virtualized list with custom rendering for the rows, because I'm having performance issues with ListBox with too many rows, at the same time I really need a custom rendering for the rows. If it's not possible to implement custom cell renderers I'm not sure what to do :-(

Thank you! Which other type can I use as reference for how subclassing works in gtk-rs? I didn't need subclassing so far.

There's this subclass example, for example: https://github.com/gtk-rs/examples/blob/master/src/bin/basic_subclass.rs It creates a subclass of a window and gtk::Application.

Please ask if something's not clear

Closing for now. Let me know if you run into any problems.

hello, yes indeed I've run into some problems :(

Based on your examples I got it to compile, but it fails compiling when I try to create the renderer using ::new. This is the code:
https://github.com/emmanueltouzery/cell_r

$ cargo run
thread 'main' panicked at 'not implemented', /home/emmanuel/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/macros.rs:13:23

when compiling with RUST_BACKTRACE=1, I get:

  11: std::panicking::begin_panic
             at /home/emmanuel/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/panicking.rs:438
  12: glib::subclass::types::ObjectSubclass::new
             at /home/emmanuel/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/macros.rs:13
  13: glib::subclass::types::ObjectSubclass::with_class
             at /home/emmanuel/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.10.1/src/subclass/types.rs:371
  14: glib::subclass::types::instance_init
             at /home/emmanuel/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.10.1/src/subclass/types.rs:429
  15: g_type_create_instance
  16: <unknown>
  17: g_object_newv
  18: glib::object::Object::new
             at /home/emmanuel/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-0.10.1/src/object.rs:1297
  19: cell_r::MyRenderer::new
             at src/main.rs:32

One idea that I have is that when you inherit from GtkApplication in the sample you gave me, you must call the "super" constructor, passing the 'application-id' and 'flags' parameters. But for GtkCellRenderer there is no ::new. There is one in GtkCellRendererText and other variants. Maybe inheriting from those would help, but generally speaking I think that inheriting from the base one is the correct move in my case.
I also think it's possible based on this C example which I found:
https://gitlab.gnome.org/GNOME/rhythmbox/-/blob/master/widgets/rb-cell-renderer-rating.c

Could you help maybe, as I'm really in over my head with all that gobject inheritance machinery :-(

Ah yes, I believe the relevant lines in my code are:

impl MyRenderer {
    pub fn new() -> Self {
        glib::Object::new(Self::static_type(), &[])
            .expect("Failed to create MyRenderer")
            .downcast()
            .expect("Created MyRenderer is of wrong type")
    }
}

Can you put your current code up somewhere? I'll take a look then

Hi, yes, I wrote in my message, it's there:

https://github.com/emmanueltouzery/cell_r

Thank you!

You have to implement ObjectSubclass::new() or ::with_class() at least. One of the two is required but this can't be expressed in Rust currently.

yes that was it, thank you very much!