Wt examples  3.7.1
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>
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 
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 
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();
49  updateUsers();
50 
51  connect();
52 }
53 
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 
71 {
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 
91 {
93 }
94 
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 
120  Wt::WWidget *userList,
121  Wt::WWidget *messageEdit,
122  Wt::WWidget *sendButton,
123  Wt::WWidget *logoutButton)
124 {
126  layout->setContentsMargins(0, 0, 0, 0);
127  layout->setSpacing(0);
128 
130 
131  layout->addWidget(bar);
133  layout->addWidget(messages, 1);
134  layout->addWidget(messageEdit);
135 
136  setLayout(layout);
137 }
138 
140 {
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 
160 {
161  if (loggedIn() && minimized()) {
162  ++missedMessages_;
163  if (missedMessages_ == 1) {
164  bar_->addStyleClass("alert");
165  }
166  }
167 }
168 
170 {
171  return minimized_;
172 }
void setLayout(std::unique_ptr< WLayout > layout)
virtual void setStyleClass(const WString &styleClass) override
bool empty() const
virtual void newMessage()
virtual void setPositionScheme(PositionScheme scheme) override
Wt::WText * title_
EventSignal< WMouseEvent > & clicked()
SimpleChatServer & server()
static WLength Auto
UserSet users()
Get the users currently logged in.
A self-contained chat widget.
bool minimized() const
virtual void setMinimumSize(const WLength &width, const WLength &height) override
void addWidget(std::unique_ptr< WWidget > widget, int stretch, WFlags< AlignmentFlag > alignment)
virtual void setId(const std::string &id) override
Wt::WString name_
virtual void updateUsers()
virtual void removeStyleClass(const WString &styleClass, bool force=false)=0
bool changeName(const Wt::WString &user, const Wt::WString &newUser)
Changes the name.
Wt::WWidget * bar_
virtual void updateUsers()
bool setText(const WString &text)
bool startChat(const Wt::WString &user)
Start a chat for the given user.
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
virtual void clear()
const Wt::WString & userName() const
virtual int count() const
void setContentsMargins(int left, int top, int right, int bottom)
WStatelessSlot * implementJavaScript(void(T::*method)(), const std::string &jsCode)
static WApplication * instance()
virtual void createLayout(Wt::WWidget *messages, Wt::WWidget *userList, Wt::WWidget *messageEdit, Wt::WWidget *sendButton, Wt::WWidget *logoutButton)
PopupChatWidget(SimpleChatServer &server, const std::string &id)
A simple chat server.
void setSpacing(int size)
Wt::WContainerWidget * createBar()
bool loggedIn() const
void setName(const Wt::WString &name)
virtual void addWidget(std::unique_ptr< WWidget > widget)
virtual void setInline(bool isInline) override
virtual void addStyleClass(const WString &styleClass, bool force=false)=0
WLayout * layout() const

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13