SimpleChatWidget.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // This may look like C code, but it's really -*- C++ -*-
  2. /*
  3. * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
  4. *
  5. * See the LICENSE file for terms of use.
  6. */
  7. #ifndef SIMPLECHATWIDGET_H_
  8. #define SIMPLECHATWIDGET_H_
  9. #include <Wt/WContainerWidget>
  10. #include <Wt/WJavaScript>
  11. #include <Wt/WSound>
  12. #include "SimpleChatServer.h"
  13. namespace Wt {
  14. class WApplication;
  15. class WPushButton;
  16. class WText;
  17. class WLineEdit;
  18. class WTextArea;
  19. }
  20. class ChatEvent;
  21. /**
  22. * \defgroup chatexample Chat example
  23. */
  24. /*@{*/
  25. /*! \brief A self-contained chat widget.
  26. */
  27. class SimpleChatWidget : public Wt::WContainerWidget,
  28. public SimpleChatServer::Client
  29. {
  30. public:
  31. /*! \brief Create a chat widget that will connect to the given server.
  32. */
  33. SimpleChatWidget(SimpleChatServer& server, Wt::WContainerWidget *parent = 0);
  34. /*! \brief Delete a chat widget.
  35. */
  36. ~SimpleChatWidget();
  37. void connect();
  38. void disconnect();
  39. /*! \brief Show a simple login screen.
  40. */
  41. void letLogin();
  42. /*! \brief Start a chat for the given user.
  43. *
  44. * Returns false if the user could not login.
  45. */
  46. bool startChat(const Wt::WString& user);
  47. void logout();
  48. SimpleChatServer& server() { return server_; }
  49. int userCount() { return users_.size(); }
  50. const Wt::WString& userName() const { return user_; }
  51. protected:
  52. virtual void createLayout(Wt::WWidget *messages, Wt::WWidget *userList,
  53. Wt::WWidget *messageEdit,
  54. Wt::WWidget *sendButton, Wt::WWidget *logoutButton);
  55. virtual void updateUsers();
  56. virtual void newMessage();
  57. virtual void render(Wt::WFlags<Wt::RenderFlag> flags);
  58. protected:
  59. bool loggedIn() const;
  60. private:
  61. typedef std::map<Wt::WString, bool> UserMap;
  62. UserMap users_;
  63. SimpleChatServer& server_;
  64. bool loggedIn_;
  65. Wt::JSlot clearInput_;
  66. Wt::WString user_;
  67. Wt::WLineEdit *userNameEdit_;
  68. Wt::WText *statusMsg_;
  69. Wt::WContainerWidget *messages_;
  70. Wt::WTextArea *messageEdit_;
  71. Wt::WPushButton *sendButton_;
  72. Wt::WContainerWidget *userList_;
  73. Wt::WSound* messageReceived_;
  74. void login();
  75. void changeName(const Wt::WString& name);
  76. void send();
  77. void updateUser();
  78. /* called from another session */
  79. void processChatEvent(const ChatEvent& event);
  80. };
  81. /*@}*/
  82. #endif // SIMPLECHATWIDGET