Wt examples
3.2.0
|
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 *b = new WPushButton("Greet me.", root()); // create a button 00051 b->setMargin(5, Left); // add 5 pixels margin 00052 00053 root()->addWidget(new WBreak()); // insert a line break 00054 00055 greeting_ = new WText(root()); // empty text 00056 00057 /* 00058 * Connect signals with slots 00059 * 00060 * - simple Wt-way 00061 */ 00062 b->clicked().connect(this, &HelloApplication::greet); 00063 00064 /* 00065 * - using an arbitrary function object (binding values with boost::bind()) 00066 */ 00067 nameEdit_->enterPressed().connect 00068 (boost::bind(&HelloApplication::greet, this)); 00069 00070 /* 00071 * - using a c++0x lambda: 00072 */ 00073 // b->clicked().connect(std::bind([=]() { 00074 // greeting_->setText("Hello there, " + nameEdit_->text()); 00075 // })); 00076 } 00077 00078 void HelloApplication::greet() 00079 { 00080 /* 00081 * Update the text, using text input into the nameEdit_ field. 00082 */ 00083 greeting_->setText("Hello there, " + nameEdit_->text()); 00084 } 00085 00086 WApplication *createApplication(const WEnvironment& env) 00087 { 00088 /* 00089 * You could read information from the environment to decide whether 00090 * the user has permission to start a new application 00091 */ 00092 return new HelloApplication(env); 00093 } 00094 00095 int main(int argc, char **argv) 00096 { 00097 /* 00098 * Your main method may set up some shared resources, but should then 00099 * start the server application (FastCGI or httpd) that starts listening 00100 * for requests, and handles all of the application life cycles. 00101 * 00102 * The last argument to WRun specifies the function that will instantiate 00103 * new application objects. That function is executed when a new user surfs 00104 * to the Wt application, and after the library has negotiated browser 00105 * support. The function should return a newly instantiated application 00106 * object. 00107 */ 00108 return WRun(argc, argv, &createApplication); 00109 } 00110