Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/fltk/fltk/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Fl_Flex is a container (layout) widget for one row or one column of widgets. It provides flexible positioning of its children either in one row (HORIZONTAL) or in one column (VERTICAL). Since: FLTK 1.4.0 Header: <FL/Fl_Flex.H> Inherits: Fl_Group

Key Features

  • Simple flexible row or column layout
  • Configurable margins on all four sides
  • Gap spacing between children
  • Fixed and flexible widget sizing
  • Drop-in replacement for Fl_Pack (recommended)
  • Predictable resizing behavior

Use Cases

  • Toolbar layouts with flexible spacing
  • Button rows with consistent sizing
  • Form layouts with aligned controls
  • Single-row or single-column arrangements
  • Nested layouts for complex UIs

Layout Algorithm

Fl_Flex positions widgets in a row or column:
  1. Margins are applied around all children at the inner side of the box frame
  2. Gaps are inserted between each child widget
  3. Fixed-size widgets maintain their specified width (HORIZONTAL) or height (VERTICAL)
  4. Flexible widgets share remaining space equally
For HORIZONTAL layout:
  • All widgets are resized to full height minus margins
  • Flexible widgets share available width equally
  • Use fixed() to set specific widget widths
For VERTICAL layout:
  • All widgets are resized to full width minus margins
  • Flexible widgets share available height equally
  • Use fixed() to set specific widget heights

Constructor

Standard Constructor

Fl_Flex(int X, int Y, int W, int H, const char *L = 0)
Creates a vertical Fl_Flex container with standard FLTK coordinates.
X
int
X position of the widget
Y
int
Y position of the widget
W
int
Width of the widget
H
int
Height of the widget
L
const char*
Optional label string (default: NULL)

Simplified Constructors

Fl_Flex(int direction)
Fl_Flex(int w, int h, int direction)
Fl_Flex(int x, int y, int w, int h, int direction)
Alternate constructors that specify layout direction directly.
direction
int
Layout direction: Fl_Flex::HORIZONTAL, Fl_Flex::VERTICAL (or aliases ROW, COLUMN)

Layout Direction

Type Constants

enum {
  VERTICAL   = 0,  // One column
  HORIZONTAL = 1,  // One row
  COLUMN     = 0,  // Alias for VERTICAL
  ROW        = 1   // Alias for HORIZONTAL
}

horizontal()

int horizontal() const
Returns non-zero if layout is horizontal. Returns: 1 if type() == HORIZONTAL, 0 if VERTICAL

Widget Sizing

fixed()

void fixed(Fl_Widget *w, int size)
void fixed(Fl_Widget &w, int size)
int fixed(Fl_Widget *w) const
Sets or gets the fixed size of a child widget.
w
Fl_Widget*
Widget to affect
size
int
Fixed width (HORIZONTAL) or height (VERTICAL) in pixels
For HORIZONTAL: Sets fixed width; widget height adjusts to container
For VERTICAL: Sets fixed height; widget width adjusts to container
Widgets without a fixed size share remaining space equally.

Margins

margin() - Get

int margin() const
Returns the left margin size (useful if all margins are equal).

margin() - Get All Four

int margin(int *left, int *top, int *right, int *bottom) const
Returns all four margin sizes.
left
int*
Pointer to receive left margin (can be NULL)
top
int*
Pointer to receive top margin (can be NULL)
right
int*
Pointer to receive right margin (can be NULL)
bottom
int*
Pointer to receive bottom margin (can be NULL)
Returns: 1 if all margins are equal, 0 otherwise

margin() - Set All Equal

void margin(int m, int g = -1)
Sets all four margins to the same size, optionally sets gap.
m
int
Margin size in pixels (must be >= 0)
g
int
Optional gap size (if >= 0); negative values leave gap unchanged

margin() - Set Individual

void margin(int left, int top, int right, int bottom)
Sets each margin individually.
left
int
Left margin in pixels (negative values set to 0)
top
int
Top margin in pixels (negative values set to 0)
right
int
Right margin in pixels (negative values set to 0)
bottom
int
Bottom margin in pixels (negative values set to 0)

Gap Spacing

gap()

int gap() const
void gap(int g)
Gets or sets the gap size between children.
g
int
Gap size in pixels (must be >= 0; negative values clamped to 0)

spacing()

int spacing() const
void spacing(int i)
Synonym for gap(). Provided for compatibility with Fl_Pack.

Layout Management

layout()

void layout()
Calculates the layout of children and redraws the widget.

need_layout()

