Wt examples  3.3.0
simpleChat.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 
7 #include <Wt/WApplication>
8 #include <Wt/WContainerWidget>
9 #include <Wt/WEnvironment>
10 #include <Wt/WPushButton>
11 #include <Wt/WServer>
12 #include <Wt/WText>
13 
14 #include "SimpleChatServer.h"
15 #include "PopupChatWidget.h"
16 
17 using namespace Wt;
18 
23 
27 {
28 public:
31  ChatApplication(const WEnvironment& env, SimpleChatServer& server);
32 
33 private:
35 
38  void addChatWidget();
39 };
40 
42  SimpleChatServer& server)
43  : WApplication(env),
44  server_(server)
45 {
46  setTitle("Wt Chat");
47  useStyleSheet("chatapp.css");
48 
49  messageResourceBundle().use(appRoot() + "simplechat");
50 
51  root()->addWidget(new WText(WString::tr("introduction")));
52 
53  SimpleChatWidget *chatWidget = new SimpleChatWidget(server_, root());
54  chatWidget->setStyleClass("chat");
55 
56  root()->addWidget(new WText(WString::tr("details")));
57 
58  WPushButton *b = new WPushButton("I'm schizophrenic ...", root());
59  b->clicked().connect(b, &WPushButton::hide);
60  b->clicked().connect(this, &ChatApplication::addChatWidget);
61 }
62 
64 {
65  SimpleChatWidget *chatWidget2 = new SimpleChatWidget(server_, root());
66  chatWidget2->setStyleClass("chat");
67 }
68 
71 class ChatWidget : public WApplication
72 {
73 public:
74  ChatWidget(const WEnvironment& env, SimpleChatServer& server);
75 
76 private:
78 };
79 
81  : WApplication(env),
82  login_(this, "login")
83 {
84  setCssTheme("");
85  useStyleSheet("chatwidget.css");
86  useStyleSheet("chatwidget_ie6.css", "lt IE 7");
87 
88  const std::string *div = env.getParameter("div");
89  std::string defaultDiv = "div";
90  if (!div)
91  div = &defaultDiv;
92 
93  if (div) {
94  setJavaScriptClass(*div);
95  PopupChatWidget *chatWidget = new PopupChatWidget(server, *div);
96  bindWidget(chatWidget, *div);
97 
99 
100  std::string chat = javaScriptClass();
101  doJavaScript("if (window." + chat + "User) "
102  + chat + ".emit(" + chat + ", 'login', " + chat + "User);"
103  + "document.body.appendChild(" + chatWidget->jsRef() + ");");
104  } else {
105  std::cerr << "Missing: parameter: 'div'" << std::endl;
106  quit();
107  }
108 }
109 
111  SimpleChatServer& server)
112 {
113  return new ChatApplication(env, server);
114 }
115 
117 {
118  return new ChatWidget(env, server);
119 }
120 
121 int main(int argc, char **argv)
122 {
123  Wt::WServer server(argv[0]);
124  SimpleChatServer chatServer(server);
125 
126  server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
127 
128  /*
129  * We add two entry points: one for the full-window application,
130  * and one for a widget that can be integrated in another page.
131  */
133  boost::bind(createApplication, _1,
134  boost::ref(chatServer)));
136  boost::bind(createWidget, _1,
137  boost::ref(chatServer)), "/chat.js");
138 
139  if (server.start()) {
140  int sig = Wt::WServer::waitForShutdown();
141  std::cerr << "Shutting down: (signal = " << sig << ")" << std::endl;
142  server.stop();
143  }
144 }
145 

Generated on Fri May 31 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.3.1