Wt examples  3.7.1
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>
11 #include <Wt/WText>
12 #include <Wt/WStringUtil>
13 
15  : WContainerWidget(parent)
16 {
17  WContainerWidget *langLayout = new WContainerWidget(this);
18  langLayout->setContentAlignment(AlignRight);
19  new WText(tr("language"), langLayout);
20 
21  const char *lang[] = { "en", "nl" };
22 
23  for (int i = 0; i < 2; ++i) {
24  WText *t = new WText(widen(lang[i]), langLayout);
25  t->setMargin(5);
26  t->clicked().connect(this, &FormExample::changeLanguage);
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 = new Form(this);
37  form->setMargin(20);
38 }
39 
40 void FormExample::setLanguage(const std::string lang)
41 {
42  bool haveLang = false;
43 
44  for (unsigned i = 0; i < languageSelects_.size(); ++i) {
45  WText *t = languageSelects_[i];
46 
47  // prefix match, e.g. en matches en-us.
48  bool isLang = lang.find(narrow(t->text().value())) == 0;
49  t->setStyleClass(isLang ? L"langcurrent" : L"lang");
50 
51  haveLang = haveLang || isLang;
52  }
53 
54  if (!haveLang) {
55  languageSelects_[0]->setStyleClass(L"langcurrent");
56  WApplication::instance()
57  ->setLocale(narrow(languageSelects_[0]->text().value()));
58  } else
59  WApplication::instance()->setLocale(lang);
60 }
61 
63 {
64  WText *t = (WText *)sender();
65  setLanguage(narrow(t->text().value()));
66 }
67 
69 {
70  WApplication *app = new WApplication(env);
71  app->messageResourceBundle().use(WApplication::appRoot() + "form-example");
72  app->setTitle("Form example");
73 
74  app->root()->addWidget(new FormExample());
75 
76  WCssDecorationStyle langStyle;
77  langStyle.font().setSize(WFont::Smaller);
78  langStyle.setCursor(PointingHandCursor);
79  langStyle.setForegroundColor(blue);
80  langStyle.setTextDecoration(WCssDecorationStyle::Underline);
81  app->styleSheet().addRule(".lang", langStyle);
82 
83  langStyle.setCursor(ArrowCursor);
84  langStyle.font().setWeight(WFont::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 
FormExample(WContainerWidget *parent=0)
Instantiate a new form example.
Definition: FormExample.C:14
virtual void setStyleClass(const WString &styleClass) override
EventSignal< WMouseEvent > & clicked()
static WString tr(const char *key)
WMessageResourceBundle & messageResourceBundle()
A simple Form.
Definition: Form.h:35
void setTitle(const WString &title)
std::vector< WText * > languageSelects_
Definition: FormExample.h:37
void setCursor(Cursor c)
void setSize(FontSize size)
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
void setLanguage(const std::string lang)
Definition: FormExample.C:40
void changeLanguage()
Change the language.
Definition: FormExample.C:62
void setWeight(FontWeight weight, int value=400)
virtual void setMargin(const WLength &margin, WFlags< Side > sides=AllSides) override
std::wstring value() const
void use(const std::string &path, bool loadInMemory=true)
WContainerWidget * root() const
WApplication * createApplication(const WEnvironment &env)
Definition: FormExample.C:68
WCssTextRule * addRule(const std::string &selector, const WString &declarations, const std::string &ruleName=std::string())
void setForegroundColor(WColor color)
WCssStyleSheet & styleSheet()
virtual void addWidget(std::unique_ptr< WWidget > widget)
const WString & text() const
int main(int argc, char **argv)
Definition: FormExample.C:90
void setTextDecoration(WFlags< TextDecoration > decoration)

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13