Wt examples
3.3.0
|
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 // - i18n 00018 00019 PopupChatWidget::PopupChatWidget(SimpleChatServer& server, 00020 const std::string& id) 00021 : SimpleChatWidget(server), 00022 missedMessages_(0) 00023 { 00024 setId(id); 00025 00026 if (Wt::WApplication::instance()->environment().agentIsIE()) { 00027 if (Wt::WApplication::instance()->environment().agent() 00028 == Wt::WEnvironment::IE6) 00029 setPositionScheme(Wt::Absolute); 00030 else 00031 setPositionScheme(Wt::Fixed); 00032 } 00033 00034 implementJavaScript 00035 (&PopupChatWidget::toggleSize, 00036 "{" 00037 """var s = $('#" + id + "');" 00038 """s.toggleClass('chat-maximized chat-minimized');" 00039 + Wt::WApplication::instance()->javaScriptClass() 00040 + ".layouts2.scheduleAdjust(true);" 00041 "}"); 00042 00043 online_ = false; 00044 minimized_ = true; 00045 setStyleClass("chat-widget chat-minimized"); 00046 00047 clear(); 00048 addWidget(createBar()); 00049 updateUsers(); 00050 00051 connect(); 00052 } 00053 00054 void PopupChatWidget::setName(const Wt::WString& name) 00055 { 00056 if (name.empty()) 00057 return; 00058 00059 if (online_) { 00060 int tries = 1; 00061 Wt::WString n = name; 00062 while (!server().changeName(name_, n)) 00063 n = name + boost::lexical_cast<std::string>(++tries); 00064 00065 name_ = n; 00066 } else 00067 name_ = name; 00068 } 00069 00070 Wt::WContainerWidget *PopupChatWidget::createBar() 00071 { 00072 Wt::WContainerWidget *bar = new Wt::WContainerWidget(); 00073 bar->setStyleClass("chat-bar"); 00074 00075 Wt::WText *toggleButton = new Wt::WText(); 00076 toggleButton->setInline(false); 00077 toggleButton->setStyleClass("chat-minmax"); 00078 bar->clicked().connect(this, &PopupChatWidget::toggleSize); 00079 bar->clicked().connect(this, &PopupChatWidget::goOnline); 00080 00081 bar->addWidget(toggleButton); 00082 00083 title_ = new Wt::WText(bar); 00084 00085 bar_ = bar; 00086 00087 return bar; 00088 } 00089 00090 void PopupChatWidget::toggleSize() 00091 { 00092 minimized_ = !minimized_; 00093 } 00094 00095 void PopupChatWidget::goOnline() 00096 { 00097 if (!online_) { 00098 online_ = true; 00099 00100 int tries = 1; 00101 Wt::WString name = name_; 00102 if (name.empty()) 00103 name = server().suggestGuest(); 00104 00105 while (!startChat(name)) { 00106 if (name_.empty()) 00107 name = server().suggestGuest(); 00108 else 00109 name = name_ + boost::lexical_cast<std::string>(++tries); 00110 } 00111 00112 name_ = name; 00113 } 00114 00115 missedMessages_ = 0; 00116 bar_->removeStyleClass("alert"); 00117 } 00118 00119 void PopupChatWidget::createLayout(Wt::WWidget *messages, 00120 Wt::WWidget *userList, 00121 Wt::WWidget *messageEdit, 00122 Wt::WWidget *sendButton, 00123 Wt::WWidget *logoutButton) 00124 { 00125 Wt::WVBoxLayout *layout = new Wt::WVBoxLayout(); 00126 layout->setContentsMargins(0, 0, 0, 0); 00127 layout->setSpacing(0); 00128 00129 Wt::WContainerWidget *bar = createBar(); 00130 00131 layout->addWidget(bar); 00132 bar->setMinimumSize(Wt::WLength::Auto, 20); 00133 layout->addWidget(messages, 1); 00134 layout->addWidget(messageEdit); 00135 00136 setLayout(layout); 00137 } 00138 00139 void PopupChatWidget::updateUsers() 00140 { 00141 SimpleChatWidget::updateUsers(); 00142 00143 int count = server().users().size(); 00144 00145 if (!loggedIn()) { 00146 if (count == 0) 00147 title_->setText("Thoughts? Ventilate."); 00148 else if (count == 1) 00149 title_->setText("Chat: 1 user online"); 00150 else 00151 title_->setText(Wt::WString("Chat: {1} users online").arg(count)); 00152 } else { 00153 title_->setText(Wt::WString("Chat: <span class=\"self\">{1}</span>" 00154 " <span class=\"online\">({2} user{3})</span>") 00155 .arg(userName()).arg(count).arg(count == 1 ? "" : "s")); 00156 } 00157 } 00158 00159 void PopupChatWidget::newMessage() 00160 { 00161 if (loggedIn() && minimized()) { 00162 ++missedMessages_; 00163 if (missedMessages_ == 1) { 00164 bar_->addStyleClass("alert"); 00165 } 00166 } 00167 } 00168 00169 bool PopupChatWidget::minimized() const 00170 { 00171 return minimized_; 00172 }