Child widget's coordinates problem

23 views
Skip to first unread message

Lucas Sanner

unread,
Aug 29, 2026, 4:25:24 AM (9 days ago) Aug 29
to fltk.general
Hi,
I'm a bit confused about how to use a child widget's coordinates.
In the following code, a parent group owns a Fl_Box child widget:

int main() {
    // Main window
    Fl_Double_Window window(400, 300, "FLTK Coordinate Test");

    // Parent group at a specific position
    Fl_Group* parentGroup = new Fl_Group(50, 50, 300, 200);
    parentGroup->box(FL_DOWN_BOX);
    parentGroup->color(FL_GRAY);

    // Simple Fl_Box owned by parentGroup
    Fl_Box* box = new Fl_Box(0, 0, 100, 100, "Fl_Box");
    box->box(FL_FLAT_BOX);
    box->color(FL_GREEN);
    parentGroup->add(box);

    parentGroup->end();

    window.end();
    window.show();

    return Fl::run();
}

It turns out that the green box appears at the window's top‑left corner.
Shouldn't it appear at the parent group's top‑left corner instead ?
Aren't x and y supposed to be relative to the parent group ?
Thanks for your enlightenment.

FTK version: 1.4.4
OS: Debian

Brian Larsen

unread,
Aug 29, 2026, 8:02:45 AM (9 days ago) Aug 29
to fltk.general
See the Coordinates and Layout Widgets in the FLTK doc (https://fltk.gitlab.io/fltk/coordinates.html).
It says "All widgets have constructors with x and y parameters to let the programmer specify the desired initial position of the top left corner during explicit manual layout within Fl_Window and Fl_Group container widgets.
This position is always relative to the enclosing Fl_Window, ..."

imm

unread,
Aug 29, 2026, 8:36:01 AM (9 days ago) Aug 29
to General FLTK
On Sat, 29 Aug 2026, 09:25 Lucas Sanner wrote:
Hi,
I'm a bit confused about how to use a child widget's coordinates.
In the following code, a parent group owns a Fl_Box child widget:

int main() {
    // Main window
    Fl_Double_Window window(400, 300, "FLTK Coordinate Test");

    // Parent group at a specific position
    Fl_Group* parentGroup = new Fl_Group(50, 50, 300, 200);
    parentGroup->box(FL_DOWN_BOX);
    parentGroup->color(FL_GRAY);

    // Simple Fl_Box owned by parentGroup
Fl_Box* box = new Fl_Box(
    0, 0, 100, 100, "Fl_Box");

In FLTK 1 widget coordinates are with respect to the enclosing window, not just the parent widget. There's good reasons for that, but it's not always the most convenient...

Anyway, something like 

int px = parentGroup-x();
int py = parentGroup-y();

Fl_Box* box = new Fl_Box(px, py, etc...

Ought to help 
--
Ian
From my Fairphone FP6

Greg Ercolano

unread,
Aug 29, 2026, 11:50:38 AM (9 days ago) Aug 29
to fltkg...@googlegroups.com


On 8/29/26 01:25, Lucas Sanner wrote:
Hi,
I'm a bit confused about how to use a child widget's coordinates.
[..]

Pretty much what everyone said so far; all xy coodinates of child widgets are relative to the parent window.

There is one caveat; if you really want 0,0 to be the upper left corner of a child group, replace Fl_Group with Fl_Window, so basically you have a "window in a window" - fltk calls this a 'subwindow'. Then the upper/left corner of that becomes the origin for the entire child hierarchy below it.

But usually when you construct widget hierarchies, you do your x,y math relative to the parent "top-level" window, and avoid windows-in-windows unless really necessary.

Albrecht Schlosser

unread,
Aug 29, 2026, 12:18:47 PM (9 days ago) Aug 29
to fltkg...@googlegroups.com
And to add even more: since FLTK 1.4 we have two more container widgets than basic Fl_Group widgets: Fl_Flex and Fl_Grid (there are others as well, but these two are new and the most flexible ones). These group widgets lay out their children in a flexible way so you don't need to calculate widget coordinates at all. For Fl_Grid you add any widget to a cell of a two-dimensional grid (like a table).

I recommend to study the documentation of these widgets, and you can view our test and demo programs in the test folder:

$ ls test/flex_*.cxx test/grid*.cxx
test/flex_demo.cxx  test/flex_login.cxx  test/grid_alignment.cxx  test/grid_buttons.cxx  test/grid_dialog.cxx  test/grid_login.cxx

and some more "real" test programs that use Fl_Grid internally (do your own search to find out which ones).

Reply all
Reply to author
Forward
0 new messages