2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/controller/text-controller-text-updater.h>
22 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/internal/text/character-set-conversion.h>
27 #include <dali-toolkit/internal/text/characters-helper-functions.h>
28 #include <dali-toolkit/internal/text/emoji-helper.h>
29 #include <dali-toolkit/internal/text/markup-processor/markup-processor.h>
30 #include <dali-toolkit/internal/text/controller/text-controller-impl.h>
31 #include <dali-toolkit/internal/text/controller/text-controller-placeholder-handler.h>
32 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
36 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
48 void Controller::TextUpdater::SetText(Controller& controller, const std::string& text)
50 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Controller::SetText\n");
52 Controller::Impl& impl = *controller.mImpl;
54 // Reset keyboard as text changed
55 impl.ResetInputMethodContext();
57 // Remove the previously set text and style.
58 ResetText(controller);
61 impl.ClearStyleData();
63 CharacterIndex lastCursorIndex = 0u;
65 EventData*& eventData = impl.mEventData;
67 if(nullptr != eventData)
69 // If popup shown then hide it by switching to Editing state
70 if((EventData::SELECTING == eventData->mState) ||
71 (EventData::EDITING_WITH_POPUP == eventData->mState) ||
72 (EventData::EDITING_WITH_GRAB_HANDLE == eventData->mState) ||
73 (EventData::EDITING_WITH_PASTE_POPUP == eventData->mState))
75 if((impl.mSelectableControlInterface != nullptr) && (EventData::SELECTING == eventData->mState))
77 impl.mSelectableControlInterface->SelectionChanged(eventData->mLeftSelectionPosition, eventData->mRightSelectionPosition, eventData->mPrimaryCursorPosition, eventData->mPrimaryCursorPosition);
80 impl.ChangeState(EventData::EDITING);
86 ModelPtr& model = impl.mModel;
87 LogicalModelPtr& logicalModel = model->mLogicalModel;
88 model->mVisualModel->SetTextColor(impl.mTextColor);
90 MarkupProcessData markupProcessData(logicalModel->mColorRuns,
91 logicalModel->mFontDescriptionRuns,
92 logicalModel->mEmbeddedItems,
93 logicalModel->mAnchors,
94 logicalModel->mUnderlinedCharacterRuns,
95 logicalModel->mBackgroundColorRuns,
96 logicalModel->mStrikethroughCharacterRuns,
97 logicalModel->mBoundedParagraphRuns,
98 logicalModel->mCharacterSpacingCharacterRuns);
100 Length textSize = 0u;
101 const uint8_t* utf8 = NULL;
102 if(impl.mMarkupProcessorEnabled)
104 ProcessMarkupString(text, markupProcessData);
105 textSize = markupProcessData.markupProcessedText.size();
107 // This is a bit horrible but std::string returns a (signed) char*
108 utf8 = reinterpret_cast<const uint8_t*>(markupProcessData.markupProcessedText.c_str());
112 textSize = text.size();
114 // This is a bit horrible but std::string returns a (signed) char*
115 utf8 = reinterpret_cast<const uint8_t*>(text.c_str());
118 // Convert text into UTF-32
119 Vector<Character>& utf32Characters = logicalModel->mText;
120 utf32Characters.Resize(textSize);
122 // Transform a text array encoded in utf8 into an array encoded in utf32.
123 // It returns the actual number of characters.
124 Length characterCount = Utf8ToUtf32(utf8, textSize, utf32Characters.Begin());
125 utf32Characters.Resize(characterCount);
127 DALI_ASSERT_DEBUG(textSize >= characterCount && "Invalid UTF32 conversion length");
128 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", &controller, textSize, logicalModel->mText.Count());
130 // The characters to be added.
131 impl.mTextUpdateInfo.mNumberOfCharactersToAdd = logicalModel->mText.Count();
133 // To reset the cursor position
134 lastCursorIndex = characterCount;
136 // Update the rest of the model during size negotiation
137 impl.QueueModifyEvent(ModifyEvent::TEXT_REPLACED);
139 // The natural size needs to be re-calculated.
140 impl.mRecalculateNaturalSize = true;
142 // The text direction needs to be updated.
143 impl.mUpdateTextDirection = true;
145 // Apply modifications to the model
146 impl.mOperationsPending = ALL_OPERATIONS;
150 PlaceholderHandler::ShowPlaceholderText(impl);
153 unsigned int oldCursorPos = (nullptr != eventData ? eventData->mPrimaryCursorPosition : 0);
155 // Resets the cursor position.
156 controller.ResetCursorPosition(lastCursorIndex);
158 // Scrolls the text to make the cursor visible.
159 impl.ResetScrollPosition();
161 impl.RequestRelayout();
163 if(nullptr != eventData)
165 // Cancel previously queued events
166 eventData->mEventQueue.clear();
169 // Do this last since it provides callbacks into application code.
170 if(NULL != impl.mEditableControlInterface)
172 impl.mEditableControlInterface->CursorPositionChanged(oldCursorPos, lastCursorIndex);
173 impl.mEditableControlInterface->TextChanged(true);
177 void Controller::TextUpdater::InsertText(Controller& controller, const std::string& text, Controller::InsertType type)
179 Controller::Impl& impl = *controller.mImpl;
180 EventData*& eventData = impl.mEventData;
182 DALI_ASSERT_DEBUG(nullptr != eventData && "Unexpected InsertText")
184 if(NULL == eventData)
189 bool removedPrevious = false;
190 bool removedSelected = false;
191 bool maxLengthReached = false;
192 unsigned int oldCursorPos = eventData->mPrimaryCursorPosition;
194 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);
196 ModelPtr& model = impl.mModel;
197 LogicalModelPtr& logicalModel = model->mLogicalModel;
199 // TODO: At the moment the underline runs are only for pre-edit.
200 model->mVisualModel->mUnderlineRuns.Clear();
202 // Remove the previous InputMethodContext pre-edit.
203 if(eventData->mPreEditFlag && (0u != eventData->mPreEditLength))
205 removedPrevious = RemoveText(controller,
206 -static_cast<int>(eventData->mPrimaryCursorPosition - eventData->mPreEditStartPosition),
207 eventData->mPreEditLength,
208 DONT_UPDATE_INPUT_STYLE);
210 eventData->mPrimaryCursorPosition = eventData->mPreEditStartPosition;
211 eventData->mPreEditLength = 0u;
215 // Remove the previous Selection.
216 removedSelected = RemoveSelectedText(controller);
219 Vector<Character> utf32Characters;
220 Length characterCount = 0u;
224 std::string redefinedText = text;
226 if(controller.mImpl->mInputFilter != NULL)
228 bool accepted = false;
229 bool rejected = false;
231 accepted = impl.mInputFilter->Filter(Toolkit::InputFilter::Property::ACCEPTED, redefinedText);
232 rejected = impl.mInputFilter->Filter(Toolkit::InputFilter::Property::REJECTED, redefinedText);
236 // Signal emits when the string to be inserted is filtered by the accepted filter.
237 controller.mImpl->mEditableControlInterface->InputFiltered(Toolkit::InputFilter::Property::ACCEPTED);
241 // Signal emits when the string to be inserted is filtered by the rejected filter.
242 controller.mImpl->mEditableControlInterface->InputFiltered(Toolkit::InputFilter::Property::REJECTED);
246 // Convert text into UTF-32
247 utf32Characters.Resize(redefinedText.size());
249 // This is a bit horrible but std::string returns a (signed) char*
250 const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(redefinedText.c_str());
252 // Transform a text array encoded in utf8 into an array encoded in utf32.
253 // It returns the actual number of characters.
254 characterCount = Utf8ToUtf32(utf8, redefinedText.size(), utf32Characters.Begin());
255 utf32Characters.Resize(characterCount);
257 DALI_ASSERT_DEBUG(redefinedText.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length");
258 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", redefinedText.size(), utf32Characters.Count());
261 if(0u != utf32Characters.Count()) // Check if Utf8ToUtf32 conversion succeeded
263 // The placeholder text is no longer needed
264 if(impl.IsShowingPlaceholderText())
266 ResetText(controller);
269 impl.ChangeState(EventData::EDITING);
271 // Handle the InputMethodContext (predicitive text) state changes
274 // InputMethodContext is no longer handling key-events
275 impl.ClearPreEditFlag();
279 if(!eventData->mPreEditFlag)
281 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Entered PreEdit state\n");
283 // Record the start of the pre-edit text
284 eventData->mPreEditStartPosition = eventData->mPrimaryCursorPosition;
287 eventData->mPreEditLength = utf32Characters.Count();
288 eventData->mPreEditFlag = true;
290 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", eventData->mPreEditStartPosition, eventData->mPreEditLength);
293 const Length numberOfCharactersInModel = logicalModel->mText.Count();
295 // Restrict new text to fit within Maximum characters setting.
296 Length temp_length = (impl.mMaximumNumberOfCharacters > numberOfCharactersInModel ? impl.mMaximumNumberOfCharacters - numberOfCharactersInModel : 0);
297 Length maxSizeOfNewText = std::min(temp_length, characterCount);
298 maxLengthReached = (characterCount > maxSizeOfNewText);
300 // The cursor position.
301 CharacterIndex& cursorIndex = eventData->mPrimaryCursorPosition;
303 // Update the text's style.
305 // Updates the text style runs by adding characters.
306 logicalModel->UpdateTextStyleRuns(cursorIndex, maxSizeOfNewText);
308 // Get the character index from the cursor index.
309 const CharacterIndex styleIndex = (cursorIndex > 0u) ? cursorIndex - 1u : 0u;
311 // Retrieve the text's style for the given index.
313 impl.RetrieveDefaultInputStyle(style);
314 logicalModel->RetrieveStyle(styleIndex, style);
316 InputStyle& inputStyle = eventData->mInputStyle;
318 // Whether to add a new text color run.
319 const bool addColorRun = (style.textColor != inputStyle.textColor) && !inputStyle.isDefaultColor;
321 // Whether to add a new font run.
322 const bool addFontNameRun = (style.familyName != inputStyle.familyName) && inputStyle.isFamilyDefined;
323 const bool addFontWeightRun = (style.weight != inputStyle.weight) && inputStyle.isWeightDefined;
324 const bool addFontWidthRun = (style.width != inputStyle.width) && inputStyle.isWidthDefined;
325 const bool addFontSlantRun = (style.slant != inputStyle.slant) && inputStyle.isSlantDefined;
326 const bool addFontSizeRun = (style.size != inputStyle.size) && inputStyle.isSizeDefined;
331 const VectorBase::SizeType numberOfRuns = logicalModel->mColorRuns.Count();
332 logicalModel->mColorRuns.Resize(numberOfRuns + 1u);
334 ColorRun& colorRun = *(logicalModel->mColorRuns.Begin() + numberOfRuns);
335 colorRun.color = inputStyle.textColor;
336 colorRun.characterRun.characterIndex = cursorIndex;
337 colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
346 const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
347 logicalModel->mFontDescriptionRuns.Resize(numberOfRuns + 1u);
349 FontDescriptionRun& fontDescriptionRun = *(logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns);
353 fontDescriptionRun.familyLength = inputStyle.familyName.size();
354 fontDescriptionRun.familyName = new char[fontDescriptionRun.familyLength];
355 memcpy(fontDescriptionRun.familyName, inputStyle.familyName.c_str(), fontDescriptionRun.familyLength);
356 fontDescriptionRun.familyDefined = true;
358 // The memory allocated for the font family name is freed when the font description is removed from the logical model.
363 fontDescriptionRun.weight = inputStyle.weight;
364 fontDescriptionRun.weightDefined = true;
369 fontDescriptionRun.width = inputStyle.width;
370 fontDescriptionRun.widthDefined = true;
375 fontDescriptionRun.slant = inputStyle.slant;
376 fontDescriptionRun.slantDefined = true;
381 fontDescriptionRun.size = static_cast<PointSize26Dot6>(inputStyle.size * impl.GetFontSizeScale() * 64.f);
382 fontDescriptionRun.sizeDefined = true;
385 fontDescriptionRun.characterRun.characterIndex = cursorIndex;
386 fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
389 // Insert at current cursor position.
390 Vector<Character>& modifyText = logicalModel->mText;
392 auto pos = modifyText.End();
393 if(cursorIndex < numberOfCharactersInModel)
395 pos = modifyText.Begin() + cursorIndex;
397 unsigned int realPos = static_cast<unsigned int>(pos - modifyText.Begin());
398 modifyText.Insert(pos, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText);
400 if(NULL != impl.mEditableControlInterface)
402 impl.mEditableControlInterface->TextInserted(realPos, maxSizeOfNewText, text);
405 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
407 // Mark the first paragraph to be updated.
408 if(Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout())
410 textUpdateInfo.mCharacterIndex = 0;
411 textUpdateInfo.mNumberOfCharactersToRemove = textUpdateInfo.mPreviousNumberOfCharacters;
412 textUpdateInfo.mNumberOfCharactersToAdd = numberOfCharactersInModel + maxSizeOfNewText;
413 textUpdateInfo.mClearAll = true;
417 textUpdateInfo.mCharacterIndex = std::min(cursorIndex, textUpdateInfo.mCharacterIndex);
418 textUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
421 if(impl.mMarkupProcessorEnabled)
423 InsertTextAnchor(controller, maxSizeOfNewText, cursorIndex);
426 // Update the cursor index.
427 cursorIndex += maxSizeOfNewText;
429 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, logicalModel->mText.Count(), eventData->mPrimaryCursorPosition);
432 if((0u == logicalModel->mText.Count()) &&
433 impl.IsPlaceholderAvailable())
435 // Show place-holder if empty after removing the pre-edit text
436 PlaceholderHandler::ShowPlaceholderText(impl);
437 eventData->mUpdateCursorPosition = true;
438 impl.ClearPreEditFlag();
440 else if(removedPrevious ||
442 (0 != utf32Characters.Count()))
444 // Queue an inserted event
445 impl.QueueModifyEvent(ModifyEvent::TEXT_INSERTED);
447 eventData->mUpdateCursorPosition = true;
450 eventData->mScrollAfterDelete = true;
454 eventData->mScrollAfterUpdatePosition = true;
458 if(nullptr != impl.mEditableControlInterface)
460 impl.mEditableControlInterface->CursorPositionChanged(oldCursorPos, eventData->mPrimaryCursorPosition);
465 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", logicalModel->mText.Count());
467 impl.ResetInputMethodContext();
469 if(NULL != impl.mEditableControlInterface)
471 // Do this last since it provides callbacks into application code
472 impl.mEditableControlInterface->MaxLengthReached();
477 void Controller::TextUpdater::PasteText(Controller& controller, const std::string& stringToPaste)
479 InsertText(controller, stringToPaste, Text::Controller::COMMIT);
480 Controller::Impl& impl = *controller.mImpl;
481 impl.ChangeState(EventData::EDITING);
482 impl.RequestRelayout();
484 if(NULL != impl.mEditableControlInterface)
486 // Do this last since it provides callbacks into application code
487 impl.mEditableControlInterface->TextChanged(true);
491 bool Controller::TextUpdater::RemoveText(
492 Controller& controller,
494 int numberOfCharacters,
495 UpdateInputStyleType type)
497 bool removed = false;
498 bool removeAll = false;
500 Controller::Impl& impl = *controller.mImpl;
501 EventData*& eventData = impl.mEventData;
503 if(nullptr == eventData)
508 ModelPtr& model = impl.mModel;
509 LogicalModelPtr& logicalModel = model->mLogicalModel;
510 VisualModelPtr& visualModel = model->mVisualModel;
512 DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveText %p mText.Count() %d cursor %d cursorOffset %d numberOfCharacters %d\n", &controller, logicalModel->mText.Count(), eventData->mPrimaryCursorPosition, cursorOffset, numberOfCharacters);
514 if(!impl.IsShowingPlaceholderText())
516 // Delete at current cursor position
517 Vector<Character>& currentText = logicalModel->mText;
518 CharacterIndex& previousCursorIndex = eventData->mPrimaryCursorPosition;
520 CharacterIndex cursorIndex = 0;
522 // Validate the cursor position & number of characters
523 if((static_cast<int>(eventData->mPrimaryCursorPosition) + cursorOffset) >= 0)
525 cursorIndex = eventData->mPrimaryCursorPosition + cursorOffset;
528 //Handle Emoji clustering for cursor handling
529 // Deletion case: this is handling the deletion cases when the cursor is before or after Emoji
530 // - Before: when use delete key and cursor is before Emoji (cursorOffset = -1)
531 // - After: when use backspace key and cursor is after Emoji (cursorOffset = 0)
533 const Script script = logicalModel->GetScript(cursorIndex);
534 if((numberOfCharacters == 1u) &&
535 (IsOneOfEmojiScripts(script)))
537 //TODO: Use this clustering for Emoji cases only. This needs more testing to generalize to all scripts.
538 CharacterRun emojiClusteredCharacters = RetrieveClusteredCharactersOfCharacterIndex(visualModel, logicalModel, cursorIndex);
539 Length actualNumberOfCharacters = emojiClusteredCharacters.numberOfCharacters;
541 //Set cursorIndex at the first characterIndex of clustred Emoji
542 cursorIndex = emojiClusteredCharacters.characterIndex;
544 numberOfCharacters = actualNumberOfCharacters;
547 if((cursorIndex + numberOfCharacters) > currentText.Count())
549 numberOfCharacters = currentText.Count() - cursorIndex;
552 if((cursorIndex == 0) && (currentText.Count() - numberOfCharacters == 0))
557 TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
559 if(eventData->mPreEditFlag || removeAll || // If the preedit flag is enabled, it means two (or more) of them came together i.e. when two keys have been pressed at the same time.
560 ((cursorIndex + numberOfCharacters) <= textUpdateInfo.mPreviousNumberOfCharacters))
562 // Mark the paragraphs to be updated.
563 if(Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout())
565 textUpdateInfo.mCharacterIndex = 0;
566 textUpdateInfo.mNumberOfCharactersToRemove = textUpdateInfo.mPreviousNumberOfCharacters;
567 textUpdateInfo.mNumberOfCharactersToAdd = textUpdateInfo.mPreviousNumberOfCharacters - numberOfCharacters;
568 textUpdateInfo.mClearAll = true;
572 textUpdateInfo.mCharacterIndex = std::min(cursorIndex, textUpdateInfo.mCharacterIndex);
573 textUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
576 // Update the input style and remove the text's style before removing the text.
578 if(UPDATE_INPUT_STYLE == type)
580 InputStyle& eventDataInputStyle = eventData->mInputStyle;
582 // Keep a copy of the current input style.
583 InputStyle currentInputStyle;
584 currentInputStyle.Copy(eventDataInputStyle);
586 // Set first the default input style.
587 impl.RetrieveDefaultInputStyle(eventDataInputStyle);
589 // Update the input style.
590 logicalModel->RetrieveStyle(cursorIndex, eventDataInputStyle);
592 // Compare if the input style has changed.
593 const bool hasInputStyleChanged = !currentInputStyle.Equal(eventDataInputStyle);
595 if(hasInputStyleChanged)
597 const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask(eventDataInputStyle);
598 // Queue the input style changed signal.
599 eventData->mInputStyleChangedQueue.PushBack(styleChangedMask);
603 // If the number of current text and the number of characters to be deleted are same,
604 // it means all texts should be removed and all Preedit variables should be initialized.
607 impl.ClearPreEditFlag();
608 textUpdateInfo.mNumberOfCharactersToAdd = 0;
611 // Updates the text style runs by removing characters. Runs with no characters are removed.
612 logicalModel->UpdateTextStyleRuns(cursorIndex, -numberOfCharacters);
614 // Remove the characters.
615 Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
616 Vector<Character>::Iterator last = first + numberOfCharacters;
618 if(NULL != impl.mEditableControlInterface)
621 Utf32ToUtf8(first, numberOfCharacters, utf8);
622 impl.mEditableControlInterface->TextDeleted(cursorIndex, numberOfCharacters, utf8);
625 currentText.Erase(first, last);
627 if(impl.mMarkupProcessorEnabled)
629 RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex);
632 if(nullptr != impl.mEditableControlInterface)
634 impl.mEditableControlInterface->CursorPositionChanged(previousCursorIndex, cursorIndex);
637 // Cursor position retreat
638 previousCursorIndex = cursorIndex;
640 eventData->mScrollAfterDelete = true;
642 if(EventData::INACTIVE == eventData->mState)
644 impl.ChangeState(EventData::EDITING);
647 DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", &controller, numberOfCharacters);
656 bool Controller::TextUpdater::RemoveSelectedText(Controller& controller)
658 bool textRemoved(false);
660 Controller::Impl& impl = *controller.mImpl;
662 if(EventData::SELECTING == impl.mEventData->mState)
664 std::string removedString;
665 uint32_t oldSelStart = impl.mEventData->mLeftSelectionPosition;
666 uint32_t oldSelEnd = impl.mEventData->mRightSelectionPosition;
668 impl.RetrieveSelection(removedString, true);
670 if(!removedString.empty())
673 impl.ChangeState(EventData::EDITING);
675 if(impl.mMarkupProcessorEnabled)
677 int cursorOffset = -1;
678 int numberOfCharacters = removedString.length();
679 CharacterIndex& cursorIndex = impl.mEventData->mPrimaryCursorPosition;
680 CharacterIndex previousCursorIndex = cursorIndex + numberOfCharacters;
682 RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex);
685 if(impl.mSelectableControlInterface != nullptr)
687 impl.mSelectableControlInterface->SelectionChanged(oldSelStart, oldSelEnd, impl.mEventData->mPrimaryCursorPosition, impl.mEventData->mPrimaryCursorPosition);
695 void Controller::TextUpdater::ResetText(Controller& controller)
697 Controller::Impl& impl = *controller.mImpl;
698 LogicalModelPtr& logicalModel = impl.mModel->mLogicalModel;
700 // Reset spanned-text
701 logicalModel->mSpannedTextPlaced = false;
704 logicalModel->mText.Clear();
706 // Reset the embedded images buffer.
707 logicalModel->ClearEmbeddedImages();
709 // Reset the anchors buffer.
710 logicalModel->ClearAnchors();
712 // We have cleared everything including the placeholder-text
713 impl.PlaceholderCleared();
715 impl.mTextUpdateInfo.mCharacterIndex = 0u;
716 impl.mTextUpdateInfo.mNumberOfCharactersToRemove = impl.mTextUpdateInfo.mPreviousNumberOfCharacters;
717 impl.mTextUpdateInfo.mNumberOfCharactersToAdd = 0u;
719 // Clear any previous text.
720 impl.mTextUpdateInfo.mClearAll = true;
722 // The natural size needs to be re-calculated.
723 impl.mRecalculateNaturalSize = true;
725 // The text direction needs to be updated.
726 impl.mUpdateTextDirection = true;
728 // Apply modifications to the model
729 impl.mOperationsPending = ALL_OPERATIONS;
732 void Controller::TextUpdater::InsertTextAnchor(Controller& controller, int numberOfCharacters, CharacterIndex previousCursorIndex)
734 Controller::Impl& impl = *controller.mImpl;
735 ModelPtr& model = impl.mModel;
736 LogicalModelPtr& logicalModel = model->mLogicalModel;
738 for(auto& anchor : logicalModel->mAnchors)
740 if(anchor.endIndex < previousCursorIndex) // [anchor] CUR
744 if(anchor.startIndex < previousCursorIndex) // [anCURr]
746 anchor.endIndex += numberOfCharacters;
750 anchor.startIndex += numberOfCharacters;
751 anchor.endIndex += numberOfCharacters;
753 DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::InsertTextAnchor[%p] Anchor[%s] start[%d] end[%d]\n", &controller, anchor.href, anchor.startIndex, anchor.endIndex);
757 void Controller::TextUpdater::RemoveTextAnchor(Controller& controller, int cursorOffset, int numberOfCharacters, CharacterIndex previousCursorIndex)
759 Controller::Impl& impl = *controller.mImpl;
760 ModelPtr& model = impl.mModel;
761 LogicalModelPtr& logicalModel = model->mLogicalModel;
762 Vector<Anchor>::Iterator it = logicalModel->mAnchors.Begin();
764 while(it != logicalModel->mAnchors.End())
766 Anchor& anchor = *it;
768 if(anchor.endIndex <= previousCursorIndex && cursorOffset == 0) // [anchor] CUR >>
772 else if(anchor.endIndex <= previousCursorIndex && cursorOffset == -1) // [anchor] << CUR
774 int endIndex = anchor.endIndex;
775 int offset = previousCursorIndex - endIndex;
776 int index = endIndex - (numberOfCharacters - offset);
783 if((int)anchor.startIndex >= endIndex)
787 delete[] anchor.href;
789 it = logicalModel->mAnchors.Erase(it);
794 anchor.endIndex = endIndex;
797 else if(anchor.startIndex >= previousCursorIndex && cursorOffset == -1) // << CUR [anchor]
799 anchor.startIndex -= numberOfCharacters;
800 anchor.endIndex -= numberOfCharacters;
802 else if(anchor.startIndex >= previousCursorIndex && cursorOffset == 0) // CUR >> [anchor]
804 int startIndex = anchor.startIndex;
805 int endIndex = anchor.endIndex;
806 int index = previousCursorIndex + numberOfCharacters - 1;
808 if(startIndex > index)
810 anchor.startIndex -= numberOfCharacters;
811 anchor.endIndex -= numberOfCharacters;
813 else if(endIndex > index + 1)
815 anchor.endIndex -= numberOfCharacters;
821 delete[] anchor.href;
823 it = logicalModel->mAnchors.Erase(it);
827 else if(cursorOffset == -1) // [<< CUR]
829 int startIndex = anchor.startIndex;
830 int index = previousCursorIndex - numberOfCharacters;
832 if(startIndex >= index)
834 anchor.startIndex = index;
836 anchor.endIndex -= numberOfCharacters;
838 else if(cursorOffset == 0) // [CUR >>]
840 anchor.endIndex -= numberOfCharacters;
844 // When this condition is reached, someting is wrong.
845 DALI_LOG_ERROR("Controller::RemoveTextAnchor[%p] Invaild state cursorOffset[%d]\n", &controller, cursorOffset);
848 DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveTextAnchor[%p] Anchor[%s] start[%d] end[%d]\n", &controller, anchor.href, anchor.startIndex, anchor.endIndex);
856 } // namespace Toolkit