Project

General

Profile

Bug #6562 ยป WPopupMenu_SubMenu_Example.cpp

Markus S, 08/23/2018 06:39 PM

 
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WEnvironment.h>
#include <Wt/WPopupMenu.h>
#include <Wt/WPushButton.h>

class TestPopupSubMenu
: public Wt::WApplication
{
public:
TestPopupSubMenu( const Wt::WEnvironment& env )
: Wt::WApplication( env )
{
root()->addNew< Wt::WPushButton >( "show WPopupMenu - Example from Reference" )
->mouseWentUp().connect( this, &TestPopupSubMenu::ShowPopupMenuReferenceExample );

root()->addNew< Wt::WPushButton >( "show WPopupMenu - Custom example" )
->mouseWentUp().connect( this, &TestPopupSubMenu::ShowPopupMenuCustom );
}

void ShowPopupMenuReferenceExample( const Wt::WMouseEvent& me )
{
Wt::WPopupMenu popup;
popup.addItem("Item 1")->setCheckable(true);
popup.addItem("Item 2");

auto subMenu = std::make_unique<Wt::WPopupMenu>();
subMenu->addItem("Sub Item 1");
subMenu->addItem("Sub Item 2");
popup.addMenu("Item 3", std::move(subMenu))->setSelectable( true );

popup.exec( me );
}

std::unique_ptr< Wt::WMenuItem > MenuItem( std::string text, bool checkable = false )
{
auto w = std::make_unique< Wt::WMenuItem >( text );
w->setCheckable( checkable );
return w;
}
std::unique_ptr< Wt::WMenuItem > MenuItem( std::string text, std::unique_ptr< Wt::WPopupMenu > subMenu )
{
auto w = MenuItem( text, false );
w->setMenu( std::move( subMenu ) );
w->setSelectable( true );
return w;
}
std::unique_ptr< Wt::WPopupMenu > CreateExampleMenu()
{
auto popup = std::make_unique< Wt::WPopupMenu >();
popup->addItem( MenuItem( "Item 1", true ) );
popup->addItem( MenuItem( "Item 2" ) );

auto subMenu1 = std::make_unique< Wt::WPopupMenu >();
subMenu1->addItem( MenuItem( "Sub Item 1" ) );
subMenu1->addItem( MenuItem( "Sub Item 2" ) );
popup->addItem( MenuItem( "Item 3", std::move( subMenu1 ) ) );

return popup;
}
void ShowPopupMenuCustom( const Wt::WMouseEvent& me )
{
auto p = root()->addChild( CreateExampleMenu() );
p->exec( me );
root()->removeChild( p );
}
};
    (1-1/1)