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 19 /** 20 * \defgroup chatexample Chat example 21 */ 22 /*@{*/ 23 24 /*! \brief A self-contained chat widget. 25 */ 26 class SimpleChatWidget : public Wt::WContainerWidget, 27 public SimpleChatServer::Client 28 { 29 public: 30 /*! \brief Create a chat widget that will connect to the given server. 31 */ 32 SimpleChatWidget(SimpleChatServer& server); 33 34 /*! \brief Delete a chat widget. 35 */ 36 ~SimpleChatWidget(); 37 38 void connect(); 39 void disconnect(); 40 41 42 /*! \brief Show a simple login screen. 43 */ 44 void letLogin(); 45 46 /*! \brief Start a chat for the given user. 47 * 48 * Returns false if the user could not login. 49 */ 50 bool startChat(const Wt::WString& user); 51 52 void logout(); 53 server()54 SimpleChatServer& server() { return server_; } 55 userCount()56 int userCount() { return users_.size(); } 57 userName()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; 75 UserMap users_; 76 77 SimpleChatServer& server_; 78 bool loggedIn_; 79 80 Wt::JSlot clearInput_; 81 82 Wt::WString user_; 83 84 Wt::WLineEdit *userNameEdit_; 85 Wt::WText *statusMsg_; 86 87 Wt::WContainerWidget *messages_; 88 Wt::WTextArea *messageEdit_; 89 Wt::Core::observing_ptr<Wt::WPushButton> sendButton_; 90 Wt::Core::observing_ptr<Wt::WContainerWidget> userList_; 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 103 /*@}*/ 104 105 #endif // SIMPLECHATWIDGET 106