Wt examples
4.3.0
builddir
build
BUILD
wt-4.3.0
examples
simplechat
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
19
PopupChatWidget::PopupChatWidget
(
SimpleChatServer
& server,
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
35
(&
PopupChatWidget::toggleSize
,
36
"{"
37
""
"var s = $('#"
+
id
+
"');"
38
""
"s.toggleClass('chat-maximized chat-minimized');"
39
"}"
);
40
41
online_
=
false
;
42
minimized_
=
true
;
43
setStyleClass
(
"chat-widget chat-minimized"
);
44
45
clear
();
46
addWidget
(
createBar
());
47
updateUsers
();
48
49
connect
();
50
}
51
52
void
PopupChatWidget::setName
(
const
Wt::WString
& name)
53
{
54
if
(name.
empty
())
55
return
;
56
57
if
(
online_
) {
58
int
tries = 1;
59
Wt::WString
n = name;
60
while
(!
server
().
changeName
(
name_
, n))
61
n = name + std::to_string(++tries);
62
63
name_
= n;
64
}
else
65
name_
= name;
66
}
67
68
std::unique_ptr<Wt::WContainerWidget>
PopupChatWidget::createBar
()
69
{
70
auto
bar(Wt::cpp14::make_unique<Wt::WContainerWidget>());
71
bar->setStyleClass(
"chat-bar"
);
72
73
auto
toggleButton(Wt::cpp14::make_unique<Wt::WText>());
74
toggleButton->setInline(
false
);
75
toggleButton->setStyleClass(
"chat-minmax"
);
76
bar->clicked().connect(
this
, &
PopupChatWidget::toggleSize
);
77
bar->clicked().connect(
this
, &
PopupChatWidget::goOnline
);
78
79
bar->addWidget(std::move(toggleButton));
80
81
title_
= bar->addWidget(Wt::cpp14::make_unique<Wt::WText>());
82
83
bar_
= bar.get();
84
85
return
bar;
86
}
87
88
void
PopupChatWidget::toggleSize
()
89
{
90
minimized_
= !
minimized_
;
91
}
92
93
void
PopupChatWidget::goOnline
()
94
{
95
if
(!
online_
) {
96
online_
=
true
;
97
98
int
tries = 1;
99
Wt::WString
name =
name_
;
100
if
(name.
empty
())
101
name =
server
().
suggestGuest
();
102
103
while
(!
startChat
(name)) {
104
if
(
name_
.
empty
())
105
name =
server
().
suggestGuest
();
106
else
107
name =
name_
+ std::to_string(++tries);
108
}
109
110
name_
= name;
111
}
112
113
missedMessages_
= 0;
114
bar_
->
removeStyleClass
(
"alert"
);
115
}
116
117
void
PopupChatWidget::createLayout
(std::unique_ptr<Wt::WWidget> messages,
118
std::unique_ptr<Wt::WWidget> userList,
119
std::unique_ptr<Wt::WWidget> messageEdit,
120
std::unique_ptr<Wt::WWidget> sendButton,
121
std::unique_ptr<Wt::WWidget> logoutButton)
122
{
123
auto
layout
(Wt::cpp14::make_unique<Wt::WVBoxLayout>());
124
layout
->
setContentsMargins
(0, 0, 0, 0);
125
layout
->setSpacing(0);
126
127
auto
bar =
layout
->
addWidget
(
createBar
());
128
bar->setMinimumSize(
Wt::WLength::Auto
, 20);
129
layout
->
addWidget
(std::move(messages), 1);
130
layout
->
addWidget
(std::move(messageEdit));
131
132
setLayout
(std::move(
layout
));
133
}
134
135
void
PopupChatWidget::updateUsers
()
136
{
137
SimpleChatWidget::updateUsers
();
138
139
int
count
=
server
().
users
().size();
140
141
if
(!
loggedIn
()) {
142
if
(
count
== 0)
143
title_
->
setText
(
"Thoughts? Ventilate."
);
144
else
if
(
count
== 1)
145
title_
->
setText
(
"Chat: 1 user online"
);
146
else
147
title_
->
setText
(
Wt::WString
(
"Chat: {1} users online"
).arg(
count
));
148
}
else
{
149
title_
->
setText
(
Wt::WString
(
"Chat: <span class=\"self\">{1}</span>"
150
" <span class=\"online\">({2} user{3})</span>"
)
151
.arg(
userName
()).arg(
count
).arg(
count
== 1 ?
""
:
"s"
));
152
}
153
}
154
155
void
PopupChatWidget::newMessage
()
156
{
157
if
(
loggedIn
() &&
minimized
()) {
158
++
missedMessages_
;
159
if
(
missedMessages_
== 1) {
160
bar_
->
addStyleClass
(
"alert"
);
161
}
162
}
163
}
164
165
bool
PopupChatWidget::minimized
()
const
166
{
167
return
minimized_
;
168
}
PopupChatWidget::PopupChatWidget
PopupChatWidget(SimpleChatServer &server, const std::string &id)
Definition:
PopupChatWidget.C:19
Wt::WContainerWidget::clear
virtual void clear()
PopupChatWidget::createBar
std::unique_ptr< Wt::WContainerWidget > createBar()
Definition:
PopupChatWidget.C:68
Wt::WApplication::instance
static WApplication * instance()
Wt::WContainerWidget::count
virtual int count() const
SimpleChatWidget::loggedIn
bool loggedIn() const
Definition:
SimpleChatWidget.C:171
SimpleChatWidget::connect
void connect()
Definition:
SimpleChatWidget.C:43
Wt::PositionScheme::Absolute
@ Absolute
PopupChatWidget::newMessage
virtual void newMessage()
Definition:
PopupChatWidget.C:155
PopupChatWidget::minimized
bool minimized() const
Definition:
PopupChatWidget.C:165
PopupChatWidget::toggleSize
void toggleSize()
Definition:
PopupChatWidget.C:88
SimpleChatWidget::startChat
bool startChat(const Wt::WString &user)
Start a chat for the given user.
Definition:
SimpleChatWidget.C:191
Wt::WWidget::addStyleClass
virtual void addStyleClass(const WString &styleClass, bool force=false)=0
PopupChatWidget::missedMessages_
int missedMessages_
Definition:
PopupChatWidget.h:40
Wt::WLength::Auto
static WLength Auto
Wt::PositionScheme::Fixed
@ Fixed
PopupChatWidget::minimized_
bool minimized_
Definition:
PopupChatWidget.h:39
PopupChatWidget::setName
void setName(const Wt::WString &name)
Definition:
PopupChatWidget.C:52
PopupChatWidget::title_
Wt::WText * title_
Definition:
PopupChatWidget.h:37
Wt::WContainerWidget::layout
WLayout * layout() const
SimpleChatServer.h
SimpleChatWidget::server
SimpleChatServer & server()
Definition:
SimpleChatWidget.h:54
Wt::WContainerWidget::addWidget
virtual void addWidget(std::unique_ptr< WWidget > widget)
Wt::WLayout::setContentsMargins
void setContentsMargins(int left, int top, int right, int bottom)
Wt::WString
Wt::WText::setText
bool setText(const WString &text)
Wt::WContainerWidget::setLayout
void setLayout(std::unique_ptr< WLayout > layout)
Wt::WString::empty
bool empty() const
PopupChatWidget::createLayout
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)
Definition:
PopupChatWidget.C:117
Wt::WWebWidget::setId
virtual void setId(const std::string &id) override
PopupChatWidget::bar_
Wt::WWidget * bar_
Definition:
PopupChatWidget.h:38
SimpleChatServer
A simple chat server.
Definition:
SimpleChatServer.h:84
Wt::WWebWidget::setPositionScheme
virtual void setPositionScheme(PositionScheme scheme) override
Wt::WWidget::removeStyleClass
virtual void removeStyleClass(const WString &styleClass, bool force=false)=0
PopupChatWidget.h
Wt::UserAgent::IE6
@ IE6
SimpleChatWidget::changeName
void changeName(const Wt::WString &name)
Definition:
SimpleChatWidget.C:306
Wt::WWebWidget::setStyleClass
virtual void setStyleClass(const WString &styleClass) override
PopupChatWidget::goOnline
void goOnline()
Definition:
PopupChatWidget.C:93
SimpleChatWidget
A self-contained chat widget.
Definition:
SimpleChatWidget.h:26
PopupChatWidget::online_
bool online_
Definition:
PopupChatWidget.h:39
SimpleChatServer::users
UserSet users()
Get the users currently logged in.
Definition:
SimpleChatServer.C:178
PopupChatWidget::name_
Wt::WString name_
Definition:
PopupChatWidget.h:36
Wt::WLayout::addWidget
void addWidget(std::unique_ptr< WWidget > widget)
Wt::WObject::implementJavaScript
WStatelessSlot * implementJavaScript(void(T::*method)(), const std::string &jsCode)
SimpleChatServer::suggestGuest
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
Definition:
SimpleChatServer.C:134
SimpleChatWidget::updateUsers
virtual void updateUsers()
Definition:
SimpleChatWidget.C:320
SimpleChatWidget::userName
const Wt::WString & userName() const
Definition:
SimpleChatWidget.h:58
PopupChatWidget::updateUsers
virtual void updateUsers()
Definition:
PopupChatWidget.C:135
Generated on Thu Mar 26 2020 for
the C++ Web Toolkit (Wt)
by
1.8.17