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
use std::borrow::Borrow;

use fltk::prelude::GroupExt;

use super::GridBuilder;
use crate::grid::{Cell, CellAlign, CellProperties, Padding, StripeCell};
use crate::{IntoWidget, LayoutElement, WrapperFactory};

pub struct CellBuilder<'l, G: GroupExt + Clone, F: Borrow<WrapperFactory>> {
    owner: &'l mut GridBuilder<G, F>,
    props: CellProperties,
}

impl<'l, G: GroupExt + Clone, F: Borrow<WrapperFactory>> CellBuilder<'l, G, F> {
    pub(super) fn new(
        owner: &'l mut GridBuilder<G, F>,
        row: usize,
        col: usize,
        row_span: usize,
        col_span: usize,
    ) -> Self {
        let padding = owner.default_cell_padding;
        let horz_align = owner.default_col_align[col];
        let vert_align = owner.default_row_align[row];
        Self {
            owner,
            props: CellProperties {
                row,
                col,
                row_span,
                col_span,
                padding,
                horz_align,
                vert_align,
            },
        }
    }

    pub fn with_left_padding(mut self, padding: i32) -> Self {
        self.props.padding.left = padding;
        self
    }

    pub fn with_top_padding(mut self, padding: i32) -> Self {
        self.props.padding.top = padding;
        self
    }

    pub fn with_right_padding(mut self, padding: i32) -> Self {
        self.props.padding.right = padding;
        self
    }

    pub fn with_bottom_padding(mut self, padding: i32) -> Self {
        self.props.padding.bottom = padding;
        self
    }

    pub fn with_padding(mut self, left: i32, top: i32, right: i32, bottom: i32) -> Self {
        self.props.padding = Padding {
            left,
            top,
            right,
            bottom,
        };
        self
    }

    pub fn with_horz_align(mut self, align: CellAlign) -> Self {
        self.props.horz_align = align;
        self
    }

    pub fn with_vert_align(mut self, align: CellAlign) -> Self {
        self.props.vert_align = align;
        self
    }

    pub fn skip(self) {
        let top = self.props.row;
        let bottom = top + self.props.row_span;
        let left = self.props.col;
        let right = left + self.props.col_span;
        for row in top..bottom {
            for col in left..right {
                self.owner.props.rows[row].cells[col] = StripeCell::Skipped;
                self.owner.props.cols[col].cells[row] = StripeCell::Skipped;
            }
        }
    }

    pub fn add<E: LayoutElement + 'static>(self, element: E) {
        self.add_boxed(Box::new(element));
    }

    pub fn wrap<W: IntoWidget + 'static>(self, widget: W) -> W {
        let element = self.owner.factory.borrow().wrap(widget.clone());
        self.add_boxed(element);
        widget
    }

    fn add_boxed(self, element: Box<dyn LayoutElement>) {
        self.owner.add_cell(Cell {
            element,
            min_size: Default::default(),
            props: self.props,
        });
    }
}