Wt examples 3.1.10
|
00001 /* 00002 * Copyright (C) 2010 Emweb bvba, Heverlee, Belgium. 00003 * 00004 * See the LICENSE file for terms of use. 00005 */ 00006 00007 #include <Wt/WApplication> 00008 #include <Wt/WEnvironment> 00009 #include <Wt/WImage> 00010 #include <Wt/WText> 00011 #include <Wt/WVBoxLayout> 00012 00013 #include "PopupChatWidget.h" 00014 #include "SimpleChatServer.h" 00015 00016 // TODO: 00017 // - oher color for jwt ? 00018 // - i18n 00019 00020 PopupChatWidget::PopupChatWidget(SimpleChatServer& server) 00021 : SimpleChatWidget(server) 00022 { 00023 if (Wt::WApplication::instance()->environment().agentIsIE()) { 00024 if (Wt::WApplication::instance()->environment().agent() == Wt::WEnvironment::IE6) 00025 setPositionScheme(Wt::Absolute); 00026 else 00027 setPositionScheme(Wt::Fixed); 00028 } 00029 00030 online_ = false; 00031 00032 minimize(); 00033 } 00034 00035 void PopupChatWidget::setName(const Wt::WString& name) 00036 { 00037 if (name.empty()) 00038 return; 00039 00040 if (online_) { 00041 int tries = 1; 00042 Wt::WString n = name; 00043 while (!server().changeName(name_, n)) 00044 n = name + boost::lexical_cast<std::string>(++tries); 00045 00046 name_ = n; 00047 } else 00048 name_ = name; 00049 } 00050 00051 void PopupChatWidget::minimize() 00052 { 00053 if (!online_) { 00054 clear(); 00055 addWidget(createBar()); 00056 title_->setText("Thoughts? Ventilate."); 00057 } 00058 00059 setStyleClass("chat-widget chat-minimized"); 00060 } 00061 00062 Wt::WContainerWidget *PopupChatWidget::createBar() 00063 { 00064 Wt::WContainerWidget *bar = new Wt::WContainerWidget(); 00065 bar->setStyleClass("chat-bar"); 00066 00067 Wt::WText *toggleButton = new Wt::WText(); 00068 toggleButton->setInline(false); 00069 toggleButton->setStyleClass("chat-minmax"); 00070 bar->clicked().connect(this, &PopupChatWidget::toggleSize); 00071 00072 bar->addWidget(toggleButton); 00073 00074 title_ = new Wt::WText(bar); 00075 00076 return bar; 00077 } 00078 00079 void PopupChatWidget::toggleSize() 00080 { 00081 if (styleClass() == "chat-widget chat-minimized") 00082 maximize(); 00083 else 00084 minimize(); 00085 } 00086 00087 void PopupChatWidget::createLayout(Wt::WWidget *messages, 00088 Wt::WWidget *userList, 00089 Wt::WWidget *messageEdit, 00090 Wt::WWidget *sendButton, 00091 Wt::WWidget *logoutButton) 00092 { 00093 Wt::WVBoxLayout *layout = new Wt::WVBoxLayout(); 00094 layout->setContentsMargins(0, 0, 0, 0); 00095 layout->setSpacing(0); 00096 00097 Wt::WContainerWidget *bar = createBar(); 00098 00099 layout->addWidget(bar); 00100 layout->addWidget(messages, 1); 00101 layout->addWidget(messageEdit); 00102 00103 setLayout(layout); 00104 } 00105 00106 void PopupChatWidget::updateUsers() 00107 { 00108 SimpleChatWidget::updateUsers(); 00109 00110 int count = server().users().size(); 00111 00112 if (count == 1) 00113 title_->setText("Chat: 1 user online"); 00114 else 00115 title_->setText("Chat: " 00116 + boost::lexical_cast<std::string>(count) + " users online"); 00117 } 00118 00119 void PopupChatWidget::maximize() 00120 { 00121 if (!online_) { 00122 online_ = true; 00123 00124 int tries = 1; 00125 Wt::WString name = name_; 00126 if (name.empty()) 00127 name = server().suggestGuest(); 00128 00129 while (!startChat(name)) { 00130 if (name_.empty()) 00131 name = server().suggestGuest(); 00132 else 00133 name = name_ + boost::lexical_cast<std::string>(++tries); 00134 } 00135 00136 name_ = name; 00137 } 00138 00139 setStyleClass("chat-widget chat-maximized"); 00140 }