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 
SimpleChatServer & server_
Definition: simpleChat.C:34
const std::string * getParameter(const std::string &name) const
WTCONNECTOR_API void stop()
ChatApplication(const WEnvironment &env, SimpleChatServer &server)
Create a new instance.
Definition: simpleChat.C:41
EventSignal< WMouseEvent > & clicked()
WMessageResourceBundle & messageResourceBundle()
A self-contained chat widget.
void setTitle(const WString &title)
std::string javaScriptClass()
static WT_API int waitForShutdown(const char *restartWatchFile=0)
void bindWidget(WWidget *widget, const std::string &domId)
A chat demo application.
Definition: simpleChat.C:26
Application
void setJavaScriptClass(const std::string &className)
WidgetSet
A popup chat widget.
virtual void setStyleClass(const WString &styleClass)
void addChatWidget()
Add another chat client.
Definition: simpleChat.C:63
void use(const std::string &path, bool loadInMemory=true)
WApplication * createApplication(const WEnvironment &env)
WTCONNECTOR_API void setServerConfiguration(int argc, char *argv[], const std::string &serverConfigurationFile=std::string())
WT_API void addEntryPoint(EntryPointType type, ApplicationCreator callback, const std::string &path=std::string(), const std::string &favicon=std::string())
boost::signals::connection connect(const F &function)
WApplication * createWidget(const WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:116
A chat application widget.
Definition: simpleChat.C:71
static std::string appRoot()
void useStyleSheet(const WLink &link, const std::string &media="all")
virtual void addWidget(WWidget *widget)
WContainerWidget * root() const
WTCONNECTOR_API bool start()
A simple chat server.
JSignal< WString > login_
Definition: simpleChat.C:77
void doJavaScript(const std::string &javascript, bool afterLoaded=true)
void setCssTheme(const std::string &name)
int main(int argc, char **argv)
ChatWidget(const WEnvironment &env, SimpleChatServer &server)
Definition: simpleChat.C:80
void setName(const Wt::WString &name)
std::string jsRef() const

Generated on Tue Dec 3 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.5