Wt examples  3.2.2
/home/koen/project/wt/public-git/wt/examples/hello/hello.C
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <Wt/WApplication>
00008 #include <Wt/WBreak>
00009 #include <Wt/WContainerWidget>
00010 #include <Wt/WLineEdit>
00011 #include <Wt/WPushButton>
00012 #include <Wt/WText>
00013 
00014 // c++0x only, for std::bind
00015 // #include <functional>
00016 
00017 using namespace Wt;
00018 
00019 /*
00020  * A simple hello world application class which demonstrates how to react
00021  * to events, read input, and give feed-back.
00022  */
00023 class HelloApplication : public WApplication
00024 {
00025 public:
00026   HelloApplication(const WEnvironment& env);
00027 
00028 private:
00029   WLineEdit *nameEdit_;
00030   WText *greeting_;
00031 
00032   void greet();
00033 };
00034 
00035 /*
00036  * The env argument contains information about the new session, and
00037  * the initial request. It must be passed to the WApplication
00038  * constructor so it is typically also an argument for your custom
00039  * application constructor.
00040 */
00041 HelloApplication::HelloApplication(const WEnvironment& env)
00042   : WApplication(env)
00043 {
00044   setTitle("Hello world");                               // application title
00045 
00046   root()->addWidget(new WText("Your name, please ? "));  // show some text
00047   nameEdit_ = new WLineEdit(root());                     // allow text input
00048   nameEdit_->setFocus();                                 // give focus
00049 
00050   WPushButton *button
00051     = new WPushButton("Greet me.", root());              // create a button
00052   button->setMargin(5, Left);                            // add 5 pixels margin
00053 
00054   root()->addWidget(new WBreak());                       // insert a line break
00055 
00056   greeting_ = new WText(root());                         // empty text
00057 
00058   /*
00059    * Connect signals with slots
00060    *
00061    * - simple Wt-way
00062    */
00063   button->clicked().connect(this, &HelloApplication::greet);
00064 
00065   /*
00066    * - using an arbitrary function object (binding values with boost::bind())
00067    */
00068   nameEdit_->enterPressed().connect
00069     (boost::bind(&HelloApplication::greet, this));
00070 
00071   /*
00072    * - using a c++0x lambda:
00073    */
00074   // b->clicked().connect(std::bind([=]() { 
00075   //       greeting_->setText("Hello there, " + nameEdit_->text());
00076   // }));
00077 }
00078 
00079 void HelloApplication::greet()
00080 {
00081   /*
00082    * Update the text, using text input into the nameEdit_ field.
00083    */
00084   greeting_->setText("Hello there, " + nameEdit_->text());
00085 }
00086 
00087 WApplication *createApplication(const WEnvironment& env)
00088 {
00089   /*
00090    * You could read information from the environment to decide whether
00091    * the user has permission to start a new application
00092    */
00093   return new HelloApplication(env);
00094 }
00095 
00096 int main(int argc, char **argv)
00097 {
00098   /*
00099    * Your main method may set up some shared resources, but should then
00100    * start the server application (FastCGI or httpd) that starts listening
00101    * for requests, and handles all of the application life cycles.
00102    *
00103    * The last argument to WRun specifies the function that will instantiate
00104    * new application objects. That function is executed when a new user surfs
00105    * to the Wt application, and after the library has negotiated browser
00106    * support. The function should return a newly instantiated application
00107    * object.
00108    */
00109   return WRun(argc, argv, &createApplication);
00110 }
00111 

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