summaryrefslogtreecommitdiff
path: root/src/gui/widgets/textfield.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>2010-09-25 03:15:26 +0200
committerThorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>2010-09-25 03:15:26 +0200
commit661d16e98c62dfff40f481177bf3f1a0c58c2124 (patch)
treea415866c4c94a0a0c53045a47220ca413ae9c5c9 /src/gui/widgets/textfield.cpp
parent758d80263b1647c712c0e0cdd3dfca9945a1bb7e (diff)
parent7d0738df0d139af3175fcc1fec5b9be4a467f4f4 (diff)
downloadMana-661d16e98c62dfff40f481177bf3f1a0c58c2124.tar.gz
Mana-661d16e98c62dfff40f481177bf3f1a0c58c2124.tar.bz2
Mana-661d16e98c62dfff40f481177bf3f1a0c58c2124.tar.xz
Mana-661d16e98c62dfff40f481177bf3f1a0c58c2124.zip
Merge branch '1.0'
Conflicts: src/actorspritemanager.h src/beingmanager.cpp src/game.cpp src/gui/beingpopup.cpp src/gui/chat.cpp src/gui/chat.h src/gui/inventorywindow.h src/gui/itempopup.cpp src/gui/socialwindow.cpp src/gui/statuswindow.cpp src/gui/widgets/chattab.cpp src/gui/widgets/chattab.h src/net/tmwa/inventoryhandler.cpp src/net/tmwa/partyhandler.cpp src/party.cpp src/sound.cpp src/utils/stringutils.cpp src/utils/stringutils.h
Diffstat (limited to 'src/gui/widgets/textfield.cpp')
-rw-r--r--src/gui/widgets/textfield.cpp121
1 files changed, 120 insertions, 1 deletions
diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp
index 9696cd59..d06df376 100644
--- a/src/gui/widgets/textfield.cpp
+++ b/src/gui/widgets/textfield.cpp
@@ -32,6 +32,7 @@
#include "utils/copynpaste.h"
#include "utils/dtor.h"
+#include "utils/stringutils.h"
#include <guichan/font.hpp>
@@ -43,7 +44,9 @@ ImageRect TextField::skin;
TextField::TextField(const std::string &text, bool loseFocusOnTab):
gcn::TextField(text),
- mNumeric(false)
+ mNumeric(false),
+ mAutoComplete(NULL),
+ mHistory(NULL)
{
setFrameSize(2);
@@ -209,6 +212,42 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent)
}
} break;
+ case Key::UP:
+ {
+ if (mHistory && !mHistory->atBegining() && !mHistory->empty())
+ {
+ // Move backward through the history
+ mHistory->current--;
+ setText(*mHistory->current);
+ setCaretPosition(getText().length());
+ }
+ } break;
+
+ case Key::DOWN:
+ {
+ if (mHistory && !mHistory->atEnd())
+ {
+ // Move forward through the history
+ TextHistoryIterator prevHist = mHistory->current++;
+
+ if (!mHistory->atEnd())
+ {
+ setText(*mHistory->current);
+ setCaretPosition(getText().length());
+ }
+ else
+ {
+ setText("");
+ mHistory->current = prevHist;
+ }
+ }
+ else if (getText() != "")
+ {
+ // Always clear (easy access to useful function)
+ setText("");
+ }
+ } break;
+
case Key::DELETE:
{
unsigned sz = mText.size();
@@ -234,6 +273,18 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent)
} break;
case Key::ENTER:
+ if (mHistory)
+ {
+ // If the input is different from previous, put it in the history
+ if (!getText().empty() && (mHistory->empty() ||
+ !mHistory->matchesLastEntry(getText())))
+ {
+ mHistory->addEntry(getText());
+ }
+
+ mHistory->toEnd();
+ }
+
distributeActionEvent();
break;
@@ -246,6 +297,7 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent)
break;
case Key::TAB:
+ autoComplete();
if (mLoseFocusOnTab)
return;
break;
@@ -259,6 +311,73 @@ void TextField::keyPressed(gcn::KeyEvent &keyEvent)
fixScroll();
}
+void TextField::autoComplete()
+{
+ if (mAutoComplete && mText.size() > 0)
+ {
+ const int caretPos = getCaretPosition();
+ int startName = 0;
+ const std::string inputText = 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 - startName);
+ break;
+ }
+ }
+
+ if (caretPos == startName)
+ return;
+
+
+ std::vector<std::string> nameList;
+ mAutoComplete->getAutoCompleteList(nameList);
+ newName = autocomplete(nameList, name);
+
+ if (newName == "" && mHistory)
+ {
+
+ TextHistoryIterator i = mHistory->history.begin();
+ std::vector<std::string> nameList;
+
+ while (i != mHistory->history.end())
+ {
+ std::string line = *i;
+ unsigned int f = 0;
+ while (f < line.length() && !isWordSeparator(line.at(f)))
+ {
+ f++;
+ }
+ line = line.substr(0, f);
+ if (line != "")
+ {
+ nameList.push_back(line);
+ }
+ ++i;
+ }
+
+ newName = autocomplete(nameList, name);
+ }
+
+ if (newName != "")
+ {
+ if(inputText[0] == '@' || inputText[0] == '/')
+ newName = "\"" + newName + "\"";
+
+ setText(inputText.substr(0, startName) + newName
+ + inputText.substr(caretPos, inputText.length()
+ - caretPos));
+
+ setCaretPosition(caretPos - name.length() + newName.length());
+ }
+ }
+}
+
void TextField::handlePaste()
{
std::string text = getText();