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