aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden
diff options
context:
space:
mode:
authorthickbrick2010-09-24 02:29:58 +0200
committerthickbrick2010-09-25 13:29:36 +0200
commitf6f5603b946a56059987071b7151a1c2e64c45d5 (patch)
treeb56773f75435cc58db0486b86e8617aa538f7847 /linden
parentFix LLTextEditor context menu translator returning "()" when nothing is selec... (diff)
downloadmeta-impy-f6f5603b946a56059987071b7151a1c2e64c45d5.zip
meta-impy-f6f5603b946a56059987071b7151a1c2e64c45d5.tar.gz
meta-impy-f6f5603b946a56059987071b7151a1c2e64c45d5.tar.bz2
meta-impy-f6f5603b946a56059987071b7151a1c2e64c45d5.tar.xz
Fix LLLineEditor context menu translator returning "()" when nothing is selected.
New behavior is to translate the word under the mouse if nothing is selected.
Diffstat (limited to 'linden')
-rw-r--r--linden/indra/llui/lllineeditor.cpp90
-rw-r--r--linden/indra/llui/lllineeditor.h3
2 files changed, 81 insertions, 12 deletions
diff --git a/linden/indra/llui/lllineeditor.cpp b/linden/indra/llui/lllineeditor.cpp
index 66ef4c6..27a1d50 100644
--- a/linden/indra/llui/lllineeditor.cpp
+++ b/linden/indra/llui/lllineeditor.cpp
@@ -96,23 +96,23 @@ static LLRegisterWidget<LLLineEditor> r1("line_editor");
96class LineChatTranslationReceiver : public LLTranslate::TranslationReceiver 96class LineChatTranslationReceiver : public LLTranslate::TranslationReceiver
97{ 97{
98public : 98public :
99 LineChatTranslationReceiver(const std::string &toLang, LLLineEditor* line): LLTranslate::TranslationReceiver("", toLang), 99 LineChatTranslationReceiver(const std::string &toLang, LLLineEditor* line, const S32 start, const S32 len):
100 m_line(line) 100 LLTranslate::TranslationReceiver("", toLang),
101 m_line(line),
102 m_position(start),
103 m_origLength(len)
101 { 104 {
102 } 105 }
103 106
104 static boost::intrusive_ptr<LineChatTranslationReceiver> build(const std::string &toLang,LLLineEditor* line) 107 static boost::intrusive_ptr<LineChatTranslationReceiver> build(const std::string &toLang,LLLineEditor* line, const S32 start, const S32 len)
105 { 108 {
106 return boost::intrusive_ptr<LineChatTranslationReceiver>(new LineChatTranslationReceiver(toLang,line)); 109 return boost::intrusive_ptr<LineChatTranslationReceiver>(new LineChatTranslationReceiver(toLang, line, start, len));
107 } 110 }
108 111
109protected: 112protected:
110 void handleResponse(const std::string &translation, const std::string &detectedLanguage) 113 void handleResponse(const std::string &translation, const std::string &detectedLanguage)
111 { 114 {
112 static BOOL* rep = rebind_llcontrol<BOOL>("EmeraldTranslateReplace", &gSavedSettings, true); 115 m_line->translationReplace(translation, m_position, m_origLength);
113 if(*rep)
114 m_line->deleteSelection();
115 m_line->insert(((*rep)?"":" (") + translation + ((*rep)?"":")"),m_line->getCursor());
116 } 116 }
117 void handleFailure() 117 void handleFailure()
118 { 118 {
@@ -120,6 +120,8 @@ protected:
120 } 120 }
121private: 121private:
122 LLLineEditor* m_line; 122 LLLineEditor* m_line;
123 S32 m_position;
124 S32 m_origLength;
123}; 125};
124 126
125LLLineEditor::LLLineEditor(const std::string& name, const LLRect& rect, 127LLLineEditor::LLLineEditor(const std::string& name, const LLRect& rect,
@@ -151,6 +153,7 @@ LLLineEditor::LLLineEditor(const std::string& name, const LLRect& rect,
151 mLastSelectionY(-1), 153 mLastSelectionY(-1),
152 mLastSelectionStart(-1), 154 mLastSelectionStart(-1),
153 mLastSelectionEnd(-1), 155 mLastSelectionEnd(-1),
156 mLastContextMenuX(-1),
154 mPrevalidateFunc( prevalidate_func ), 157 mPrevalidateFunc( prevalidate_func ),
155 mCursorColor( LLUI::sColorsGroup->getColor( "TextCursorColor" ) ), 158 mCursorColor( LLUI::sColorsGroup->getColor( "TextCursorColor" ) ),
156 mFgColor( LLUI::sColorsGroup->getColor( "TextFgColor" ) ), 159 mFgColor( LLUI::sColorsGroup->getColor( "TextFgColor" ) ),
@@ -539,16 +542,36 @@ void LLLineEditor::spell_correct(void* data)
539 542
540 } 543 }
541} 544}
545
542void LLLineEditor::translateText(void * data) 546void LLLineEditor::translateText(void * data)
543{ 547{
544 SpellMenuBind* t = (SpellMenuBind*)data; 548 SpellMenuBind* t = (SpellMenuBind*)data;
545 LLLineEditor* line = t->origin; 549 LLLineEditor* line = t->origin;
546 const std::string &toLang = t->word;//LLTranslate::getTranslateLanguage(); 550 const std::string &toLang = t->word;//LLTranslate::getTranslateLanguage();
547 LLHTTPClient::ResponderPtr result = LineChatTranslationReceiver::build(toLang,line); 551
548 S32 left_pos = llmin( line->mSelectionStart, line->mSelectionEnd ); 552 bool has_text = false;
549 S32 length = abs( line->mSelectionStart - line->mSelectionEnd ); 553 S32 start, length;
550 LLTranslate::translateMessage(result,"", toLang, line->mText.getString().substr(left_pos, length)); 554 if (line->hasSelection())
555 {
556 // translate selection
557 start = llmin(line->mSelectionStart, line->mSelectionEnd);
558 length = abs(line->mSelectionEnd - line->mSelectionStart);
559 has_text = length > 0;
560 }
561 else
562 {
563 // translate one word at click position
564 S32 at = line->calculateCursorFromMouse(line->mLastContextMenuX);
565 has_text = line->getWordBoundriesAt(at, &start, &length);
566 }
567
568 if (has_text)
569 {
570 LLHTTPClient::ResponderPtr result = LineChatTranslationReceiver::build(toLang, line, start, length);
571 LLTranslate::translateMessage(result,"", toLang, line->getText().substr(start, length));
572 }
551} 573}
574
552void LLLineEditor::spell_show(void * data) 575void LLLineEditor::spell_show(void * data)
553{ 576{
554 SpellMenuBind* tempBind = (SpellMenuBind*)data; 577 SpellMenuBind* tempBind = (SpellMenuBind*)data;
@@ -836,6 +859,7 @@ BOOL LLLineEditor::handleRightMouseDown( S32 x, S32 y, MASK mask )
836 menu->append(suggMenuItem); 859 menu->append(suggMenuItem);
837 } 860 }
838 861
862 mLastContextMenuX = x;
839 menu->buildDrawLabels(); 863 menu->buildDrawLabels();
840 menu->updateParent(LLMenuGL::sMenuContainer); 864 menu->updateParent(LLMenuGL::sMenuContainer);
841 LLMenuGL::showPopup(this, menu, x, y); 865 LLMenuGL::showPopup(this, menu, x, y);
@@ -1152,6 +1176,26 @@ S32 LLLineEditor::nextWordPos(S32 cursorPos) const
1152 return cursorPos; 1176 return cursorPos;
1153} 1177}
1154 1178
1179BOOL LLLineEditor::getWordBoundriesAt(const S32 at, S32* word_begin, S32* word_length) const
1180{
1181 const LLWString& wtext = mText.getWString();
1182 S32 pos = at;
1183 if (LLTextEditor::isPartOfWord(wtext[pos]))
1184 {
1185 while ( (pos > 0) && LLTextEditor::isPartOfWord(wtext[pos - 1]) )
1186 {
1187 pos--;
1188 }
1189 *word_begin = pos;
1190 while ( (pos < getLength()) && LLTextEditor::isPartOfWord(wtext[pos]) )
1191 {
1192 pos++;
1193 }
1194 *word_length = pos - *word_begin;
1195 return TRUE;
1196 }
1197 return FALSE;
1198}
1155 1199
1156BOOL LLLineEditor::handleSelectionKey(KEY key, MASK mask) 1200BOOL LLLineEditor::handleSelectionKey(KEY key, MASK mask)
1157{ 1201{
@@ -1315,6 +1359,28 @@ void LLLineEditor::spellReplace(SpellMenuBind* spellData)
1315 1359
1316 1360
1317} 1361}
1362
1363void LLLineEditor::translationReplace(const std::string &translation, const S32 orig_start, const S32 orig_length)
1364{
1365 //*TODO: should probably check if the content was modified since the http query
1366 // was made, so we don't insert text in the wrong place.
1367 BOOL replace = gSavedSettings.getBOOL("EmeraldTranslateReplace");
1368 std::string text = replace ? translation : " (" + translation + ")";
1369 S32 pos = replace ? orig_start : orig_start + orig_length;
1370 if (replace)
1371 {
1372 mText.erase(orig_start, orig_length);
1373 }
1374 insert(text, pos);
1375
1376 if (hasSelection())
1377 {
1378 mSelectionStart = pos;
1379 mSelectionEnd = pos + text.length();
1380 }
1381 setCursor(pos + text.length());
1382}
1383
1318void LLLineEditor::insert(std::string what, S32 wher) 1384void LLLineEditor::insert(std::string what, S32 wher)
1319{ 1385{
1320 LLLineEditorRollback rollback(this); 1386 LLLineEditorRollback rollback(this);
diff --git a/linden/indra/llui/lllineeditor.h b/linden/indra/llui/lllineeditor.h
index 147b232..af37f62 100644
--- a/linden/indra/llui/lllineeditor.h
+++ b/linden/indra/llui/lllineeditor.h
@@ -107,6 +107,7 @@ public:
107 }; 107 };
108 108
109 virtual void spellReplace(SpellMenuBind* spellData); 109 virtual void spellReplace(SpellMenuBind* spellData);
110 virtual void translationReplace(const std::string &translation, const S32 orig_start, const S32 orig_length);
110 virtual void insert(std::string what,S32 wher); 111 virtual void insert(std::string what,S32 wher);
111 112
112 // LLEditMenuHandler overrides 113 // LLEditMenuHandler overrides
@@ -215,6 +216,7 @@ public:
215 // get the cursor position of the beginning/end of the prev/next word in the text 216 // get the cursor position of the beginning/end of the prev/next word in the text
216 S32 prevWordPos(S32 cursorPos) const; 217 S32 prevWordPos(S32 cursorPos) const;
217 S32 nextWordPos(S32 cursorPos) const; 218 S32 nextWordPos(S32 cursorPos) const;
219 BOOL getWordBoundriesAt(const S32 at, S32* word_begin, S32* word_length) const;
218 220
219 BOOL hasSelection() const { return (mSelectionStart != mSelectionEnd); } 221 BOOL hasSelection() const { return (mSelectionStart != mSelectionEnd); }
220 void startSelection(); 222 void startSelection();
@@ -298,6 +300,7 @@ protected:
298 LLFrameTimer mSpellTimer; 300 LLFrameTimer mSpellTimer;
299 //to keep track of what we have to remove before showing menu 301 //to keep track of what we have to remove before showing menu
300 std::vector<SpellMenuBind* > suggestionMenuItems; 302 std::vector<SpellMenuBind* > suggestionMenuItems;
303 S32 mLastContextMenuX;
301 304
302 // line history support: 305 // line history support:
303 BOOL mHaveHistory; // flag for enabled line history 306 BOOL mHaveHistory; // flag for enabled line history