FLTK 1.4.0
Loading...
Searching...
No Matches
Fl_Grid.H
Go to the documentation of this file.
1//
2// Fl_Grid widget header for the Fast Light Tool Kit (FLTK).
3//
4// Copyright 2021-2022 by Albrecht Schlosser.
5// Copyright 2022-2023 by Bill Spitzak and others.
6//
7// This library is free software. Distribution and use rights are outlined in
8// the file "COPYING" which should have been included with this file. If this
9// file is missing or damaged, see the license at:
10//
11// https://www.fltk.org/COPYING.php
12//
13// Please see the following page on how to report bugs and issues:
14//
15// https://www.fltk.org/bugs.php
16//
17
18#ifndef _FL_FL_GRID_H_
19#define _FL_FL_GRID_H_
20
25#include <FL/Fl_Group.H>
26#include <FL/Fl_Rect.H>
27
29typedef unsigned short Fl_Grid_Align;
30
33
36
39
42
45
48
51
54
57
58const Fl_Grid_Align FL_GRID_TOP_LEFT = FL_GRID_TOP | FL_GRID_LEFT;
59const Fl_Grid_Align FL_GRID_TOP_RIGHT = FL_GRID_TOP | FL_GRID_RIGHT;
60const Fl_Grid_Align FL_GRID_BOTTOM_LEFT = FL_GRID_BOTTOM | FL_GRID_LEFT;
61const Fl_Grid_Align FL_GRID_BOTTOM_RIGHT = FL_GRID_BOTTOM | FL_GRID_RIGHT;
62
147class FL_EXPORT Fl_Grid : public Fl_Group {
148 friend class Fl_Grid_Type;
149
150public:
151 class Cell {
152 friend class Fl_Grid;
153 private:
154 Cell *next_; // next cell in row
155 short row_; // row number
156 short col_; // column number
157 short rowspan_; // row span (1 - n)
158 short colspan_; // column span (1 - n)
159 Fl_Grid_Align align_; // widget alignment in its cell
160 Fl_Widget *widget_; // assigned widget
161 int w_; // minimal widget width
162 int h_; // minimal widget height
163
164 public:
165
166 void Cell_() {
167 next_ = NULL;
168 row_ = 0;
169 col_ = 0;
170 rowspan_ = 1;
171 colspan_ = 1;
172 widget_ = NULL;
173 w_ = 0;
174 h_ = 0;
175 align_ = 0;
176 }
177
178 Cell(int row, int col) {
179 Cell_();
180 row_ = row;
181 col_ = col;
182 }
183
184 Cell(Fl_Widget *w, int row, int col) {
185 Cell_();
186 widget_ = w;
187 row_ = row;
188 col_ = col;
189 }
190
191 ~Cell() {}
192
193 Fl_Widget *widget() const { return widget_; }
194
195 short row() const { return row_; }
196 short col() const { return col_; }
197
198 void rowspan(short v) { rowspan_ = v; }
199 void colspan(short v) { colspan_ = v; }
200 short rowspan() const { return rowspan_; }
201 short colspan() const { return colspan_; }
202
203 void align(Fl_Grid_Align align) { align_ = align; }
204 Fl_Grid_Align align() const { return align_; }
205
206 void minimum_size(int w, int h) { if (w>=0) w_ = w; if (h>=0) h_ = h; }
207 void minimum_size(int *w, int *h) const { if (w) *w = w_; if (h) *h = h_; }
208 }; // class Cell
209
210private:
211 class Row;
212 class Col;
213 short rows_;
214 short cols_;
215
216 short margin_left_; // left margin
217 short margin_top_; // top margin
218 short margin_right_; // right margin
219 short margin_bottom_; // bottom margin
220 short gap_row_; // gap between rows
221 short gap_col_; // gap between columns
222 Fl_Rect old_size; // only for resize callback (TBD)
223 Col *Cols_; // array of columns
224 Row *Rows_; // array of rows
225 bool need_layout_; // true if layout needs to be calculated
226
227protected:
228 Fl_Color grid_color; // color for drawing the grid lines (design helper)
229 bool draw_grid_; // draw the grid for testing / design
230
231protected:
232 void init();
233 Cell *add_cell(int row, int col);
234 void remove_cell(int row, int col);
235
236public:
237 Fl_Grid(int X, int Y, int W, int H, const char *L = 0);
238 virtual ~Fl_Grid();
239
240 // define and manage the layout and resizing
241
242 virtual void layout(int rows, int cols, int margin = -1, int gap = -1);
243 virtual void layout();
244 virtual void clear_layout();
245 virtual void resize(int X, int Y, int W, int H) FL_OVERRIDE;
246
247 short rows() const { return rows_; }
248 short cols() const { return cols_; }
249
260 void need_layout(int set) {
261 if (set) {
262 need_layout_ = true;
263 redraw();
264 }
265 else {
266 need_layout_ = false;
267 }
268 }
269
273 bool need_layout() const {
274 return need_layout_;
275 }
276
277protected:
278 virtual void draw() FL_OVERRIDE;
279 void on_remove(int) FL_OVERRIDE;
280 virtual void draw_grid(); // draw grid lines for debugging
281
282public:
283
284 // get and set individual margins
285
286 virtual void margin(int left, int top = -1, int right = -1, int bottom = -1);
287 int margin(int *left, int *top, int *right, int *bottom) const;
288
289 // get and set default row and column gaps for all rows and columns, respectively
290
291 virtual void gap(int row_gap, int col_gap = -1); // set default row and column gap(s)
292 void gap(int *row_gap, int *col_gap) const;
293
294 // find cells, get cell pointers
295
296 Fl_Grid::Cell* cell(int row, int col) const;
297 Fl_Grid::Cell* cell(Fl_Widget *widget) const;
298
299 // assign a widget to a cell
300
301 Fl_Grid::Cell* widget(Fl_Widget *wi, int row, int col, Fl_Grid_Align align = FL_GRID_FILL);
302 Fl_Grid::Cell* widget(Fl_Widget *wi, int row, int col, int rowspan, int colspan, Fl_Grid_Align align = FL_GRID_FILL);
303
304 // set minimal column and row sizes (widths and heights, respectively),
305 // set row and column specific gaps and weights
306
307 void col_width(int col, int value);
308 void col_width(const int *value, size_t size);
309 int col_width(int col) const;
310
311 void col_weight(int col, int value);
312 void col_weight(const int *value, size_t size);
313 int col_weight(int col) const;
314
315 void col_gap(int col, int value);
316 void col_gap(const int *value, size_t size);
317 int col_gap(int col) const;
318
319 void row_height(int row, int value);
320 void row_height(const int *value, size_t size);
321 int row_height(int row) const;
322
323 void row_weight(int row, int value);
324 void row_weight(const int *value, size_t size);
325 int row_weight(int row) const;
326
327 void row_gap(int row, int value);
328 void row_gap(const int *value, size_t size);
329 int row_gap(int row) const;
330
331 int computed_col_width(int col) const;
332 int computed_row_height(int row) const;
333
351 void show_grid(int set) {
352 draw_grid_ = set ? true : false;
353 }
354
368 void show_grid(int set, Fl_Color col) {
369 draw_grid_ = set ? true : false;
370 grid_color = col;
371 }
372
373 void debug(int level = 127);
374
375}; // class Fl_Grid
376
377#endif // _FL_FL_GRID_H_
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1101
const Fl_Grid_Align FL_GRID_RIGHT
Align the widget at the right side of the cell.
Definition Fl_Grid.H:44
unsigned short Fl_Grid_Align
Fl_Grid type for child widget alignment control.
Definition Fl_Grid.H:29
const Fl_Grid_Align FL_GRID_VERTICAL
Stretch the widget vertically to fill the cell.
Definition Fl_Grid.H:50
const Fl_Grid_Align FL_GRID_HORIZONTAL
Stretch the widget horizontally to fill the cell.
Definition Fl_Grid.H:47
const Fl_Grid_Align FL_GRID_CENTER
Align the widget in the middle of the cell (default).
Definition Fl_Grid.H:32
const Fl_Grid_Align FL_GRID_PROPORTIONAL
Stretch the widget proportionally.
Definition Fl_Grid.H:56
const Fl_Grid_Align FL_GRID_FILL
Stretch the widget in both directions to fill the cell.
Definition Fl_Grid.H:53
const Fl_Grid_Align FL_GRID_TOP
Align the widget at the top of the cell.
Definition Fl_Grid.H:35
const Fl_Grid_Align FL_GRID_LEFT
Align the widget at the left side of the cell.
Definition Fl_Grid.H:41
const Fl_Grid_Align FL_GRID_BOTTOM
Align the widget at the bottom of the cell.
Definition Fl_Grid.H:38
Fl_Group and Fl_End classes.
Definition Fl_Grid.H:151
Fl_Grid is a container (layout) widget with multiple columns and rows.
Definition Fl_Grid.H:147
void show_grid(int set, Fl_Color col)
Enable or disable drawing of the grid helper lines for visualization.
Definition Fl_Grid.H:368
void need_layout(int set)
Request or reset the request to calculate the layout of children.
Definition Fl_Grid.H:260
bool need_layout() const
Return whether layout calculation is required.
Definition Fl_Grid.H:273
The Fl_Group class is the FLTK container widget.
Definition Fl_Group.H:56
void resize(int, int, int, int) FL_OVERRIDE
Resizes the Fl_Group widget and all of its children.
Definition Fl_Group.cxx:823
void draw() FL_OVERRIDE
Draws the widget.
Definition Fl_Group.cxx:926
Rectangle with standard FLTK coordinates (X, Y, W, H).
Definition Fl_Rect.H:30
Fl_Widget is the base class for all widgets in FLTK.
Definition Fl_Widget.H:104
void redraw()
Schedules the drawing of the widget.
Definition Fl.cxx:1586
#define FL_OVERRIDE
This macro makes it safe to use the C++11 keyword override with older compilers.
Definition fl_attr.h:46