Wt examples  3.2.1
/home/koen/project/wt/public-git/wt/examples/wt-homepage/WtHome.C
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include "WtHome.h"
00008 
00009 #ifdef WT_EMWEB_BUILD
00010 #include "QuoteForm.h"
00011 #endif // WT_EMWEB_BUILD
00012 
00013 #include <Wt/WAnchor>
00014 #include <Wt/WEnvironment>
00015 #include <Wt/WLogger>
00016 #include <Wt/WMenuItem>
00017 #include <Wt/WStackedWidget>
00018 #include <Wt/WTable>
00019 #include <Wt/WTabWidget>
00020 #include <Wt/WText>
00021 #include <Wt/WTreeNode>
00022 #include <Wt/WViewWidget>
00023 #include <Wt/WWidget>
00024 
00025 #include "ExampleSourceViewer.h"
00026 
00027 WtHome::WtHome(const WEnvironment& env)
00028   : Home(env, "Wt, C++ Web Toolkit", "wt-home", "css/wt")
00029 {
00030   addLanguage(Lang("en", "/", "en", "English"));
00031   addLanguage(Lang("cn", "/cn/", "汉语", "中文 (Chinese)"));
00032   addLanguage(Lang("ru", "/ru/", "ру", "Русский (Russian)"));
00033 
00034   char* wtExamplePath  = getenv("WT_EXAMPLE_PATH");
00035   if (wtExamplePath)
00036     wtExamplePath_ = wtExamplePath;
00037   else
00038     wtExamplePath_ = "../";
00039 
00040   init();
00041 }
00042 
00043 WWidget *WtHome::example(const char *textKey, const std::string& sourceDir)
00044 {
00045   WContainerWidget *result = new WContainerWidget();
00046   WText *w = new WText(tr(textKey), result);
00047   w->setInternalPathEncoding(true);
00048   result->addWidget(linkSourceBrowser(sourceDir));
00049   return result;
00050 }
00051 
00052 WWidget *WtHome::helloWorldExample()
00053 {
00054   return example("home.examples.hello", "hello");
00055 }
00056 
00057 WWidget *WtHome::chartExample()
00058 {
00059   return example("home.examples.chart", "charts");
00060 }
00061 
00062 WWidget *WtHome::homepageExample()
00063 {
00064   return example("home.examples.wt", "wt-homepage");
00065 }
00066 
00067 WWidget *WtHome::treeviewExample()
00068 {
00069   return example("home.examples.treeview", "treeview-dragdrop");
00070 }
00071 
00072 WWidget *WtHome::gitExample()
00073 {
00074   return example("home.examples.git", "gitmodel");
00075 }
00076 
00077 WWidget *WtHome::chatExample()
00078 {
00079   return example("home.examples.chat", "simplechat");
00080 }
00081 
00082 WWidget *WtHome::composerExample()
00083 {
00084   return example("home.examples.composer", "composer");
00085 }
00086 
00087 WWidget *WtHome::widgetGalleryExample()
00088 {
00089   return example("home.examples.widgetgallery", "widgetgallery");
00090 }
00091 
00092 WWidget *WtHome::hangmanExample()
00093 {
00094   return example("home.examples.hangman", "hangman");
00095 }
00096 
00097 WWidget *WtHome::examples()
00098 {
00099   WContainerWidget *result = new WContainerWidget();
00100 
00101   WText *intro = new WText(tr("home.examples"));
00102   intro->setInternalPathEncoding(true);
00103   result->addWidget(intro);
00104 
00105   examplesMenu_ = new WTabWidget(AlignTop | AlignJustify, result);
00106 
00107   WAnimation animation(WAnimation::SlideInFromRight, WAnimation::EaseIn);
00108   examplesMenu_->contentsStack()->setTransitionAnimation(animation, true);
00109 
00110   /*
00111    * The following code is functionally equivalent to:
00112    *
00113    *   examplesMenu_->addTab(helloWorldExample(), "Hello world");
00114    *
00115    * However, we optimize here for memory consumption (it is a homepage
00116    * after all, and we hope to be slashdotted some day)
00117    *
00118    * Therefore, we wrap all the static content (including the tree
00119    * widgets), into WViewWidgets with static models. In this way the
00120    * widgets are not actually stored in memory on the server.
00121    */
00122 
00123   // The call ->setPathComponent() is to use "/examples/" instead of
00124   // "/examples/hello_world" as internal path
00125   examplesMenu_->addTab(wrapView(&WtHome::helloWorldExample),
00126                         tr("hello-world"))->setPathComponent("");
00127   examplesMenu_->addTab(wrapView(&WtHome::chartExample),
00128                         tr("charts"));
00129   examplesMenu_->addTab(wrapView(&WtHome::homepageExample),
00130                         tr("wt-homepage"));
00131   examplesMenu_->addTab(wrapView(&WtHome::treeviewExample),
00132                         tr("treeview"));
00133   examplesMenu_->addTab(wrapView(&WtHome::gitExample),
00134                         tr("git"));
00135   examplesMenu_->addTab(wrapView(&WtHome::chatExample),
00136                         tr("chat"));
00137   examplesMenu_->addTab(wrapView(&WtHome::composerExample),
00138                         tr("mail-composer"));
00139   examplesMenu_->addTab(wrapView(&WtHome::hangmanExample),
00140                         tr("hangman"));
00141   examplesMenu_->addTab(wrapView(&WtHome::widgetGalleryExample),
00142                         tr("widget-gallery"));
00143 
00144   // Enable internal paths for the example menu
00145   examplesMenu_->setInternalPathEnabled("/examples");
00146   examplesMenu_->currentChanged().connect(this, &Home::googleAnalyticsLogger);
00147 
00148   return result;
00149 }
00150 
00151 WWidget *WtHome::createQuoteForm()
00152 {
00153 #ifdef WT_EMWEB_BUILD
00154   return new QuoteForm(QuoteForm::Wt);
00155 #else
00156   return 0;
00157 #endif
00158 }
00159 
00160 WWidget *WtHome::sourceViewer(const std::string& deployPath)
00161 {
00162   return new ExampleSourceViewer(deployPath, wtExamplePath_ + "/", "CPP");
00163 }
00164 
00165 WWidget *WtHome::wrapView(WWidget *(WtHome::*createWidget)())
00166 {
00167   return makeStaticModel(boost::bind(createWidget, this));
00168 }
00169 
00170 WApplication *createWtHomeApplication(const WEnvironment& env)
00171 {
00172   // support for old (< Wt-2.2) homepage URLS: redirect from "states"
00173   // to "internal paths"
00174   // this contains the initial "history state" in old Wt versions
00175   const std::string *historyKey = env.getParameter("historyKey");
00176 
00177   if (historyKey) {
00178     const char *mainStr[]
00179       = { "main:0", "/",
00180           "main:1", "/news",
00181           "main:2", "/features",
00182           "main:4", "/examples",
00183           "main:3", "/documentation",
00184           "main:5", "/download",
00185           "main:6", "/community" };
00186 
00187     const char *exampleStr[]
00188       = { "example:0", "/examples",
00189           "example:1", "/examples/charts",
00190           "example:2", "/examples/wt-homepage",
00191           "example:3", "/examples/treelist",
00192           "example:4", "/examples/hangman",
00193           "example:5", "/examples/chat",
00194           "example:6", "/examples/mail-composer",
00195           "example:7", "/examples/drag-and-drop",
00196           "example:8", "/examples/file-explorer",
00197           "example:9", "/examples/calendar" };
00198 
00199     if (historyKey->find("main:4") != std::string::npos) {
00200       for (unsigned i = 0; i < 10; ++i)
00201         if (historyKey->find(exampleStr[i*2]) != std::string::npos) {
00202           WApplication *app = new WApplication(env);
00203           app->log("notice") << "redirecting old style URL '"
00204                              << *historyKey << "' to internal path: '"
00205                              << exampleStr[i*2+1] << "'";
00206           app->redirect(app->bookmarkUrl(exampleStr[i*2+1]));
00207           app->quit();
00208           return app;
00209         }
00210     } else
00211       for (unsigned i = 0; i < 6; ++i)
00212         if (historyKey->find(mainStr[i*2]) != std::string::npos) {
00213           WApplication *app = new WApplication(env);
00214 
00215           app->log("notice") << "redirecting old style URL '"
00216                              << *historyKey << "' to internal path: '"
00217                              << mainStr[i*2+1] << "'";
00218           app->redirect(app->bookmarkUrl(mainStr[i*2+1]));
00219           app->quit();
00220           return app;
00221         }
00222 
00223     // unknown history key, just continue
00224   }
00225 
00226   return new WtHome(env);
00227 }

Generated on Fri Mar 30 2012 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1