Wt examples  4.10.4
SimpleChatWidget.h
Go to the documentation of this file.
1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 
8 #ifndef SIMPLECHATWIDGET_H_
9 #define SIMPLECHATWIDGET_H_
10 
11 #include <Wt/WContainerWidget.h>
12 #include <Wt/WJavaScript.h>
13 #include <Wt/WSound.h>
14 
15 #include "SimpleChatServer.h"
16 
17 class ChatEvent;
18 
23 
28 {
29 public:
33 
37 
38  void connect();
39  void disconnect();
40 
41 
44  void letLogin();
45 
50  bool startChat(const Wt::WString& user);
51 
52  void logout();
53 
55 
56  int userCount() { return users_.size(); }
57 
58  const Wt::WString& userName() const { return user_; }
59 
60 protected:
61  virtual void createLayout(std::unique_ptr<Wt::WWidget> messages, std::unique_ptr<Wt::WWidget> userList,
62  std::unique_ptr<Wt::WWidget> messageEdit,
63  std::unique_ptr<Wt::WWidget> sendButton, std::unique_ptr<Wt::WWidget> logoutButton);
64 
65  virtual void updateUsers();
66  virtual void newMessage();
67 
68  virtual void render(Wt::WFlags<Wt::RenderFlag> flags);
69 
70 protected:
71  bool loggedIn() const;
72 
73 private:
74  typedef std::map<Wt::WString, bool> UserMap;
76 
78  bool loggedIn_;
79 
81 
83 
86 
91 
92  std::unique_ptr<Wt::WSound> messageReceived_;
93 
94  void login();
95  void changeName(const Wt::WString& name);
96  void send();
97  void updateUser(Wt::WCheckBox *b);
98 
99  /* called from another session */
100  void processChatEvent(const ChatEvent& event);
101 };
102 
105 #endif // SIMPLECHATWIDGET
Encapsulate a chat event.
A simple chat server.
A self-contained chat widget.
bool startChat(const Wt::WString &user)
Start a chat for the given user.
virtual void updateUsers()
void updateUser(Wt::WCheckBox *b)
const Wt::WString & userName() const
Wt::WContainerWidget * messages_
Wt::WTextArea * messageEdit_
Wt::WLineEdit * userNameEdit_
SimpleChatWidget(SimpleChatServer &server)
Create a chat widget that will connect to the given server.
void letLogin()
Show a simple login screen.
SimpleChatServer & server()
std::unique_ptr< Wt::WSound > messageReceived_
bool loggedIn() const
Wt::WText * statusMsg_
virtual void createLayout(std::unique_ptr< Wt::WWidget > messages, std::unique_ptr< Wt::WWidget > userList, std::unique_ptr< Wt::WWidget > messageEdit, std::unique_ptr< Wt::WWidget > sendButton, std::unique_ptr< Wt::WWidget > logoutButton)
virtual void render(Wt::WFlags< Wt::RenderFlag > flags)
Wt::Core::observing_ptr< Wt::WContainerWidget > userList_
SimpleChatServer & server_
virtual void newMessage()
Wt::Core::observing_ptr< Wt::WPushButton > sendButton_
void changeName(const Wt::WString &name)
void processChatEvent(const ChatEvent &event)
std::map< Wt::WString, bool > UserMap
~SimpleChatWidget()
Delete a chat widget.