fltk-table 0.3.5

A smart table widget for fltk-rs
Documentation
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#![doc = include_str!("../README.md")]
#![allow(clippy::needless_doctest_main)]

use fltk::{
    app, draw,
    enums::*,
    input,
    prelude::{GroupExt, InputExt, TableExt, WidgetBase, WidgetExt},
    table, window,
};
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};

#[derive(Debug, Default, Clone)]
pub struct Cell {
    pub label: String,
    pub color: Option<Color>,
    pub font: Option<Font>,
    pub font_color: Option<Color>,
    pub font_size: Option<i32>,
    pub selection_color: Option<Color>,
    pub align: Option<Align>,
    pub border_color: Option<Color>,
}

impl Cell {
    fn with_label(l: &str) -> Cell {
        Cell {
            label: l.to_string(),
            ..Default::default()
        }
    }
}

type CellMatrix = Vec<Vec<Cell>>;

// Needed to store cell information during the draw_cell call
#[derive(Default)]
struct CellData {
    pub row: i32, // row
    pub col: i32, // column
    pub x: i32,
    pub y: i32,
    pub w: i32,
    pub h: i32,
}

impl CellData {
    fn select(&mut self, row: i32, col: i32, x: i32, y: i32, w: i32, h: i32) {
        self.row = row;
        self.col = col;
        self.x = x;
        self.y = y;
        self.w = w;
        self.h = h;
    }
}

/// Contains the parameters for our table, including rows, columns and other styling params
#[derive(Debug, Clone, Copy)]
pub struct TableOpts {
    pub rows: i32,
    pub cols: i32,
    pub editable: bool,
    pub cell_color: Color,
    pub cell_font: Font,
    pub cell_font_color: Color,
    pub cell_font_size: i32,
    pub cell_selection_color: Color,
    pub cell_align: Align,
    pub cell_border_color: Color,
    pub cell_padding: i32,
    pub header_font: Font,
    pub header_frame: FrameType,
    pub header_color: Color,
    pub header_font_color: Color,
    pub header_font_size: i32,
    pub header_align: Align,
}

impl Default for TableOpts {
    fn default() -> Self {
        Self {
            rows: 1,
            cols: 1,
            editable: false,
            cell_color: Color::BackGround2,
            cell_font: Font::Helvetica,
            cell_font_color: Color::Gray0,
            cell_font_size: 14,
            cell_selection_color: Color::from_u32(0x00D3_D3D3),
            cell_align: Align::Center,
            cell_border_color: Color::Gray0,
            cell_padding: 1,
            header_font: Font::Helvetica,
            header_frame: FrameType::ThinUpBox,
            header_color: Color::FrameDefault,
            header_font_color: Color::Black,
            header_font_size: 14,
            header_align: Align::Center,
        }
    }
}

/// Smart table widget
#[derive(Clone)]
pub struct SmartTable {
    table: table::TableRow,
    inp: Option<input::Input>,
    data: Arc<Mutex<CellMatrix>>,
    row_headers: Arc<Mutex<Vec<String>>>,
    col_headers: Arc<Mutex<Vec<String>>>,
    on_update_callback: Arc<Mutex<Box<dyn FnMut(i32, i32, String) + Send>>>,
}

impl Default for SmartTable {
    fn default() -> Self {
        Self::new(0, 0, 0, 0, None)
    }
}

impl SmartTable {
    /// Construct a new SmartTable widget using coords, size and label
    pub fn new<S: Into<Option<&'static str>>>(x: i32, y: i32, w: i32, h: i32, label: S) -> Self {
        let table = table::TableRow::new(x, y, w, h, label);
        table.end();
        let inp = None;
        let on_update_callback: Box<dyn FnMut(i32, i32, String) + Send> = Box::new(|_, _, _| ());
        let on_update_callback = Arc::new(Mutex::new(on_update_callback));

