form/
form.rs

1use fltk::{prelude::*, *};
2use fltk_grid::Grid;
3
4struct Form {
5    grid: Grid,
6    name: input::Input,
7    age: input::IntInput,
8    occupation: input::Input,
9    btn: button::Button,
10}
11
12impl Form {
13    pub fn default() -> Self {
14        let mut grid = Grid::default_fill();
15        grid.set_layout(10, 5); // construct a new grid
16        let name = input::Input::default();
17        let age = input::IntInput::default();
18        let occupation = input::Input::default();
19        let btn = button::Button::default().with_label("Submit");
20        let mut g = Self {
21            grid,
22            name,
23            age,
24            occupation,
25            btn,
26        };
27        g.fill();
28        g
29    }
30
31    fn fill(&mut self) {
32        let grid = &mut self.grid;
33        grid.show_grid(false); // set to true to see cell outlines
34        let mut title = frame::Frame::default().with_label("Employee Form");
35        title.set_frame(enums::FrameType::FlatBox);
36        title.set_color(enums::Color::Red);
37        title.set_label_color(enums::Color::White);
38        grid.set_widget(
39            &mut title,
40            0,
41            1..4,
42        ).unwrap();
43        grid.set_widget(&mut frame::Frame::default().with_label("Name"), 2, 1).unwrap();
44        grid.set_widget(&mut self.name, 2, 3).unwrap();
45        grid.set_widget(&mut frame::Frame::default().with_label("Age"), 4, 1).unwrap();
46        grid.set_widget(&mut self.age, 4, 3).unwrap();
47        grid.set_widget(&mut frame::Frame::default().with_label("Occupation"), 6, 1).unwrap();
48        grid.set_widget(&mut self.occupation, 6, 3).unwrap();
49        grid.set_widget(&mut self.btn, 8, 2).unwrap();
50    }
51
52    fn register_default_callback(&mut self) {
53        self.btn.set_callback({
54            let name = self.name.clone();
55            let age = self.age.clone();
56            let occupation = self.occupation.clone();
57            move |_| {
58                println!("Name: {}", name.value());
59                println!("Age: {}", age.value());
60                println!("Occupation: {}", occupation.value());
61            }
62        });
63    }
64
65    pub fn resize(&mut self, x: i32, y: i32, w: i32, h: i32) {
66        self.grid.resize(x, y, w, h); // determine how it's resized
67    }
68}
69
70fn main() {
71    let a = app::App::default().with_scheme(app::Scheme::Gtk);
72    let mut win = window::Window::default().with_size(500, 300);
73    let mut form = Form::default();
74    form.register_default_callback();
75    win.end();
76    win.make_resizable(true);
77    win.show();
78
79    win.resize_callback(move |_, _, _, w, h| form.resize(0, 0, w, h));
80
81    a.run().unwrap();
82}