Adapt container to its child's width

Started by guatto, 25 January 2022, 23:42:14

guatto

Hi, i have a ChildWindow and i want to make it fit to the width of the HorizontalWrap, i didn't found any function to do that "automatically", here's my code :


    auto childWindow = tgui::ChildWindow::create();
    auto layout = tgui::HorizontalWrap::create();
    childWindow->add(layout);

    auto wgt1 = tgui::Label::create("Some words");
    wgt1->getRenderer()->setBackgroundColor({250,0,0});
    wgt1->getRenderer()->setTextColor({0,0,0});
    wgt1->setVerticalAlignment(tgui::Label::VerticalAlignment::Center);

    auto wgt2 = tgui::Label::create("Another things");
    wgt2->getRenderer()->setBackgroundColor({0,0,255});
    wgt2->getRenderer()->setTextColor({0,0,0});
    wgt2->setVerticalAlignment(tgui::Label::VerticalAlignment::Center);

    layout->add(wgt1);
    layout->add(wgt2);

    //HERE - How to make the parent's size automatically fit its children's size ?

    gui.add(childWindow);


Any idea please ?

texus

Sorry for the late reply, I forgot to answer this before. ChildWindow is a bit of a special case: you probably want to set its client size, but you can't change that automatically. The only thing you can change automatically is the entire size, which includes the borders and title bar. You could try something like this, where the hardcodes values correspond to border width and title bar height:
childWindow->setSize(bindWidth(layout) + 2, bindHeight(layout) + 22);

guatto

Hi again, it's okay for the late reply, i'm the one to be sorry for this basic questions, i know you have a busy schedule, so, i tried your method but all it shows me is the titlebar with its default width (400), so i replaced the ChildWindow with a Panel and removed the hardcodes values, but the result is worse , it doesn't show me anything, the returned size is equal to 0 :


    auto panel = tgui::Panel::create(); // Changed to Panel
    auto layout = tgui::HorizontalWrap::create();
    panel->add(layout);

    auto wgt1 = tgui::Label::create("Some words");
    ...

    auto wgt2 = tgui::Label::create("Another things");
    ...

    layout->add(wgt1);
    layout->add(wgt2);

    panel->setSize(bindWidth(layout),bindHeight(layout));

    gui.add(panel);

texus

Are you setting a size for the HorizontalWrap?
By default the layout will fill the parent (i.e. the child window or panel), but you are setting the parent size to equal the HorizontalWrap.
You have to explicitly specify a size for either the panel or the layout.

guatto

No, precisely, i wanted my Panel to take the same width as the layout but depending on the width of the widgets that are inside the layout, here is an image to clarify what I want to do:


texus

TGUI unfortunately doesn't work like that, widgets can't automatically size based on their children. If there is only a single child then you can use the hack that I suggested for the child window, but if you want a parent to get the size of 5 buttons then you will have to calculate it manually.

HorizontalWrap also doesn't even come close to what you want to do. It has to be given a width, and when the widgets that are placed next to each other no longer fit inside this width, a new row will be added.
HorizontalLayout might be a better choice, but even then you still have to specify a fixed width, as HorizontalLayout will divide its width to its children (i.e. the buttons will get a size based on the layout size, not the other way around).

guatto

I guess i'll recalculate all that manually or maybe do the calculation inside a class to capture the layout width change. thanks again Texus for all your time !