From 1cc154166fe504e17735669b7b9e366f93e34d64 Mon Sep 17 00:00:00 2001
From: Tank_Master <none@none>
Date: Tue, 20 Dec 2011 22:17:20 -0800
Subject: STORM-1738 - Add autocorrect functionality Ported with owner
 permission from Firestorm, inital work done by LordGregGreg Back

---
 indra/llui/lllineeditor.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++
 indra/llui/lllineeditor.h   |  1 +
 2 files changed, 58 insertions(+)

(limited to 'indra/llui')

diff --git a/indra/llui/lllineeditor.cpp b/indra/llui/lllineeditor.cpp
index 06dfc90d83..cee84cc53f 100644
--- a/indra/llui/lllineeditor.cpp
+++ b/indra/llui/lllineeditor.cpp
@@ -51,6 +51,8 @@
 #include "lluictrlfactory.h"
 #include "llclipboard.h"
 #include "llmenugl.h"
+#include "../newview/llautocorrect.h"
+#include "../newview/llviewercontrol.h"
 
 //
 // Imported globals
@@ -194,7 +196,60 @@ LLLineEditor::~LLLineEditor()
 	// calls onCommit() while LLLineEditor still valid
 	gFocusMgr.releaseFocusIfNeeded( this );
 }
+void LLLineEditor::autoCorrectText()
+{
+	static LLCachedControl<bool> doAnything(gSavedSettings, "EnableAutoCorrect");
+	if( (!mReadOnly) && (doAnything))// && (isDirty()))
+	{
+		S32 wordStart = 0;
+		S32 wordEnd = mCursorPos-1;
+		//llinfos <<"Checking Word, Cursor is at "<<mCursorPos<<" and text is "<<mText.getString().c_str()<<llendl;
+
+		if(wordEnd < 1)
+			return;
+
+		LLWString text = mText.getWString();
+
+		if(text.size()<1)
+			return;
+
+		if( LLWStringUtil::isPartOfWord( text[wordEnd] ))
+			return;//we only check on word breaks
+
+		wordEnd--;
+
+		if( LLWStringUtil::isPartOfWord( text[wordEnd] ) )
+		{
+			while ((wordEnd > 0) && (' '!=text[wordEnd-1]))
+			{
+				wordEnd--;
+			}
+
+			wordStart=wordEnd;		
+
+			while ((wordEnd < (S32)text.length()) && (' '!=text[wordEnd] ) )
+			{
+				wordEnd++;
+			}
+
+			std::string strLastWord = std::string(text.begin(), text.end());
+			std::string lastTypedWord = strLastWord.substr( wordStart, wordEnd-wordStart);
+			std::string correctedWord( AutoCorrect::getInstance()->replaceWord(lastTypedWord));
 
+			if(correctedWord!=lastTypedWord)
+			{
+				LLWString strNew = utf8str_to_wstring( correctedWord );
+				LLWString strOld = utf8str_to_wstring( lastTypedWord );
+				int nDiff = strNew.size() - strOld.size();
+
+				//int wordStart = regText.find(lastTypedWord);
+				text.replace(wordStart,lastTypedWord.length(),strNew);
+				mText = wstring_to_utf8str(text);
+				mCursorPos+=nDiff;
+			}
+		}
+	}
+}
 
 void LLLineEditor::onFocusReceived()
 {
@@ -866,6 +921,8 @@ void LLLineEditor::addChar(const llwchar uni_char)
 		LLUI::reportBadKeystroke();
 	}
 
+	autoCorrectText();
+
 	getWindow()->hideCursorUntilMouseMove();
 }
 
diff --git a/indra/llui/lllineeditor.h b/indra/llui/lllineeditor.h
index 2518dbe3c7..8dcc801b62 100644
--- a/indra/llui/lllineeditor.h
+++ b/indra/llui/lllineeditor.h
@@ -169,6 +169,7 @@ public:
 	virtual BOOL	setTextArg( const std::string& key, const LLStringExplicit& text );
 	virtual BOOL	setLabelArg( const std::string& key, const LLStringExplicit& text );
 
+	void autoCorrectText();
 	void			setLabel(const LLStringExplicit &new_label) { mLabel = new_label; }
 	const std::string& 	getLabel()	{ return mLabel.getString(); }
 
-- 
cgit v1.2.3