Wt examples
3.3.0
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
build
buildd
witty-3.3.0
examples
simplechat
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
26
class
ChatApplication
:
public
WApplication
27
{
28
public
:
31
ChatApplication
(
const
WEnvironment
& env,
SimpleChatServer
& server);
32
33
private
:
34
SimpleChatServer
&
server_
;
35
38
void
addChatWidget();
39
};
40
41
ChatApplication::ChatApplication
(
const
WEnvironment
& env,
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
63
void
ChatApplication::addChatWidget
()
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
:
77
JSignal<WString>
login_
;
78
};
79
80
ChatWidget::ChatWidget
(
const
WEnvironment
& env,
SimpleChatServer
& server)
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
98
login_
.
connect
(chatWidget, &
PopupChatWidget::setName
);
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
110
WApplication
*
createApplication
(
const
WEnvironment
& env,
111
SimpleChatServer
& server)
112
{
113
return
new
ChatApplication
(env, server);
114
}
115
116
WApplication
*
createWidget
(
const
WEnvironment
& env,
SimpleChatServer
& server)
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
*/
132
server.
addEntryPoint
(
Wt::Application
,
133
boost::bind(
createApplication
, _1,
134
boost::ref(chatServer)));
135
server.
addEntryPoint
(
Wt::WidgetSet
,
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
1.8.3.1