Go to the documentation of this file.00001
00002
00003
00004
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 SimpleChatServer theServer;
00027
00030 class ChatApplication : public WApplication
00031 {
00032 public:
00035 ChatApplication(const WEnvironment& env);
00036
00037 private:
00040 void addChatWidget();
00041 };
00042
00043 ChatApplication::ChatApplication(const WEnvironment& env)
00044 : WApplication(env)
00045 {
00046 setTitle("Wt Chat");
00047 useStyleSheet("chatapp.css");
00048 messageResourceBundle().use(Wt::WApplication::appRoot() + "simplechat");
00049
00050 root()->addWidget(new WText(WString::tr("introduction")));
00051
00052 SimpleChatWidget *chatWidget = new SimpleChatWidget(theServer, root());
00053 chatWidget->setStyleClass("chat");
00054
00055 root()->addWidget(new WText(WString::tr("details")));
00056
00057 WPushButton *b = new WPushButton("I'm schizophrenic ...", root());
00058 b->clicked().connect(b, &WPushButton::hide);
00059 b->clicked().connect(this, &ChatApplication::addChatWidget);
00060 }
00061
00062 void ChatApplication::addChatWidget()
00063 {
00064 SimpleChatWidget *chatWidget2 = new SimpleChatWidget(theServer, root());
00065 chatWidget2->setStyleClass("chat");
00066 }
00067
00070 class ChatWidget : public WApplication
00071 {
00072 public:
00073 ChatWidget(const WEnvironment& env);
00074
00075 private:
00076 JSignal<WString> login_;
00077 };
00078
00079 ChatWidget::ChatWidget(const WEnvironment& env)
00080 : WApplication(env),
00081 login_(this, "login")
00082 {
00083 useStyleSheet("chatwidget.css");
00084 useStyleSheet("chatwidget_ie6.css", "lt IE 7");
00085
00086 const std::string *div = env.getParameter("div");
00087
00088 if (div) {
00089 setJavaScriptClass(*div);
00090 PopupChatWidget *chatWidget = new PopupChatWidget(theServer);
00091 bindWidget(chatWidget, *div);
00092
00093 login_.connect(chatWidget, &PopupChatWidget::setName);
00094
00095 std::string chat = javaScriptClass();
00096 doJavaScript("if (window." + chat + "User) "
00097 + chat + ".emit(" + chat + ", 'login', " + chat + "User);"
00098 + "document.body.appendChild(" + chatWidget->jsRef() + ");");
00099 } else {
00100 std::cerr << "Missing: parameter: 'div'" << std::endl;
00101 quit();
00102 }
00103 }
00104
00105 WApplication *createApplication(const WEnvironment& env)
00106 {
00107 return new ChatApplication(env);
00108 }
00109
00110 WApplication *createWidget(const WEnvironment& env)
00111 {
00112 return new ChatWidget(env);
00113 }
00114
00115 int main(int argc, char **argv)
00116 {
00117 Wt::WServer server(argv[0]);
00118
00119 server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
00120 server.addEntryPoint(Wt::Application, createApplication);
00121 server.addEntryPoint(Wt::WidgetSet, createWidget, "/chat.js");
00122
00123 if (server.start()) {
00124 Wt::WServer::waitForShutdown();
00125 server.stop();
00126 }
00127 }
00128