Wt examples 3.1.10
|
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/WContainerWidget> 00009 #include <Wt/WEnvironment> 00010 #include <Wt/WPushButton> 00011 #include <Wt/WServer> 00012 #include <Wt/WText> 00013 00014 #include "SimpleChatServer.h" 00015 #include "PopupChatWidget.h" 00016 00017 using namespace Wt; 00018 00023 00026 class ChatApplication : public WApplication 00027 { 00028 public: 00031 ChatApplication(const WEnvironment& env, SimpleChatServer& server); 00032 00033 private: 00034 SimpleChatServer& server_; 00035 00038 void addChatWidget(); 00039 }; 00040 00041 ChatApplication::ChatApplication(const WEnvironment& env, 00042 SimpleChatServer& server) 00043 : WApplication(env), 00044 server_(server) 00045 { 00046 setTitle("Wt Chat"); 00047 useStyleSheet("chatapp.css"); 00048 00049 messageResourceBundle().use(appRoot() + "simplechat"); 00050 00051 root()->addWidget(new WText(WString::tr("introduction"))); 00052 00053 SimpleChatWidget *chatWidget = new SimpleChatWidget(server_, root()); 00054 chatWidget->setStyleClass("chat"); 00055 00056 root()->addWidget(new WText(WString::tr("details"))); 00057 00058 WPushButton *b = new WPushButton("I'm schizophrenic ...", root()); 00059 b->clicked().connect(b, &WPushButton::hide); 00060 b->clicked().connect(this, &ChatApplication::addChatWidget); 00061 } 00062 00063 void ChatApplication::addChatWidget() 00064 { 00065 SimpleChatWidget *chatWidget2 = new SimpleChatWidget(server_, root()); 00066 chatWidget2->setStyleClass("chat"); 00067 } 00068 00071 class ChatWidget : public WApplication 00072 { 00073 public: 00074 ChatWidget(const WEnvironment& env, SimpleChatServer& server); 00075 00076 private: 00077 JSignal<WString> login_; 00078 }; 00079 00080 ChatWidget::ChatWidget(const WEnvironment& env, SimpleChatServer& server) 00081 : WApplication(env), 00082 login_(this, "login") 00083 { 00084 useStyleSheet("chatwidget.css"); 00085 useStyleSheet("chatwidget_ie6.css", "lt IE 7"); 00086 00087 const std::string *div = env.getParameter("div"); 00088 std::string defaultDiv = "div"; 00089 if (!div) 00090 div = &defaultDiv; 00091 00092 if (div) { 00093 setJavaScriptClass(*div); 00094 PopupChatWidget *chatWidget = new PopupChatWidget(server); 00095 bindWidget(chatWidget, *div); 00096 00097 login_.connect(chatWidget, &PopupChatWidget::setName); 00098 00099 std::string chat = javaScriptClass(); 00100 doJavaScript("if (window." + chat + "User) " 00101 + chat + ".emit(" + chat + ", 'login', " + chat + "User);" 00102 + "document.body.appendChild(" + chatWidget->jsRef() + ");"); 00103 } else { 00104 std::cerr << "Missing: parameter: 'div'" << std::endl; 00105 quit(); 00106 } 00107 } 00108 00109 WApplication *createApplication(const WEnvironment& env, 00110 SimpleChatServer& server) 00111 { 00112 return new ChatApplication(env, server); 00113 } 00114 00115 WApplication *createWidget(const WEnvironment& env, SimpleChatServer& server) 00116 { 00117 return new ChatWidget(env, server); 00118 } 00119 00120 int main(int argc, char **argv) 00121 { 00122 Wt::WServer server(argv[0]); 00123 SimpleChatServer chatServer(server); 00124 00125 server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION); 00126 00127 /* 00128 * We add two entry points: one for the full-window application, 00129 * and one for a widget that can be integrated in another page. 00130 */ 00131 server.addEntryPoint(Wt::Application, 00132 boost::bind(createApplication, _1, 00133 boost::ref(chatServer))); 00134 server.addEntryPoint(Wt::WidgetSet, 00135 boost::bind(createWidget, _1, 00136 boost::ref(chatServer)), "/chat.js"); 00137 00138 if (server.start()) { 00139 int sig = Wt::WServer::waitForShutdown(); 00140 std::cerr << "Shutting down: (signal = " << sig << ")" << std::endl; 00141 server.stop(); 00142 } 00143 } 00144