void need_layout(int set)
bool need_layout() const
Sets or gets whether layout calculation is required.
set
int
1 to request layout recalculation, 0 to reset (rarely use 0)
Call with set=1 if you change child attributes or sizes to ensure proper layout calculation.

resize()

void resize(int x, int y, int w, int h) override
Resizes the Fl_Flex container and recalculates child layout.

Example: Simple Toolbar

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Flex.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>

int main(int argc, char **argv) {
  Fl_Double_Window window(410, 40, "Simple Fl_Flex Demo");
  
  // Create horizontal flex container
  Fl_Flex flex(5, 5, 400, 30, Fl_Flex::HORIZONTAL);
  
  // Add buttons - position/size will be managed by Fl_Flex
  Fl_Button b1(0, 0, 0, 0, "File");
  Fl_Button b2(0, 0, 0, 0, "Save");
  Fl_Box    bx(0, 0, 0, 0);  // Invisible spacer
  Fl_Button b3(0, 0, 0, 0, "Exit");
  
  // Configure layout
  flex.fixed(bx, 60);  // Fixed width spacer
  flex.gap(10);        // 10 pixel gap between widgets
  flex.end();
  
  window.resizable(flex);
  window.end();
  window.size_range(300, 30);  // Prevent too small
  window.show(argc, argv);
  return Fl::run();
}

Example: Vertical Form Layout

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Flex.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>

int main() {
  Fl_Window win(300, 250, "Form Example");
  
  // Vertical flex container with margins
  Fl_Flex *vbox = new Fl_Flex(0, 0, 300, 250, Fl_Flex::VERTICAL);
  vbox->margin(10);
  vbox->gap(5);
  
  Fl_Input *name = new Fl_Input(0, 0, 0, 30, "Name:");
  Fl_Input *email = new Fl_Input(0, 0, 0, 30, "Email:");
  
  // Flexible spacer
  Fl_Box *spacer = new Fl_Box(0, 0, 0, 0);
  
  // Fixed height button
  Fl_Button *submit = new Fl_Button(0, 0, 0, 30, "Submit");
  vbox->fixed(submit, 30);
  
  vbox->end();
  win.resizable(vbox);
  win.show();
  return Fl::run();
}

Best Practices

Size Constraints

Always set a minimum window size to prevent resizing below designed dimensions:
window.size_range(min_width, min_height);

Nested Layouts

Nest Fl_Flex containers for complex multi-row/column layouts:
// Outer vertical container
Fl_Flex *vbox = new Fl_Flex(0, 0, 300, 200, Fl_Flex::VERTICAL);

// Top horizontal row
Fl_Flex *hbox1 = new Fl_Flex(0, 0, 0, 0, Fl_Flex::HORIZONTAL);
hbox1->gap(5);
// Add buttons to hbox1...
vbox->fixed(hbox1, 40);  // Fixed height row
hbox1->end();

// Middle flexible area
Fl_Box *content = new Fl_Box(0, 0, 0, 0);

// Bottom horizontal row
Fl_Flex *hbox2 = new Fl_Flex(0, 0, 0, 0, Fl_Flex::HORIZONTAL);
// Add buttons to hbox2...
vbox->fixed(hbox2, 40);
hbox2->end();

vbox->end();

Invisible Spacers

Use Fl_Box widgets for flexible or fixed spacing:
// Flexible spacer (takes remaining space)
Fl_Box *spacer1 = new Fl_Box(0, 0, 0, 0);

// Fixed spacer
Fl_Box *spacer2 = new Fl_Box(0, 0, 0, 0);
flex.fixed(spacer2, 50);

Replacement for Fl_Pack

Fl_Flex is the recommended replacement for Fl_Pack since FLTK 1.4.0:
// Old Fl_Pack code:
Fl_Pack *pack = new Fl_Pack(x, y, w, h);
pack->type(Fl_Pack::HORIZONTAL);
pack->spacing(10);

// New Fl_Flex code:
Fl_Flex *flex = new Fl_Flex(x, y, w, h, Fl_Flex::HORIZONTAL);
flex->gap(10);

Notes

  • Default box type is FL_NO_BOX (inherited from Fl_Group)
  • Default margin is 0 on all sides
  • Default gap is 0
  • Default type is VERTICAL
  • Undefined behavior if resized below minimum child sizes
  • For complex grids, consider using Fl_Grid instead

See Also

  • Fl_Grid - Multi-row and multi-column grid layout
  • Fl_Pack - Legacy pack container
  • Fl_Group - Base container class
  • Fl_Tile - User-resizable tile layout