Project

General

Profile

WDialog and layout » main.cpp

Maximilian Kleinert, 03/27/2017 11:25 AM

 
1
#include <functional>
2
#include <Wt/WBootstrapTheme>
3
#include <Wt/WApplication>
4
#include <Wt/WContainerWidget>
5
#include <Wt/WDialog>
6
#include <Wt/WEnvironment>
7
#include <Wt/WPushButton>
8
#include <Wt/WText>
9

    
10
void showDialog(const bool percentage)
11
{
12
	Wt::WDialog *dialog = new Wt::WDialog("Layout Test");
13
	if (percentage)
14
		dialog->setMaximumSize(Wt::WLength(50, Wt::WLength::Percentage), Wt::WLength(50, Wt::WLength::Percentage));
15
	else
16
		dialog->setMaximumSize(Wt::WLength(400), Wt::WLength(400));
17
	Wt::WContainerWidget* form = dialog->contents();
18
	form->setOverflow(Wt::WContainerWidget::OverflowAuto);
19
	for (int i = 0; i < 100; ++i) {
20
		Wt::WString str("<p>Row {1}</p>");
21
		form->addWidget(new Wt::WText(str.arg(i)));
22
	}
23

    
24
	Wt::WPushButton *ok = new Wt::WPushButton("OK", dialog->footer());
25
	ok->setDefault(true);
26

    
27
	Wt::WPushButton *cancel = new Wt::WPushButton("Cancel", dialog->footer());
28
	dialog->rejectWhenEscapePressed();
29

    
30
	ok->clicked().connect(dialog, &Wt::WDialog::accept);
31
	cancel->clicked().connect(dialog, &Wt::WDialog::reject);
32
	dialog->finished().connect(std::bind([=]() { delete dialog; }));
33
	dialog->show();
34
}
35

    
36
class TestApplication : public Wt::WApplication
37
{
38
public:
39
	TestApplication(const Wt::WEnvironment& env);
40
};
41

    
42
TestApplication::TestApplication(const Wt::WEnvironment& env)
43
	: WApplication(env)
44
{
45
	Wt::WBootstrapTheme *bootstrapTheme = new Wt::WBootstrapTheme(this);
46
	bootstrapTheme->setVersion(Wt::WBootstrapTheme::Version3);
47
	bootstrapTheme->setResponsive(true);
48
	this->setTheme(bootstrapTheme);
49
	// load the default bootstrap3 (sub-)theme
50
	this->useStyleSheet("resources/themes/bootstrap/3/bootstrap-theme.min.css");
51
	this->useStyleSheet("resources/font-awesome/css/font-awesome.min.css");
52

    
53
	setTitle("WDialog Test");
54

    
55
	const auto cerateButton = [this](const Wt::WString& name, const bool percentage)
56
	{
57
		Wt::WPushButton *button = new Wt::WPushButton(name, root());
58
		button->setMargin(5);
59
		button->clicked().connect(std::bind([=]() {
60
			showDialog(percentage);
61
		}));
62
	};
63
	cerateButton("Dialog Percentage", true);
64
	cerateButton("Dialog Absolute", false);
65
}
66

    
67
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
68
{
69
	/*
70
	* You could read information from the environment to decide whether
71
	* the user has permission to start a new application
72
	*/
73
	return new TestApplication(env);
74
}
75

    
76
int main(int argc, char **argv)
77
{
78
	/*
79
	* Your main method may set up some shared resources, but should then
80
	* start the server application (FastCGI or httpd) that starts listening
81
	* for requests, and handles all of the application life cycles.
82
	*
83
	* The last argument to WRun specifies the function that will instantiate
84
	* new application objects. That function is executed when a new user surfs
85
	* to the Wt application, and after the library has negotiated browser
86
	* support. The function should return a newly instantiated application
87
	* object.
88
	*/
89
	return Wt::WRun(argc, argv, &createApplication);
90
}
(3-3/3)