Wt examples
3.2.1
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
build
buildd
witty-3.2.1
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
"}"
);
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
53
void
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 + boost::lexical_cast<std::string>(++tries);
63
64
name_
= n;
65
}
else
66
name_
= name;
67
}
68
69
Wt::WContainerWidget
*
PopupChatWidget::createBar
()
70
{
71
Wt::WContainerWidget
*bar =
new
Wt::WContainerWidget
();
72
bar->
setStyleClass
(
"chat-bar"
);
73
74
Wt::WText
*toggleButton =
new
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
(toggleButton);
81
82
title_
=
new
Wt::WText
(bar);
83
84
bar_
= bar;
85
86
return
bar;
87
}
88
89
void
PopupChatWidget::toggleSize
()
90
{
91
minimized_
= !
minimized_
;
92
}
93
94
void
PopupChatWidget::goOnline
()
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_
+ boost::lexical_cast<std::string>(++tries);
109
}
110
111
name_
= name;
112
}
113
114
missedMessages_
= 0;
115
bar_
->
removeStyleClass
(
"alert"
);
116
}
117
118
void
PopupChatWidget::createLayout
(
Wt::WWidget
*messages,
119
Wt::WWidget
*userList,
120
Wt::WWidget
*messageEdit,
121
Wt::WWidget
*sendButton,
122
Wt::WWidget
*logoutButton)
123
{
124
Wt::WVBoxLayout
*
layout
=
new
Wt::WVBoxLayout
();
125
layout->
setContentsMargins
(0, 0, 0, 0);
126
layout->
setSpacing
(0);
127
128
Wt::WContainerWidget
*bar =
createBar
();
129
130
layout->
addWidget
(bar);
131
layout->
addWidget
(messages, 1);
132
layout->
addWidget
(messageEdit);
133
134
setLayout
(layout);
135
}
136
137
void
PopupChatWidget::updateUsers
()
138
{
139
SimpleChatWidget::updateUsers
();
140
141
int
count
=
server
().
users
().size();
142
143
if
(!
loggedIn
()) {
144
if
(count == 0)
145
title_
->
setText
(
"Thoughts? Ventilate."
);
146
else
if
(count == 1)
147
title_
->
setText
(
"Chat: 1 user online"
);
148
else
149
title_
->
setText
(
Wt::WString
(
"Chat: {1} users online"
).arg(count));
150
}
else
{
151
title_
->
setText
(
Wt::WString
(
"Chat: <span class=\"self\">{1}</span>"
152
" <span class=\"online\">({2} user{3})</span>"
)
153
.arg(
userName
()).arg(count).arg(count == 1 ?
""
:
"s"
));
154
}
155
}
156
157
void
PopupChatWidget::newMessage
()
158
{
159
if
(
loggedIn
() &&
minimized
()) {
160
++
missedMessages_
;
161
if
(
missedMessages_
== 1) {
162
bar_
->
addStyleClass
(
"alert"
);
163
}
164
}
165
}
166
167
bool
PopupChatWidget::minimized
()
const
168
{
169
return
minimized_
;
170
}
Generated on Thu Jun 14 2012 for
the C++ Web Toolkit (Wt)
by
1.8.1