Project

General

Profile

Support #7454 » main.cpp

Marco Kinski, 02/11/2020 01:22 PM

 
// popup.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#pragma warning(push)
#pragma warning(disable: 4267) // wt\wstringstream.h(110): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data
#include <Wt/WApplication.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WVBoxLayout.h>
#include <Wt/WText.h>
#include <Wt/WLineEdit.h>
#include <Wt/WCompositeWidget.h>
#include <Wt/WCssStyleSheet.h>
#include <Wt/WPushButton.h>
#include <Wt/WDialog.h>
#pragma warning(pop)

class Popup : public Wt::WCompositeWidget
{
std::unique_ptr<Wt::WText> popup;
bool in{false};

Wt::WLineEdit* edit{};

public:
Popup (const std::string& what)
{
using namespace Wt;
using std::make_unique;

WCssDecorationStyle style;
style.setBackgroundColor(WColor{"yellow"});
wApp->styleSheet().addRule(".foo", style);

popup = make_unique<WText>("popup");
popup->setPositionScheme(PositionScheme::Absolute);
popup->setPopup(true);
popup->resize(250, 150);
popup->addStyleClass("foo");
popup->addStyleClass("wt-no-reparent"); // Workaround for strange scrolling behaviour after update to 3.3.2 (removed with commit: 0e5edc630ad542cda9294e1449a537363d069d67)

edit = setImplementation(make_unique<WLineEdit>(what));
edit->focussed().connect(this, &Popup::OnFocussed);
edit->blurred().connect(this, &Popup::OnBlurred);
}

~Popup ()
{
if (in) {
wApp->removeGlobalWidget(popup.get());
}
}

void OnBlurred ()
{
if (in) {
wApp->removeGlobalWidget(popup.get());
in = false;
}
}

void OnFocussed ()
{
if (!in) {
wApp->addGlobalWidget(popup.get());
popup->positionAt(edit);
in = true;
}
}
};


class App : public Wt::WApplication {
public:
App (const Wt::WEnvironment& env) : WApplication(env)
{
using namespace Wt;
using std::make_unique;

auto wrapper = root()->setLayout(make_unique<WVBoxLayout>());
auto scollarea = wrapper->addWidget(make_unique<WContainerWidget>());
scollarea->setOverflow(Overflow::Auto);

auto contents = scollarea->addWidget(make_unique<WContainerWidget>());
auto layout = contents->setLayout(make_unique<WVBoxLayout>());

for (int i = 0; i < 10; ++i) {
layout->addWidget(make_unique<Popup>("Hello, World!"));
}

auto btn = layout->addWidget(make_unique<WPushButton>("dlg"));
btn->clicked().connect(this, &App::OpenDialog);

layout->addStretch(1);
}

void OpenDialog ()
{
using namespace Wt;
using std::make_unique;

auto dlg = make_unique<WDialog>("title");

auto wrapper = dlg->contents()->setLayout(make_unique<WVBoxLayout>());
auto scollarea = wrapper->addWidget(make_unique<WContainerWidget>());
scollarea->setOverflow(Overflow::Auto);

auto contents = scollarea->addWidget(make_unique<WContainerWidget>());
auto layout = contents->setLayout(make_unique<WVBoxLayout>());

for (int i = 0; i < 10; ++i) layout->addWidget(make_unique<Popup>("Hello, World!"));

dlg->setClosable(true);
dlg->setResizable(true);

dlg->setMinimumSize(150, 150);
dlg->resize(230, 280);
dlg->exec();
}

static std::unique_ptr<Wt::WApplication> Create (const Wt::WEnvironment& env) {
return std::make_unique<App>(env);
}
};











#include <iostream>
#include <Wt/WServer.h>

int main(int argc, char** argv)
{
try {
Wt::WLayout::setDefaultImplementation(Wt::LayoutImplementation::JavaScript); // use old layout behaviour

Wt::WServer server(argc, argv);
server.addEntryPoint(Wt::EntryPointType::Application, &App::Create);

server.run();

return EXIT_SUCCESS;
}
catch (std::exception& e) {
std::cerr << e.what() << "\n";
return EXIT_FAILURE;
}
}
(1-1/4)