table/
table.rs

1// For a simpler boilerplate-less table, check the fltk-table crate
2
3use fltk::{prelude::*, *};
4
5fn main() {
6    let app = app::App::default().with_scheme(app::Scheme::Gtk);
7    let mut wind = window::Window::default().with_size(800, 600);
8    let mut table = table::Table::default()
9        .with_size(800 - 10, 600 - 10)
10        .center_of(&wind);
11
12    table.set_rows(30);
13    table.set_row_header(true);
14    table.set_row_resize(true);
15    table.set_cols(26);
16    table.set_col_header(true);
17    table.set_col_width_all(80);
18    table.set_col_resize(true);
19    table.end();
20
21    wind.make_resizable(true);
22    wind.end();
23    wind.show();
24
25    // Called when the table is drawn then when it's redrawn due to events
26    table.draw_cell(move |t, ctx, row, col, x, y, w, h| match ctx {
27        table::TableContext::StartPage => draw::set_font(enums::Font::Helvetica, 14),
28        table::TableContext::ColHeader => {
29            draw_header(&format!("{}", (col + 65) as u8 as char), x, y, w, h)
30        } // Column titles
31        table::TableContext::RowHeader => draw_header(&format!("{}", row + 1), x, y, w, h), // Row titles
32        table::TableContext::Cell => draw_data(
33            &format!("{}", row + col),
34            x,
35            y,
36            w,
37            h,
38            t.is_selected(row, col),
39        ), // Data in cells
40        _ => (),
41    });
42
43    app.run().unwrap();
44}
45
46fn draw_header(txt: &str, x: i32, y: i32, w: i32, h: i32) {
47    draw::push_clip(x, y, w, h);
48    draw::draw_box(
49        enums::FrameType::ThinUpBox,
50        x,
51        y,
52        w,
53        h,
54        enums::Color::FrameDefault,
55    );
56    draw::set_draw_color(enums::Color::Black);
57    draw::set_font(enums::Font::Helvetica, 14);
58    draw::draw_text_boxed(txt, x, y, w, h, enums::Align::Center);
59    draw::pop_clip();
60}
61
62// The selected flag sets the color of the cell to a grayish color, otherwise white
63fn draw_data(txt: &str, x: i32, y: i32, w: i32, h: i32, selected: bool) {
64    draw::push_clip(x, y, w, h);
65    if selected {
66        draw::set_draw_color(enums::Color::from_u32(0x00D3_D3D3));
67    } else {
68        draw::set_draw_color(enums::Color::White);
69    }
70    draw::draw_rectf(x, y, w, h);
71    draw::set_draw_color(enums::Color::Gray0);
72    draw::set_font(enums::Font::Helvetica, 14);
73    draw::draw_text_boxed(txt, x, y, w, h, enums::Align::Center);
74    draw::draw_rect(x, y, w, h);
75    draw::pop_clip();
76}