Project

General

Profile

RE: WStackedWidget prevents WMessageBox from showing ยป main_rev1.cpp

Minimal example code, revision 1 - Georg Wenig, 05/27/2021 12:19 PM

 
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLink.h>
#include <Wt/WMessageBox.h>
#include <Wt/WPushButton.h>
#include <Wt/WSpinBox.h>
#include <Wt/WStackedWidget.h>
#include <Wt/WStreamResource.h>
#include <Wt/WText.h>

#include <iostream>
#include <memory>
#include <sstream>

class MY_APPLICATION;

class MY_RESOURCE : public Wt::WStreamResource
{
public:
explicit MY_RESOURCE(MY_APPLICATION * i_application) :
m_application(i_application)
{
// Removed - the update lock is taken manually in handleRequest(...).
//setTakesUpdateLock(true);
setMimeType("text/plain");
suggestFileName("data.txt");
}
void handleRequest(const Wt::Http::Request & request, Wt::Http::Response & response) override;
private:
MY_APPLICATION * m_application {nullptr};
};

class MY_APPLICATION : public Wt::WApplication
{
public:
explicit MY_APPLICATION(const Wt::WEnvironment & i_enviromment) :
WApplication(i_enviromment)
{
#ifdef NO_WIDGET_STACK
auto container = root()->addWidget(std::make_unique<Wt::WContainerWidget>());
#else
auto spin_box = root()->addWidget(std::make_unique<Wt::WSpinBox>());
spin_box->setRange(0, 1);
auto main_stack = root()->addWidget(std::make_unique<Wt::WStackedWidget>());
spin_box->valueChanged().connect<Wt::WStackedWidget, Wt::WStackedWidget, int>(main_stack, & Wt::WStackedWidget::setCurrentIndex);
main_stack->addWidget(std::make_unique<Wt::WText>("This is the boring page 0."));
auto container = main_stack->addWidget(std::make_unique<Wt::WContainerWidget>());
#endif

m_spin_box = container->addWidget(std::make_unique<Wt::WSpinBox>());
auto validate_button = container->addWidget(std::make_unique<Wt::WPushButton>("Validate"));
validate_button->clicked().connect(this, & MY_APPLICATION::validate);
auto export_button = container->addWidget(std::make_unique<Wt::WPushButton>("Export"));
auto my_resource = std::make_shared<MY_RESOURCE>(this);
m_spin_box->valueChanged().connect(my_resource.get(), & Wt::WResource::setChanged);
export_button->setLink(Wt::WLink(my_resource));

enableUpdates(true);
}

void validate()
{
const int current_value = m_spin_box->value();
if (current_value == 0)
{
auto message_box = m_spin_box->addChild(std::make_unique<Wt::WMessageBox>(
"Error",
"Value must not be 0",
Wt::Icon::Critical,
Wt::StandardButton::Ok));

message_box->buttonClicked().connect([=] {m_spin_box->removeChild(message_box);});
message_box->show();
}
}

Wt::WSpinBox * m_spin_box {nullptr};
};

void MY_RESOURCE::handleRequest(const Wt::Http::Request & request, Wt::Http::Response & response)
{
Wt::WApplication::UpdateLock update_lock(m_application);
if (update_lock)
{
m_application->validate();
m_application->triggerUpdate();
std::stringstream stream;
stream << "Your data: " << m_application->m_spin_box->value();
handleRequestPiecewise(request, response, stream);
}
else
{
std::istringstream stream("Unable to acquire update lock");
handleRequestPiecewise(request, response, stream);
}
}

int main(int i_argc, char ** i_argv)
{
return Wt::WRun(i_argc, i_argv, [](const Wt::WEnvironment &env)
{
return std::make_unique<MY_APPLICATION>(env);
});
}
    (1-1/1)