Project

General

Profile

Bug #7914 ยป Main.cpp

hello example with menu - jeroen zpam, 12/14/2020 11:07 PM

 
/** @file
@brief Implementation of main function */


#pragma region include standard libraries
#include <stdio.h>
#pragma include standard libraries endregion


#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WEnvironment>
#include <Wt/WLineEdit>
#include <Wt/WMenu>
#include <Wt/WMenuItem>
#include <Wt/WPopupMenu>
#include <Wt/WPushButton>
#include <Wt/WServer>
#include <Wt/WText>

// c++0x only, for std::bind
// #include <functional>

/*
* A simple hello world application class which demonstrates how to react
* to events, read input, and give feed-back.
*/
class HelloApplication : public Wt::WApplication
{
public:
HelloApplication(const Wt::WEnvironment& env, bool embedded);

private:
Wt::WLineEdit *nameEdit_;
Wt::WText *greeting_;

void greet();
};

/*
* The env argument contains information about the new session, and
* the initial request. It must be passed to the WApplication
* constructor so it is typically also an argument for your custom
* application constructor.
*/
HelloApplication::HelloApplication(const Wt::WEnvironment& env, bool embedded)
: Wt::WApplication(env)
{
setTitle("Hello world"); // application title

Wt::WContainerWidget* top = embedded ? new Wt::WContainerWidget() : root();
if (embedded)
{
const std::string* div = env.getParameter("div");
if (div)
{
setJavaScriptClass(*div);
bindWidget(top, *div);
}
else { return; }
}

Wt::WMenu* pMainMenu = new Wt::WMenu();
pMainMenu->addItem("AAA");

Wt::WPopupMenu* pSubMenu = new Wt::WPopupMenu();
pSubMenu->addItem(new Wt::WMenuItem("BBB1"));
pSubMenu->addItem(new Wt::WMenuItem("BBB2"));
pSubMenu->addItem(new Wt::WMenuItem("BBB3"));
pMainMenu->addMenu("BBB", pSubMenu);

pMainMenu->addItem("CCC");

top->addWidget(pMainMenu);


top->addWidget(new Wt::WText("Your name, please ? ")); // show some text
nameEdit_ = new Wt::WLineEdit(top); // allow text input
nameEdit_->setFocus(); // give focus

Wt::WPushButton *button
= new Wt::WPushButton("Greet me.", top); // create a button
button->setMargin(5, Wt::Left); // add 5 pixels margin

top->addWidget(new Wt::WBreak()); // insert a line break

greeting_ = new Wt::WText(top); // empty text

/*
* Connect signals with slots
*
* - simple Wt-way
*/
button->clicked().connect(this, &HelloApplication::greet);

/*
* - using an arbitrary function object (binding values with boost::bind())
*/
nameEdit_->enterPressed().connect
(boost::bind(&HelloApplication::greet, this));

/*
* - using a c++0x lambda:
*/
// button->clicked().connect(std::bind([=]() {
// greeting_->setText("Hello there, " + nameEdit_->text());
// }));
}

void HelloApplication::greet()
{
/*
* Update the text, using text input into the nameEdit_ field.
*/
greeting_->setText("Hello there, " + nameEdit_->text());
}

Wt::WApplication *CreateHome(const Wt::WEnvironment& env)
{
/*
* You could read information from the environment to decide whether
* the user has permission to start a new application
*/
return new HelloApplication(env, false);
}

Wt::WApplication *CreateHomeEmbedded(const Wt::WEnvironment& env)
{
/*
* You could read information from the environment to decide whether
* the user has permission to start a new application
*/
return new HelloApplication(env, true);
}


int main(int argc, char** argv)
{
static const char STR_HTTP_ARG[] = "--http-address=";
static const char STR_APPROOT_ARG[] = "--approot=/tmp/approot";
static const char STR_DOCROOT_ARG[] = "--docroot=/tmp/docroot";
static const char STR_HTTP_PORT_ARG[] = "--http-port=80";
static const char STR_THREADS_ARG[] = "--threads=1";
static const char STR_CONFIG_FILE[] = "/tmp/wt_config.xml";
static const char STR_ACCESSLOG_ARG[] = "--accesslog=/tmp/webui_log.txt";

char httpArg[32];
snprintf(httpArg, sizeof(httpArg), "%s%s", STR_HTTP_ARG, "0.0.0.0");
const char* szProcName = "hello";

const char* myArgv[]=
{
szProcName,
STR_APPROOT_ARG,
STR_DOCROOT_ARG,
httpArg,
STR_HTTP_PORT_ARG,
STR_ACCESSLOG_ARG,
STR_THREADS_ARG
};
int myArgc = sizeof(myArgv) / sizeof(char*);

Wt::WServer* pWtServer = new Wt::WServer(myArgv[0], STR_CONFIG_FILE);
pWtServer->setServerConfiguration(myArgc, (char**)myArgv, WTHTTP_CONFIGURATION);
pWtServer->addEntryPoint(Wt::Application, &CreateHome);
pWtServer->addEntryPoint(Wt::WidgetSet, &CreateHomeEmbedded, "/home.js");
pWtServer->start();
printf("press any key to quit\n");
getchar();
pWtServer->stop();
}
    (1-1/1)