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

Generated on Wed Jul 27 2011 for the C++ Web Toolkit (Wt) by doxygen 1.7.4