Merge "[AT-SPI] Use WeakHandle<Actor> in Accessible objects" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-text-updater.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-controller-text-updater.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <memory.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/character-set-conversion.h>
27 #include <dali-toolkit/internal/text/markup-processor.h>
28 #include <dali-toolkit/internal/text/text-controller-impl.h>
29 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
30
31 namespace
32 {
33 #if defined(DEBUG_ENABLED)
34 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
35 #endif
36
37 } // namespace
38
39 namespace Dali
40 {
41 namespace Toolkit
42 {
43 namespace Text
44 {
45 void Controller::TextUpdater::SetText(Controller& controller, const std::string& text)
46 {
47   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Controller::SetText\n");
48
49   Controller::Impl& impl = *controller.mImpl;
50
51   // Reset keyboard as text changed
52   impl.ResetInputMethodContext();
53
54   // Remove the previously set text and style.
55   ResetText(controller);
56
57   // Remove the style.
58   controller.ClearStyleData();
59
60   CharacterIndex lastCursorIndex = 0u;
61
62   EventData*& eventData = impl.mEventData;
63
64   if(nullptr != eventData)
65   {
66     // If popup shown then hide it by switching to Editing state
67     if((EventData::SELECTING == eventData->mState) ||
68        (EventData::EDITING_WITH_POPUP == eventData->mState) ||
69        (EventData::EDITING_WITH_GRAB_HANDLE == eventData->mState) ||
70        (EventData::EDITING_WITH_PASTE_POPUP == eventData->mState))
71     {
72       impl.ChangeState(EventData::EDITING);
73     }
74   }
75
76   if(!text.empty())
77   {
78     ModelPtr&        model        = impl.mModel;
79     LogicalModelPtr& logicalModel = model->mLogicalModel;
80     model->mVisualModel->SetTextColor(impl.mTextColor);
81
82     MarkupProcessData markupProcessData(logicalModel->mColorRuns,
83                                         logicalModel->mFontDescriptionRuns,
84                                         logicalModel->mEmbeddedItems,
85                                         logicalModel->mAnchors);
86
87     Length         textSize = 0u;
88     const uint8_t* utf8     = NULL;
89     if(impl.mMarkupProcessorEnabled)
90     {
91       ProcessMarkupString(text, markupProcessData);
92       textSize = markupProcessData.markupProcessedText.size();
93
94       // This is a bit horrible but std::string returns a (signed) char*
95       utf8 = reinterpret_cast<const uint8_t*>(markupProcessData.markupProcessedText.c_str());
96     }
97     else
98     {
99       textSize = text.size();
100
101       // This is a bit horrible but std::string returns a (signed) char*
102       utf8 = reinterpret_cast<const uint8_t*>(text.c_str());
103     }
104
105     //  Convert text into UTF-32
106     Vector<Character>& utf32Characters = logicalModel->mText;
107     utf32Characters.Resize(textSize);
108
109     // Transform a text array encoded in utf8 into an array encoded in utf32.
110     // It returns the actual number of characters.
111     Length characterCount = Utf8ToUtf32(utf8, textSize, utf32Characters.Begin());
112     utf32Characters.Resize(characterCount);
113
114     DALI_ASSERT_DEBUG(textSize >= characterCount && "Invalid UTF32 conversion length");
115     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Controller::SetText %p UTF8 size %d, UTF32 size %d\n", &controller, textSize, logicalModel->mText.Count());
116
117     // The characters to be added.
118     impl.mTextUpdateInfo.mNumberOfCharactersToAdd = logicalModel->mText.Count();
119
120     // To reset the cursor position
121     lastCursorIndex = characterCount;
122
123     // Update the rest of the model during size negotiation
124     impl.QueueModifyEvent(ModifyEvent::TEXT_REPLACED);
125
126     // The natural size needs to be re-calculated.
127     impl.mRecalculateNaturalSize = true;
128
129     // The text direction needs to be updated.
130     impl.mUpdateTextDirection = true;
131
132     // Apply modifications to the model
133     impl.mOperationsPending = ALL_OPERATIONS;
134   }
135   else
136   {
137     controller.ShowPlaceholderText();
138   }
139
140   // Resets the cursor position.
141   controller.ResetCursorPosition(lastCursorIndex);
142
143   // Scrolls the text to make the cursor visible.
144   controller.ResetScrollPosition();
145
146   impl.RequestRelayout();
147
148   if(nullptr != eventData)
149   {
150     // Cancel previously queued events
151     eventData->mEventQueue.clear();
152   }
153
154   // Do this last since it provides callbacks into application code.
155   if(NULL != impl.mEditableControlInterface)
156   {
157     impl.mEditableControlInterface->TextChanged(true);
158   }
159 }
160
161 void Controller::TextUpdater::InsertText(Controller& controller, const std::string& text, Controller::InsertType type)
162 {
163   Controller::Impl& impl      = *controller.mImpl;
164   EventData*&       eventData = impl.mEventData;
165
166   DALI_ASSERT_DEBUG(nullptr != eventData && "Unexpected InsertText")
167
168   if(NULL == eventData)
169   {
170     return;
171   }
172
173   bool removedPrevious  = false;
174   bool removedSelected  = false;
175   bool maxLengthReached = false;
176
177   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);
178
179   ModelPtr&        model        = impl.mModel;
180   LogicalModelPtr& logicalModel = model->mLogicalModel;
181
182   // TODO: At the moment the underline runs are only for pre-edit.
183   model->mVisualModel->mUnderlineRuns.Clear();
184
185   // Remove the previous InputMethodContext pre-edit.
186   if(eventData->mPreEditFlag && (0u != eventData->mPreEditLength))
187   {
188     removedPrevious = RemoveText(controller,
189                                  -static_cast<int>(eventData->mPrimaryCursorPosition - eventData->mPreEditStartPosition),
190                                  eventData->mPreEditLength,
191                                  DONT_UPDATE_INPUT_STYLE);
192
193     eventData->mPrimaryCursorPosition = eventData->mPreEditStartPosition;
194     eventData->mPreEditLength         = 0u;
195   }
196   else
197   {
198     // Remove the previous Selection.
199     removedSelected = RemoveSelectedText(controller);
200   }
201
202   Vector<Character> utf32Characters;
203   Length            characterCount = 0u;
204
205   if(!text.empty())
206   {
207     //  Convert text into UTF-32
208     utf32Characters.Resize(text.size());
209
210     // This is a bit horrible but std::string returns a (signed) char*
211     const uint8_t* utf8 = reinterpret_cast<const uint8_t*>(text.c_str());
212
213     // Transform a text array encoded in utf8 into an array encoded in utf32.
214     // It returns the actual number of characters.
215     characterCount = Utf8ToUtf32(utf8, text.size(), utf32Characters.Begin());
216     utf32Characters.Resize(characterCount);
217
218     DALI_ASSERT_DEBUG(text.size() >= utf32Characters.Count() && "Invalid UTF32 conversion length");
219     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "UTF8 size %d, UTF32 size %d\n", text.size(), utf32Characters.Count());
220   }
221
222   if(0u != utf32Characters.Count()) // Check if Utf8ToUtf32 conversion succeeded
223   {
224     // The placeholder text is no longer needed
225     if(impl.IsShowingPlaceholderText())
226     {
227       ResetText(controller);
228     }
229
230     impl.ChangeState(EventData::EDITING);
231
232     // Handle the InputMethodContext (predicitive text) state changes
233     if(COMMIT == type)
234     {
235       // InputMethodContext is no longer handling key-events
236       impl.ClearPreEditFlag();
237     }
238     else // PRE_EDIT
239     {
240       if(!eventData->mPreEditFlag)
241       {
242         DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Entered PreEdit state\n");
243
244         // Record the start of the pre-edit text
245         eventData->mPreEditStartPosition = eventData->mPrimaryCursorPosition;
246       }
247
248       eventData->mPreEditLength = utf32Characters.Count();
249       eventData->mPreEditFlag   = true;
250
251       DALI_LOG_INFO(gLogFilter, Debug::Verbose, "mPreEditStartPosition %d mPreEditLength %d\n", eventData->mPreEditStartPosition, eventData->mPreEditLength);
252     }
253
254     const Length numberOfCharactersInModel = logicalModel->mText.Count();
255
256     // Restrict new text to fit within Maximum characters setting.
257     Length maxSizeOfNewText = std::min((impl.mMaximumNumberOfCharacters - numberOfCharactersInModel), characterCount);
258     maxLengthReached        = (characterCount > maxSizeOfNewText);
259
260     // The cursor position.
261     CharacterIndex& cursorIndex = eventData->mPrimaryCursorPosition;
262
263     // Update the text's style.
264
265     // Updates the text style runs by adding characters.
266     logicalModel->UpdateTextStyleRuns(cursorIndex, maxSizeOfNewText);
267
268     // Get the character index from the cursor index.
269     const CharacterIndex styleIndex = (cursorIndex > 0u) ? cursorIndex - 1u : 0u;
270
271     // Retrieve the text's style for the given index.
272     InputStyle style;
273     impl.RetrieveDefaultInputStyle(style);
274     logicalModel->RetrieveStyle(styleIndex, style);
275
276     InputStyle& inputStyle = eventData->mInputStyle;
277
278     // Whether to add a new text color run.
279     const bool addColorRun = (style.textColor != inputStyle.textColor) && !inputStyle.isDefaultColor;
280
281     // Whether to add a new font run.
282     const bool addFontNameRun   = (style.familyName != inputStyle.familyName) && inputStyle.isFamilyDefined;
283     const bool addFontWeightRun = (style.weight != inputStyle.weight) && inputStyle.isWeightDefined;
284     const bool addFontWidthRun  = (style.width != inputStyle.width) && inputStyle.isWidthDefined;
285     const bool addFontSlantRun  = (style.slant != inputStyle.slant) && inputStyle.isSlantDefined;
286     const bool addFontSizeRun   = (style.size != inputStyle.size) && inputStyle.isSizeDefined;
287
288     // Add style runs.
289     if(addColorRun)
290     {
291       const VectorBase::SizeType numberOfRuns = logicalModel->mColorRuns.Count();
292       logicalModel->mColorRuns.Resize(numberOfRuns + 1u);
293
294       ColorRun& colorRun                       = *(logicalModel->mColorRuns.Begin() + numberOfRuns);
295       colorRun.color                           = inputStyle.textColor;
296       colorRun.characterRun.characterIndex     = cursorIndex;
297       colorRun.characterRun.numberOfCharacters = maxSizeOfNewText;
298     }
299
300     if(addFontNameRun ||
301        addFontWeightRun ||
302        addFontWidthRun ||
303        addFontSlantRun ||
304        addFontSizeRun)
305     {
306       const VectorBase::SizeType numberOfRuns = logicalModel->mFontDescriptionRuns.Count();
307       logicalModel->mFontDescriptionRuns.Resize(numberOfRuns + 1u);
308
309       FontDescriptionRun& fontDescriptionRun = *(logicalModel->mFontDescriptionRuns.Begin() + numberOfRuns);
310
311       if(addFontNameRun)
312       {
313         fontDescriptionRun.familyLength = inputStyle.familyName.size();
314         fontDescriptionRun.familyName   = new char[fontDescriptionRun.familyLength];
315         memcpy(fontDescriptionRun.familyName, inputStyle.familyName.c_str(), fontDescriptionRun.familyLength);
316         fontDescriptionRun.familyDefined = true;
317
318         // The memory allocated for the font family name is freed when the font description is removed from the logical model.
319       }
320
321       if(addFontWeightRun)
322       {
323         fontDescriptionRun.weight        = inputStyle.weight;
324         fontDescriptionRun.weightDefined = true;
325       }
326
327       if(addFontWidthRun)
328       {
329         fontDescriptionRun.width        = inputStyle.width;
330         fontDescriptionRun.widthDefined = true;
331       }
332
333       if(addFontSlantRun)
334       {
335         fontDescriptionRun.slant        = inputStyle.slant;
336         fontDescriptionRun.slantDefined = true;
337       }
338
339       if(addFontSizeRun)
340       {
341         fontDescriptionRun.size        = static_cast<PointSize26Dot6>(inputStyle.size * impl.mFontSizeScale * 64.f);
342         fontDescriptionRun.sizeDefined = true;
343       }
344
345       fontDescriptionRun.characterRun.characterIndex     = cursorIndex;
346       fontDescriptionRun.characterRun.numberOfCharacters = maxSizeOfNewText;
347     }
348
349     // Insert at current cursor position.
350     Vector<Character>& modifyText = logicalModel->mText;
351
352     auto pos = modifyText.End();
353     if(cursorIndex < numberOfCharactersInModel)
354     {
355       pos = modifyText.Begin() + cursorIndex;
356     }
357     unsigned int realPos = pos - modifyText.Begin();
358     modifyText.Insert(pos, utf32Characters.Begin(), utf32Characters.Begin() + maxSizeOfNewText);
359
360     if(NULL != impl.mEditableControlInterface)
361     {
362       impl.mEditableControlInterface->TextInserted(realPos, maxSizeOfNewText, text);
363     }
364
365     TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
366
367     // Mark the first paragraph to be updated.
368     if(Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout())
369     {
370       textUpdateInfo.mCharacterIndex             = 0;
371       textUpdateInfo.mNumberOfCharactersToRemove = textUpdateInfo.mPreviousNumberOfCharacters;
372       textUpdateInfo.mNumberOfCharactersToAdd    = numberOfCharactersInModel + maxSizeOfNewText;
373       textUpdateInfo.mClearAll                   = true;
374     }
375     else
376     {
377       textUpdateInfo.mCharacterIndex = std::min(cursorIndex, textUpdateInfo.mCharacterIndex);
378       textUpdateInfo.mNumberOfCharactersToAdd += maxSizeOfNewText;
379     }
380
381     if(impl.mMarkupProcessorEnabled)
382     {
383       InsertTextAnchor(controller, maxSizeOfNewText, cursorIndex);
384     }
385
386     // Update the cursor index.
387     cursorIndex += maxSizeOfNewText;
388
389     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Inserted %d characters, new size %d new cursor %d\n", maxSizeOfNewText, logicalModel->mText.Count(), eventData->mPrimaryCursorPosition);
390   }
391
392   if((0u == logicalModel->mText.Count()) &&
393      impl.IsPlaceholderAvailable())
394   {
395     // Show place-holder if empty after removing the pre-edit text
396     controller.ShowPlaceholderText();
397     eventData->mUpdateCursorPosition = true;
398     impl.ClearPreEditFlag();
399   }
400   else if(removedPrevious ||
401           removedSelected ||
402           (0 != utf32Characters.Count()))
403   {
404     // Queue an inserted event
405     impl.QueueModifyEvent(ModifyEvent::TEXT_INSERTED);
406
407     eventData->mUpdateCursorPosition = true;
408     if(removedSelected)
409     {
410       eventData->mScrollAfterDelete = true;
411     }
412     else
413     {
414       eventData->mScrollAfterUpdatePosition = true;
415     }
416   }
417
418   if(maxLengthReached)
419   {
420     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "MaxLengthReached (%d)\n", logicalModel->mText.Count());
421
422     impl.ResetInputMethodContext();
423
424     if(NULL != impl.mEditableControlInterface)
425     {
426       // Do this last since it provides callbacks into application code
427       impl.mEditableControlInterface->MaxLengthReached();
428     }
429   }
430 }
431
432 void Controller::TextUpdater::PasteText(Controller& controller, const std::string& stringToPaste)
433 {
434   InsertText(controller, stringToPaste, Text::Controller::COMMIT);
435   Controller::Impl& impl = *controller.mImpl;
436   impl.ChangeState(EventData::EDITING);
437   impl.RequestRelayout();
438
439   if(NULL != impl.mEditableControlInterface)
440   {
441     // Do this last since it provides callbacks into application code
442     impl.mEditableControlInterface->TextChanged(true);
443   }
444 }
445
446 bool Controller::TextUpdater::RemoveText(
447   Controller&          controller,
448   int                  cursorOffset,
449   int                  numberOfCharacters,
450   UpdateInputStyleType type)
451 {
452   bool removed = false;
453
454   Controller::Impl& impl      = *controller.mImpl;
455   EventData*&       eventData = impl.mEventData;
456
457   if(nullptr == eventData)
458   {
459     return removed;
460   }
461
462   ModelPtr&        model        = impl.mModel;
463   LogicalModelPtr& logicalModel = model->mLogicalModel;
464
465   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);
466
467   if(!impl.IsShowingPlaceholderText())
468   {
469     // Delete at current cursor position
470     Vector<Character>& currentText         = logicalModel->mText;
471     CharacterIndex&    previousCursorIndex = eventData->mPrimaryCursorPosition;
472
473     CharacterIndex cursorIndex = 0;
474
475     // Validate the cursor position & number of characters
476     if((static_cast<int>(eventData->mPrimaryCursorPosition) + cursorOffset) >= 0)
477     {
478       cursorIndex = eventData->mPrimaryCursorPosition + cursorOffset;
479     }
480
481     if((cursorIndex + numberOfCharacters) > currentText.Count())
482     {
483       numberOfCharacters = currentText.Count() - cursorIndex;
484     }
485
486     TextUpdateInfo& textUpdateInfo = impl.mTextUpdateInfo;
487
488     if(eventData->mPreEditFlag || // 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.
489        ((cursorIndex + numberOfCharacters) <= textUpdateInfo.mPreviousNumberOfCharacters))
490     {
491       // Mark the paragraphs to be updated.
492       if(Layout::Engine::SINGLE_LINE_BOX == impl.mLayoutEngine.GetLayout())
493       {
494         textUpdateInfo.mCharacterIndex             = 0;
495         textUpdateInfo.mNumberOfCharactersToRemove = textUpdateInfo.mPreviousNumberOfCharacters;
496         textUpdateInfo.mNumberOfCharactersToAdd    = textUpdateInfo.mPreviousNumberOfCharacters - numberOfCharacters;
497         textUpdateInfo.mClearAll                   = true;
498       }
499       else
500       {
501         textUpdateInfo.mCharacterIndex = std::min(cursorIndex, textUpdateInfo.mCharacterIndex);
502         textUpdateInfo.mNumberOfCharactersToRemove += numberOfCharacters;
503       }
504
505       // Update the input style and remove the text's style before removing the text.
506
507       if(UPDATE_INPUT_STYLE == type)
508       {
509         InputStyle& eventDataInputStyle = eventData->mInputStyle;
510
511         // Keep a copy of the current input style.
512         InputStyle currentInputStyle;
513         currentInputStyle.Copy(eventDataInputStyle);
514
515         // Set first the default input style.
516         impl.RetrieveDefaultInputStyle(eventDataInputStyle);
517
518         // Update the input style.
519         logicalModel->RetrieveStyle(cursorIndex, eventDataInputStyle);
520
521         // Compare if the input style has changed.
522         const bool hasInputStyleChanged = !currentInputStyle.Equal(eventDataInputStyle);
523
524         if(hasInputStyleChanged)
525         {
526           const InputStyle::Mask styleChangedMask = currentInputStyle.GetInputStyleChangeMask(eventDataInputStyle);
527           // Queue the input style changed signal.
528           eventData->mInputStyleChangedQueue.PushBack(styleChangedMask);
529         }
530       }
531
532       // If the number of current text and the number of characters to be deleted are same,
533       // it means all texts should be removed and all Preedit variables should be initialized.
534       if((currentText.Count() - numberOfCharacters == 0) && (cursorIndex == 0))
535       {
536         impl.ClearPreEditFlag();
537         textUpdateInfo.mNumberOfCharactersToAdd = 0;
538       }
539
540       // Updates the text style runs by removing characters. Runs with no characters are removed.
541       logicalModel->UpdateTextStyleRuns(cursorIndex, -numberOfCharacters);
542
543       // Remove the characters.
544       Vector<Character>::Iterator first = currentText.Begin() + cursorIndex;
545       Vector<Character>::Iterator last  = first + numberOfCharacters;
546
547       if(NULL != impl.mEditableControlInterface)
548       {
549         std::string utf8;
550         Utf32ToUtf8(first, numberOfCharacters, utf8);
551         impl.mEditableControlInterface->TextDeleted(cursorIndex, numberOfCharacters, utf8);
552       }
553
554       currentText.Erase(first, last);
555
556       if(impl.mMarkupProcessorEnabled)
557       {
558         RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex);
559       }
560
561       // Cursor position retreat
562       previousCursorIndex = cursorIndex;
563
564       eventData->mScrollAfterDelete = true;
565
566       if(EventData::INACTIVE == eventData->mState)
567       {
568         impl.ChangeState(EventData::EDITING);
569       }
570
571       DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveText %p removed %d\n", &controller, numberOfCharacters);
572       removed = true;
573     }
574   }
575
576   return removed;
577 }
578
579 bool Controller::TextUpdater::RemoveSelectedText(Controller& controller)
580 {
581   bool textRemoved(false);
582
583   Controller::Impl& impl = *controller.mImpl;
584
585   if(EventData::SELECTING == impl.mEventData->mState)
586   {
587     std::string removedString;
588     impl.RetrieveSelection(removedString, true);
589
590     if(!removedString.empty())
591     {
592       textRemoved = true;
593       impl.ChangeState(EventData::EDITING);
594
595       if(impl.mMarkupProcessorEnabled)
596       {
597         int             cursorOffset        = -1;
598         int             numberOfCharacters  = removedString.length();
599         CharacterIndex& cursorIndex         = impl.mEventData->mPrimaryCursorPosition;
600         CharacterIndex  previousCursorIndex = cursorIndex + numberOfCharacters;
601
602         RemoveTextAnchor(controller, cursorOffset, numberOfCharacters, previousCursorIndex);
603       }
604     }
605   }
606
607   return textRemoved;
608 }
609
610 void Controller::TextUpdater::ResetText(Controller& controller)
611 {
612   Controller::Impl& impl         = *controller.mImpl;
613   LogicalModelPtr&  logicalModel = impl.mModel->mLogicalModel;
614
615   // Reset buffers.
616   logicalModel->mText.Clear();
617
618   // Reset the embedded images buffer.
619   logicalModel->ClearEmbeddedImages();
620
621   // Reset the anchors buffer.
622   logicalModel->ClearAnchors();
623
624   // We have cleared everything including the placeholder-text
625   impl.PlaceholderCleared();
626
627   impl.mTextUpdateInfo.mCharacterIndex             = 0u;
628   impl.mTextUpdateInfo.mNumberOfCharactersToRemove = impl.mTextUpdateInfo.mPreviousNumberOfCharacters;
629   impl.mTextUpdateInfo.mNumberOfCharactersToAdd    = 0u;
630
631   // Clear any previous text.
632   impl.mTextUpdateInfo.mClearAll = true;
633
634   // The natural size needs to be re-calculated.
635   impl.mRecalculateNaturalSize = true;
636
637   // The text direction needs to be updated.
638   impl.mUpdateTextDirection = true;
639
640   // Apply modifications to the model
641   impl.mOperationsPending = ALL_OPERATIONS;
642 }
643
644 void Controller::TextUpdater::InsertTextAnchor(Controller& controller, int numberOfCharacters, CharacterIndex previousCursorIndex)
645 {
646   Controller::Impl& impl         = *controller.mImpl;
647   ModelPtr&         model        = impl.mModel;
648   LogicalModelPtr&  logicalModel = model->mLogicalModel;
649
650   for(auto& anchor : logicalModel->mAnchors)
651   {
652     if(anchor.endIndex < previousCursorIndex) //      [anchor]  CUR
653     {
654       continue;
655     }
656     if(anchor.startIndex < previousCursorIndex) //      [anCURr]
657     {
658       anchor.endIndex += numberOfCharacters;
659     }
660     else // CUR  [anchor]
661     {
662       anchor.startIndex += numberOfCharacters;
663       anchor.endIndex += numberOfCharacters;
664     }
665     DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::InsertTextAnchor[%p] Anchor[%s] start[%d] end[%d]\n", &controller, anchor.href, anchor.startIndex, anchor.endIndex);
666   }
667 }
668
669 void Controller::TextUpdater::RemoveTextAnchor(Controller& controller, int cursorOffset, int numberOfCharacters, CharacterIndex previousCursorIndex)
670 {
671   Controller::Impl&        impl         = *controller.mImpl;
672   ModelPtr&                model        = impl.mModel;
673   LogicalModelPtr&         logicalModel = model->mLogicalModel;
674   Vector<Anchor>::Iterator it           = logicalModel->mAnchors.Begin();
675
676   while(it != logicalModel->mAnchors.End())
677   {
678     Anchor& anchor = *it;
679
680     if(anchor.endIndex <= previousCursorIndex && cursorOffset == 0) // [anchor]    CUR >>
681     {
682       // Nothing happens.
683     }
684     else if(anchor.endIndex <= previousCursorIndex && cursorOffset == -1) // [anchor] << CUR
685     {
686       int endIndex = anchor.endIndex;
687       int offset   = previousCursorIndex - endIndex;
688       int index    = endIndex - (numberOfCharacters - offset);
689
690       if(index < endIndex)
691       {
692         endIndex = index;
693       }
694
695       if((int)anchor.startIndex >= endIndex)
696       {
697         if(anchor.href)
698         {
699           delete[] anchor.href;
700         }
701         it = logicalModel->mAnchors.Erase(it);
702         continue;
703       }
704       else
705       {
706         anchor.endIndex = endIndex;
707       }
708     }
709     else if(anchor.startIndex >= previousCursorIndex && cursorOffset == -1) // << CUR    [anchor]
710     {
711       anchor.startIndex -= numberOfCharacters;
712       anchor.endIndex -= numberOfCharacters;
713     }
714     else if(anchor.startIndex >= previousCursorIndex && cursorOffset == 0) //    CUR >> [anchor]
715     {
716       int startIndex = anchor.startIndex;
717       int endIndex   = anchor.endIndex;
718       int index      = previousCursorIndex + numberOfCharacters - 1;
719
720       if(startIndex > index)
721       {
722         anchor.startIndex -= numberOfCharacters;
723         anchor.endIndex -= numberOfCharacters;
724       }
725       else if(endIndex > index + 1)
726       {
727         anchor.endIndex -= numberOfCharacters;
728       }
729       else
730       {
731         if(anchor.href)
732         {
733           delete[] anchor.href;
734         }
735         it = logicalModel->mAnchors.Erase(it);
736         continue;
737       }
738     }
739     else if(cursorOffset == -1) // [<< CUR]
740     {
741       int startIndex = anchor.startIndex;
742       int index      = previousCursorIndex - numberOfCharacters;
743
744       if(startIndex >= index)
745       {
746         anchor.startIndex = index;
747       }
748       anchor.endIndex -= numberOfCharacters;
749     }
750     else if(cursorOffset == 0) // [CUR >>]
751     {
752       anchor.endIndex -= numberOfCharacters;
753     }
754     else
755     {
756       // When this condition is reached, someting is wrong.
757       DALI_LOG_ERROR("Controller::RemoveTextAnchor[%p] Invaild state cursorOffset[%d]\n", &controller, cursorOffset);
758     }
759
760     DALI_LOG_INFO(gLogFilter, Debug::General, "Controller::RemoveTextAnchor[%p] Anchor[%s] start[%d] end[%d]\n", &controller, anchor.href, anchor.startIndex, anchor.endIndex);
761
762     it++;
763   }
764 }
765
766 } // namespace Text
767
768 } // namespace Toolkit
769
770 } // namespace Dali