Wt examples  4.10.4
WtHome.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 "WtHome.h"
8 
9 #ifdef WT_EMWEB_BUILD
10 #include "QuoteForm.h"
11 #endif // WT_EMWEB_BUILD
12 
13 #include <Wt/WAnchor.h>
14 #include <Wt/WEnvironment.h>
15 #include <Wt/WLogger.h>
16 #include <Wt/WMenuItem.h>
17 #include <Wt/WStackedWidget.h>
18 #include <Wt/WTable.h>
19 #include <Wt/WTabWidget.h>
20 #include <Wt/WText.h>
21 #include <Wt/WTreeNode.h>
22 #include <Wt/WViewWidget.h>
23 #include <Wt/WWidget.h>
24 
25 #include "ExampleSourceViewer.h"
26 
27 using namespace Wt;
28 
30  : Home(env, blogDb, "Wt, C++ Web Toolkit", "wt-home", "css/wt")
31 {
32  addLanguage(Lang("en", "/", "en", "English"));
33  addLanguage(Lang("cn", "/cn/", "汉语", "中文 (Chinese)"));
34  addLanguage(Lang("ru", "/ru/", "ру", "Русский (Russian)"));
35 
36  char* wtExamplePath = getenv("WT_EXAMPLE_PATH");
37  if (wtExamplePath)
38  wtExamplePath_ = wtExamplePath;
39  else
40  wtExamplePath_ = "..";
41 
42  init();
43 }
44 
45 std::unique_ptr<WWidget> WtHome::example(const char *textKey, const std::string& sourceDir)
46 {
47  auto result = std::make_unique<WContainerWidget>();
48  WText *w = result->addWidget(std::make_unique<WText>(tr(textKey)));
49  w->setInternalPathEncoding(true);
50  result->addWidget(linkSourceBrowser(sourceDir));
51  return std::move(result);
52 }
53 
54 std::unique_ptr<WWidget> WtHome::helloWorldExample()
55 {
56  return example("home.examples.hello", "hello");
57 }
58 
59 std::unique_ptr<WWidget> WtHome::chartExample()
60 {
61  return example("home.examples.chart", "charts");
62 }
63 
64 std::unique_ptr<WWidget> WtHome::homepageExample()
65 {
66  return example("home.examples.wt", "wt-homepage");
67 }
68 
69 std::unique_ptr<WWidget> WtHome::treeviewExample()
70 {
71  return example("home.examples.treeview", "treeview-dragdrop");
72 }
73 
74 std::unique_ptr<WWidget> WtHome::gitExample()
75 {
76  return example("home.examples.git", "gitmodel");
77 }
78 
79 std::unique_ptr<WWidget> WtHome::chatExample()
80 {
81  return example("home.examples.chat", "simplechat");
82 }
83 
84 std::unique_ptr<WWidget> WtHome::composerExample()
85 {
86  return example("home.examples.composer", "composer");
87 }
88 
89 std::unique_ptr<WWidget> WtHome::widgetGalleryExample()
90 {
91  return example("home.examples.widgetgallery", "widgetgallery");
92 }
93 
94 std::unique_ptr<WWidget> WtHome::hangmanExample()
95 {
96  return example("home.examples.hangman", "hangman");
97 }
98 
99 std::unique_ptr<WWidget> WtHome::examples()
100 {
101  auto result = std::make_unique<WContainerWidget>();
102 
103  auto intro = std::make_unique<WText>(tr("home.examples"));
104  intro->setInternalPathEncoding(true);
105  result->addWidget(std::move(intro));
106 
107  examplesMenu_ = result->addWidget(std::make_unique<WTabWidget>());
108 
109  WAnimation animation(AnimationEffect::SlideInFromRight, TimingFunction::EaseIn);
111 
112  /*
113  * The following code is functionally equivalent to:
114  *
115  * examplesMenu_->addTab(helloWorldExample(), "Hello world");
116  *
117  * However, we optimize here for memory consumption (it is a homepage
118  * after all, and we hope to be slashdotted some day)
119  *
120  * Therefore, we wrap all the static content (including the tree
121  * widgets), into WViewWidgets with static models. In this way the
122  * widgets are not actually stored in memory on the server.
123  */
124 
125  // The call ->setPathComponent() is to use "/examples/" instead of
126  // "/examples/hello_world" as internal path
128  tr("hello-world"))->setPathComponent("");
130  tr("widget-gallery"));
132  tr("charts"));
134  tr("wt-homepage"));
136  tr("treeview"));
138  tr("git"));
140  tr("chat"));
142  tr("mail-composer"));
144  tr("hangman"));
145 
146  // Enable internal paths for the example menu
149 
150  return std::move(result);
151 }
152 
153 std::unique_ptr<WWidget> WtHome::createQuoteForm()
154 {
155 #ifdef WT_EMWEB_BUILD
156  return std::make_unique<QuoteForm>(QuoteForm::Wt);
157 #else
158  return nullptr;
159 #endif
160 }
161 
162 std::unique_ptr<WWidget> WtHome::sourceViewer(const std::string& deployPath)
163 {
164  return std::make_unique<ExampleSourceViewer>(deployPath, wtExamplePath_ + "/", "CPP");
165 }
166 
167 std::unique_ptr<WWidget> WtHome::wrapView(std::unique_ptr<WWidget> (WtHome::*createWidget)())
168 {
169  return makeStaticModel(std::bind(createWidget, this));
170 }
171 
172 std::unique_ptr<WApplication> createWtHomeApplication(const WEnvironment& env,
173  Dbo::SqlConnectionPool *blogDb)
174 {
175  return std::make_unique<WtHome>(env, *blogDb);
176 }
std::unique_ptr< WApplication > createWtHomeApplication(const WEnvironment &env, Dbo::SqlConnectionPool *blogDb)
Definition: WtHome.C:172
Definition: Home.h:68
std::unique_ptr< WWidget > linkSourceBrowser(const std::string &examplePath)
Definition: Home.C:202
void addLanguage(const Lang &l)
Definition: Home.h:86
WTabWidget * examplesMenu_
Definition: Home.h:89
void googleAnalyticsLogger()
Definition: Home.C:430
void init()
Definition: Home.C:63
WString tr(const char *key)
Definition: Home.C:425
Definition: WtHome.h:17
std::unique_ptr< WWidget > wrapView(std::unique_ptr< WWidget >(WtHome::*createFunction)())
Definition: WtHome.C:167
virtual std::unique_ptr< WWidget > createQuoteForm() override
Definition: WtHome.C:153
std::unique_ptr< WWidget > composerExample()
Definition: WtHome.C:84
std::unique_ptr< WWidget > widgetGalleryExample()
Definition: WtHome.C:89
std::unique_ptr< WWidget > treeviewExample()
Definition: WtHome.C:69
std::unique_ptr< WWidget > chartExample()
Definition: WtHome.C:59
std::unique_ptr< WWidget > chatExample()
Definition: WtHome.C:79
virtual std::unique_ptr< WWidget > examples() override
Definition: WtHome.C:99
std::unique_ptr< WWidget > example(const char *textKey, const std::string &sourceDir)
Definition: WtHome.C:45
std::unique_ptr< WWidget > homepageExample()
Definition: WtHome.C:64
std::unique_ptr< WWidget > gitExample()
Definition: WtHome.C:74
WtHome(const WEnvironment &env, Dbo::SqlConnectionPool &blogDb)
Definition: WtHome.C:29
std::unique_ptr< WWidget > hangmanExample()
Definition: WtHome.C:94
std::unique_ptr< WWidget > helloWorldExample()
Definition: WtHome.C:54
std::string wtExamplePath_
Definition: WtHome.h:28
virtual std::unique_ptr< WWidget > sourceViewer(const std::string &internalPath) override
Definition: WtHome.C:162
virtual Wt::Signals::connection connect(WObject *target, WObject::Method method) override
virtual void setPathComponent(const std::string &path)
void setTransitionAnimation(const WAnimation &animation, bool autoReverse=false)
Signal< int > & currentChanged()
void setInternalPathEnabled(const std::string &basePath="")
WMenuItem * addTab(std::unique_ptr< WWidget > child, const WString &label, ContentLoading loadPolicy=ContentLoading::Lazy)
WStackedWidget * contentsStack() const
void setInternalPathEncoding(bool enabled)
std::unique_ptr< Wt::WApplication > createWidget(const Wt::WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:148
Definition: Home.h:23