Wt examples
3.3.0
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
build
buildd
witty-3.3.0
examples
simplechat
PopupChatWidget.C
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2010 Emweb bvba, Heverlee, Belgium.
3
*
4
* See the LICENSE file for terms of use.
5
*/
6
7
#include <Wt/WApplication>
8
#include <Wt/WEnvironment>
9
#include <Wt/WImage>
10
#include <Wt/WText>
11
#include <Wt/WVBoxLayout>
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::WEnvironment::IE6
)
29
setPositionScheme
(
Wt::Absolute
);
30
else
31
setPositionScheme
(
Wt::Fixed
);
32
}
33
34
implementJavaScript
35
(&
PopupChatWidget::toggleSize
,
36
"{"
37
""
"var s = $('#"
+
id
+
"');"
38
""
"s.toggleClass('chat-maximized chat-minimized');"
39
+
Wt::WApplication::instance
()->javaScriptClass()
40
+
".layouts2.scheduleAdjust(true);"
41
"}"
);
42
43
online_
=
false
;
44
minimized_
=
true
;
45
setStyleClass
(
"chat-widget chat-minimized"
);
46
47
clear();
48
addWidget(
createBar
());
49
updateUsers
();
50
51
connect
();
52
}
53
54
void
PopupChatWidget::setName
(
const
Wt::WString
& name)
55
{
56
if
(name.
empty
())
57
return
;
58
59
if
(
online_
) {
60
int
tries = 1;
61
Wt::WString
n = name;
62
while
(!
server
().
changeName
(
name_
, n))
63
n = name + boost::lexical_cast<std::string>(++tries);
64
65
name_
= n;
66
}
else
67
name_
= name;
68
}
69
70
Wt::WContainerWidget
*
PopupChatWidget::createBar
()
71
{
72
Wt::WContainerWidget
*bar =
new
Wt::WContainerWidget
();
73
bar->
setStyleClass
(
"chat-bar"
);
74
75
Wt::WText
*toggleButton =
new
Wt::WText
();
76
toggleButton->
setInline
(
false
);
77
toggleButton->
setStyleClass
(
"chat-minmax"
);
78
bar->clicked().connect(
this
, &
PopupChatWidget::toggleSize
);
79
bar->clicked().connect(
this
, &
PopupChatWidget::goOnline
);
80
81
bar->addWidget(toggleButton);
82
83
title_
=
new
Wt::WText
(bar);
84
85
bar_
= bar;
86
87
return
bar;
88
}
89
90
void
PopupChatWidget::toggleSize
()
91
{
92
minimized_
= !
minimized_
;
93
}
94
95
void
PopupChatWidget::goOnline
()
96
{
97
if
(!
online_
) {
98
online_
=
true
;
99
100
int
tries = 1;
101
Wt::WString
name =
name_
;
102
if
(name.
empty
())
103
name =
server
().
suggestGuest
();
104
105
while
(!
startChat
(name)) {
106
if
(
name_
.
empty
())
107
name =
server
().
suggestGuest
();
108
else
109
name =
name_
+ boost::lexical_cast<std::string>(++tries);
110
}
111
112
name_
= name;
113
}
114
115
missedMessages_
= 0;
116
bar_
->
removeStyleClass
(
"alert"
);
117
}
118
119
void
PopupChatWidget::createLayout
(
Wt::WWidget
*messages,
120
Wt::WWidget
*userList,
121
Wt::WWidget
*messageEdit,
122
Wt::WWidget
*sendButton,
123
Wt::WWidget
*logoutButton)
124
{
125
Wt::WVBoxLayout
*layout =
new
Wt::WVBoxLayout
();
126
layout->setContentsMargins(0, 0, 0, 0);
127
layout->
setSpacing
(0);
128
129
Wt::WContainerWidget
*bar =
createBar
();
130
131
layout->
addWidget
(bar);
132
bar->
setMinimumSize
(
Wt::WLength::Auto
, 20);
133
layout->
addWidget
(messages, 1);
134
layout->
addWidget
(messageEdit);
135
136
setLayout(layout);
137
}
138
139
void
PopupChatWidget::updateUsers
()
140
{
141
SimpleChatWidget::updateUsers
();
142
143
int
count =
server
().
users
().size();
144
145
if
(!
loggedIn
()) {
146
if
(count == 0)
147
title_
->setText(
"Thoughts? Ventilate."
);
148
else
if
(count == 1)
149
title_
->setText(
"Chat: 1 user online"
);
150
else
151
title_
->setText(
Wt::WString
(
"Chat: {1} users online"
).arg(count));
152
}
else
{
153
title_
->setText(
Wt::WString
(
"Chat: <span class=\"self\">{1}</span>"
154
" <span class=\"online\">({2} user{3})</span>"
)
155
.arg(
userName
()).arg(count).arg(count == 1 ?
""
:
"s"
));
156
}
157
}
158
159
void
PopupChatWidget::newMessage
()
160
{
161
if
(
loggedIn
() &&
minimized
()) {
162
++
missedMessages_
;
163
if
(
missedMessages_
== 1) {
164
bar_
->
addStyleClass
(
"alert"
);
165
}
166
}
167
}
168
169
bool
PopupChatWidget::minimized
()
const
170
{
171
return
minimized_
;
172
}
Generated on Fri May 31 2013 for
the C++ Web Toolkit (Wt)
by
1.8.3.1