X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Ftext-controller-text-updater.cpp;h=caf44f7d3ba63b08152614727ea399579905a55e;hp=6fb472ecc9280e1e4e371fffa92763454e994c14;hb=0c862d5823332bc2161c64f901395f2390c82fd2;hpb=5bc22f12881a16d7c5143c5e220fc0075aef6061 diff --git a/dali-toolkit/internal/text/text-controller-text-updater.cpp b/dali-toolkit/internal/text/text-controller-text-updater.cpp index 6fb472e..caf44f7 100644 --- a/dali-toolkit/internal/text/text-controller-text-updater.cpp +++ b/dali-toolkit/internal/text/text-controller-text-updater.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ #include #include #include +#include #include namespace @@ -55,7 +56,7 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string& ResetText(controller); // Remove the style. - controller.ClearStyleData(); + impl.ClearStyleData(); CharacterIndex lastCursorIndex = 0u; @@ -69,6 +70,11 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string& (EventData::EDITING_WITH_GRAB_HANDLE == eventData->mState) || (EventData::EDITING_WITH_PASTE_POPUP == eventData->mState)) { + if((impl.mSelectableControlInterface != nullptr) && (EventData::SELECTING == eventData->mState)) + { + impl.mSelectableControlInterface->SelectionChanged(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition, eventData->mPrimaryCursorPosition, eventData->mPrimaryCursorPosition); + } + impl.ChangeState(EventData::EDITING); } } @@ -81,7 +87,11 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string& MarkupProcessData markupProcessData(logicalModel->mColorRuns, logicalModel->mFontDescriptionRuns, - logicalModel->mEmbeddedItems); + logicalModel->mEmbeddedItems, + logicalModel->mAnchors, + logicalModel->mUnderlinedCharacterRuns, + logicalModel->mBackgroundColorRuns, + logicalModel->mStrikethroughCharacterRuns); Length textSize = 0u; const uint8_t* utf8 = NULL; @@ -133,14 +143,16 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string& } else { - controller.ShowPlaceholderText(); + PlaceholderHandler::ShowPlaceholderText(impl); } + unsigned int oldCursorPos = (nullptr != eventData ? eventData->mPrimaryCursorPosition : 0); + // Resets the cursor position. controller.ResetCursorPosition(lastCursorIndex); // Scrolls the text to make the cursor visible. - controller.ResetScrollPosition(); + impl.ResetScrollPosition(); impl.RequestRelayout(); @@ -153,6 +165,7 @@ void Controller::TextUpdater::SetText(Controller& controller, const std::string& // Do this last since it provides callbacks into application code. if(NULL != impl.mEditableControlInterface) { + impl.mEditableControlInterface->CursorPositionChanged(oldCursorPos, lastCursorIndex); impl.mEditableControlInterface->TextChanged(true); } } @@ -169,9 +182,10 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri return; } - bool removedPrevious = false; - bool removedSelected = false; - bool maxLengthReached = false; + bool removedPrevious = false; + bool removedSelected = false; + bool maxLengthReached = false; + unsigned int oldCursorPos = eventData->mPrimaryCursorPosition; DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Controller::InsertText %p %s (%s) mPrimaryCursorPosition %d mPreEditFlag %d mPreEditStartPosition %d mPreEditLength %d\n", &controller, text.c_str(), (COMMIT == type ? "COMMIT" : "PRE_EDIT"), eventData->mPrimaryCursorPosition, eventData->mPreEditFlag, eventData->mPreEditStartPosition, eventData->mPreEditLength); @@ -253,7 +267,8 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri const Length numberOfCharactersInModel = logicalModel->mText.Count(); // Restrict new text to fit within Maximum characters setting. - Length maxSizeOfNewText = std::min((impl.mMaximumNumberOfCharacters - numberOfCharactersInModel), characterCount); + Length temp_length = (impl.mMaximumNumberOfCharacters > numberOfCharactersInModel ? impl.mMaximumNumberOfCharacters - numberOfCharactersInModel : 0); + Length maxSizeOfNewText = std::min(temp_length, characterCount); maxLengthReached = (characterCount > maxSizeOfNewText); // The cursor position. @@ -337,7 +352,7 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri if(addFontSizeRun) { - fontDescriptionRun.size = static_cast(inputStyle.size * impl.mFontSizeScale * 64.f); + fontDescriptionRun.size = static_cast(inputStyle.size * impl.GetFontSizeScale() * 64.f); fontDescriptionRun.sizeDefined = true; } @@ -377,6 +392,11 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri textUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText; } + if(impl.mMarkupProcessorEnabled) + { + InsertTextAnchor(controller, maxSizeOfNewText, cursorIndex); + } + // Update the cursor index. cursorIndex += maxSizeOfNewText; @@ -387,7 +407,7 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri impl.IsPlaceholderAvailable()) { // Show place-holder if empty after removing the pre-edit text - controller.ShowPlaceholderText(); + PlaceholderHandler::ShowPlaceholderText(impl); eventData->mUpdateCursorPosition = true; impl.ClearPreEditFlag(); } @@ -409,6 +429,11 @@ void Controller::TextUpdater::InsertText(Controller& controller, const std::stri } } + if(nullptr != impl.mEditableControlInterface) + { + impl.mEditableControlInterface->CursorPositionChanged(oldCursorPos, eventData->mPrimaryCursorPosition); + } + if(maxLengthReached) { DALI_LOG_INFO(gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", logicalModel->mText.Count()); @@ -461,8 +486,8 @@ bool Controller::TextUpdater::RemoveText( if(!impl.IsShowingPlaceholderText()) { // Delete at current cursor position - Vector& currentText = logicalModel->mText; - CharacterIndex& oldCursorIndex = eventData->mPrimaryCursorPosition; + Vector& currentText = logicalModel->mText; + CharacterIndex& previousCursorIndex = eventData->mPrimaryCursorPosition; CharacterIndex cursorIndex = 0; @@ -547,8 +572,18 @@ bool Controller::TextUpdater::RemoveText( currentText.Erase(first, last); + if(impl.mMarkupProcessorEnabled) + { + RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex); + } + + if(nullptr != impl.mEditableControlInterface) + { + impl.mEditableControlInterface->CursorPositionChanged(previousCursorIndex, cursorIndex); + } + // Cursor position retreat - oldCursorIndex = cursorIndex; + previousCursorIndex = cursorIndex; eventData->mScrollAfterDelete = true; @@ -574,12 +609,30 @@ bool Controller::TextUpdater::RemoveSelectedText(Controller& controller) if(EventData::SELECTING == impl.mEventData->mState) { std::string removedString; + uint32_t oldSelStart = impl.mEventData->mLeftSelectionPosition; + uint32_t oldSelEnd = impl.mEventData->mRightSelectionPosition; + impl.RetrieveSelection(removedString, true); if(!removedString.empty()) { textRemoved = true; impl.ChangeState(EventData::EDITING); + + if(impl.mMarkupProcessorEnabled) + { + int cursorOffset = -1; + int numberOfCharacters = removedString.length(); + CharacterIndex& cursorIndex = impl.mEventData->mPrimaryCursorPosition; + CharacterIndex previousCursorIndex = cursorIndex + numberOfCharacters; + + RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex); + } + + if(impl.mSelectableControlInterface != nullptr) + { + impl.mSelectableControlInterface->SelectionChanged(oldSelStart, oldSelEnd, impl.mEventData->mPrimaryCursorPosition, impl.mEventData->mPrimaryCursorPosition); + } } } @@ -597,6 +650,9 @@ void Controller::TextUpdater::ResetText(Controller& controller) // Reset the embedded images buffer. logicalModel->ClearEmbeddedImages(); + // Reset the anchors buffer. + logicalModel->ClearAnchors(); + // We have cleared everything including the placeholder-text impl.PlaceholderCleared(); @@ -617,6 +673,128 @@ void Controller::TextUpdater::ResetText(Controller& controller) impl.mOperationsPending = ALL_OPERATIONS; } +void Controller::TextUpdater::InsertTextAnchor(Controller& controller, int numberOfCharacters, CharacterIndex previousCursorIndex) +{ + Controller::Impl& impl = *controller.mImpl; + ModelPtr& model = impl.mModel; + LogicalModelPtr& logicalModel = model->mLogicalModel; + + for(auto& anchor : logicalModel->mAnchors) + { + if(anchor.endIndex < previousCursorIndex) // [anchor] CUR + { + continue; + } + if(anchor.startIndex < previousCursorIndex) // [anCURr] + { + anchor.endIndex += numberOfCharacters; + } + else // CUR [anchor] + { + anchor.startIndex += numberOfCharacters; + anchor.endIndex += numberOfCharacters; + } + DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::InsertTextAnchor[%p] Anchor[%s] start[%d] end[%d]\n", &controller, anchor.href, anchor.startIndex, anchor.endIndex); + } +} + +void Controller::TextUpdater::RemoveTextAnchor(Controller& controller, int cursorOffset, int numberOfCharacters, CharacterIndex previousCursorIndex) +{ + Controller::Impl& impl = *controller.mImpl; + ModelPtr& model = impl.mModel; + LogicalModelPtr& logicalModel = model->mLogicalModel; + Vector::Iterator it = logicalModel->mAnchors.Begin(); + + while(it != logicalModel->mAnchors.End()) + { + Anchor& anchor = *it; + + if(anchor.endIndex <= previousCursorIndex && cursorOffset == 0) // [anchor] CUR >> + { + // Nothing happens. + } + else if(anchor.endIndex <= previousCursorIndex && cursorOffset == -1) // [anchor] << CUR + { + int endIndex = anchor.endIndex; + int offset = previousCursorIndex - endIndex; + int index = endIndex - (numberOfCharacters - offset); + + if(index < endIndex) + { + endIndex = index; + } + + if((int)anchor.startIndex >= endIndex) + { + if(anchor.href) + { + delete[] anchor.href; + } + it = logicalModel->mAnchors.Erase(it); + continue; + } + else + { + anchor.endIndex = endIndex; + } + } + else if(anchor.startIndex >= previousCursorIndex && cursorOffset == -1) // << CUR [anchor] + { + anchor.startIndex -= numberOfCharacters; + anchor.endIndex -= numberOfCharacters; + } + else if(anchor.startIndex >= previousCursorIndex && cursorOffset == 0) // CUR >> [anchor] + { + int startIndex = anchor.startIndex; + int endIndex = anchor.endIndex; + int index = previousCursorIndex + numberOfCharacters - 1; + + if(startIndex > index) + { + anchor.startIndex -= numberOfCharacters; + anchor.endIndex -= numberOfCharacters; + } + else if(endIndex > index + 1) + { + anchor.endIndex -= numberOfCharacters; + } + else + { + if(anchor.href) + { + delete[] anchor.href; + } + it = logicalModel->mAnchors.Erase(it); + continue; + } + } + else if(cursorOffset == -1) // [<< CUR] + { + int startIndex = anchor.startIndex; + int index = previousCursorIndex - numberOfCharacters; + + if(startIndex >= index) + { + anchor.startIndex = index; + } + anchor.endIndex -= numberOfCharacters; + } + else if(cursorOffset == 0) // [CUR >>] + { + anchor.endIndex -= numberOfCharacters; + } + else + { + // When this condition is reached, someting is wrong. + DALI_LOG_ERROR("Controller::RemoveTextAnchor[%p] Invaild state cursorOffset[%d]\n", &controller, cursorOffset); + } + + DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveTextAnchor[%p] Anchor[%s] start[%d] end[%d]\n", &controller, anchor.href, anchor.startIndex, anchor.endIndex); + + it++; + } +} + } // namespace Text } // namespace Toolkit