Project

General

Profile

Bug #6014 » test_20171014a_oscillation_painted_widget.C

Bruce Toll, 10/17/2017 05:04 PM

 
#include <Wt/WApplication.h>
#include <Wt/WRectArea.h>
#include <Wt/WEnvironment.h>
#include <Wt/WPaintedWidget.h>
#include <Wt/WPainter.h>
#include <Wt/WPushButton.h>
#include <Wt/WText.h>
#include <Wt/WVBoxLayout.h>

using namespace Wt;
using namespace std;

class TestApp : public WApplication {
public:
TestApp(const WEnvironment& env);
void testWindow();
private:
int windowInstance_ = 0;
};

class PaintedWidget : public WPaintedWidget
{
public:
PaintedWidget() : WPaintedWidget() { }

protected:
void paintEvent(WPaintDevice *paintDevice) {
WPainter painter(paintDevice);
painter.setBrush(WBrush(StandardColor::Green));
if (!areas().empty())
removeArea(areas()[0]);
auto area = make_unique<WRectArea>(painter.window());
area->setToolTip("Green Rectangle");
addArea(move(area));
painter.drawRect(painter.window());
}
};

void TestApp::testWindow()
{
auto topLayout = make_unique<WVBoxLayout>();
topLayout->setPreferredImplementation(LayoutImplementation::JavaScript);

auto paintedWidget = make_unique<PaintedWidget>();
topLayout->addWidget(move(paintedWidget), 1);
auto footer = make_unique<WText>("<p>Footer with red top border</p>");
footer->addStyleClass("test_ft");
styleSheet().addRule(".test_ft", "border-top: 2px solid red;");
topLayout->addWidget(move(footer), 0);
root()->setLayout(move(topLayout));
}

TestApp::TestApp(const WEnvironment& env) : WApplication(env)
{
auto param = env.getParameter("p");
setTitle(string("Oscillation Test") + (param ? (" Window " + *param) : ""));

if (param)
testWindow();
else {
root()->addWidget(move(make_unique<WText>("<p>Push button to open window.</p>"
"<p>Watch for oscillations in Chrome at different zoom levels, e.g. 125%.</p>")));
auto button = root()->addWidget(make_unique<WPushButton>("Open window"));

button->clicked().connect([=] {
WString url = wApp->resolveRelativeUrl("?p=" + to_string(++windowInstance_));
button->doJavaScript("window.open(" + url.jsStringLiteral() + ", 'Test Window " +
to_string(windowInstance_) + "', 'height=389,width=400');");
});
}
}

int main(int argc, char **argv)
{
return WRun(argc, argv, [](const WEnvironment& env) {return make_unique<TestApp>(env);});
}
(1-1/3)