Wt examples  4.10.0
Loading...
Searching...
No Matches
PopupChatWidget.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 Emweb bv, Herent, Belgium.
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include <Wt/WApplication.h>
8#include <Wt/WEnvironment.h>
9#include <Wt/WImage.h>
10#include <Wt/WText.h>
11#include <Wt/WVBoxLayout.h>
12
13#include "PopupChatWidget.h"
14#include "SimpleChatServer.h"
15
16// TODO:
17// - i18n
18
20 const std::string& id)
21 : SimpleChatWidget(server),
22 missedMessages_(0)
23{
24 setId(id);
25
26 if (Wt::WApplication::instance()->environment().agentIsIE()) {
27 if (Wt::WApplication::instance()->environment().agent()
28 == Wt::UserAgent::IE6)
29 setPositionScheme(Wt::PositionScheme::Absolute);
30 else
31 setPositionScheme(Wt::PositionScheme::Fixed);
32 }
33
34 implementJavaScript
36 "{"
37 """let s = " WT_CLASS ".$('" + id + "');"
38 """s.classList.toggle('chat-maximized');"
39 """s.classList.toggle('chat-minimized');"
40 "}");
41
42 online_ = false;
43 minimized_ = true;
44 setStyleClass("chat-widget chat-minimized");
45
46 clear();
47 addWidget(createBar());
49
50 connect();
51}
52
53void PopupChatWidget::setName(const Wt::WString& name)
54{
55 if (name.empty())
56 return;
57
58 if (online_) {
59 int tries = 1;
60 Wt::WString n = name;
61 while (!server().changeName(name_, n))
62 n = name + std::to_string(++tries);
63
64 name_ = n;
65 } else
66 name_ = name;
67}
68
69std::unique_ptr<Wt::WContainerWidget> PopupChatWidget::createBar()
70{
71 auto bar(std::make_unique<Wt::WContainerWidget>());
72 bar->setStyleClass("chat-bar");
73
74 auto toggleButton(std::make_unique<Wt::WText>());
75 toggleButton->setInline(false);
76 toggleButton->setStyleClass("chat-minmax");
77 bar->clicked().connect(this, &PopupChatWidget::toggleSize);
78 bar->clicked().connect(this, &PopupChatWidget::goOnline);
79
80 bar->addWidget(std::move(toggleButton));
81
82 title_ = bar->addWidget(std::make_unique<Wt::WText>());
83
84 bar_ = bar.get();
85
86 return bar;
87}
88
90{
92}
93
95{
96 if (!online_) {
97 online_ = true;
98
99 int tries = 1;
100 Wt::WString name = name_;
101 if (name.empty())
102 name = server().suggestGuest();
103
104 while (!startChat(name)) {
105 if (name_.empty())
106 name = server().suggestGuest();
107 else
108 name = name_ + std::to_string(++tries);
109 }
110
111 name_ = name;
112 }
113
114 missedMessages_ = 0;
115 bar_->removeStyleClass("alert");
116}
117
118void PopupChatWidget::createLayout(std::unique_ptr<Wt::WWidget> messages,
119 std::unique_ptr<Wt::WWidget> userList,
120 std::unique_ptr<Wt::WWidget> messageEdit,
121 std::unique_ptr<Wt::WWidget> sendButton,
122 std::unique_ptr<Wt::WWidget> logoutButton)
123{
124 auto layout(std::make_unique<Wt::WVBoxLayout>());
125 layout->setContentsMargins(0, 0, 0, 0);
126 layout->setSpacing(0);
127
128 auto bar = layout->addWidget(createBar());
129 bar->setMinimumSize(Wt::WLength::Auto, 20);
130 layout->addWidget(std::move(messages), 1);
131 layout->addWidget(std::move(messageEdit));
132
133 setLayout(std::move(layout));
134}
135
137{
139
140 int count = server().users().size();
141
142 if (!loggedIn()) {
143 if (count == 0)
144 title_->setText("Thoughts? Ventilate.");
145 else if (count == 1)
146 title_->setText("Chat: 1 user online");
147 else
148 title_->setText(Wt::WString("Chat: {1} users online").arg(count));
149 } else {
150 title_->setText(Wt::WString("Chat: <span class=\"self\">{1}</span>"
151 " <span class=\"online\">({2} user{3})</span>")
152 .arg(userName()).arg(count).arg(count == 1 ? "" : "s"));
153 }
154}
155
157{
158 if (loggedIn() && minimized()) {
160 if (missedMessages_ == 1) {
161 bar_->addStyleClass("alert");
162 }
163 }
164}
165
167{
168 return minimized_;
169}
virtual void newMessage()
PopupChatWidget(SimpleChatServer &server, const std::string &id)
std::unique_ptr< Wt::WContainerWidget > createBar()
void setName(const Wt::WString &name)
Wt::WString name_
virtual void updateUsers()
Wt::WWidget * bar_
virtual void createLayout(std::unique_ptr< WWidget > messages, std::unique_ptr< WWidget > userList, std::unique_ptr< WWidget > messageEdit, std::unique_ptr< WWidget > sendButton, std::unique_ptr< WWidget > logoutButton)
bool minimized() const
Wt::WText * title_
A simple chat server.
UserSet users()
Get the users currently logged in.
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
A self-contained chat widget.
bool startChat(const Wt::WString &user)
Start a chat for the given user.
virtual void updateUsers()
SimpleChatServer & server()
bool loggedIn() const
void changeName(const Wt::WString &name)
const Wt::WString & userName() const