Wt examples  3.3.1
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
PopupChatWidget Class Reference

A popup chat widget. More...

#include <PopupChatWidget.h>

Inheritance diagram for PopupChatWidget:
Inheritance graph
[legend]

Public Member Functions

 PopupChatWidget (SimpleChatServer &server, const std::string &id)
 
void setName (const Wt::WString &name)
 
- Public Member Functions inherited from SimpleChatWidget
 SimpleChatWidget (SimpleChatServer &server, Wt::WContainerWidget *parent=0)
 Create a chat widget that will connect to the given server. More...
 
 ~SimpleChatWidget ()
 Delete a chat widget. More...
 
void connect ()
 
void disconnect ()
 
void letLogin ()
 Show a simple login screen. More...
 
bool startChat (const Wt::WString &user)
 Start a chat for the given user. More...
 
void logout ()
 
SimpleChatServerserver ()
 
int userCount ()
 
const Wt::WStringuserName () const
 
- Public Member Functions inherited from Wt::WWidget
virtual void setPositionScheme (PositionScheme scheme)=0
 
virtual PositionScheme positionScheme () const =0
 
virtual void setOffsets (const WLength &offset, WFlags< Side > sides=All)=0
 
virtual WLength offset (Side side) const =0
 
virtual void resize (const WLength &width, const WLength &height)
 
virtual WLength width () const =0
 
virtual WLength height () const =0
 
virtual void setMinimumSize (const WLength &width, const WLength &height)=0
 
virtual WLength minimumWidth () const =0
 
virtual WLength minimumHeight () const =0
 
virtual void setMaximumSize (const WLength &width, const WLength &height)=0
 
virtual WLength maximumWidth () const =0
 
virtual WLength maximumHeight () const =0
 
virtual void setLineHeight (const WLength &height)=0
 
virtual WLength lineHeight () const =0
 
virtual void setFloatSide (Side s)=0
 
virtual Side floatSide () const =0
 
virtual void setClearSides (WFlags< Side > sides)=0
 
virtual WFlags< SideclearSides () const =0
 
virtual void setMargin (const WLength &margin, WFlags< Side > sides=All)=0
 
virtual WLength margin (Side side) const =0
 
virtual void setHiddenKeepsGeometry (bool enabled)=0
 
virtual bool hiddenKeepsGeometry () const =0
 
virtual void setHidden (bool hidden, const WAnimation &animation=WAnimation())=0
 
virtual bool isHidden () const =0
 
virtual bool isVisible () const =0
 
virtual void setDisabled (bool disabled)=0
 
virtual bool isDisabled () const =0
 
virtual bool isEnabled () const =0
 
virtual void setPopup (bool popup)=0
 
virtual bool isPopup () const =0
 
virtual void setInline (bool inlined)=0
 
virtual bool isInline () const =0
 
virtual void setDecorationStyle (const WCssDecorationStyle &style)=0
 
virtual WCssDecorationStyledecorationStyle ()=0
 
virtual void setStyleClass (const WString &styleClass)=0
 
virtual WString styleClass () const =0
 
virtual void addStyleClass (const WString &styleClass, bool force=false)=0
 
virtual void removeStyleClass (const WString &styleClass, bool force=false)=0
 
virtual bool hasStyleClass (const WString &styleClass) const =0
 
virtual void setVerticalAlignment (AlignmentFlag alignment, const WLength &length=WLength::Auto)=0
 
virtual AlignmentFlag verticalAlignment () const =0
 
virtual WLength verticalAlignmentLength () const =0
 
virtual void setToolTip (const WString &text, TextFormat textFormat=PlainText)=0
 
virtual const WStringtoolTip () const =0
 
virtual void refresh ()
 
virtual void setAttributeValue (const std::string &name, const WString &value)=0
 
virtual WString attributeValue (const std::string &name) const =0
 
virtual void setJavaScriptMember (const std::string &name, const std::string &value)=0
 
virtual std::string javaScriptMember (const std::string &name) const =0
 
virtual void callJavaScriptMember (const std::string &name, const std::string &args)=0
 
virtual void load ()=0
 
virtual bool loaded () const =0
 
virtual void setTabIndex (int index)=0
 
virtual int tabIndex () const =0
 
virtual void setId (const std::string &id)=0
 
virtual WWidgetfind (const std::string &name)=0
 
virtual void setSelectable (bool selectable)=0
 
virtual void doJavaScript (const std::string &js)=0
 

Protected Member Functions

virtual void createLayout (Wt::WWidget *messages, Wt::WWidget *userList, Wt::WWidget *messageEdit, Wt::WWidget *sendButton, Wt::WWidget *logoutButton)
 
virtual void updateUsers ()
 
virtual void newMessage ()
 
- Protected Member Functions inherited from SimpleChatWidget
virtual void render (Wt::WFlags< Wt::RenderFlag > flags)
 
bool loggedIn () const
 
- Protected Member Functions inherited from Wt::WWidget
virtual void enableAjax ()=0
 
