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
use fltk::{
    app,
    draw::{
        draw_line, draw_point, draw_rect_fill, set_draw_color, set_line_style,
        LineStyle,
    },
    enums::{Color, Event, FrameType},
    frame::Frame,
    prelude::*,
    surface::ImageSurface,
    window::Window,
};
use std::cell::RefCell;
use std::rc::Rc;

struct Canvas {
    frame: Frame,
    #[allow(dead_code)]
    surf: Rc<RefCell<ImageSurface>>,
}

impl Canvas {
    pub fn new(w: i32, h: i32) -> Self {
        let mut frame = Frame::default()
            .with_size(w, h)
            .center_of_parent();
        frame.set_color(Color::White);
        frame.set_frame(FrameType::DownBox);

        let surf = ImageSurface::new(frame.width(), frame.height(), false);
        ImageSurface::push_current(&surf);
        draw_rect_fill(0, 0, w, h, Color::White);
        ImageSurface::pop_current();

        let surf = Rc::from(RefCell::from(surf));

        frame.draw({
            let surf = surf.clone();
            move |f| {
                let surf = surf.borrow();
                let mut img = surf.image().unwrap();
                img.draw(f.x(), f.y(), f.w(), f.h());
            }
        });

        frame.handle({
            let mut x = 0;
            let mut y = 0;
            let surf = surf.clone();
            move |f, ev| {
                // println!("{}", ev);
                // println!("coords {:?}", app::event_coords());
                // println!("get mouse {:?}", app::get_mouse());
                let surf = surf.borrow_mut();
                match ev {
                    Event::Push => {
                        ImageSurface::push_current(&surf);
                        set_draw_color(Color::Red);
                        set_line_style(LineStyle::Solid, 3);
                        let coords = app::event_coords();
                        x = coords.0;
                        y = coords.1;
                        draw_point(x, y);
                        ImageSurface::pop_current();
                        f.redraw();
                        true
                    }
                    Event::Drag => {
                        ImageSurface::push_current(&surf);
                        set_draw_color(Color::Red);
                        set_line_style(LineStyle::Solid, 3);
                        let coords = app::event_coords();
                        draw_line(x, y, coords.0, coords.1);
                        x = coords.0;
                        y = coords.1;
                        ImageSurface::pop_current();
                        f.redraw();
                        true
                    }
                    _ => false,
                }
            }
        });
        Self { frame, surf }
    }
}

const WIDTH: i32 = 800;
const HEIGHT: i32 = 600;

fltk::widget_extends!(Canvas, Frame, frame);

fn main() {
    let app = app::App::default().with_scheme(app::Scheme::Gtk);

    let mut wind = Window::default()
        .with_size(WIDTH, HEIGHT)
        .with_label("RustyPainter");

    Canvas::new(WIDTH - 10, HEIGHT - 10);

    wind.end();
    wind.show();

    app.run().unwrap();
}