Wt examples
4.2.2
builddir
build
BUILD
wt-4.2.2
examples
form
FormExample.C
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
3
*
4
* See the LICENSE file for terms of use.
5
*/
6
7
#include "
FormExample.h
"
8
#include "
Form.h
"
9
10
#include <Wt/WApplication.h>
11
#include <Wt/WText.h>
12
#include <Wt/WStringUtil.h>
13
14
FormExample::FormExample
()
15
:
WContainerWidget
()
16
{
17
WContainerWidget
*langLayout = this->
addWidget
(cpp14::make_unique<WContainerWidget>());
18
langLayout->
setContentAlignment
(AlignmentFlag::Right);
19
langLayout->
addWidget
(cpp14::make_unique<WText>(
tr
(
"language"
)));
20
21
const
char
*lang[] = {
"en"
,
"nl"
};
22
23
for
(
int
i = 0; i < 2; ++i) {
24
WText
*t = langLayout->
addWidget
(cpp14::make_unique<WText>(lang[i]));
25
t->
setMargin
(5);
26
t->
clicked
().
connect
(std::bind(&
FormExample::changeLanguage
,
this
, t));
27
28
languageSelects_
.push_back(t);
29
}
30
31
/*
32
* Start with the reported locale, if available
33
*/
34
setLanguage
(wApp->locale().name());
35
36
Form
*form = this->
addWidget
(cpp14::make_unique<Form>());
37
form->
setMargin
(20);
38
}
39
40
void
FormExample::setLanguage
(
const
std::string lang)
41
{
42
bool
haveLang =
false
;
43
44
for
(
auto
i :
languageSelects_
) {
45
WText
*t = i;
46
47
// prefix match, e.g. en matches en-us.
48
bool
isLang = lang.
find
(t->
text
().
toUTF8
()) == 0;
49
t->
setStyleClass
(isLang ?
"langcurrent"
:
"lang"
);
50
51
haveLang = haveLang || isLang;
52
}
53
54
if
(!haveLang) {
55
languageSelects_
[0]->setStyleClass(
"langcurrent"
);
56
WApplication::instance()
57
->setLocale(
languageSelects_
[0]->text().toUTF8());
58
}
else
59
WApplication::instance()->setLocale(lang);
60
}
61
62
void
FormExample::changeLanguage
(
WText
*t)
63
{
64
setLanguage
(t->
text
().
toUTF8
());
65
}
66
67
std::unique_ptr<WApplication>
createApplication
(
const
WEnvironment
& env)
68
{
69
std::unique_ptr<WApplication> app
70
= cpp14::make_unique<WApplication>(env);
71
app->messageResourceBundle().use(WApplication::appRoot() +
"form-example"
);
72
app->setTitle(
"Form example"
);
73
74
app->root()->addWidget(cpp14::make_unique<FormExample>());
75
76
WCssDecorationStyle
langStyle;
77
langStyle.
font
().
setSize
(FontSize::Smaller);
78
langStyle.
setCursor
(Cursor::PointingHand);
79
langStyle.
setForegroundColor
(
WColor
(
"blue"
));
80
langStyle.
setTextDecoration
(TextDecoration::Underline);
81
app->styleSheet().addRule(
".lang"
, langStyle);
82
83
langStyle.
setCursor
(Cursor::Arrow);
84
langStyle.
font
().
setWeight
(FontWeight::Bold);
85
app->styleSheet().addRule(
".langcurrent"
, langStyle);
86
87
return
app;
88
}
89
90
int
main
(
int
argc,
char
**argv)
91
{
92
return
WRun(argc, argv, &
createApplication
);
93
}
94
Wt::WCssDecorationStyle::setForegroundColor
void setForegroundColor(WColor color)
FormExample::FormExample
FormExample()
Instantiate a new form example.
Definition:
FormExample.C:14
Form.h
Wt::WContainerWidget
FormExample::languageSelects_
std::vector< WText * > languageSelects_
Definition:
FormExample.h:37
Wt::WWidget::tr
static WString tr(const char *key)
main
int main(int argc, char **argv)
Definition:
FormExample.C:90
Wt::WFont::setWeight
void setWeight(FontWeight weight, int value=400)
Wt::WCssDecorationStyle::setCursor
void setCursor(Cursor c)
Wt::WContainerWidget::addWidget
virtual void addWidget(std::unique_ptr< WWidget > widget)
Wt::WFont::setSize
void setSize(FontSize size)
Wt::WCssDecorationStyle::setTextDecoration
void setTextDecoration(WFlags< TextDecoration > decoration)
FormExample.h
Wt::WWebWidget::find
virtual WWidget * find(const std::string &name) override
FormExample::changeLanguage
void changeLanguage(WText *t)
Change the language.
Definition:
FormExample.C:62
FormExample::setLanguage
void setLanguage(const std::string lang)
Definition:
FormExample.C:40
Wt::WInteractWidget::clicked
EventSignal< WMouseEvent > & clicked()
Wt::WCssDecorationStyle
Wt::WWebWidget::setStyleClass
virtual void setStyleClass(const WString &styleClass) override
Wt::EventSignal::connect
Wt::Signals::connection connect(F function)
Wt::WEnvironment
Wt::WString::toUTF8
std::string toUTF8() const
Wt::WContainerWidget::setContentAlignment
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
Form
A simple Form.
Definition:
Form.h:35
Wt::WColor
Wt::WCssDecorationStyle::font
WFont & font()
Wt::WText::text
const WString & text() const
Wt::WWebWidget::setMargin
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
Wt::WText
createApplication
std::unique_ptr< WApplication > createApplication(const WEnvironment &env)
Definition:
FormExample.C:67
Generated on Fri Mar 20 2020 for
the C++ Web Toolkit (Wt)
by
1.8.17