From 1d30fc204176ff23a4a55e0319246ab530ae6454 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 20 Apr 2008 17:48:45 +0000 Subject: Merged two maps into one, avoid non-const operator[] and check whether channel was found. --- src/gui/chat.cpp | 49 ++++++++++++++++++++++++------------------------- src/gui/chat.h | 30 ++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index a9f3b931..2ed6cade 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -76,8 +76,8 @@ ChatWindow::ChatWindow(): mChatTabs->addTab("General", scrollArea); mChatTabs->setPosition(mChatTabs->getFrameSize(), mChatTabs->getFrameSize()); - mChannelOutput["General"] = textOutput; - mChannelScroll["General"] = scrollArea; + mChannels.insert( + std::make_pair("General", ChatArea(textOutput, scrollArea))); add(mChatTabs); add(mChatInput); @@ -99,9 +99,6 @@ ChatWindow::logic() // todo: only do this when the size changes (updateWidgets?) const gcn::Rectangle area = getChildrenArea(); - std::string channelName = mChatTabs->getSelectedTab()->getCaption(); - ScrollArea *scroll = mChannelScroll[channelName]; - mChatInput->setPosition(mChatInput->getFrameSize(), area.height - mChatInput->getHeight() - mChatInput->getFrameSize()); @@ -110,9 +107,14 @@ ChatWindow::logic() mChatTabs->setWidth(area.width - 2 * mChatTabs->getFrameSize()); mChatTabs->setHeight(area.height - 2 * mChatTabs->getFrameSize()); - scroll->setWidth(area.width - 2 * scroll->getFrameSize()); - scroll->setHeight(area.height - 2 * scroll->getFrameSize() - - mChatInput->getHeight() - 26); + const std::string &channelName = mChatTabs->getSelectedTab()->getCaption(); + ChannelMap::const_iterator chan = mChannels.find(channelName); + if (chan != mChannels.end()) { + ScrollArea *scroll = chan->second.scroll; + scroll->setWidth(area.width - 2 * scroll->getFrameSize()); + scroll->setHeight(area.height - 2 * scroll->getFrameSize() - + mChatInput->getHeight() - 26); + } Window::logic(); } @@ -120,6 +122,13 @@ ChatWindow::logic() void ChatWindow::chatLog(std::string line, int own, const std::string &channelName) { + ChannelMap::const_iterator chan = mChannels.find(channelName); + if (chan == mChannels.end()) + return; + + BrowserBox * const output = chan->second.browser; + ScrollArea * const scroll = chan->second.scroll; + // Trim whitespace trim(line); @@ -128,15 +137,6 @@ ChatWindow::chatLog(std::string line, int own, const std::string &channelName) tmp.nick = ""; tmp.text = line; - BrowserBox *output = mChannelOutput[channelName]; - ScrollArea *scroll = mChannelScroll[channelName]; - - // Fix the owner of welcome message. - if (line.substr(0, 7) == "Welcome") - { - own = BY_SERVER; - } - std::string::size_type pos = line.find(" : "); if (pos != std::string::npos) { tmp.nick = line.substr(0, pos); @@ -372,9 +372,9 @@ void ChatWindow::chatSend(std::string const &nick, std::string const &msg, } else if (command == "clear") { - BrowserBox *output = mChannelOutput[channelName]; - if (output) - output->clearRows(); + ChannelMap::const_iterator chan = mChannels.find(channelName); + if (chan != mChannels.end()) + chan->second.browser->clearRows(); } else { @@ -485,14 +485,13 @@ ChatWindow::removeChannel(const std::string &channelName) void ChatWindow::removeChannel(Channel *channel) { - if(channel) + if (channel) { gcn::Tab *tab = mChatTabs->getTab(channel->getName()); if (!tab) return; mChatTabs->removeTab(tab); - mChannelOutput.erase(channel->getName()); - mChannelScroll.erase(channel->getName()); + mChannels.erase(channel->getName()); channelManager->removeChannel(channel); logic(); @@ -513,8 +512,8 @@ ChatWindow::createNewChannelTab(const std::string &channelName) // Add channel to the tabbed area mChatTabs->addTab(channelName, scrollArea); - mChannelOutput[channelName] = textOutput; - mChannelScroll[channelName] = scrollArea; + mChannels.insert( + std::make_pair(channelName, ChatArea(textOutput, scrollArea))); // Ask for channel users Net::ChatServer::getUserList(channelName); diff --git a/src/gui/chat.h b/src/gui/chat.h index ab8c3985..08fed0be 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -234,20 +234,38 @@ class ChatWindow : public Window, public gcn::ActionListener, int own; }; + /** + * A structure combining a BrowserBox with its ScrollArea. + */ + struct ChatArea + { + ChatArea(BrowserBox *b, ScrollArea *s): + browser(b), scroll(s) + {} + + BrowserBox *browser; + ScrollArea *scroll; + }; + #if 0 /** Constructs failed messages for actions */ std::string const_msg(CHATSKILL);*/ #endif - TabbedArea *mChatTabs; /** < Chat Tabbed area for holding each channel */ - gcn::TextField *mChatInput; /**< Input box for typing chat messages */ - std::map mChannelOutput; /**< Map each TextOutput to a tab */ - std::map mChannelScroll; /**< Map each ScrollArea to a tab */ + /** Tabbed area for holding each channel. */ + TabbedArea *mChatTabs; + + /** Input box for typing chat messages. */ + gcn::TextField *mChatInput; + + typedef std::map ChannelMap; + /** Map each tab to its browser and scroll area. */ + ChannelMap mChannels; typedef std::list History; typedef History::iterator HistoryIterator; - History mHistory; /**< Command history */ - HistoryIterator mCurHist; /**< History iterator */ + History mHistory; /**< Command history. */ + HistoryIterator mCurHist; /**< History iterator. */ }; extern ChatWindow *chatWindow; -- cgit v1.2.3-70-g09d2