Wt examples: /home/koen/project/wt/public-git/wt/examples/simplechat/simpleChat.C Source File

Wt examples 3.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

/home/koen/project/wt/public-git/wt/examples/simplechat/simpleChat.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/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   setCssTheme("");
    00085   useStyleSheet("chatwidget.css");
    00086   useStyleSheet("chatwidget_ie6.css", "lt IE 7");
    00087
    00088   const std::string *div = env.getParameter("div");
    00089   std::string defaultDiv = "div";
    00090   if (!div)
    00091    div = &defaultDiv;
    00092
    00093   if (div) {
    00094     setJavaScriptClass(*div);
    00095     PopupChatWidget *chatWidget = new PopupChatWidget(server, *div);
    00096     bindWidget(chatWidget, *div);
    00097
    00098     login_.connect(chatWidget, &PopupChatWidget::setName);
    00099
    00100     std::string chat = javaScriptClass();
    00101     doJavaScript("if (window." + chat + "User) "
    00102                  + chat + ".emit(" + chat + ", 'login', " + chat + "User);"
    00103                  + "document.body.appendChild(" + chatWidget->jsRef() + ");");
    00104   } else {
    00105     std::cerr << "Missing: parameter: 'div'" << std::endl;
    00106     quit();
    00107   }
    00108 }
    00109
    00110 WApplication *createApplication(const WEnvironment& env,
    00111                                 SimpleChatServer& server)
    00112 {
    00113   return new ChatApplication(env, server);
    00114 }
    00115
    00116 WApplication *createWidget(const WEnvironment& env, SimpleChatServer& server)
    00117 {
    00118   return new ChatWidget(env, server);
    00119 }
    00120
    00121 int main(int argc, char **argv)
    00122 {
    00123   Wt::WServer server(argv[0]);
    00124   SimpleChatServer chatServer(server);
    00125
    00126   server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
    00127
    00128   /*
    00129    * We add two entry points: one for the full-window application,
    00130    * and one for a widget that can be integrated in another page.
    00131    */
    00132   server.addEntryPoint(Wt::Application,
    00133                        boost::bind(createApplication, _1,
    00134                                    boost::ref(chatServer)));
    00135   server.addEntryPoint(Wt::WidgetSet,
    00136                        boost::bind(createWidget, _1,
    00137                                    boost::ref(chatServer)), "/chat.js");
    00138
    00139   if (server.start()) {
    00140     int sig = Wt::WServer::waitForShutdown();
    00141     std::cerr << "Shutting down: (signal = " << sig << ")" << std::endl;
    00142     server.stop();
    00143   }
    00144 }
    00145