summaryrefslogtreecommitdiff
path: root/src/gui/chat.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2009-10-31 15:42:28 +0200
committerBlue <bluesansdouze@gmail.com>2010-01-07 23:12:10 +0100
commitbe5460f2a294bb8e50b40f498f29a556b31bedd7 (patch)
tree5256167e0b8487fdb2c49090986cc883614fb9b1 /src/gui/chat.cpp
parent84da747711ed1713984ca514a8bb786219a85d9b (diff)
downloadmana-be5460f2a294bb8e50b40f498f29a556b31bedd7.tar.gz
mana-be5460f2a294bb8e50b40f498f29a556b31bedd7.tar.bz2
mana-be5460f2a294bb8e50b40f498f29a556b31bedd7.tar.xz
mana-be5460f2a294bb8e50b40f498f29a556b31bedd7.zip
Chat auto completing
Diffstat (limited to 'src/gui/chat.cpp')
-rw-r--r--src/gui/chat.cpp92
1 files changed, 91 insertions, 1 deletions
diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp
index c0597a64..3c4961cf 100644
--- a/src/gui/chat.cpp
+++ b/src/gui/chat.cpp
@@ -25,6 +25,7 @@
#include "gui/recorder.h"
#include "gui/setup.h"
#include "gui/sdlinput.h"
+#include "gui/partywindow.h"
#include "gui/widgets/chattab.h"
#include "gui/widgets/scrollarea.h"
@@ -54,7 +55,8 @@
class ChatInput : public TextField, public gcn::FocusListener
{
public:
- ChatInput()
+ ChatInput():
+ TextField("", false)
{
setVisible(false);
addFocusListener(this);
@@ -394,6 +396,12 @@ void ChatWindow::keyPressed(gcn::KeyEvent &event)
mChatInput->setText(*mCurHist);
mChatInput->setCaretPosition(mChatInput->getText().length());
}
+ else if (event.getKey().getValue() == Key::TAB &&
+ mChatInput->getText() != "")
+ {
+ autoComplete();
+ return;
+ }
}
void ChatWindow::addInputText(const std::string &text)
@@ -499,3 +507,85 @@ ChatTab *ChatWindow::addWhisperTab(const std::string &nick, bool switchTo)
return ret;
}
+
+void ChatWindow::autoComplete()
+{
+ int caretPos = mChatInput->getCaretPosition();
+ int startName = 0;
+ const std::string inputText = mChatInput->getText();
+ std::string name = inputText.substr(0, caretPos);
+ std::string newName("");
+
+ for (int f = caretPos - 1; f > -1; f --)
+ {
+ if (isWordSeparator(inputText[f]))
+ {
+ startName = f + 1;
+ name = inputText.substr(f + 1, caretPos - f);
+ break;
+ }
+ }
+
+ if (caretPos - 1 + 1 == startName)
+ return;
+
+
+ ChatTab *cTab = static_cast<ChatTab*>(mChatTabs->getSelectedTab());
+ if (cTab && cTab->getType() == ChatTab::PARTY)
+ {
+ newName = partyWindow->getAutoCompleteName(name);
+ }
+ if (newName == "")
+ {
+ newName = beingManager->getAutoCompletePlayerName(name);
+ }
+ if (newName == "")
+ {
+ newName = autoCompleteHistory(name);
+ }
+
+ if (newName != "")
+ {
+ mChatInput->setText(inputText.substr(0, startName) + newName
+ + inputText.substr(caretPos, inputText.length() - caretPos));
+
+ if (startName > 0)
+ mChatInput->setCaretPosition(caretPos - name.length() + newName.length() + 1);
+ else
+ mChatInput->setCaretPosition(caretPos - name.length() + newName.length());
+ }
+}
+
+std::string ChatWindow::autoCompleteHistory(std::string partName)
+{
+ History::iterator i = mHistory.begin();
+ std::string newName = "";
+
+ while (i != mHistory.end())
+ {
+ std::string line = *i;
+ int f = 0;
+ while (f < line.length() && !isWordSeparator(line.at(f)))
+ {
+ f++;
+ }
+ line = line.substr(0, f);
+ if (line != "")
+ {
+ std::string::size_type pos = line.find(partName, 0);
+ if (pos == 0)
+ {
+ if (newName != "")
+ {
+ newName = findSameSubstring(line, newName);
+ }
+ else
+ {
+ newName = line;
+ }
+ }
+ }
+ ++i;
+ }
+ return newName;
+}