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 #include "SimpleChatServer.h" 00016 00017 namespace Wt { 00018 class WApplication; 00019 class WPushButton; 00020 class WText; 00021 class WLineEdit; 00022 class WTextArea; 00023 } 00024 00025 class ChatEvent; 00026 00031 00034 class SimpleChatWidget : public Wt::WContainerWidget, 00035 public SimpleChatServer::Client 00036 { 00037 public: 00040 SimpleChatWidget(SimpleChatServer& server, Wt::WContainerWidget *parent = 0); 00041 00044 ~SimpleChatWidget(); 00045 00046 void connect(); 00047 void disconnect(); 00048 00049 00052 void letLogin(); 00053 00058 bool startChat(const Wt::WString& user); 00059 00060 void logout(); 00061 00062 SimpleChatServer& server() { return server_; } 00063 00064 int userCount() { return users_.size(); } 00065 00066 const Wt::WString& userName() const { return user_; } 00067 00068 protected: 00069 virtual void createLayout(Wt::WWidget *messages, Wt::WWidget *userList, 00070 Wt::WWidget *messageEdit, 00071 Wt::WWidget *sendButton, Wt::WWidget *logoutButton); 00072 00073 virtual void updateUsers(); 00074 virtual void newMessage(); 00075 00076 virtual void render(Wt::WFlags<Wt::RenderFlag> flags); 00077 00078 protected: 00079 bool loggedIn() const; 00080 00081 private: 00082 typedef std::map<Wt::WString, bool> UserMap; 00083 UserMap users_; 00084 00085 SimpleChatServer& server_; 00086 00087 Wt::JSlot clearInput_; 00088 00089 Wt::WString user_; 00090 00091 Wt::WLineEdit *userNameEdit_; 00092 Wt::WText *statusMsg_; 00093 00094 Wt::WContainerWidget *messages_; 00095 Wt::WContainerWidget *messageEditArea_; 00096 Wt::WTextArea *messageEdit_; 00097 Wt::WPushButton *sendButton_; 00098 Wt::WContainerWidget *userList_; 00099 00100 Wt::WSound* messageReceived_; 00101 00102 void login(); 00103 void send(); 00104 void updateUser(); 00105 00106 /* called from another session */ 00107 void processChatEvent(const ChatEvent& event); 00108 }; 00109 00112 #endif // SIMPLECHATWIDGET
1.7.2