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

use fltk::group::Group;
use fltk::prelude::*;

use crate::{Padding, WrapperFactory};

use super::{Cell, CellAlign, Grid, GridProperties, StripeCell};

use self::group::StripeGroupBuilder;

mod cell;
mod group;
mod stripe;

pub use cell::CellBuilder;
pub use stripe::StripeBuilder;

pub struct GridBuilder<G: GroupExt + Clone = Group, F: Borrow<WrapperFactory> = WrapperFactory> {
    props: GridProperties<G>,
    factory: F,
    default_cell_padding: Padding,
    default_row_align: Vec<CellAlign>,
    default_col_align: Vec<CellAlign>,
    next_row: usize,
    next_col: usize,
}

#[derive(Clone, Copy)]
pub struct StripeGroupRef {
    kind: StripeKind,
    idx: usize,
}

#[derive(Clone, Copy)]
enum StripeKind {
    Row,
    Column,
}

impl<G: GroupExt + Clone> GridBuilder<G> {
    pub fn new(group: G) -> Self {
        Self::with_factory(group, WrapperFactory::new())
    }
}

impl<G: GroupExt + Clone, F: Borrow<WrapperFactory>> GridBuilder<G, F> {
    pub fn with_factory(group: G, factory: F) -> Self {
        Self {
            props: GridProperties {
                group,
                padding: Default::default(),
                row_spacing: 0,
                col_spacing: 0,
                cells: Vec::new(),
                spans: Vec::new(),
                groups: Vec::new(),
                rows: Vec::new(),
                cols: Vec::new(),
            },
            factory,
            default_cell_padding: Default::default(),
            default_row_align: Vec::new(),
            default_col_align: Vec::new(),
            next_row: 0,
            next_col: 0,
        }
    }

    pub fn with_row_spacing(mut self, spacing: i32) -> Self {
        self.props.row_spacing = std::cmp::max(0, spacing);
        self
    }

    pub fn with_col_spacing(mut self, spacing: i32) -> Self {
        self.props.col_spacing = std::cmp::max(0, spacing);
        self
    }

    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_default_cell_padding(
        mut self,
        left: i32,
        top: i32,
        right: i32,
        bottom: i32,
    ) -> Self {
        self.default_cell_padding = Padding {
            left,
            top,
            right,
            bottom,
        };
        self
    }

    pub fn row(&mut self) -> StripeBuilder<G, F> {
        StripeBuilder::new(self, StripeKind::Row, None)
    }

    pub fn col(&mut self) -> StripeBuilder<G, F> {
        StripeBuilder::new(self, StripeKind::Column, None)
    }

    pub fn row_group(&mut self) -> StripeGroupBuilder<G, F> {
        StripeGroupBuilder::new(self, StripeKind::Row)
    }

    pub fn col_group(&mut self) -> StripeGroupBuilder<G, F> {
        StripeGroupBuilder::new(self, StripeKind::Column)
    }

    pub fn extend_group(&mut self, group: StripeGroupRef) -> StripeBuilder<G, F> {
        StripeBuilder::new(self, group.kind, Some(group.idx))
    }

    pub fn cell(&mut self) -> Option<CellBuilder<G, F>> {
        let (row, col) = self.next_free_cell()?;
        Some(CellBuilder::new(self, row, col, 1, 1))
    }

    pub fn cell_at(&mut self, row: usize, col: usize) -> Option<CellBuilder<G, F>> {
        if (row >= self.props.rows.len()) && (col >= self.props.cols.len()) {
            return None;
        }
        match self.props.rows[row].cells[col] {
            StripeCell::Free | StripeCell::Skipped => Some(CellBuilder::new(self, row, col, 1, 1)),
            _ => None,
        }
    }

    pub fn span(&mut self, row_span: usize, col_span: usize) -> Option<CellBuilder<G, F>> {
        if (row_span == 0) || (col_span == 0) {
            return None;
        }

        let (row, col) = self.next_free_cell()?;
        if self.is_span_available(row, col, row_span, col_span, false) {
            Some(CellBuilder::new(self, row, col, row_span, col_span))
        } else {
            None
        }
    }

    pub fn span_at(
        &mut self,
        row: usize,
        col: usize,
        row_span: usize,
        col_span: usize,
    ) -> Option<CellBuilder<G, F>> {
        if (row_span == 0) || (col_span == 0) {
            return None;
        }

        if (row >= self.props.rows.len()) && (col >= self.props.cols.len()) {
            return None;
        }
        if self.is_span_available(row, col, row_span, col_span, true) {
            Some(CellBuilder::new(self, row, col, row_span, col_span))
        } else {
            None
        }
    }

    pub fn end(self) -> Grid<G> {
        self.props.group.end();
        Grid::new(self.props)
    }

    fn next_free_cell(&mut self) -> Option<(usize, usize)> {
        let mut row = self.next_row;
        let mut col = self.next_col;

        loop {
            if col >= self.props.cols.len() {
                col = 0;
                row += 1;
            }
            if row >= self.props.rows.len() {
                return None;
            }
            if let StripeCell::Free = self.props.rows[row].cells[col] {
                break;
            }
            col += 1;
        }

        self.next_row = row;
        self.next_col = col;

        Some((row, col))
    }

    fn is_span_available(
        &self,
        row: usize,
        col: usize,
        row_span: usize,
        col_span: usize,
        allow_skipped: bool,
    ) -> bool {
        let top = row;
        let bottom = top + row_span;
        let left = col;
        let right = left + col_span;

        if (bottom > self.props.rows.len()) || (right > self.props.cols.len()) {
            return false;
        }

        for cell_row in top..bottom {
            for cell_col in left..right {
                let cell_available = match self.props.rows[cell_row].cells[cell_col] {
                    StripeCell::Free => true,
                    StripeCell::Skipped if allow_skipped => true,
                    _ => false,
                };
                if !cell_available {
                    return false;
                }
            }
        }

        true
    }

    fn add_cell(&mut self, cell: Cell) {
        if (cell.props.row_span > 1) || (cell.props.col_span > 1) {
            return self.add_span(cell);
        }

        let row = cell.props.row;
        let col = cell.props.col;

        let cell_idx = self.props.cells.len();
        self.props.cells.push(cell);

        self.props.rows[row].cells[col] = StripeCell::Cell(cell_idx);
        self.props.cols[col].cells[row] = StripeCell::Cell(cell_idx);
    }

    fn add_span(&mut self, span: Cell) {
        let top = span.props.row;
        let bottom = top + span.props.row_span;
        let left = span.props.col;
        let right = left + span.props.col_span;

        self.props.spans.push(span);

        for row in top..bottom {
            for col in left..right {
                self.props.rows[row].cells[col] = StripeCell::Span;
                self.props.cols[col].cells[row] = StripeCell::Span;
            }
        }
    }
}