10 #include <Wt/WApplication>
11 #include <Wt/WContainerWidget>
12 #include <Wt/WEnvironment>
13 #include <Wt/WInPlaceEdit>
14 #include <Wt/WHBoxLayout>
15 #include <Wt/WVBoxLayout>
17 #include <Wt/WLineEdit>
18 #include <Wt/WTemplate>
20 #include <Wt/WTextArea>
21 #include <Wt/WPushButton>
22 #include <Wt/WCheckBox>
29 Wt::WContainerWidget *parent)
30 : WContainerWidget(parent),
51 Wt::WApplication::instance()->enableUpdates(
true);
57 Wt::WApplication::instance()->enableUpdates(
false);
66 WVBoxLayout *vLayout =
new WVBoxLayout();
69 WHBoxLayout *hLayout =
new WHBoxLayout();
70 vLayout->addLayout(hLayout, 0, AlignTop | AlignLeft);
72 hLayout->addWidget(
new WLabel(
"User name:"), 0, AlignMiddle);
76 WPushButton *b =
new WPushButton(
"Login");
77 hLayout->addWidget(b, 0, AlignMiddle);
95 statusMsg_->setText(
"Sorry, name '" + escapeText(name) +
96 "' is already taken.");
111 WWidget *messageEdit,
112 WWidget *sendButton, WWidget *logoutButton)
132 WVBoxLayout *vLayout =
new WVBoxLayout();
135 WHBoxLayout *hLayout =
new WHBoxLayout();
138 hLayout->addWidget(messages, 1);
139 messages->setStyleClass(
"chat-msgs");
142 hLayout->addWidget(userList);
143 userList->setStyleClass(
"chat-users");
145 hLayout->setResizable(0,
true);
148 vLayout->addLayout(hLayout, 1);
151 vLayout->addWidget(messageEdit);
152 messageEdit->setStyleClass(
"chat-noedit");
155 hLayout =
new WHBoxLayout();
158 hLayout->addWidget(sendButton);
161 hLayout->addWidget(logoutButton);
164 vLayout->addLayout(hLayout, 0, AlignLeft);
176 if (flags & RenderFull) {
180 doJavaScript(
"setTimeout(function() { "
182 +
messages_->jsRef() +
".scrollHeight;}, 0);");
186 WContainerWidget::render(flags);
211 messages_->setOverflow(WContainerWidget::OverflowAuto);
212 userList_->setOverflow(WContainerWidget::OverflowAuto);
215 WPushButton *logoutButton =
new WPushButton(
"Logout");
232 (
"function(o, e) { setTimeout(function() {"
250 WInPlaceEdit *nameEdit =
new WInPlaceEdit();
251 nameEdit->addStyleClass(
"name-edit");
252 nameEdit->setButtonsEnabled(
false);
253 nameEdit->setText(
user_);
256 WTemplate *joinMsg =
new WTemplate(tr(
"join-msg.template"),
messages_);
257 joinMsg->bindWidget(
"name", nameEdit);
258 joinMsg->setStyleClass(
"chat-msg");
270 if (!logoutButton->parent())
304 for (SimpleChatServer::UserSet::iterator i = users.begin();
305 i != users.end(); ++i) {
306 WCheckBox *w =
new WCheckBox(escapeText(*i),
userList_);
309 UserMap::const_iterator j = oldUsers.find(*i);
310 if (j != oldUsers.end())
311 w->setChecked(j->second);
315 users_[*i] = w->isChecked();
319 w->setStyleClass(
"chat-self");
329 WCheckBox *b =
dynamic_cast<WCheckBox *
>(sender());
330 users_[b->text()] = b->isChecked();
335 WApplication *app = WApplication::instance();
367 app->triggerUpdate();
383 w->setTextFormat(XHTMLText);
387 w->setStyleClass(
"chat-msg");
398 app->doJavaScript(
messages_->jsRef() +
".scrollTop += "
399 +
messages_->jsRef() +
".scrollHeight;");
410 app->triggerUpdate();
void sendMessage(const Wt::WString &user, const Wt::WString &message)
Send a message on behalve of a user.
bool connect(Client *client, const ChatEventCallback &handleEvent)
Connects to the chat server.
Encapsulate a chat event.
const Wt::WString formattedHTML(const Wt::WString &user, Wt::TextFormat format) const
Get the message formatted as HTML, rendered for the given user.
UserSet users()
Get the users currently logged in.
std::set< Wt::WString > UserSet
Typedef for a collection of user names.
bool changeName(const Wt::WString &user, const Wt::WString &newUser)
Changes the name.
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
bool login(const Wt::WString &user)
Try to login with given user name.
bool disconnect(Client *client)
Disconnect from the chat server.
void logout(const Wt::WString &user)
Logout from the server.
const Wt::WString & user() const
Get the user who caused the event.
Type type() const
Get the event type.
const Wt::WString & data() const
Get the extra data for this event.