Project

General

Profile

Bug #944 ยป humenu.cpp

example with sub-menus - Anonymous, 08/10/2011 10:34 AM

 

#include <Wt/WApplication>
#include <Wt/WContainerWidget>

#include <Wt/WMenu>
#include <Wt/WSubMenuItem>
#include <Wt/WStackedWidget>
#include <Wt/WEnvironment>
#include <Wt/WText>
//#include <Wt/WText>
//#include <Wt/WText>

using namespace Wt;

class testApplication : public WApplication
{
public:
testApplication(const WEnvironment& env);

private:
Wt::WStackedWidget *contents;
Wt::WMenu *menu;
Wt::WSubMenuItem *itemC;
Wt::WMenu *itemCsub;
Wt::WSubMenuItem *itemD;
Wt::WMenu *itemDsub;

};



testApplication::testApplication(const WEnvironment& env)
: WApplication(env)
{
setTitle("Menu test"); // application title
//root()->addWidget(new WText("Your name, please ? ")); // show some text
useStyleSheet("css/menutest.css"); // css for menu
// create the stack where the contents will be located
Wt::WStackedWidget *contents = new Wt::WStackedWidget();

// create a top-level menu
Wt::WMenu *menu = new Wt::WMenu(contents, Wt::Vertical);
menu->setRenderAsList(true);
menu->setStyleClass("mymenu");
menu->setInternalPathEnabled("/");

// add two plain items
menu->addItem("ItemA", new Wt::WText("<h2>A</h2>ItemA contents"))->setPathComponent("");
menu->addItem("ItemB", new Wt::WText("<h2>B</h2>ItemB contents"));

// add an item with a sub menu
Wt::WSubMenuItem *itemC = new Wt::WSubMenuItem("ItemC", new Wt::WText("<h2>C</h2>ItemC contents"));
Wt::WMenu *itemCsub = new Wt::WMenu(contents, Wt::Vertical);
itemCsub->setRenderAsList(true);
itemCsub->setInternalPathEnabled("itemc/");
itemCsub->addItem("ItemC1", new Wt::WText("<h2>C1</h2>ItemC1 contents"));
itemCsub->addItem("ItemC2", new Wt::WText("<h2>C2</h2>ItemC2 contents"));
itemCsub->addItem("ItemC3", new Wt::WText("<h2>C3</h2>ItemC3 contents"));
itemC->setSubMenu(itemCsub);
menu->addItem(itemC);
// add an item with a sub menu
Wt::WSubMenuItem *itemD = new Wt::WSubMenuItem("ItemD", new Wt::WText("<h2>D</h2>ItemD contents"));
Wt::WMenu *itemDsub = new Wt::WMenu(contents, Wt::Vertical);
itemDsub->setRenderAsList(true);
itemDsub->setInternalPathEnabled("itemd/");
itemDsub->addItem("ItemD1", new Wt::WText("<h2>D1</h2>ItemD1 contents"));
itemDsub->addItem("ItemD2", new Wt::WText("<h2>D2</h2>ItemD2 contents"));
itemD->setSubMenu(itemDsub);
menu->addItem(itemD);

// add two plain items
menu->addItem("ItemE", new Wt::WText("<h2>E</h2>ItemE contents"));
root()->addWidget(menu);
root()->addWidget(new WText("<hr/>"));
root()->addWidget(contents);
}



WApplication *createApplication(const WEnvironment& env)
{
return new testApplication(env);
}

int main(int argc, char **argv)
{
return WRun(argc, argv, &createApplication);
}

    (1-1/1)