        Self {
            table,
            inp,
            data: Default::default(),
            row_headers: Default::default(),
            col_headers: Default::default(),
            on_update_callback,
        }
    }

    /// Create a SmartTable the size of the parent widget
    pub fn default_fill() -> Self {
        Self::new(0, 0, 0, 0, None)
            .size_of_parent()
            .center_of_parent()
    }

    /// Sets the tables options
    pub fn set_opts(&mut self, opts: TableOpts) {
        let mut data = self.data.try_lock().unwrap();
        data.resize(opts.rows as _, vec![]);
        for v in data.iter_mut() {
            v.resize(opts.cols as _, Cell::default());
        }
        drop(data);

        let mut row_headers = vec![];
        for i in 0..opts.rows {
            row_headers.push((i + 1).to_string());
        }
        let row_headers = Arc::new(Mutex::new(row_headers));
        self.row_headers = row_headers;

        let mut col_headers = vec![];
        for i in 0..opts.cols {
            let mut pref = String::new();
            if i > 25 {
                let t = (i / 26) as i32;
                if t > 26 {
                    col_headers.push(i.to_string());
                } else {
                    pref.push((t - 1 + 65) as u8 as char);
                    col_headers.push(format!("{}{}", pref, (i - (26 * t) + 65) as u8 as char));
                }
            } else {
                col_headers.push(format!("{}", (i + 65) as u8 as char));
            }
        }
        let col_headers = Arc::new(Mutex::new(col_headers));
        self.col_headers = col_headers;

        let len = opts.rows;
        let inner_len = opts.cols;

        let cell = Rc::from(RefCell::from(CellData::default()));
        self.table.set_rows(len as i32);
        self.table.set_cols(inner_len as i32);
        self.table.set_row_header(true);
        self.table.set_row_resize(true);
        self.table.set_col_header(true);
        self.table.set_col_resize(true);
        self.table.end();

        // Called when the table is drawn then when it's redrawn due to events
        self.table.draw_cell({
            let cell = cell.clone();
            let data = self.data.clone();
            let row_headers = self.row_headers.clone();
            let col_headers = self.col_headers.clone();
            move |t, ctx, row, col, x, y, w, h| {
                if let Ok(data) = data.try_lock() {
                    let row_headers = row_headers.try_lock().unwrap();
                    let col_headers = col_headers.try_lock().unwrap();
                    match ctx {
                        table::TableContext::StartPage => draw::set_font(Font::Helvetica, 14),
                        table::TableContext::ColHeader => {
                            Self::draw_header(&col_headers[col as usize], x, y, w, h, &opts)
                        } // Column titles
                        table::TableContext::RowHeader => {
                            Self::draw_header(&row_headers[row as usize], x, y, w, h, &opts)
                        } // Row titles
                        table::TableContext::Cell => {
                            if t.is_selected(row, col) {
                                cell.borrow_mut().select(row, col, x, y, w, h); // Captures the cell information
                            }
                            Self::draw_data(
                                &data[row as usize][col as usize],
                                x,
                                y,
                                w,
                                h,
                                t.is_selected(row, col),
                                &opts,
                            );
                        }
                        _ => (),
                    }
                }
            }
        });

        if opts.editable {
            self.inp = Some(input::Input::default());
            let mut inp = self.inp.as_ref().unwrap().clone();
            inp.set_trigger(CallbackTrigger::EnterKey);
            let win = window::Window::from_dyn_widget_ptr(
                self.table.top_window().unwrap().as_widget_ptr(),
            );
            win.unwrap().add(&inp);
            inp.hide();

            inp.set_callback({
                let cell = cell.clone();
                let data = self.data.clone();
                let mut table = self.table.clone();
                let on_update_callback = self.on_update_callback.clone();
                move |i| {
                    let cell = cell.borrow();
                    on_update_callback.try_lock().unwrap()(cell.row, cell.col, i.value());
                    data.try_lock().unwrap()[cell.row as usize][cell.col as usize].label =
                        i.value();
                    i.set_value("");
                    i.hide();
                    table.redraw();
                }
            });

            inp.handle(|i, ev| match ev {
                Event::KeyUp => {
                    if app::event_key() == Key::Escape {
                        i.hide();
                        true
                    } else {
                        false
                    }
                }
                _ => false,
            });

            self.table.handle({
                let data = self.data.clone();
                move |_, ev| match ev {
                    Event::Released => {
                        if let Ok(data) = data.try_lock() {
                            let cell = cell.borrow();
                            inp.resize(cell.x, cell.y, cell.w, cell.h);
                            inp.set_value(&data[cell.row as usize][cell.col as usize].label);
                            inp.show();
                            inp.take_focus().ok();
                            inp.redraw();
                            true
                        } else {
                            false
                        }
                    }
                    _ => false,
                }
            });
        }
    }

    /// Instantiate with TableOpts
    pub fn with_opts(mut self, opts: TableOpts) -> Self {
        self.set_opts(opts);
        self
    }

    /// Get the input widget
    pub fn input(&mut self) -> &mut Option<input::Input> {
        &mut self.inp
    }

    /// Get a copy of the data
    pub fn data(&self) -> CellMatrix {
        self.data.try_lock().unwrap().clone()
    }

    /// Get the inner data
    pub fn data_ref(&self) -> Arc<Mutex<CellMatrix>> {
        self.data.clone()
    }

    fn draw_header(txt: &str, x: i32, y: i32, w: i32, h: i32, opts: &TableOpts) {
        draw::push_clip(x, y, w, h);
        draw::draw_box(opts.header_frame, x, y, w, h, opts.header_color);
        draw::set_draw_color(opts.header_font_color);
        draw::set_font(opts.header_font, opts.header_font_size);
        draw::draw_text2(txt, x, y, w, h, opts.header_align);
        draw::pop_clip();
    }

    // The selected flag sets the color of the cell to a grayish color, otherwise white
    fn draw_data(cell: &Cell, x: i32, y: i32, w: i32, h: i32, selected: bool, opts: &TableOpts) {
        draw::push_clip(x, y, w, h);
        let sel_col = if let Some(sel_col) = cell.selection_color {
            sel_col
        } else {
            opts.cell_selection_color
        };
        let bg = if let Some(col) = cell.color {
            col
        } else {
            opts.cell_color
        };
        if selected {
            draw::set_draw_color(sel_col);
        } else {
            draw::set_draw_color(bg);
        }
        draw::draw_rectf(x, y, w, h);
        draw::set_draw_color(if let Some(col) = cell.font_color {
            col
        } else {
            opts.cell_font_color
        });
        draw::set_font(
            if let Some(font) = cell.font {
                font
            } else {
                opts.cell_font
            },
            if let Some(font) = cell.font_size {
                font
            } else {
                opts.cell_font_size
            },
        );
        draw::draw_text2(
            &cell.label,
            x + opts.cell_padding,
            y,
            w - opts.cell_padding * 2,
            h,
            if let Some(a) = cell.align {
                a
            } else {
                opts.cell_align
            },
        );
        draw::set_draw_color(if let Some(col) = cell.border_color {
            col
        } else {
            opts.cell_border_color
        });
        draw::draw_rect(x, y, w, h);
        draw::pop_clip();
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_value(&mut self, row: i32, col: i32, val: &str) {
        self.data.try_lock().unwrap()[row as usize][col as usize].label = val.to_string();
    }

    /// Get the cell value, using the row and column to index the data
    pub fn cell_value(&self, row: i32, col: i32) -> String {
        self.data.try_lock().unwrap()[row as usize][col as usize]
            .label
            .clone()
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_color(&mut self, row: i32, col: i32, color: Color) {
        self.data.try_lock().unwrap()[row as usize][col as usize].color = Some(color);
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_selection_color(&mut self, row: i32, col: i32, color: Color) {
        self.data.try_lock().unwrap()[row as usize][col as usize].selection_color = Some(color);
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_font_color(&mut self, row: i32, col: i32, color: Color) {
        self.data.try_lock().unwrap()[row as usize][col as usize].font_color = Some(color);
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_border_color(&mut self, row: i32, col: i32, color: Color) {
        self.data.try_lock().unwrap()[row as usize][col as usize].border_color = Some(color);
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_font(&mut self, row: i32, col: i32, font: Font) {
        self.data.try_lock().unwrap()[row as usize][col as usize].font = Some(font);
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_font_size(&mut self, row: i32, col: i32, sz: i32) {
        self.data.try_lock().unwrap()[row as usize][col as usize].font_size = Some(sz);
    }

    /// Set the cell value, using the row and column to index the data
    pub fn set_cell_align(&mut self, row: i32, col: i32, align: Align) {
        self.data.try_lock().unwrap()[row as usize][col as usize].align = Some(align);
    }

    /// Set the row header value at the row index
    pub fn set_row_header_value(&mut self, row: i32, val: &str) {
        self.row_headers.try_lock().unwrap()[row as usize] = val.to_string();
    }

    /// Set the column header value at the column index
    pub fn set_col_header_value(&mut self, col: i32, val: &str) {
        self.col_headers.try_lock().unwrap()[col as usize] = val.to_string();
    }

    /// Get the row header value at the row index
    pub fn row_header_value(&mut self, row: i32) -> String {
        self.row_headers.try_lock().unwrap()[row as usize].clone()
    }

    /// Get the column header value at the column index
    pub fn col_header_value(&mut self, col: i32) -> String {
        self.col_headers.try_lock().unwrap()[col as usize].clone()
    }

    /// Insert an empty row at the row index
    pub fn insert_empty_row(&mut self, row: i32, row_header: &str) {
        let mut data = self.data.try_lock().unwrap();
        let cols = self.column_count() as usize;
        data.insert(row as _, vec![]);
        data[row as usize].resize(cols as _, Cell::default());
        self.row_headers
            .try_lock()
            .unwrap()
            .insert(row as _, row_header.to_string());
        self.table.set_rows(self.table.rows() + 1);
    }

    /// Append a row to your table
    pub fn insert_row(&mut self, row: i32, row_header: &str, vals: &[&str]) {
        let mut data = self.data.try_lock().unwrap();
        let cols = self.column_count() as usize;
        assert!(cols == vals.len());
        data.insert(row as _, vals.iter().map(|v| Cell::with_label(v)).collect());
        self.row_headers
            .try_lock()
            .unwrap()
            .push(row_header.to_string());
        self.table.set_rows(self.table.rows() + 1);
    }

    /// Append an empty row to your table
    pub fn append_empty_row(&mut self, row_header: &str) {
        let mut data = self.data.try_lock().unwrap();
        let cols = self.column_count() as usize;
        data.push(vec![]);
        data.last_mut().unwrap().resize(cols as _, Cell::default());
        self.row_headers
            .try_lock()
            .unwrap()
            .push(row_header.to_string());
        self.table.set_rows(self.table.rows() + 1);
    }

    /// Append a row to your table
    pub fn append_row(&mut self, row_header: &str, vals: &[&str]) {
        let mut data = self.data.try_lock().unwrap();
        let cols = self.column_count() as usize;
        assert!(cols == vals.len());
        data.push(vals.iter().map(|v| Cell::with_label(v)).collect());
        self.row_headers
            .try_lock()
            .unwrap()
            .push(row_header.to_string());
        self.table.set_rows(self.table.rows() + 1);
    }

    /// Insert an empty column at the column index
    pub fn insert_empty_col(&mut self, col: i32, col_header: &str) {
        let mut data = self.data.try_lock().unwrap();
        for v in data.iter_mut() {
            v.insert(col as _, Cell::default());
        }
        self.col_headers
            .try_lock()
            .unwrap()
            .insert(col as _, col_header.to_string());
        self.table.set_cols(self.table.cols() + 1);
    }

    /// Append a column to your table
    pub fn insert_col(&mut self, col: i32, col_header: &str, vals: &[&str]) {
        let mut data = self.data.try_lock().unwrap();
        assert!(vals.len() == self.table.rows() as usize);
        let mut count = 0;
        for v in data.iter_mut() {
            v.insert(col as _, Cell::with_label(vals[count]));
            count += 1;
        }
        self.col_headers
            .try_lock()
            .unwrap()
            .push(col_header.to_string());
        self.table.set_cols(self.table.cols() + 1);
    }

    /// Append an empty column to your table
    pub fn append_empty_col(&mut self, col_header: &str) {
        let mut data = self.data.try_lock().unwrap();
        for v in data.iter_mut() {
            v.push(Cell::default());
        }
        self.col_headers
            .try_lock()
            .unwrap()
            .push(col_header.to_string());
        self.table.set_cols(self.table.cols() + 1);
    }

    /// Append a column to your table
    pub fn append_col(&mut self, col_header: &str, vals: &[&str]) {
        let mut data = self.data.try_lock().unwrap();
        assert!(vals.len() == self.table.rows() as usize);
        let mut count = 0;
        for v in data.iter_mut() {
            v.push(Cell::with_label(vals[count]));
            count += 1;
        }
        self.col_headers
            .try_lock()
            .unwrap()
            .push(col_header.to_string());
        self.table.set_cols(self.table.cols() + 1);
    }

    /// Remove a row at the row index
    pub fn remove_row(&mut self, row: i32) {
        let mut data = self.data.try_lock().unwrap();
        data.remove(row as _);
        self.row_headers.try_lock().unwrap().remove(row as _);
        self.table.set_rows(self.table.rows() - 1);
    }

    /// Remove a column at the column index
    pub fn remove_col(&mut self, col: i32) {
        let mut data = self.data.try_lock().unwrap();
        for v in data.iter_mut() {
            v.remove(col as _);
        }
        self.col_headers.try_lock().unwrap().remove(col as _);
        self.table.set_cols(self.table.cols() - 1);
    }

    /// Set a callback for the SmartTable
    pub fn set_callback<F: FnMut(&mut Self) + 'static>(&mut self, mut cb: F) {
        let mut s = self.clone();
        self.table.set_callback(move |_| {
            cb(&mut s);
        });
    }

    /// Set a callback for on user input
    /// callback function takes the values row, col, and the new value of the cell
    pub fn set_on_update_callback<F: FnMut(i32, i32, String) + Send + 'static>(&mut self, cb: F) {
        *self.on_update_callback.try_lock().unwrap() = Box::new(cb);
    }

    /// Clears all cells in the table
    pub fn clear(&mut self) {
        let mut data = self.data.try_lock().unwrap();
        for v in data.iter_mut() {
            for c in v.iter_mut() {
                *c = Cell::default();
            }
        }
    }

    /// Returns the row count
    pub fn row_count(&self) -> i32 {
        self.table.rows()
    }

    /// Returns the column count
    pub fn column_count(&self) -> i32 {
        self.table.cols()
    }

    /// Get the column's width
    pub fn col_width(&self, col: i32) -> i32 {
        self.table.col_width(col)
    }

    /// Get the row's height
    pub fn row_height(&self, row: i32) -> i32 {
        self.table.row_height(row)
    }

    /// Set column's width
    pub fn set_col_width(&mut self, col: i32, width: i32) {
        self.table.set_col_width(col, width);
    }

    /// Set the row's height
    pub fn set_row_height(&mut self, row: i32, height: i32) {
        self.table.set_row_height(row, height);
    }

    /// Get the column header height
    pub fn col_header_height(&self) -> i32 {
        self.table.col_header_height()
    }

    /// Get the row header width
    pub fn row_header_width(&self) -> i32 {
        self.table.row_header_width()
    }

    /// Set column header height
    pub fn set_col_header_height(&mut self, height: i32) {
        self.table.set_col_header_height(height);
    }

    /// Set the row header width
    pub fn set_row_header_width(&mut self, width: i32) {
        self.table.set_row_header_width(width);
    }
}

impl std::fmt::Debug for SmartTable {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        fmt.debug_struct("SmartTable")
            .field("table", &self.table)
            .field("data", &self.data)
            .field("row_headers", &self.row_headers)
            .field("col_headers", &self.col_headers)
            .finish()
    }
}

fltk::widget_extends!(SmartTable, table::TableRow, table);