... or, How can I subclass a gtk::Widget?
I have the following dependencies in my Cargo.toml
:
[dependencies]
num = "*"
gtk = "*"
cairo-rs = "*"
gdk = "*"
time = "*"
I want to create my own type of widget (for rendering fractals). I have:
extern crate cairo;
extern crate gtk;
use cairo::Context;
use gtk::signal::Inhibit;
use gtk::signal::WidgetSignals;
use gtk::traits::ContainerTrait;
use gtk::traits::WidgetTrait;
struct MyWidget {
widget: gtk::DrawingArea,
foo: u32,
}
impl MyWidget {
fn new() -> MyWidget {
let result = MyWidget {
widget: gtk::DrawingArea::new().unwrap(),
foo: 17
};
result.widget.connect_draw(move |_w, c| {
// Cannot do: result.redraw(c)
Inhibit(true)
});
result
}
fn modify(&mut self, x: u32) {
self.foo += x;
self.widget.queue_draw();
}
fn redraw(&self, _ : Context) -> Inhibit {
println!("Should redraw for {}", self.foo);
Inhibit(true)
}
}
fn main() {
gtk::init().ok();
let window = gtk::Window::new(gtk::WindowType::TopLevel).unwrap();
let area = MyWidget::new();
window.add(&area.widget);
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(true)
});
window.connect_key_release_event(move |_w, _event| {
// Cannot do: area.modify(3);
Inhibit(true)
});
window.connect_button_release_event(move |_w, _event| {
// Cannot do: area.modify(17);
Inhibit(true)
});
window.show_all();
gtk::main();
}
But when redraw
gets called, w is of course the gtk::DrawingArea
and not my FractalWidget
. I have laborated with calling connect_draw
with a closure, but not managed to use result
in it (I have tried Box
ing the result and move
ing it into the lambda, but I'm new to this, so there is probably some way I haven't tried).
So, my actual question: Is there a way to either send more data into a rust-gnome redraw method (and other similar callbacks), or is there a way to extend a widget struct to contain my own data?