gtk-rs / gtk

DEPRECATED, use https://github.com/gtk-rs/gtk3-rs repository instead!
https://gtk-rs.org/
MIT License
1.25k stars 82 forks source link

is it possible create custom cell renderers? #1029

Closed emmanueltouzery closed 4 years ago

emmanueltouzery commented 4 years ago

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 :-(

sdroege commented 4 years ago

On July 4, 2020 10:12:37 AM GMT+03:00, emmanueltouzery notifications@github.com wrote:

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 :-(

Implementing custom cell renderers is possible with the git version of the gtk bindings. I'm not aware of any examples though, but it basically works the same as subclassing other types and for the cell renderer specific pieces check the C documentation for details. -- Sent from my Android device with K-9 Mail. Please excuse my brevity.

emmanueltouzery commented 4 years ago

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

sdroege commented 4 years ago

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

sdroege commented 4 years ago

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

emmanueltouzery commented 4 years ago

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 :-(

emmanueltouzery commented 4 years ago

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")
    }
}
sdroege commented 4 years ago

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

emmanueltouzery commented 4 years ago

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

https://github.com/emmanueltouzery/cell_r

Thank you!

sdroege commented 4 years ago

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.

emmanueltouzery commented 4 years ago

yes that was it, thank you very much!