Key Features
- Automatic horizontal or vertical packing
- Self-resizing to wrap children
- Configurable spacing between children
- Simple API for basic layouts
Creating Fl_Pack
Fl_Pack::VERTICAL (stacks children vertically)
Pack Types
Vertical Packing
- Widgets stacked top to bottom
- Each widget keeps its height
- All widgets resized to pack’s width
- Pack height = sum of child heights + spacing
Horizontal Packing
- Widgets arranged left to right
- Each widget keeps its width
- All widgets resized to pack’s height
- Pack width = sum of child widths + spacing
Check Direction
Spacing
Set space between adjacent children:Resize Behavior
Fl_Pack has unique resize behavior:- Self-resizing: Pack resizes itself to “shrink-wrap” around children
- Child sizing:
- Vertical: Children get pack’s width
- Horizontal: Children get pack’s height
- resizable(): Default is
NULL(fixed positioning) - Special case: If resizable is the last child, it extends to fill available space
Complete Example
Vertical button list:Common Patterns
Scrollable List
Combine with Fl_Scroll for long lists:Horizontal Button Bar
Mixed with Fl_Group
Clearing Contents
Limitations
Self-resizing can be unpredictable
Self-resizing can be unpredictable
Fl_Pack resizes itself during draw(), which can cause:
- Unexpected size changes
- Complex interactions with parent containers
- Difficulty achieving specific layouts
Type changes after display not supported
Type changes after display not supported
Changing
type() after the widget is displayed requires manual resize:Nested packs can be confusing
Nested packs can be confusing
Nesting Fl_Pack widgets often leads to surprising behavior due to self-resizing.Solution: Use Fl_Flex or Fl_Grid for nested layouts
Box type limitations
Box type limitations
Works best with frame-only boxes (
FL_*_FRAME). Background boxes (FL_*_BOX) can be slower with many children.Avoid: Irregular boxes like FL_DIAMOND_BOXFl_Flex vs Fl_Pack
| Feature | Fl_Pack | Fl_Flex |
|---|---|---|
| Size | Self-resizing | Fixed size |
| Margins | No | Yes |
| Fixed widgets | No | Yes |
| Predictable | No | Yes |
| Recommended | Legacy code | New code |
| Since | FLTK 1.0 | FLTK 1.4.0 |
Best Practices
Use Inside Fl_Scroll
Fl_Pack works well for scrollable content:
Set resizable(NULL)
Keep children fixed:
Prefer Fl_Flex
Use Fl_Flex for new projects:
Frame Boxes
Use frame-style boxes:
Reference
Header File
Key Methods
| Method | Description |
|---|---|
type(int) | Set VERTICAL or HORIZONTAL |
spacing(int) | Set space between children |
horizontal() | Check if horizontal layout |
clear() | Remove all children |
resize(x,y,w,h) | Resize pack (triggers reflow) |