123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /**
- * @file hbfloatertextinput.cpp
- * @brief HBFloaterTextInput class implementation
- *
- * $LicenseInfo:firstyear=2012&license=viewergpl$
- *
- * Copyright (c) 2012-2023, Henri Beauchamp.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- #include "llviewerprecompiledheaders.h"
- #include "hbfloatertextinput.h"
- #include "lllineeditor.h"
- #include "lltexteditor.h"
- #include "lluictrlfactory.h"
- #include "llagent.h"
- #include "llchatbar.h"
- #include "llviewercontrol.h"
- //static
- std::map<LLLineEditor*, HBFloaterTextInput*> HBFloaterTextInput::sInstancesMap;
- HBFloaterTextInput::HBFloaterTextInput(LLLineEditor* input_line,
- const std::string& dest,
- void (*typing_callback)(void*, bool),
- void* callback_data)
- : LLFloater("text input"),
- mCallerLineEditor(input_line),
- mIsChatInput(dest.empty()),
- mTypingCallback(typing_callback),
- mTypingCallbackData(callback_data),
- mMustClose(false)
- {
- LLUICtrlFactory::getInstance()->buildFloater(this,
- "floater_text_input.xml");
- std::string title;
- if (mIsChatInput)
- {
- title = getString("chat");
- mRectControl = "ChatInputEditorRect";
- }
- else
- {
- LLStringUtil::format_map_t arg;
- arg["[NAME]"] = dest;
- title = getString("im", arg);
- mRectControl = "IMInputEditorRect";
- }
- setTitle(title);
- LLRect rect = gSavedSettings.getRect(mRectControl.c_str());
- reshape(rect.getWidth(), rect.getHeight());
- setRect(rect);
- sInstancesMap.emplace(input_line, this);
- }
- //virtual
- HBFloaterTextInput::~HBFloaterTextInput()
- {
- if (mCallerLineEditor)
- {
- std::map<LLLineEditor*, HBFloaterTextInput*>::iterator it;
- it = sInstancesMap.find(mCallerLineEditor);
- if (it != sInstancesMap.end())
- {
- sInstancesMap.erase(it);
- }
- else
- {
- llwarns << "Could not find the floater in the instances map"
- << llendl;
- }
- mCallerLineEditor->setText(mTextEditor->getText());
- mCallerLineEditor->setCursorToEnd();
- mCallerLineEditor->setFocus(true);
- if (mIsChatInput)
- {
- gAgent.stopTyping();
- }
- else if (mTypingCallback)
- {
- mTypingCallback(mTypingCallbackData, false);
- }
- }
- gSavedSettings.setRect(mRectControl.c_str(), getRect());
- }
- //virtual
- bool HBFloaterTextInput::postBuild()
- {
- mTextEditor = getChild<LLTextEditor>("text");
- mTextEditor->setFocusLostCallback(onTextEditorFocusLost, this);
- mTextEditor->setKeystrokeCallback(onTextEditorKeystroke, this);
- mTextEditor->setOnHandleKeyCallback(onHandleKeyCallback, this);
- if (mCallerLineEditor)
- {
- mTextEditor->setText(mCallerLineEditor->getText());
- mTextEditor->setCursorPos(mCallerLineEditor->getCursor());
- }
- mTextEditor->setFocus(true);
- mTextEditor->setCustomMenuType("text_input");
- if (mIsChatInput && gSavedSettings.getBool("TabAutoCompleteName"))
- {
- mTextEditor->setTabsToNextField(false);
- }
- return true;
- }
- //virtual
- void HBFloaterTextInput::draw()
- {
- if (mMustClose)
- {
- close();
- }
- else
- {
- LLFloater::draw();
- }
- }
- //static
- HBFloaterTextInput* HBFloaterTextInput::show(LLLineEditor* input_line,
- const std::string& dest,
- void (*typing_callback)(void*, bool),
- void* callback_data)
- {
- HBFloaterTextInput* instance = NULL;
- std::map<LLLineEditor*, HBFloaterTextInput*>::iterator it;
- it = sInstancesMap.find(input_line);
- if (it != sInstancesMap.end())
- {
- instance = it->second;
- instance->setFocus(true);
- instance->open();
- }
- else
- {
- instance = new HBFloaterTextInput(input_line, dest, typing_callback,
- callback_data);
- }
- return instance;
- }
- // This method *must* be invoked by the caller of show() when it gets
- // destroyed. It ensures the text input floater gets destroyed in its turn
- // and does not attempt to call back methods pertaining to the destroyed
- // caller object or its children.
- //static
- void HBFloaterTextInput::abort(LLLineEditor* input_line)
- {
- std::map<LLLineEditor*, HBFloaterTextInput*>::iterator it;
- it = sInstancesMap.find(input_line);
- if (it != sInstancesMap.end())
- {
- HBFloaterTextInput* self = it->second;
- self->mCallerLineEditor = NULL;
- self->close();
- sInstancesMap.erase(it);
- }
- }
- //static
- bool HBFloaterTextInput::hasFloaterFor(LLLineEditor* input_line)
- {
- return sInstancesMap.find(input_line) != sInstancesMap.end();
- }
- //static
- void HBFloaterTextInput::onTextEditorFocusLost(LLFocusableElement* caller,
- void* userdata)
- {
- HBFloaterTextInput* self = (HBFloaterTextInput*)userdata;
- if (self)
- {
- if (self->mIsChatInput)
- {
- gAgent.stopTyping();
- }
- else if (self->mTypingCallback)
- {
- self->mTypingCallback(self->mTypingCallbackData, false);
- }
- }
- }
- //static
- void HBFloaterTextInput::onTextEditorKeystroke(LLTextEditor* caller,
- void* userdata)
- {
- HBFloaterTextInput* self = (HBFloaterTextInput*)userdata;
- if (self)
- {
- if (self->mIsChatInput)
- {
- std::string text = self->mTextEditor->getText();
- if (text.length() > 0 && text[0] != '/')
- {
- gAgent.startTyping();
- }
- }
- else if (self->mTypingCallback)
- {
- self->mTypingCallback(self->mTypingCallbackData, true);
- }
- }
- }
- //static
- bool HBFloaterTextInput::onHandleKeyCallback(KEY key, MASK mask,
- LLTextEditor* caller,
- void* userdata)
- {
- bool handled = false;
- HBFloaterTextInput* self = (HBFloaterTextInput*)userdata;
- if (self)
- {
- if (key == KEY_RETURN)
- {
- if (mask == MASK_NONE)
- {
- // Flag for closing. We cannot close now because then we would
- // destroy the object to which pertains the method that called
- // us...
- self->mMustClose = true;
- handled = true;
- }
- else if (mask == (MASK_SHIFT | MASK_CONTROL))
- {
- S32 cursor = self->mTextEditor->getCursorPos();
- std::string text = self->mTextEditor->getText();
- // For some reason, the event is triggered twice: let's insert
- // only one newline character.
- if (cursor == 0 || text[cursor - 1] != '\n')
- {
- text = text.insert(cursor, "\n");
- self->mTextEditor->setText(text);
- self->mTextEditor->setCursorPos(cursor + 1);
- }
- handled = true;
- }
- }
- else if (KEY_TAB == key && mask == MASK_NONE)
- {
- std::string text = self->mTextEditor->getText();
- S32 word_start = 0;
- S32 word_len = 0;
- S32 cursor = self->mTextEditor->getCursorPos();
- S32 pos = cursor;
- if (pos > 0 && pos != (S32)text.length() - 1)
- {
- // Make sure the word will be found if the cursor is at its
- // end
- --pos;
- }
- if (self->mTextEditor->getWordBoundriesAt(pos, &word_start,
- &word_len))
- {
- std::string word = text.substr(word_start, word_len);
- std::string suggestion = LLChatBar::getMatchingAvatarName(word);
- if (suggestion != word)
- {
- text = text.replace(word_start, word_len, suggestion);
- self->mTextEditor->setText(text);
- S32 word_end = cursor + suggestion.length() -
- word.length();
- if (gSavedSettings.getBool("SelectAutoCompletedPart"))
- {
- self->mTextEditor->setSelection(cursor, word_end);
- }
- else
- {
- self->mTextEditor->setCursorPos(word_end);
- }
- }
- }
- handled = true;
- }
- }
- return handled;
- }
|