00001 // This may look like C code, but it's really -*- C++ -*- 00002 /* 00003 * Copyright (C) 2008 Emweb bvba, Heverlee, Belgium. 00004 * 00005 * See the LICENSE file for terms of use. 00006 */ 00007 00008 #ifndef SIMPLECHATWIDGET_H_ 00009 #define SIMPLECHATWIDGET_H_ 00010 00011 #include <Wt/WContainerWidget> 00012 #include <Wt/WJavaScript> 00013 #include <Wt/WSound> 00014 00015 namespace Wt { 00016 class WApplication; 00017 class WPushButton; 00018 class WText; 00019 class WLineEdit; 00020 class WTextArea; 00021 } 00022 00023 class SimpleChatServer; 00024 class ChatEvent; 00025 00030 00033 class SimpleChatWidget : public Wt::WContainerWidget 00034 { 00035 public: 00038 SimpleChatWidget(SimpleChatServer& server, Wt::WContainerWidget *parent = 0); 00039 00042 ~SimpleChatWidget(); 00043 00046 void letLogin(); 00047 00052 bool startChat(const Wt::WString& user); 00053 00054 void logout(); 00055 00056 SimpleChatServer& server() { return server_; } 00057 00058 int userCount() { return users_.size(); } 00059 00060 protected: 00061 virtual void createLayout(WWidget *messages, WWidget *userList, 00062 WWidget *messageEdit, 00063 WWidget *sendButton, WWidget *logoutButton); 00064 00065 virtual void updateUsers(); 00066 00067 private: 00068 typedef std::map<Wt::WString, bool> UserMap; 00069 UserMap users_; 00070 00071 SimpleChatServer& server_; 00072 Wt::WApplication *app_; 00073 00074 Wt::JSlot clearInput_; 00075 00076 Wt::WString user_; 00077 00078 Wt::WLineEdit *userNameEdit_; 00079 Wt::WText *statusMsg_; 00080 00081 Wt::WContainerWidget *messages_; 00082 Wt::WContainerWidget *messageEditArea_; 00083 Wt::WTextArea *messageEdit_; 00084 Wt::WPushButton *sendButton_; 00085 Wt::WContainerWidget *userList_; 00086 00087 boost::signals::connection eventConnection_; 00088 00089 Wt::WSound messageReceived_; 00090 00091 void login(); 00092 void send(); 00093 void updateUser(); 00094 00095 /* called from another session */ 00096 void processChatEvent(const ChatEvent& event); 00097 }; 00098 00101 #endif // SIMPLECHATWIDGET