Wt examples 3.1.10
|
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(Wt::WWidget *messages, Wt::WWidget *userList, 00062 Wt::WWidget *messageEdit, 00063 Wt::WWidget *sendButton, Wt::WWidget *logoutButton); 00064 00065 virtual void updateUsers(); 00066 00067 virtual void render(Wt::WFlags<Wt::RenderFlag> flags); 00068 00069 private: 00070 typedef std::map<Wt::WString, bool> UserMap; 00071 UserMap users_; 00072 00073 SimpleChatServer& server_; 00074 00075 Wt::JSlot clearInput_; 00076 00077 Wt::WString user_; 00078 00079 Wt::WLineEdit *userNameEdit_; 00080 Wt::WText *statusMsg_; 00081 00082 Wt::WContainerWidget *messages_; 00083 Wt::WContainerWidget *messageEditArea_; 00084 Wt::WTextArea *messageEdit_; 00085 Wt::WPushButton *sendButton_; 00086 Wt::WContainerWidget *userList_; 00087 00088 Wt::WSound* messageReceived_; 00089 00090 void login(); 00091 void send(); 00092 void updateUser(); 00093 bool loggedIn() const; 00094 00095 /* called from another session */ 00096 void processChatEvent(const ChatEvent& event); 00097 }; 00098 00101 #endif // SIMPLECHATWIDGET