FLTK 1.4.3
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-2025 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 the same 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_() { // common initialization
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) { // constructor
179 Cell_();
180 row_ = row;
181 col_ = col;
182 }
183
184 Cell(Fl_Widget *w, int row, int col) { // widget assignment
185 Cell_();
186 widget_ = w;
187 row_ = row;
188 col_ = col;
189 }
190
197 ~Cell() {}
198
203 return next_;
204 }
205
217 void next(Cell *c) {
218 next_ = c;
219 }
220
221 Fl_Widget *widget() const { return widget_; }
222
223 short row() const { return row_; }
224 short col() const { return col_; }
225
226 void rowspan(short v) { rowspan_ = v; }
227 void colspan(short v) { colspan_ = v; }
228 short rowspan() const { return rowspan_; }
229 short colspan() const { return colspan_; }
230
231 void align(Fl_Grid_Align align) { align_ = align; }
232 Fl_Grid_Align align() const { return align_; }
233
234 void minimum_size(int w, int h) { if (w>=0) w_ = w; if (h>=0) h_ = h; }
235 void minimum_size(int *w, int *h) const { if (w) *w = w_; if (h) *h = h_; }
236 }; // class Cell
237
238private:
239 class Row;
240 class Col;
241 short rows_;
242 short cols_;
243
244 short margin_left_; // left margin
245 short margin_top_; // top margin
246 short margin_right_; // right margin
247 short margin_bottom_; // bottom margin
248 short gap_row_; // gap between rows
249 short gap_col_; // gap between columns
250 Fl_Rect old_size; // only for resize callback (TBD)
251 Col *Cols_; // array of columns
252 Row *Rows_; // array of rows
253 bool need_layout_; // true if layout needs to be calculated
254
255protected:
256 Fl_Color grid_color; // color for drawing the grid lines (design helper)
257 bool draw_grid_; // draw the grid for testing / design
258
259protected:
260 void init();
261 Cell *add_cell(int row, int col);
262 void remove_cell(int row, int col);
263
264public:
265 Fl_Grid(int X, int Y, int W, int H, const char *L = 0);
266 virtual ~Fl_Grid();
267
268 // define and manage the layout and resizing
269
270 virtual void layout(int rows, int cols, int margin = -1, int gap = -1);
271 virtual void layout();
272 virtual void clear_layout();
273 virtual void resize(int X, int Y, int W, int H) FL_OVERRIDE;
274
275 short rows() const { return rows_; }
276 short cols() const { return cols_; }
277
288 void need_layout(int set) {
289 if (set) {
290 need_layout_ = true;
291 redraw();
292 }
293 else {
294 need_layout_ = false;
295 }
296 }
297
301 bool need_layout() const {
302 return need_layout_;
303 }
304
305protected:
306 virtual void draw() FL_OVERRIDE;
307 void on_remove(int) FL_OVERRIDE;
308 virtual void draw_grid(); // draw grid lines for debugging
309
310public:
311
312 // get and set individual margins
313
314 virtual void margin(int left, int top = -1, int right = -1, int bottom = -1);
315 int margin(int *left, int *top, int *right, int *bottom) const;
316
317 // get and set default row and column gaps for all rows and columns, respectively
318
319 virtual void gap(int row_gap, int col_gap = -1); // set default row and column gap(s)
320 void gap(int *row_gap, int *col_gap) const;
321
322 // find cells, get cell pointers
323
324 Fl_Grid::Cell* cell(int row, int col) const;
325 Fl_Grid::Cell* cell(Fl_Widget *widget) const;
326
327 // assign a widget to a cell
328
329 Fl_Grid::Cell* widget(Fl_Widget *wi, int row, int col, Fl_Grid_Align align = FL_GRID_FILL);
330 Fl_Grid::Cell* widget(Fl_Widget *wi, int row, int col, int rowspan, int colspan, Fl_Grid_Align align = FL_GRID_FILL);
331
332 // set minimal column and row sizes (widths and heights, respectively),
333 // set row and column specific gaps and weights
334
335 void col_width(int col, int value);
336 void col_width(const int *value, size_t size);
337 int col_width(int col) const;
338
339 void col_weight(int col, int value);
340 void col_weight(const int *value, size_t size);
341 int col_weight(int col) const;
342
343 void col_gap(int col, int value);
344 void col_gap(const int *value, size_t size);
345 int col_gap(int col) const;
346
347 void row_height(int row, int value);
348 void row_height(const int *value, size_t size);
349 int row_height(int row) const;
350
351 void row_weight(int row, int value);
352 void row_weight(const int *value, size_t size);
353 int row_weight(int row) const;
354
355 void row_gap(int row, int value);
356 void row_gap(const int *value, size_t size);
357 int row_gap(int row) const;
358
359 int computed_col_width(int col) const;
360 int computed_row_height(int row) const;
361
379 void show_grid(int set) {
380 draw_grid_ = set ? true : false;
381 }
382
396 void show_grid(int set, Fl_Color col) {
397 draw_grid_ = set ? true : false;
398 grid_color = col;
399 }
400
401 void debug(int level = 127);
402
403}; // class Fl_Grid
404
405#endif // _FL_FL_GRID_H_
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1118
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
~Cell()
The destructor deletes the cell.
Definition Fl_Grid.H:197
void next(Cell *c)
Sets the next pointer of a grid's cell.
Definition Fl_Grid.H:217
Cell * next()
Returns the next widget cell of the same row of this cell.
Definition Fl_Grid.H:202
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:396
void need_layout(int set)
Request or reset the request to calculate the layout of children.
Definition Fl_Grid.H:288
bool need_layout() const
Return whether layout calculation is required.
Definition Fl_Grid.H:301
The Fl_Group class is the main 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:825
void draw() FL_OVERRIDE
Draws the widget.
Definition Fl_Group.cxx:943
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:112
void redraw()
Schedules the drawing of the widget.
Definition Fl.cxx:1668
#define FL_OVERRIDE
This macro makes it safe to use the C++11 keyword override with older compilers.
Definition fl_attr.h:46