Using GTK v0.18.1 on rust to draw

26 Views Asked by At

Im using GTK to make an Drawing board and need to implement a draw function. Here is the draw function:

pub struct Drawing {
    pointer_option: PointerOption,
    previous_point: Option<(f64, f64)>,
}

impl Drawing {
    pub fn draw(&mut self, curr_pt: (f64, f64), buff: &Pixbuf) {
        match self.previous_point {
            Some(previous_point) => {
                plot(
                    buff,
                    previous_point.0 as i32,
                    previous_point.1 as i32,
                    curr_pt.0 as i32,
                    curr_pt.1 as i32,
                );
            }
            None => {
                let (x, y) = curr_pt;
                buff.put_pixel(x as i32, y as i32, 255, 255, 255, 0);
            }
        }
    }
}

But when i try to connect it with button press function:

        drawarea.connect_button_press_event(move |w, e| {
            *std::cell::RefCell::<_>::borrow_mut(&is_pointer_down_up) = true;
            if let (x, y) = e.position() {
                let buff = buffer_press.borrow();
                let mut drawing = drawing_press.borrow_mut();
                drawing.draw(e.position(), &buff);
                w.queue_draw();
            };

i get an error saying:

error[E0599]: the method `draw` exists for mutable reference `&mut Rc<RefCell<DrawingArea>>`, but its trait bounds were not satisfied
   --> src/main.rs:75:25
    |
75  |                   drawing.draw(e.position(), &buff);
    |                           ^^^^ method cannot be called on `&mut Rc<RefCell<DrawingArea>>` due to unsatisfied trait bounds
    |
   ::: /home/perc/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cell.rs:691:1
    |
691 |   pub struct RefCell<T: ?Sized> {
    |   -----------------------------
    |   |
    |   doesn't satisfy `RefCell<gtk::DrawingArea>: gtk::prelude::WidgetExt`
    |   doesn't satisfy `_: IsA<Widget>`
    |
   ::: /home/perc/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/rc.rs:316:1
    |
316 | / pub struct Rc<
317 | |     T: ?Sized,
318 | |     #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
319 | | > {
    | | -
    | | |
    | |_doesn't satisfy `_: IsA<Widget>`
    |   doesn't satisfy `_: WidgetExt`
    |
    = note: the following trait bounds were not satisfied:
            `&mut Rc<RefCell<gtk::DrawingArea>>: gtk::prelude::IsA<gtk::Widget>`
            which is required by `&mut Rc<RefCell<gtk::DrawingArea>>: gtk::prelude::WidgetExt`
            `Rc<RefCell<gtk::DrawingArea>>: gtk::prelude::IsA<gtk::Widget>`
            which is required by `Rc<RefCell<gtk::DrawingArea>>: gtk::prelude::WidgetExt`
            `RefCell<gtk::DrawingArea>: gtk::prelude::IsA<gtk::Widget>`
            which is required by `RefCell<gtk::DrawingArea>: gtk::prelude::WidgetExt`

how do i implement the traits? using gtk4 version 0.18.1 trying to build a drawing area to draw on using rust.

0

There are 0 best solutions below