Use this file to discover all available pages before exploring further.
FLTK provides flexible layout options through manual positioning, resize callbacks, and layout manager groups. Understanding these systems is essential for creating responsive user interfaces.
FLTK uses absolute positioning relative to the parent widget. All coordinates are in pixels.
// Widget position is relative to parentFl_Widget *widget = new Fl_Widget( 10, // x - distance from left edge of parent 20, // y - distance from top edge of parent 100, // width in pixels 30 // height in pixels);// Access position and size (from FL/Fl_Widget.H:360-378)int x = widget->x(); // X positionint y = widget->y(); // Y positionint w = widget->w(); // Widthint h = widget->h(); // Height
Fl_Pack *pack = new Fl_Pack(x, y, w, h);pack->type(Fl_Pack::VERTICAL);pack->spacing(5);pack->begin();// Add widgets - they stack top to bottomnew Fl_Widget(0, 0, w, h1);new Fl_Widget(0, 0, w, h2);pack->end();
Control which widget grows when the parent resizes:
Fl_Window *win = new Fl_Window(400, 300);win->begin();// Toolbar - fixed heightFl_Group *toolbar = new Fl_Group(0, 0, 400, 40);// ... add toolbar buttons ...toolbar->end();// Content area - should growFl_Box *content = new Fl_Box(0, 40, 400, 220);content->box(FL_DOWN_BOX);// Status bar - fixed heightFl_Box *status = new Fl_Box(0, 260, 400, 40);status->box(FL_UP_BOX);win->end();// Make content area resizablewin->resizable(content);// Toolbar and status bar stay fixed sizewin->show();
Only one widget can be the resizable widget. It receives all extra space when the parent grows.
class MainWindow : public Fl_Window { Fl_Group *toolbar; Fl_Scroll *content; Fl_Box *status;public: MainWindow(int W, int H, const char *L = 0) : Fl_Window(W, H, L) { begin(); // Fixed toolbar at top toolbar = new Fl_Group(0, 0, W, 40); toolbar->box(FL_UP_BOX); // ... add toolbar items ... toolbar->end(); // Scrollable content area content = new Fl_Scroll(0, 40, W, H - 80); content->box(FL_DOWN_BOX); content->end(); // Fixed status bar at bottom status = new Fl_Box(0, H - 40, W, 40); status->box(FL_UP_BOX); status->align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE); end(); resizable(content); } void resize(int X, int Y, int W, int H) override { Fl_Window::resize(X, Y, W, H); toolbar->resize(0, 0, W, 40); content->resize(0, 40, W, H - 80); status->resize(0, H - 40, W, 40); }};
Button Row
void create_button_row(Fl_Group *parent, int y) { Fl_Pack *pack = new Fl_Pack( 10, y, parent->w() - 20, 30 ); pack->type(Fl_Pack::HORIZONTAL); pack->spacing(5); pack->begin(); new Fl_Button(0, 0, 80, 30, "OK"); new Fl_Button(0, 0, 80, 30, "Cancel"); new Fl_Button(0, 0, 80, 30, "Apply"); pack->end();}
Form Layout
void create_form(Fl_Group *parent) { int y = 10; int label_w = 100; int field_w = 200; int h = 30; int spacing = 10; // Name field new Fl_Box(10, y, label_w, h, "Name:"); new Fl_Input(10 + label_w, y, field_w, h); y += h + spacing; // Email field new Fl_Box(10, y, label_w, h, "Email:"); new Fl_Input(10 + label_w, y, field_w, h); y += h + spacing; // Phone field new Fl_Box(10, y, label_w, h, "Phone:"); new Fl_Input(10 + label_w, y, field_w, h);}
Sidebar Layout
class SidebarWindow : public Fl_Window { Fl_Group *sidebar; Fl_Group *main_area;public: SidebarWindow(int W, int H) : Fl_Window(W, H, "Sidebar") { begin(); // Fixed-width sidebar sidebar = new Fl_Group(0, 0, 200, H); sidebar->box(FL_FLAT_BOX); sidebar->color(FL_DARK1); sidebar->end(); // Main area grows main_area = new Fl_Group(200, 0, W - 200, H); main_area->box(FL_DOWN_BOX); main_area->end(); end(); resizable(main_area); } void resize(int X, int Y, int W, int H) override { Fl_Window::resize(X, Y, W, H); sidebar->resize(0, 0, 200, H); main_area->resize(200, 0, W - 200, H); }};
FLTK doesn’t have built-in margins, but you can simulate them:
// Add margins to a groupclass PaddedGroup : public Fl_Group { int padding;public: PaddedGroup(int X, int Y, int W, int H, int P = 10) : Fl_Group(X, Y, W, H), padding(P) {} void resize(int X, int Y, int W, int H) override { Fl_Group::resize(X, Y, W, H); // Resize children with padding for (int i = 0; i < children(); i++) { Fl_Widget *w = child(i); w->resize( X + padding, Y + padding, W - 2 * padding, H - 2 * padding ); } }};