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