Menu doesn't expand(menu items don't show) from layout.

Started by Kvaz1r, 17 August 2019, 20:02:43

Kvaz1r

Menu items don't show when I press on a menu. Menubar is added into layout. Is this expected behaviour or bug? If it's first case - how to fix?

MCVE:

#include <TGUI/TGUI.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "TGUI window");
tgui::Gui gui(window);
auto topLayout = tgui::VerticalLayout::create();
topLayout->setSize("100%", "10%");

tgui::MenuBar::Ptr menuBar = tgui::MenuBar::create();
menuBar->addMenu("File");
menuBar->addMenuItem("File", "New");

topLayout->add(menuBar);
topLayout->add(tgui::Button::create("Button"));
gui.add(topLayout);

while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();

gui.handleEvent(event); // Pass the event to the widgets
}

window.clear();
gui.draw(); // Draw all widgets
window.display();
}
}

texus

It could be considered a bug, but the layouts simply weren't designed for something like this. Layout widgets such as VerticalLayout are just container widget, so they will clip everything outside their size. The size of a menu bar is only the size of the bar itself, so all of the menus fall outside of the visible area of the container and are thus clipped.

You simply can't have a menu bar in a vertical layout. The only reason that I can think of where it is useful to put it in a VerticalLayout is if you want to have a panel underneath it that fills the rest of the screen so that all widgets inside that panel can have a top position relative to the bottom of the menu bar. In that case you will just have to manually give that panel a position and size (which might actually be easier because you can use an absolute size for the menu bar height instead of a ratio).

texus

My explanation is wrong for this case. I had a better look at the code and it should only have clipped if you have more than 1 menu item. The first menu item still falls within the vertical layout and could thus still be visible. The reason why it isn't visible is because the button is drawn in front of the menu bar. Try adding the following at the end:
Code (cpp) Select
menuBar->moveToFront();

In the future I will likely make menus work like the list of a combo box and always bring it to the front when opened, but for now the menus are part of the MenuBar widget itself and they thus have the same z-value.

Kvaz1r

Yes, I'm used to menu bars that separate from frame so idea put it to layout was attempted make it works. At begin I didn't use layout at all and there was my mistake.

Yeah moveToFront works well, thanks for answer.