Project

General

Profile

RE: Resizable (Splitter) Layout not working correctly in ... ยป main.cpp

Stefan Bn, 11/29/2022 10:11 PM

 
#ifndef WT_WIN32
extern char **environ;
#endif // WT_WIN32


#include <Wt/WApplication.h>
#include <Wt/WBootstrap5Theme.h>
#include <Wt/WServer.h>

#include <Wt/WContainerWidget.h>
#include <Wt/WHBoxLayout.h>
#include <Wt/WVBoxLayout.h>
#include <Wt/WMenu.h>
#include <Wt/WStackedWidget.h>
#include <Wt/WText.h>

using namespace Wt;


/*
* A utility container widget which defers creation of its single
* child widget until the container is loaded (which is done on-demand
* by a WMenu). The constructor takes the create function for the
* widget as a parameter.
*
* We use this to defer widget creation until needed, which also defers
* loading auxiliary javascript libraries.
*/
#if !defined(WT_TARGET_JAVA)
template <typename Function>
class DeferredWidget : public Wt::WContainerWidget
{
public:
DeferredWidget(Function f)
: f_(f) { }

private:
void load()
{
addWidget(f_());
Wt::WContainerWidget::load();
}

Function f_;
};

template <typename Function>
std::unique_ptr<DeferredWidget<Function>> deferCreate(Function f)
{
return std::make_unique<DeferredWidget<Function>>(f);
}
#else
class DeferredWidget : public Wt::WContainerWidget
{
public:
DeferredWidget(std::bound f) {}
};
std::unique_ptr<DeferredWidget> deferCreate(std::bound b)
{
return std::make_unique<DeferredWidget>(b);
}
#endif



// -----------------------------------------------------------------------------------
std::unique_ptr<WContainerWidget> createSplitView()
{
std::cerr << "*** Create Split View" << std::endl;

auto flexCont = std::make_unique<WContainerWidget>();
flexCont->setAttributeValue("style", "width: 100%; height: 100%; background-color: yellow;");

auto vLayout = flexCont->setLayout(std::make_unique<WVBoxLayout>());

auto splitHLayout = vLayout->addLayout(std::make_unique<WHBoxLayout>(), 1);
splitHLayout->setPreferredImplementation(LayoutImplementation::JavaScript);

auto leftCont = splitHLayout->addWidget(std::make_unique<WContainerWidget>());
leftCont->setAttributeValue("style", "width: 100%; height: 100%;");
leftCont->decorationStyle().setBackgroundColor(WColor("blue"));
leftCont->setMinimumSize(WLength::Auto, 50);
leftCont->addNew<WText>("Left Container, should be 100% height of the browser window");

auto rightCont = splitHLayout->addWidget(std::make_unique<WContainerWidget>());
rightCont->setAttributeValue("style", "width: 100%; height: 100%;");
rightCont->decorationStyle().setBackgroundColor(WColor("green"));
rightCont->setMinimumSize(WLength::Auto, 50);
rightCont->addNew<WText>("Right Container, should be 100% height of the browser window");

splitHLayout->setResizable(0, true, WLength(70, LengthUnit::Percentage));

return flexCont;
}


// -----------------------------------------------------------------------------------
std::unique_ptr<WApplication> createHelloApplication(const WEnvironment &env)
{
auto app = std::make_unique<WApplication>(env);

auto theme = std::make_shared<WBootstrap5Theme>();
app->setTheme(theme);

app->root()->setAttributeValue("style", "width: 100%; height: 100%;");
auto hLayout = app->root()->setLayout(std::make_unique<WHBoxLayout>());

auto leftCont = hLayout->addWidget(std::make_unique<WContainerWidget>());
leftCont->setAttributeValue("style", "background-color: #b3daff;");
leftCont->setWidth(280);

auto rightCont = hLayout->addWidget(std::make_unique<WContainerWidget>(), 1);
rightCont->setAttributeValue("style", "background-color: grey;");

auto rightVLayout = rightCont->setLayout(std::make_unique<WVBoxLayout>());

auto stackUP = std::make_unique<WStackedWidget>();

auto menu = leftCont->addNew<WMenu>(stackUP.get());

rightVLayout->addWidget(std::move(stackUP), 1);

menu->addItem("Stack Level 0 - Empty", std::make_unique<WText>("Empty"));

// This will work:
// menu->addItem("Show Split Layout - Click here", createSplitView());

// This will not work:
menu->addItem("Show Split Layout - Click here", deferCreate(std::bind(&createSplitView)));

return app;
}


// -----------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
try
{
WServer server(argc, argv, WTHTTP_CONFIGURATION);

server.addEntryPoint(EntryPointType::Application,
std::bind(&createHelloApplication, std::placeholders::_1));

if (server.start())
{
int sig = WServer::waitForShutdown();

std::cerr << "*** Shutdown (signal = " << sig << ")" << std::endl;
server.stop();

// if (sig == 1) // SIGHUP
// WServer::restart(argc, argv, environ);

return 0;
}
}
catch (WServer::Exception &e)
{
std::cerr << "*** Server Exception: " << e.what() << std::endl;
}
catch (std::exception &e)
{
std::cerr << "*** Std Exception: " << e.what() << std::endl;
}
}
    (1-1/1)