virtual void propagateSetEnabled (bool enabled)=0
 
virtual void render (WFlags< RenderFlag > flags)
 

Private Member Functions

void toggleSize ()
 
void goOnline ()
 
bool minimized () const
 
Wt::WContainerWidgetcreateBar ()
 

Private Attributes

Wt::WString name_
 
Wt::WTexttitle_
 
Wt::WWidgetbar_
 
bool online_
 
bool minimized_
 
int missedMessages_
 

Detailed Description

A popup chat widget.

Definition at line 20 of file PopupChatWidget.h.

Constructor & Destructor Documentation

PopupChatWidget::PopupChatWidget ( SimpleChatServer server,
const std::string &  id 
)

Definition at line 19 of file PopupChatWidget.C.

21  : SimpleChatWidget(server),
23 {
24  setId(id);
25 
26  if (Wt::WApplication::instance()->environment().agentIsIE()) {
27  if (Wt::WApplication::instance()->environment().agent()
30  else
32  }
33 
34  implementJavaScript
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 }
virtual void setId(const std::string &id)=0
virtual void updateUsers()
static WApplication * instance()
virtual void setPositionScheme(PositionScheme scheme)=0
Wt::WContainerWidget * createBar()
virtual void setStyleClass(const WString &styleClass)=0
SimpleChatWidget(SimpleChatServer &server, Wt::WContainerWidget *parent=0)
Create a chat widget that will connect to the given server.

Member Function Documentation

Wt::WContainerWidget * PopupChatWidget::createBar ( )
private

Definition at line 70 of file PopupChatWidget.C.

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 }
Wt::WText * title_
Wt::WWidget * bar_
virtual void setInline(bool inlined)=0
virtual void setStyleClass(const WString &styleClass)=0
void PopupChatWidget::createLayout ( Wt::WWidget messages,
Wt::WWidget userList,
Wt::WWidget messageEdit,
Wt::WWidget sendButton,
Wt::WWidget logoutButton 
)
protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 119 of file PopupChatWidget.C.

124 {
125  Wt::WVBoxLayout *layout = new Wt::WVBoxLayout();
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 }
static WLength Auto
virtual void setMinimumSize(const WLength &width, const WLength &height)=0
void addWidget(WWidget *widget, int stretch=0, WFlags< AlignmentFlag > alignment=0)
void setSpacing(int size)
Wt::WContainerWidget * createBar()
void PopupChatWidget::goOnline ( )
private

Definition at line 95 of file PopupChatWidget.C.

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 }
SimpleChatServer & server()
Wt::WString name_
virtual void removeStyleClass(const WString &styleClass, bool force=false)=0
Wt::WWidget * bar_
bool startChat(const Wt::WString &user)
Start a chat for the given user.
Wt::WString suggestGuest()
Get a suggestion for a guest user name.
bool empty() const
bool PopupChatWidget::minimized ( ) const
private

Definition at line 169 of file PopupChatWidget.C.

170 {
171  return minimized_;
172 }
void PopupChatWidget::newMessage ( )
protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 159 of file PopupChatWidget.C.

160 {
161  if (loggedIn() && minimized()) {
162  ++missedMessages_;
163  if (missedMessages_ == 1) {
164  bar_->addStyleClass("alert");
165  }
166  }
167 }
bool loggedIn() const
Wt::WWidget * bar_
bool minimized() const
virtual void addStyleClass(const WString &styleClass, bool force=false)=0
void PopupChatWidget::setName ( const Wt::WString name)

Definition at line 54 of file PopupChatWidget.C.

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 }
SimpleChatServer & server()
Wt::WString name_
bool changeName(const Wt::WString &user, const Wt::WString &newUser)
Changes the name.
bool empty() const
void PopupChatWidget::toggleSize ( )
private

Definition at line 90 of file PopupChatWidget.C.

91 {
93 }
void PopupChatWidget::updateUsers ( )
protectedvirtual

Reimplemented from SimpleChatWidget.

Definition at line 139 of file PopupChatWidget.C.

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 }
bool loggedIn() const
Wt::WText * title_
const Wt::WString & userName() const
SimpleChatServer & server()
UserSet users()
Get the users currently logged in.
virtual void updateUsers()

Member Data Documentation

Wt::WWidget* PopupChatWidget::bar_
private

Definition at line 38 of file PopupChatWidget.h.

bool PopupChatWidget::minimized_
private

Definition at line 39 of file PopupChatWidget.h.

int PopupChatWidget::missedMessages_
private

Definition at line 40 of file PopupChatWidget.h.

Wt::WString PopupChatWidget::name_
private

Definition at line 36 of file PopupChatWidget.h.

bool PopupChatWidget::online_
private

Definition at line 39 of file PopupChatWidget.h.

Wt::WText* PopupChatWidget::title_
private

Definition at line 37 of file PopupChatWidget.h.


The documentation for this class was generated from the following files:

Generated on Wed Jun 11 2014 for the C++ Web Toolkit (Wt) by doxygen 1.8.7