5707e4805598d4835c53f2b113b3cc1df072bb75
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-editor-impl.cpp
1 /*
2  * Copyright (c) 2022 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/controls/text-controls/text-editor-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/common/stage.h>
24 #include <dali/devel-api/object/property-helper-devel.h>
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/actors/layer.h>
28 #include <dali/public-api/adaptor-framework/key.h>
29 #include <dali/public-api/common/dali-common.h>
30 #include <dali/public-api/object/type-registry-helper.h>
31 #include <cstring>
32 #include <limits>
33
34 // INTERNAL INCLUDES
35 #include <dali-toolkit/devel-api/controls/control-devel.h>
36 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
37 #include <dali-toolkit/devel-api/text/rendering-backend.h>
38 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
39 #include <dali-toolkit/internal/controls/text-controls/common-text-utils.h>
40 #include <dali-toolkit/internal/controls/text-controls/text-editor-property-handler.h>
41 #include <dali-toolkit/internal/styling/style-manager-impl.h>
42 #include <dali-toolkit/internal/text/rendering/text-backend.h>
43 #include <dali-toolkit/internal/text/text-effects-style.h>
44 #include <dali-toolkit/internal/text/text-enumerations-impl.h>
45 #include <dali-toolkit/internal/text/text-font-style.h>
46 #include <dali-toolkit/internal/text/text-view.h>
47 #include <dali-toolkit/public-api/text/text-enumerations.h>
48 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
49 #include <dali-toolkit/public-api/visuals/visual-properties.h>
50
51 using namespace Dali::Toolkit::Text;
52
53 #if defined(DEBUG_ENABLED)
54 Debug::Filter* gTextEditorLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_CONTROLS");
55 #endif
56
57 namespace Dali
58 {
59 namespace Toolkit
60 {
61 namespace Internal
62 {
63 namespace // unnamed namespace
64 {
65 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::DevelText::DEFAULT_RENDERING_BACKEND;
66 const float        DEFAULT_SCROLL_SPEED      = 1200.f; ///< The default scroll speed for the text editor in pixels/second.
67 } // unnamed namespace
68
69 namespace
70 {
71 const char* const SCROLL_BAR_POSITION("sourcePosition");
72 const char* const SCROLL_BAR_POSITION_MIN("sourcePositionMin");
73 const char* const SCROLL_BAR_POSITION_MAX("sourcePositionMax");
74 const char* const SCROLL_BAR_CONTENT_SIZE("sourceContentSize");
75
76 // Type registration
77 BaseHandle Create()
78 {
79   return Toolkit::TextEditor::New();
80 }
81
82 // clang-format off
83 // Setup properties, signals and actions using the type-registry.
84 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::TextEditor, Toolkit::Control, Create);
85
86 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "text",                                 STRING,    TEXT                                )
87 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "textColor",                            VECTOR4,   TEXT_COLOR                          )
88 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "fontFamily",                           STRING,    FONT_FAMILY                         )
89 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "fontStyle",                            MAP,       FONT_STYLE                          )
90 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "pointSize",                            FLOAT,     POINT_SIZE                          )
91 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "horizontalAlignment",                  STRING,    HORIZONTAL_ALIGNMENT                )
92 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "scrollThreshold",                      FLOAT,     SCROLL_THRESHOLD                    )
93 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "scrollSpeed",                          FLOAT,     SCROLL_SPEED                        )
94 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "primaryCursorColor",                   VECTOR4,   PRIMARY_CURSOR_COLOR                )
95 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "secondaryCursorColor",                 VECTOR4,   SECONDARY_CURSOR_COLOR              )
96 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "enableCursorBlink",                    BOOLEAN,   ENABLE_CURSOR_BLINK                 )
97 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "cursorBlinkInterval",                  FLOAT,     CURSOR_BLINK_INTERVAL               )
98 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "cursorBlinkDuration",                  FLOAT,     CURSOR_BLINK_DURATION               )
99 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "cursorWidth",                          INTEGER,   CURSOR_WIDTH                        )
100 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "grabHandleImage",                      STRING,    GRAB_HANDLE_IMAGE                   )
101 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "grabHandlePressedImage",               STRING,    GRAB_HANDLE_PRESSED_IMAGE           )
102 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHandleImageLeft",             MAP,       SELECTION_HANDLE_IMAGE_LEFT         )
103 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHandleImageRight",            MAP,       SELECTION_HANDLE_IMAGE_RIGHT        )
104 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHandlePressedImageLeft",      MAP,       SELECTION_HANDLE_PRESSED_IMAGE_LEFT )
105 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHandlePressedImageRight",     MAP,       SELECTION_HANDLE_PRESSED_IMAGE_RIGHT)
106 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHandleMarkerImageLeft",       MAP,       SELECTION_HANDLE_MARKER_IMAGE_LEFT  )
107 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHandleMarkerImageRight",      MAP,       SELECTION_HANDLE_MARKER_IMAGE_RIGHT )
108 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "selectionHighlightColor",              VECTOR4,   SELECTION_HIGHLIGHT_COLOR           )
109 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "decorationBoundingBox",                RECTANGLE, DECORATION_BOUNDING_BOX             )
110 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "enableMarkup",                         BOOLEAN,   ENABLE_MARKUP                       )
111 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputColor",                           VECTOR4,   INPUT_COLOR                         )
112 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputFontFamily",                      STRING,    INPUT_FONT_FAMILY                   )
113 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputFontStyle",                       MAP,       INPUT_FONT_STYLE                    )
114 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputPointSize",                       FLOAT,     INPUT_POINT_SIZE                    )
115 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "lineSpacing",                          FLOAT,     LINE_SPACING                        )
116 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputLineSpacing",                     FLOAT,     INPUT_LINE_SPACING                  )
117 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "underline",                            MAP,       UNDERLINE                           )
118 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputUnderline",                       MAP,       INPUT_UNDERLINE                     )
119 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "shadow",                               MAP,       SHADOW                              )
120 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputShadow",                          MAP,       INPUT_SHADOW                        )
121 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "emboss",                               MAP,       EMBOSS                              )
122 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputEmboss",                          MAP,       INPUT_EMBOSS                        )
123 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "outline",                              MAP,       OUTLINE                             )
124 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "inputOutline",                         MAP,       INPUT_OUTLINE                       )
125 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "smoothScroll",                         BOOLEAN,   SMOOTH_SCROLL                       )
126 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "smoothScrollDuration",                 FLOAT,     SMOOTH_SCROLL_DURATION              )
127 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "enableScrollBar",                      BOOLEAN,   ENABLE_SCROLL_BAR                   )
128 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "scrollBarShowDuration",                FLOAT,     SCROLL_BAR_SHOW_DURATION            )
129 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "scrollBarFadeDuration",                FLOAT,     SCROLL_BAR_FADE_DURATION            )
130 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "pixelSize",                            FLOAT,     PIXEL_SIZE                          )
131 DALI_PROPERTY_REGISTRATION_READ_ONLY(Toolkit,       TextEditor, "lineCount",                            INTEGER,   LINE_COUNT                          )
132 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "enableSelection",                      BOOLEAN,   ENABLE_SELECTION                    )
133 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "placeholder",                          MAP,       PLACEHOLDER                         )
134 DALI_PROPERTY_REGISTRATION(Toolkit,                 TextEditor, "lineWrapMode",                         INTEGER,   LINE_WRAP_MODE                      )
135 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "placeholderText",                      STRING,    PLACEHOLDER_TEXT                    )
136 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "placeholderTextColor",                 VECTOR4,   PLACEHOLDER_TEXT_COLOR              )
137 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "enableShiftSelection",                 BOOLEAN,   ENABLE_SHIFT_SELECTION              )
138 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "enableGrabHandle",                     BOOLEAN,   ENABLE_GRAB_HANDLE                  )
139 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "matchSystemLanguageDirection",         BOOLEAN,   MATCH_SYSTEM_LANGUAGE_DIRECTION     )
140 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "renderingBackend",                     INTEGER,   RENDERING_BACKEND                   )
141 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "maxLength",                            INTEGER,   MAX_LENGTH                          )
142 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "selectedTextStart",                    INTEGER,   SELECTED_TEXT_START                 )
143 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "selectedTextEnd",                      INTEGER,   SELECTED_TEXT_END                   )
144 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "horizontalScrollPosition",             FLOAT,     HORIZONTAL_SCROLL_POSITION          )
145 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "verticalScrollPosition",               INTEGER,   VERTICAL_SCROLL_POSITION            )
146 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "enableEditing",                        BOOLEAN,   ENABLE_EDITING                      )
147 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY(Toolkit, TextEditor, "selectedText",                         STRING,    SELECTED_TEXT                       )
148 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "fontSizeScale",                        FLOAT,     FONT_SIZE_SCALE                     )
149 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "enableFontSizeScale",                  BOOLEAN,   ENABLE_FONT_SIZE_SCALE              )
150 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "primaryCursorPosition",                INTEGER,   PRIMARY_CURSOR_POSITION             )
151 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "grabHandleColor",                      VECTOR4,   GRAB_HANDLE_COLOR                   )
152 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "enableGrabHandlePopup",                BOOLEAN,   ENABLE_GRAB_HANDLE_POPUP            )
153 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "inputMethodSettings",                  MAP,       INPUT_METHOD_SETTINGS               )
154 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "inputFilter",                          MAP,       INPUT_FILTER                        )
155 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "ellipsis",                             BOOLEAN,   ELLIPSIS                            )
156 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "ellipsisPosition",                     INTEGER,   ELLIPSIS_POSITION                   )
157 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "minLineSize",                          FLOAT,     MIN_LINE_SIZE                       )
158 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "strikethrough",                        MAP,       STRIKETHROUGH                       )
159 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "inputStrikethrough",                   MAP,       INPUT_STRIKETHROUGH                 )
160 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "characterSpacing",                     FLOAT,     CHARACTER_SPACING                   )
161 DALI_DEVEL_PROPERTY_REGISTRATION(Toolkit,           TextEditor, "relativeLineSize",                     FLOAT,     RELATIVE_LINE_SIZE                  )
162
163 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "textChanged",           SIGNAL_TEXT_CHANGED           )
164 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "inputStyleChanged",     SIGNAL_INPUT_STYLE_CHANGED    )
165 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "maxLengthReached",      SIGNAL_MAX_LENGTH_REACHED     )
166 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "anchorClicked",         SIGNAL_ANCHOR_CLICKED         )
167 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "inputFiltered",         SIGNAL_INPUT_FILTERED         )
168 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "cursorPositionChanged", SIGNAL_CURSOR_POSITION_CHANGED)
169 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "selectionChanged",      SIGNAL_SELECTION_CHANGED      )
170 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "selectionCleared",      SIGNAL_SELECTION_CLEARED      )
171 DALI_SIGNAL_REGISTRATION(Toolkit, TextEditor, "selectionStarted",      SIGNAL_SELECTION_STARTED      )
172
173 DALI_TYPE_REGISTRATION_END()
174 // clang-format on
175
176 Toolkit::TextEditor::InputStyle::Mask ConvertInputStyle(Text::InputStyle::Mask inputStyleMask)
177 {
178   Toolkit::TextEditor::InputStyle::Mask editorInputStyleMask = Toolkit::TextEditor::InputStyle::NONE;
179
180   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_COLOR))
181   {
182     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::COLOR);
183   }
184   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_FONT_FAMILY))
185   {
186     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::FONT_FAMILY);
187   }
188   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_POINT_SIZE))
189   {
190     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::POINT_SIZE);
191   }
192   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_FONT_WEIGHT))
193   {
194     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::FONT_STYLE);
195   }
196   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_FONT_WIDTH))
197   {
198     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::FONT_STYLE);
199   }
200   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_FONT_SLANT))
201   {
202     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::FONT_STYLE);
203   }
204   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_LINE_SPACING))
205   {
206     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::LINE_SPACING);
207   }
208   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_UNDERLINE))
209   {
210     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::UNDERLINE);
211   }
212   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_SHADOW))
213   {
214     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::SHADOW);
215   }
216   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_EMBOSS))
217   {
218     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::EMBOSS);
219   }
220   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_OUTLINE))
221   {
222     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::OUTLINE);
223   }
224   if(InputStyle::NONE != static_cast<InputStyle::Mask>(inputStyleMask & InputStyle::INPUT_STRIKETHROUGH))
225   {
226     editorInputStyleMask = static_cast<Toolkit::TextEditor::InputStyle::Mask>(editorInputStyleMask | Toolkit::TextEditor::InputStyle::STRIKETHROUGH);
227   }
228
229   return editorInputStyleMask;
230 }
231
232 } // namespace
233
234 Toolkit::TextEditor TextEditor::New()
235 {
236   // Create the implementation, temporarily owned by this handle on stack
237   IntrusivePtr<TextEditor> impl = new TextEditor();
238
239   // Pass ownership to CustomActor handle
240   Toolkit::TextEditor handle(*impl);
241
242   // Second-phase init of the implementation
243   // This can only be done after the CustomActor connection has been made...
244   impl->Initialize();
245
246   return handle;
247 }
248
249 void TextEditor::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
250 {
251   Toolkit::TextEditor textEditor = Toolkit::TextEditor::DownCast(Dali::BaseHandle(object));
252
253   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor SetProperty\n");
254
255   if(textEditor)
256   {
257     PropertyHandler::SetProperty(textEditor, index, value);
258   }
259 }
260
261 Property::Value TextEditor::GetProperty(BaseObject* object, Property::Index index)
262 {
263   Property::Value value;
264
265   Toolkit::TextEditor textEditor = Toolkit::TextEditor::DownCast(Dali::BaseHandle(object));
266
267   if(textEditor)
268   {
269     value = PropertyHandler::GetProperty(textEditor, index);
270   }
271   return value;
272 }
273
274 void TextEditor::SelectWholeText()
275 {
276   if(mController && mController->IsShowingRealText())
277   {
278     mController->SelectWholeText();
279     SetKeyInputFocus();
280   }
281 }
282
283 void TextEditor::SelectNone()
284 {
285   if(mController && mController->IsShowingRealText())
286   {
287     mController->SelectNone();
288   }
289 }
290
291 void TextEditor::SelectText(const uint32_t start, const uint32_t end)
292 {
293   if(mController && mController->IsShowingRealText())
294   {
295     mController->SelectText(start, end);
296     SetKeyInputFocus();
297   }
298 }
299
300 string TextEditor::CopyText()
301 {
302   string copiedText = "";
303   if(mController && mController->IsShowingRealText())
304   {
305     copiedText = mController->CopyText();
306   }
307   return copiedText;
308 }
309
310 string TextEditor::CutText()
311 {
312   string cutText = "";
313   if(mController && mController->IsShowingRealText())
314   {
315     cutText = mController->CutText();
316   }
317   return cutText;
318 }
319
320 void TextEditor::PasteText()
321 {
322   if(mController)
323   {
324     SetKeyInputFocus(); //Giving focus to the editor that was passed to the PasteText in case the passed editor (current editor) doesn't have focus.
325     mController->PasteText();
326   }
327 }
328
329 void TextEditor::ScrollBy(Vector2 scroll)
330 {
331   if(mController && mController->IsShowingRealText())
332   {
333     mController->ScrollBy(scroll);
334   }
335 }
336
337 float TextEditor::GetHorizontalScrollPosition()
338 {
339   if(mController && mController->IsShowingRealText())
340   {
341     return mController->GetHorizontalScrollPosition();
342   }
343   return 0;
344 }
345
346 float TextEditor::GetVerticalScrollPosition()
347 {
348   if(mController && mController->IsShowingRealText())
349   {
350     return mController->GetVerticalScrollPosition();
351   }
352   return 0;
353 }
354
355 Vector<Vector2> TextEditor::GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const
356 {
357   return mController->GetTextSize(startIndex, endIndex);
358 }
359
360 Vector<Vector2> TextEditor::GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const
361 {
362   return mController->GetTextPosition(startIndex, endIndex);
363 }
364
365 string TextEditor::GetSelectedText() const
366 {
367   string selectedText = "";
368   if(mController && mController->IsShowingRealText())
369   {
370     selectedText = mController->GetSelectedText();
371   }
372   return selectedText;
373 }
374
375 InputMethodContext TextEditor::GetInputMethodContext()
376 {
377   return mInputMethodContext;
378 }
379
380 DevelTextEditor::MaxLengthReachedSignalType& TextEditor::MaxLengthReachedSignal()
381 {
382   return mMaxLengthReachedSignal;
383 }
384
385 DevelTextEditor::AnchorClickedSignalType& TextEditor::AnchorClickedSignal()
386 {
387   return mAnchorClickedSignal;
388 }
389
390 DevelTextEditor::CursorPositionChangedSignalType& TextEditor::CursorPositionChangedSignal()
391 {
392   return mCursorPositionChangedSignal;
393 }
394
395 DevelTextEditor::InputFilteredSignalType& TextEditor::InputFilteredSignal()
396 {
397   return mInputFilteredSignal;
398 }
399
400 DevelTextEditor::SelectionChangedSignalType& TextEditor::SelectionChangedSignal()
401 {
402   return mSelectionChangedSignal;
403 }
404
405 DevelTextEditor::SelectionClearedSignalType& TextEditor::SelectionClearedSignal()
406 {
407   return mSelectionClearedSignal;
408 }
409
410 DevelTextEditor::SelectionStartedSignalType& TextEditor::SelectionStartedSignal()
411 {
412   return mSelectionStartedSignal;
413 }
414
415 Text::ControllerPtr TextEditor::GetTextController()
416 {
417   return mController;
418 }
419
420 bool TextEditor::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
421 {
422   Dali::BaseHandle handle(object);
423
424   bool                connected(true);
425   Toolkit::TextEditor editor = Toolkit::TextEditor::DownCast(handle);
426
427   if(0 == strcmp(signalName.c_str(), SIGNAL_TEXT_CHANGED))
428   {
429     editor.TextChangedSignal().Connect(tracker, functor);
430   }
431   else if(0 == strcmp(signalName.c_str(), SIGNAL_INPUT_STYLE_CHANGED))
432   {
433     editor.InputStyleChangedSignal().Connect(tracker, functor);
434   }
435   else if(0 == strcmp(signalName.c_str(), SIGNAL_MAX_LENGTH_REACHED))
436   {
437     if(editor)
438     {
439       Internal::TextEditor& editorImpl(GetImpl(editor));
440       editorImpl.MaxLengthReachedSignal().Connect(tracker, functor);
441     }
442   }
443   else if(0 == strcmp(signalName.c_str(), SIGNAL_ANCHOR_CLICKED))
444   {
445     if(editor)
446     {
447       Internal::TextEditor& editorImpl(GetImpl(editor));
448       editorImpl.AnchorClickedSignal().Connect(tracker, functor);
449     }
450   }
451   else if(0 == strcmp(signalName.c_str(), SIGNAL_CURSOR_POSITION_CHANGED))
452   {
453     if(editor)
454     {
455       Internal::TextEditor& editorImpl(GetImpl(editor));
456       editorImpl.CursorPositionChangedSignal().Connect(tracker, functor);
457     }
458   }
459   else if(0 == strcmp(signalName.c_str(), SIGNAL_INPUT_FILTERED))
460   {
461     if(editor)
462     {
463       Internal::TextEditor& editorImpl(GetImpl(editor));
464       editorImpl.InputFilteredSignal().Connect(tracker, functor);
465     }
466   }
467   else if(0 == strcmp(signalName.c_str(), SIGNAL_SELECTION_CHANGED))
468   {
469     if(editor)
470     {
471       Internal::TextEditor& editorImpl(GetImpl(editor));
472       editorImpl.SelectionChangedSignal().Connect(tracker, functor);
473     }
474   }
475   else if(0 == strcmp(signalName.c_str(), SIGNAL_SELECTION_CLEARED))
476   {
477     if(editor)
478     {
479       Internal::TextEditor& editorImpl(GetImpl(editor));
480       editorImpl.SelectionClearedSignal().Connect(tracker, functor);
481     }
482   }
483   else if(0 == strcmp(signalName.c_str(), SIGNAL_SELECTION_STARTED))
484   {
485     if(editor)
486     {
487       Internal::TextEditor& editorImpl(GetImpl(editor));
488       editorImpl.SelectionStartedSignal().Connect(tracker, functor);
489     }
490   }
491   else
492   {
493     // signalName does not match any signal
494     connected = false;
495   }
496
497   return connected;
498 }
499
500 Toolkit::TextEditor::TextChangedSignalType& TextEditor::TextChangedSignal()
501 {
502   return mTextChangedSignal;
503 }
504
505 Toolkit::TextEditor::InputStyleChangedSignalType& TextEditor::InputStyleChangedSignal()
506 {
507   return mInputStyleChangedSignal;
508 }
509
510 Toolkit::TextEditor::ScrollStateChangedSignalType& TextEditor::ScrollStateChangedSignal()
511 {
512   return mScrollStateChangedSignal;
513 }
514
515 void TextEditor::OnAccessibilityStatusChanged()
516 {
517   CommonTextUtils::SynchronizeTextAnchorsInParent(Self(), mController, mAnchorActors);
518 }
519
520 void TextEditor::OnInitialize()
521 {
522   Actor self = Self();
523
524   mController = Text::Controller::New(this, this, this, this);
525
526   mDecorator = Text::Decorator::New(*mController,
527                                     *mController);
528
529   mInputMethodContext = InputMethodContext::New(self);
530
531   mController->GetLayoutEngine().SetLayout(Layout::Engine::MULTI_LINE_BOX);
532
533   // Enables the text input.
534   mController->EnableTextInput(mDecorator, mInputMethodContext);
535
536   // Enables the vertical scrolling after the text input has been enabled.
537   mController->SetVerticalScrollEnabled(true);
538
539   // Disables the horizontal scrolling.
540   mController->SetHorizontalScrollEnabled(false);
541
542   // Sets the maximum number of characters.
543   mController->SetMaximumNumberOfCharacters(std::numeric_limits<Length>::max());
544
545   // Enable the smooth handle panning.
546   mController->SetSmoothHandlePanEnabled(true);
547
548   mController->SetNoTextDoubleTapAction(Controller::NoTextTap::HIGHLIGHT);
549   mController->SetNoTextLongPressAction(Controller::NoTextTap::HIGHLIGHT);
550
551   // Sets layoutDirection value
552   Dali::Stage                 stage           = Dali::Stage::GetCurrent();
553   Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(stage.GetRootLayer().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
554   mController->SetLayoutDirection(layoutDirection);
555
556   self.LayoutDirectionChangedSignal().Connect(this, &TextEditor::OnLayoutDirectionChanged);
557
558   // Forward input events to controller
559   EnableGestureDetection(static_cast<GestureType::Value>(GestureType::TAP | GestureType::PAN | GestureType::LONG_PRESS));
560   GetTapGestureDetector().SetMaximumTapsRequired(2);
561   GetTapGestureDetector().ReceiveAllTapEvents(true);
562
563   self.TouchedSignal().Connect(this, &TextEditor::OnTouched);
564
565   // Set BoundingBox to stage size if not already set.
566   Rect<int> boundingBox;
567   mDecorator->GetBoundingBox(boundingBox);
568
569   if(boundingBox.IsEmpty())
570   {
571     Vector2 stageSize = Dali::Stage::GetCurrent().GetSize();
572     mDecorator->SetBoundingBox(Rect<int>(0.0f, 0.0f, stageSize.width, stageSize.height));
573   }
574
575   // Whether to flip the selection handles as soon as they cross.
576   mDecorator->FlipSelectionHandlesOnCrossEnabled(true);
577
578   // Set the default scroll speed.
579   mDecorator->SetScrollSpeed(DEFAULT_SCROLL_SPEED);
580
581   // Fill-parent area by default
582   self.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
583   self.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
584   self.OnSceneSignal().Connect(this, &TextEditor::OnSceneConnect);
585
586   //Enable highightability
587   self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
588
589   DevelControl::SetInputMethodContext(*this, mInputMethodContext);
590
591   // Creates an extra control to be used as stencil buffer.
592   mStencil = Control::New();
593   mStencil.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
594   mStencil.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
595
596   // Creates a background visual. Even if the color is transparent it updates the stencil.
597   mStencil.SetProperty(Toolkit::Control::Property::BACKGROUND,
598                        Property::Map().Add(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR).Add(ColorVisual::Property::MIX_COLOR, Color::TRANSPARENT));
599
600   // Enable the clipping property.
601   mStencil.SetProperty(Actor::Property::CLIPPING_MODE, ClippingMode::CLIP_TO_BOUNDING_BOX);
602   mStencil.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
603
604   self.Add(mStencil);
605
606   DevelControl::SetAccessibilityConstructor(self, [](Dali::Actor actor) {
607     return std::make_unique<TextEditorAccessible>(actor, Dali::Accessibility::Role::ENTRY);
608   });
609
610   Accessibility::Bridge::EnabledSignal().Connect(this, &TextEditor::OnAccessibilityStatusChanged);
611   Accessibility::Bridge::DisabledSignal().Connect(this, &TextEditor::OnAccessibilityStatusChanged);
612 }
613
614 void TextEditor::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
615 {
616   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnStyleChange\n");
617
618   switch(change)
619   {
620     case StyleChange::DEFAULT_FONT_CHANGE:
621     {
622       DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnStyleChange DEFAULT_FONT_CHANGE\n");
623       const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
624       // Property system did not set the font so should update it.
625       mController->UpdateAfterFontChange(newFont);
626       RelayoutRequest();
627       break;
628     }
629
630     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
631     {
632       GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
633       RelayoutRequest();
634       break;
635     }
636     case StyleChange::THEME_CHANGE:
637     {
638       // Nothing to do, let control base class handle this
639       break;
640     }
641   }
642
643   // Up call to Control
644   Control::OnStyleChange(styleManager, change);
645 }
646
647 Vector3 TextEditor::GetNaturalSize()
648 {
649   Extents padding;
650   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
651
652   Vector3 naturalSize = mController->GetNaturalSize();
653   naturalSize.width += (padding.start + padding.end);
654   naturalSize.height += (padding.top + padding.bottom);
655
656   return naturalSize;
657 }
658
659 float TextEditor::GetHeightForWidth(float width)
660 {
661   Extents padding;
662   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
663   return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
664 }
665
666 void TextEditor::ResizeActor(Actor& actor, const Vector2& size)
667 {
668   if(actor.GetProperty<Vector3>(Dali::Actor::Property::SIZE).GetVectorXY() != size)
669   {
670     actor.SetProperty(Actor::Property::SIZE, size);
671   }
672 }
673
674 void TextEditor::OnRelayout(const Vector2& size, RelayoutContainer& container)
675 {
676   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor OnRelayout\n");
677
678   Actor self = Self();
679
680   Extents padding;
681   padding = self.GetProperty<Extents>(Toolkit::Control::Property::PADDING);
682
683   Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
684
685   // Support Right-To-Left of padding
686   Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(self);
687
688   if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
689   {
690     std::swap(padding.start, padding.end);
691   }
692
693   if(mStencil)
694   {
695     mStencil.SetProperty(Actor::Property::POSITION, Vector2(padding.start, padding.top));
696     ResizeActor(mStencil, contentSize);
697   }
698   if(mActiveLayer)
699   {
700     mActiveLayer.SetProperty(Actor::Property::POSITION, Vector2(padding.start, padding.top));
701     ResizeActor(mActiveLayer, contentSize);
702   }
703   if(mCursorLayer)
704   {
705     mCursorLayer.SetProperty(Actor::Property::POSITION, Vector2(padding.start, padding.top));
706     ResizeActor(mCursorLayer, contentSize);
707   }
708
709   // If there is text changed, callback is called.
710   if(mTextChanged)
711   {
712     EmitTextChangedSignal();
713   }
714
715   const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
716
717   if((Text::Controller::NONE_UPDATED != updateTextType) ||
718      !mRenderer)
719   {
720     DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnRelayout %p Displaying new contents\n", mController.Get());
721
722     if(mDecorator &&
723        (Text::Controller::NONE_UPDATED != (Text::Controller::DECORATOR_UPDATED & updateTextType)))
724     {
725       mDecorator->Relayout(contentSize);
726     }
727
728     if(!mRenderer)
729     {
730       mRenderer = Backend::Get().NewRenderer(mRenderingBackend);
731     }
732
733     RenderText(updateTextType);
734   }
735
736   if(mCursorPositionChanged)
737   {
738     EmitCursorPositionChangedSignal();
739   }
740
741   if(mSelectionStarted)
742   {
743     EmitSelectionStartedSignal();
744   }
745
746   if(mSelectionChanged)
747   {
748     EmitSelectionChangedSignal();
749   }
750
751   if(mSelectionCleared)
752   {
753     EmitSelectionClearedSignal();
754   }
755
756   // The text-editor emits signals when the input style changes. These changes of style are
757   // detected during the relayout process (size negotiation), i.e after the cursor has been moved. Signals
758   // can't be emitted during the size negotiation as the callbacks may update the UI.
759   // The text-editor adds an idle callback to the adaptor to emit the signals after the size negotiation.
760   if(!mController->IsInputStyleChangedSignalsQueueEmpty())
761   {
762     if(Adaptor::IsAvailable())
763     {
764       Adaptor& adaptor = Adaptor::Get();
765
766       if(NULL == mIdleCallback)
767       {
768         // @note: The callback manager takes the ownership of the callback object.
769         mIdleCallback = MakeCallback(this, &TextEditor::OnIdleSignal);
770         adaptor.AddIdle(mIdleCallback, false);
771       }
772     }
773   }
774 }
775
776 void TextEditor::RenderText(Text::Controller::UpdateTextType updateTextType)
777 {
778   CommonTextUtils::RenderText(Self(), mRenderer, mController, mDecorator, mAlignmentOffset, mRenderableActor, mBackgroundActor, mStencil, mClippingDecorationActors, mAnchorActors, updateTextType);
779   if(mRenderableActor)
780   {
781     ApplyScrollPosition();
782   }
783   UpdateScrollBar();
784 }
785
786 void TextEditor::OnKeyInputFocusGained()
787 {
788   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnKeyInputFocusGained %p\n", mController.Get());
789   if(mInputMethodContext && IsEditable())
790   {
791     // All input panel properties, such as layout, return key type, and input hint, should be set before input panel activates (or shows).
792     mInputMethodContext.ApplyOptions(mInputMethodOptions);
793     mInputMethodContext.NotifyTextInputMultiLine(true);
794
795     mInputMethodContext.StatusChangedSignal().Connect(this, &TextEditor::KeyboardStatusChanged);
796
797     mInputMethodContext.EventReceivedSignal().Connect(this, &TextEditor::OnInputMethodContextEvent);
798
799     // Notify that the text editing start.
800     mInputMethodContext.Activate();
801
802     // When window gain lost focus, the InputMethodContext is deactivated. Thus when window gain focus again, the InputMethodContext must be activated.
803     mInputMethodContext.SetRestoreAfterFocusLost(true);
804   }
805   ClipboardEventNotifier notifier(ClipboardEventNotifier::Get());
806
807   if(notifier)
808   {
809     notifier.ContentSelectedSignal().Connect(this, &TextEditor::OnClipboardTextSelected);
810   }
811
812   mController->KeyboardFocusGainEvent(); // Called in the case of no virtual keyboard to trigger this event
813
814   EmitKeyInputFocusSignal(true); // Calls back into the Control hence done last.
815 }
816
817 void TextEditor::OnKeyInputFocusLost()
818 {
819   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor:OnKeyInputFocusLost %p\n", mController.Get());
820   if(mInputMethodContext)
821   {
822     mInputMethodContext.StatusChangedSignal().Disconnect(this, &TextEditor::KeyboardStatusChanged);
823
824     // The text editing is finished. Therefore the InputMethodContext don't have restore activation.
825     mInputMethodContext.SetRestoreAfterFocusLost(false);
826
827     // Notify that the text editing finish.
828     mInputMethodContext.Deactivate();
829
830     mInputMethodContext.EventReceivedSignal().Disconnect(this, &TextEditor::OnInputMethodContextEvent);
831   }
832   ClipboardEventNotifier notifier(ClipboardEventNotifier::Get());
833
834   if(notifier)
835   {
836     notifier.ContentSelectedSignal().Disconnect(this, &TextEditor::OnClipboardTextSelected);
837   }
838
839   mController->KeyboardFocusLostEvent();
840
841   EmitKeyInputFocusSignal(false); // Calls back into the Control hence done last.
842 }
843
844 bool TextEditor::OnAccessibilityActivated()
845 {
846   SetKeyInputFocus();
847   return true;
848 }
849
850 void TextEditor::OnTap(const TapGesture& gesture)
851 {
852   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnTap %p\n", mController.Get());
853   if(mInputMethodContext && IsEditable())
854   {
855     mInputMethodContext.Activate();
856   }
857   // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
858   Extents padding;
859   padding                   = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
860   const Vector2& localPoint = gesture.GetLocalPoint();
861   mController->TapEvent(gesture.GetNumberOfTaps(), localPoint.x - padding.start, localPoint.y - padding.top);
862   mController->AnchorEvent(localPoint.x - padding.start, localPoint.y - padding.top);
863
864   SetKeyInputFocus();
865 }
866
867 void TextEditor::OnPan(const PanGesture& gesture)
868 {
869   mController->PanEvent(gesture.GetState(), gesture.GetDisplacement());
870 }
871
872 void TextEditor::OnLongPress(const LongPressGesture& gesture)
873 {
874   if(mInputMethodContext && IsEditable())
875   {
876     mInputMethodContext.Activate();
877   }
878   Extents padding;
879   padding                   = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
880   const Vector2& localPoint = gesture.GetLocalPoint();
881   mController->LongPressEvent(gesture.GetState(), localPoint.x - padding.start, localPoint.y - padding.top);
882
883   SetKeyInputFocus();
884 }
885
886 bool TextEditor::OnKeyEvent(const KeyEvent& event)
887 {
888   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnKeyEvent %p keyCode %d\n", mController.Get(), event.GetKeyCode());
889
890   if(Dali::DALI_KEY_ESCAPE == event.GetKeyCode() && mController->ShouldClearFocusOnEscape())
891   {
892     // Make sure ClearKeyInputFocus when only key is up
893     if(event.GetState() == KeyEvent::UP)
894     {
895       ClearKeyInputFocus();
896     }
897
898     return true;
899   }
900
901   return mController->KeyEvent(event);
902 }
903
904 void TextEditor::RequestTextRelayout()
905 {
906   RelayoutRequest();
907 }
908
909 void TextEditor::TextInserted(unsigned int position, unsigned int length, const std::string& content)
910 {
911   if(Accessibility::IsUp())
912   {
913     Control::Impl::GetAccessibilityObject(Self())->EmitTextInserted(position, length, content);
914   }
915 }
916
917 void TextEditor::TextDeleted(unsigned int position, unsigned int length, const std::string& content)
918 {
919   if(Accessibility::IsUp())
920   {
921     Control::Impl::GetAccessibilityObject(Self())->EmitTextDeleted(position, length, content);
922   }
923 }
924
925 void TextEditor::CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition)
926 {
927   if(Accessibility::IsUp())
928   {
929     Control::Impl::GetAccessibilityObject(Self())->EmitTextCursorMoved(newPosition);
930   }
931
932   if((oldPosition != newPosition) && !mCursorPositionChanged)
933   {
934     mCursorPositionChanged = true;
935     mOldPosition           = oldPosition;
936   }
937 }
938
939 void TextEditor::TextChanged(bool immediate)
940 {
941   if(immediate) // Emits TextChangedSignal immediately
942   {
943     EmitTextChangedSignal();
944   }
945   else
946   {
947     mTextChanged = true;
948   }
949 }
950
951 void TextEditor::EmitTextChangedSignal()
952 {
953   Dali::Toolkit::TextEditor handle(GetOwner());
954   mTextChangedSignal.Emit(handle);
955   mTextChanged = false;
956 }
957
958 void TextEditor::MaxLengthReached()
959 {
960   Dali::Toolkit::TextEditor handle(GetOwner());
961   mMaxLengthReachedSignal.Emit(handle);
962 }
963
964 void TextEditor::InputStyleChanged(Text::InputStyle::Mask inputStyleMask)
965 {
966   Dali::Toolkit::TextEditor handle(GetOwner());
967   mInputStyleChangedSignal.Emit(handle, ConvertInputStyle(inputStyleMask));
968 }
969
970 void TextEditor::AnchorClicked(const std::string& href)
971 {
972   Dali::Toolkit::TextEditor handle(GetOwner());
973   mAnchorClickedSignal.Emit(handle, href.c_str(), href.length());
974 }
975
976 void TextEditor::EmitCursorPositionChangedSignal()
977 {
978   Dali::Toolkit::TextEditor handle(GetOwner());
979   mCursorPositionChanged = false;
980   mCursorPositionChangedSignal.Emit(handle, mOldPosition);
981 }
982
983 void TextEditor::InputFiltered(Toolkit::InputFilter::Property::Type type)
984 {
985   Dali::Toolkit::TextEditor handle(GetOwner());
986   mInputFilteredSignal.Emit(handle, type);
987 }
988
989 void TextEditor::EmitSelectionChangedSignal()
990 {
991   Dali::Toolkit::TextEditor handle(GetOwner());
992   mSelectionChangedSignal.Emit(handle, mOldSelectionStart, mOldSelectionEnd);
993   mSelectionChanged = false;
994 }
995
996 void TextEditor::EmitSelectionClearedSignal()
997 {
998   Dali::Toolkit::TextEditor handle(GetOwner());
999   mSelectionClearedSignal.Emit(handle);
1000   mSelectionCleared = false;
1001 }
1002
1003 void TextEditor::EmitSelectionStartedSignal()
1004 {
1005   Dali::Toolkit::TextEditor handle(GetOwner());
1006   mSelectionStartedSignal.Emit(handle);
1007   mSelectionStarted = false;
1008 }
1009
1010 void TextEditor::SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd)
1011 {
1012   if(((oldStart != newStart) || (oldEnd != newEnd)) && !mSelectionChanged)
1013   {
1014     if(newStart == newEnd)
1015     {
1016       mSelectionCleared = true;
1017     }
1018     else
1019     {
1020       if(oldStart == oldEnd)
1021       {
1022         mSelectionStarted = true;
1023       }
1024     }
1025
1026     mSelectionChanged  = true;
1027     mOldSelectionStart = oldStart;
1028     mOldSelectionEnd   = oldEnd;
1029
1030     if(mOldSelectionStart > mOldSelectionEnd)
1031     {
1032       //swap
1033       uint32_t temp      = mOldSelectionStart;
1034       mOldSelectionStart = mOldSelectionEnd;
1035       mOldSelectionEnd   = temp;
1036     }
1037   }
1038 }
1039
1040 void TextEditor::AddDecoration(Actor& actor, DecorationType type, bool needsClipping)
1041 {
1042   if(actor)
1043   {
1044     if(needsClipping)
1045     {
1046       mClippingDecorationActors.push_back(actor);
1047     }
1048
1049     // If the actor is a layer type, add it.
1050     if(type == DecorationType::ACTIVE_LAYER)
1051     {
1052       AddLayer(mActiveLayer, actor);
1053     }
1054     else if(type == DecorationType::CURSOR_LAYER)
1055     {
1056       AddLayer(mCursorLayer, actor);
1057     }
1058   }
1059 }
1060
1061 void TextEditor::AddLayer(Actor& layer, Actor& actor)
1062 {
1063   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
1064   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
1065   Self().Add(actor);
1066   layer = actor;
1067 }
1068
1069 void TextEditor::SetTextSelectionRange(const uint32_t* start, const uint32_t* end)
1070 {
1071   if(mController && mController->IsShowingRealText())
1072   {
1073     mController->SetTextSelectionRange(start, end);
1074     SetKeyInputFocus();
1075   }
1076 }
1077
1078 Uint32Pair TextEditor::GetTextSelectionRange() const
1079 {
1080   Uint32Pair range(0, 0);
1081   if(mController && mController->IsShowingRealText())
1082   {
1083     range = mController->GetTextSelectionRange();
1084   }
1085   return range;
1086 }
1087
1088 void TextEditor::GetControlBackgroundColor(Vector4& color) const
1089 {
1090   Property::Value propValue = Self().GetProperty(Toolkit::Control::Property::BACKGROUND);
1091   Property::Map*  resultMap = propValue.GetMap();
1092
1093   Property::Value* colorValue = nullptr;
1094   if(resultMap && (colorValue = resultMap->Find(ColorVisual::Property::MIX_COLOR)))
1095   {
1096     colorValue->Get(color);
1097   }
1098 }
1099
1100 void TextEditor::UpdateScrollBar()
1101 {
1102   using namespace Dali;
1103
1104   float scrollPosition;
1105   float controlSize;
1106   float layoutSize;
1107   bool  latestScrolled;
1108
1109   if(!mScrollBarEnabled)
1110   {
1111     return;
1112   }
1113   latestScrolled = mController->GetTextScrollInfo(scrollPosition, controlSize, layoutSize);
1114   if(!latestScrolled || controlSize > layoutSize)
1115   {
1116     return;
1117   }
1118
1119   CustomActor self = Self();
1120   if(!mScrollBar)
1121   {
1122     mScrollBar = Toolkit::ScrollBar::New(Toolkit::ScrollBar::VERTICAL);
1123     mScrollBar.SetIndicatorHeightPolicy(Toolkit::ScrollBar::VARIABLE);
1124     mScrollBar.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT);
1125     mScrollBar.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT);
1126     mScrollBar.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
1127     mScrollBar.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH);
1128
1129     // Register the scroll position property
1130     Property::Index propertyScrollPosition = self.RegisterProperty(SCROLL_BAR_POSITION, scrollPosition);
1131     // Register the minimum scroll position property
1132     Property::Index propertyMinScrollPosition = self.RegisterProperty(SCROLL_BAR_POSITION_MIN, 0.0f);
1133     // Register the maximum scroll position property
1134     Property::Index propertyMaxScrollPosition = self.RegisterProperty(SCROLL_BAR_POSITION_MAX, (layoutSize - controlSize));
1135     // Register the scroll content size property
1136     Property::Index propertyScrollContentSize = self.RegisterProperty(SCROLL_BAR_CONTENT_SIZE, layoutSize);
1137
1138     mScrollBar.SetScrollPropertySource(self, propertyScrollPosition, propertyMinScrollPosition, propertyMaxScrollPosition, propertyScrollContentSize);
1139
1140     // Set style name of ScrollBar for styling
1141     mScrollBar.SetStyleName("TextEditorScrollBar");
1142     Toolkit::Control scrollIndicator = Toolkit::Control::DownCast(mScrollBar.GetScrollIndicator());
1143     if(scrollIndicator)
1144     {
1145       // Set style name of ScrollBarIndicator for styling
1146       scrollIndicator.SetStyleName("TextEditorScrollBarIndicator");
1147     }
1148
1149     self.Add(mScrollBar);
1150   }
1151   else
1152   {
1153     Property::Index propertyScrollPosition    = self.GetPropertyIndex(SCROLL_BAR_POSITION);
1154     Property::Index propertyMaxScrollPosition = self.GetPropertyIndex(SCROLL_BAR_POSITION_MAX);
1155     Property::Index propertyScrollContentSize = self.GetPropertyIndex(SCROLL_BAR_CONTENT_SIZE);
1156
1157     self.SetProperty(propertyScrollPosition, scrollPosition);
1158     self.SetProperty(propertyMaxScrollPosition, (layoutSize - controlSize));
1159     self.SetProperty(propertyScrollContentSize, layoutSize);
1160   }
1161
1162   // If scrolling is not started, start scrolling and emit ScrollStateChangedSignal
1163   if(!mScrollStarted)
1164   {
1165     mScrollStarted = true;
1166     Dali::Toolkit::TextEditor handle(GetOwner());
1167     mScrollStateChangedSignal.Emit(handle, Toolkit::TextEditor::Scroll::STARTED);
1168   }
1169
1170   Actor indicator = mScrollBar.GetScrollIndicator();
1171   if(mAnimation)
1172   {
1173     mAnimation.Stop(); // Cancel any animation
1174   }
1175   else
1176   {
1177     mAnimation = Animation::New(mAnimationPeriod.durationSeconds);
1178   }
1179   indicator.SetProperty(Actor::Property::OPACITY, 1.0f);
1180   mAnimation.AnimateTo(Property(indicator, Actor::Property::COLOR_ALPHA), 0.0f, AlphaFunction::EASE_IN, mAnimationPeriod);
1181   mAnimation.Play();
1182   mAnimation.FinishedSignal().Connect(this, &TextEditor::OnScrollIndicatorAnimationFinished);
1183 }
1184
1185 void TextEditor::OnScrollIndicatorAnimationFinished(Animation& animation)
1186 {
1187   // If animation is successfully ended, then emit ScrollStateChangedSignal
1188   if(animation.GetCurrentProgress() == 0.0f)
1189   {
1190     mScrollStarted = false;
1191     Dali::Toolkit::TextEditor handle(GetOwner());
1192     mScrollStateChangedSignal.Emit(handle, Toolkit::TextEditor::Scroll::FINISHED);
1193   }
1194 }
1195
1196 void TextEditor::OnSceneConnect(Dali::Actor actor)
1197 {
1198   if(mHasBeenStaged)
1199   {
1200     RenderText(static_cast<Text::Controller::UpdateTextType>(Text::Controller::MODEL_UPDATED | Text::Controller::DECORATOR_UPDATED));
1201   }
1202   else
1203   {
1204     mHasBeenStaged = true;
1205   }
1206 }
1207
1208 InputMethodContext::CallbackData TextEditor::OnInputMethodContextEvent(Dali::InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
1209 {
1210   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnInputMethodContextEvent %p eventName %d\n", mController.Get(), inputMethodContextEvent.eventName);
1211   return mController->OnInputMethodContextEvent(inputMethodContext, inputMethodContextEvent);
1212 }
1213
1214 void TextEditor::GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType)
1215 {
1216   if(mDecorator)
1217   {
1218     Property::Map map;
1219     map[TextEditor::PropertyHandler::IMAGE_MAP_FILENAME_STRING] = mDecorator->GetHandleImage(handleType, handleImageType);
1220
1221     value = map;
1222   }
1223 }
1224
1225 void TextEditor::OnClipboardTextSelected(ClipboardEventNotifier& clipboard)
1226 {
1227   mController->PasteClipboardItemEvent();
1228 }
1229
1230 void TextEditor::KeyboardStatusChanged(bool keyboardShown)
1231 {
1232   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::KeyboardStatusChanged %p keyboardShown %d\n", mController.Get(), keyboardShown);
1233
1234   // Just hide the grab handle when keyboard is hidden.
1235   if(!keyboardShown)
1236   {
1237     mController->KeyboardFocusLostEvent();
1238   }
1239   else
1240   {
1241     mController->KeyboardFocusGainEvent(); // Initially called by OnKeyInputFocusGained
1242   }
1243 }
1244
1245 void TextEditor::OnSceneConnection(int depth)
1246 {
1247   // Sets the depth to the visuals inside the text's decorator.
1248   mDecorator->SetTextDepth(depth);
1249
1250   // The depth of the text renderer is set in the RenderText() called from OnRelayout().
1251
1252   // Call the Control::OnSceneConnection() to set the depth of the background.
1253   Control::OnSceneConnection(depth);
1254 }
1255
1256 bool TextEditor::OnTouched(Actor actor, const TouchEvent& touch)
1257 {
1258   return false;
1259 }
1260
1261 void TextEditor::OnIdleSignal()
1262 {
1263   // Emits the change of input style signals.
1264   mController->ProcessInputStyleChangedSignals();
1265
1266   // Set the pointer to null as the callback manager deletes the callback after execute it.
1267   mIdleCallback = NULL;
1268 }
1269
1270 void TextEditor::ApplyScrollPosition()
1271 {
1272   const Vector2& scrollOffset = mController->GetTextModel()->GetScrollPosition();
1273   float          scrollAmount = 0.0f;
1274
1275   if(mScrollAnimationEnabled)
1276   {
1277     scrollAmount = mController->GetScrollAmountByUserInput();
1278   }
1279   if(mTextVerticalScroller)
1280   {
1281     mTextVerticalScroller->CheckStartAnimation(mRenderableActor, scrollOffset.x + mAlignmentOffset, scrollOffset.y - scrollAmount, scrollAmount);
1282   }
1283   else if(Equals(scrollAmount, 0.0f, Math::MACHINE_EPSILON_1))
1284   {
1285     mRenderableActor.SetProperty(Actor::Property::POSITION, Vector2(scrollOffset.x + mAlignmentOffset, scrollOffset.y - scrollAmount));
1286   }
1287   else
1288   {
1289     mTextVerticalScroller = Text::TextVerticalScroller::New();
1290     if(!Equals(mScrollAnimationDuration, 0.0f, Math::MACHINE_EPSILON_1))
1291     {
1292       mTextVerticalScroller->SetDuration(mScrollAnimationDuration);
1293     }
1294     mTextVerticalScroller->CheckStartAnimation(mRenderableActor, scrollOffset.x + mAlignmentOffset, scrollOffset.y - scrollAmount, scrollAmount);
1295   }
1296 }
1297
1298 bool TextEditor::IsEditable() const
1299 {
1300   return mController->IsEditable();
1301 }
1302
1303 void TextEditor::SetEditable(bool editable)
1304 {
1305   mController->SetEditable(editable);
1306   if(mInputMethodContext && !editable)
1307   {
1308     mInputMethodContext.Deactivate();
1309   }
1310 }
1311
1312 void TextEditor::OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type)
1313 {
1314   mController->ChangedLayoutDirection();
1315 }
1316
1317 TextEditor::TextEditor()
1318 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
1319   mAnimationPeriod(0.0f, 0.0f),
1320   mIdleCallback(NULL),
1321   mAlignmentOffset(0.f),
1322   mScrollAnimationDuration(0.f),
1323   mLineSpacing(0.f),
1324   mRenderingBackend(DEFAULT_RENDERING_BACKEND),
1325   mHasBeenStaged(false),
1326   mScrollAnimationEnabled(false),
1327   mScrollBarEnabled(false),
1328   mScrollStarted(false),
1329   mTextChanged(false),
1330   mCursorPositionChanged(false),
1331   mSelectionChanged(false),
1332   mSelectionCleared(false),
1333   mOldPosition(0u),
1334   mOldSelectionStart(0u),
1335   mOldSelectionEnd(0u),
1336   mSelectionStarted(false)
1337 {
1338 }
1339
1340 TextEditor::~TextEditor()
1341 {
1342   UnparentAndReset(mStencil);
1343
1344   if((NULL != mIdleCallback) && Adaptor::IsAvailable())
1345   {
1346     // Removes the callback from the callback manager in case the text-editor is destroyed before the callback is executed.
1347     Adaptor::Get().RemoveIdle(mIdleCallback);
1348   }
1349 }
1350
1351 std::string TextEditor::TextEditorAccessible::GetName() const
1352 {
1353   auto self = Toolkit::TextEditor::DownCast(Self());
1354   return self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1355 }
1356
1357 std::string TextEditor::TextEditorAccessible::GetText(size_t startOffset, size_t endOffset) const
1358 {
1359   if(endOffset <= startOffset)
1360   {
1361     return {};
1362   }
1363
1364   auto self = Toolkit::TextEditor::DownCast(Self());
1365   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1366
1367   if(startOffset > text.size() || endOffset > text.size())
1368   {
1369     return {};
1370   }
1371
1372   return text.substr(startOffset, endOffset - startOffset);
1373 }
1374
1375 size_t TextEditor::TextEditorAccessible::GetCharacterCount() const
1376 {
1377   auto self = Toolkit::TextEditor::DownCast(Self());
1378   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1379
1380   return text.size();
1381 }
1382
1383 size_t TextEditor::TextEditorAccessible::GetCursorOffset() const
1384 {
1385   auto slf = Toolkit::TextEditor::DownCast(Self());
1386   return Dali::Toolkit::GetImpl(slf).GetTextController()->GetCursorPosition();
1387 }
1388
1389 bool TextEditor::TextEditorAccessible::SetCursorOffset(size_t offset)
1390 {
1391   auto slf = Toolkit::TextEditor::DownCast(Self());
1392   auto txt = slf.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1393   if(offset > txt.size())
1394   {
1395     return false;
1396   }
1397
1398   auto& slfImpl = Dali::Toolkit::GetImpl(slf);
1399   slfImpl.GetTextController()->ResetCursorPosition(offset);
1400   slfImpl.RequestTextRelayout();
1401
1402   return true;
1403 }
1404
1405 Dali::Accessibility::Range TextEditor::TextEditorAccessible::GetTextAtOffset(size_t offset, Dali::Accessibility::TextBoundary boundary) const
1406 {
1407   auto self     = Toolkit::TextEditor::DownCast(Self());
1408   auto text     = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1409   auto textSize = text.size();
1410
1411   auto range = Dali::Accessibility::Range{};
1412
1413   switch(boundary)
1414   {
1415     case Dali::Accessibility::TextBoundary::CHARACTER:
1416     {
1417       if(offset < textSize)
1418       {
1419         range.content     = text[offset];
1420         range.startOffset = offset;
1421         range.endOffset   = offset + 1;
1422       }
1423       break;
1424     }
1425     case Dali::Accessibility::TextBoundary::WORD:
1426     case Dali::Accessibility::TextBoundary::LINE:
1427     {
1428       auto textString = text.c_str();
1429       auto breaks     = std::vector<char>(textSize, 0);
1430
1431       if(boundary == Dali::Accessibility::TextBoundary::WORD)
1432       {
1433         Accessibility::Accessible::FindWordSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1434       }
1435       else
1436       {
1437         Accessibility::Accessible::FindLineSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1438       }
1439
1440       auto index   = 0u;
1441       auto counter = 0u;
1442       while(index < textSize && counter <= offset)
1443       {
1444         auto start = index;
1445         if(breaks[index])
1446         {
1447           while(breaks[index])
1448           {
1449             index++;
1450           }
1451           counter++;
1452         }
1453         else
1454         {
1455           if(boundary == Dali::Accessibility::TextBoundary::WORD)
1456           {
1457             index++;
1458           }
1459           if(boundary == Dali::Accessibility::TextBoundary::LINE)
1460           {
1461             counter++;
1462           }
1463         }
1464
1465         if((counter > 0) && ((counter - 1) == offset))
1466         {
1467           range.content     = text.substr(start, index - start + 1);
1468           range.startOffset = start;
1469           range.endOffset   = index + 1;
1470         }
1471
1472         if(boundary == Dali::Accessibility::TextBoundary::LINE)
1473         {
1474           index++;
1475         }
1476       }
1477       break;
1478     }
1479     case Dali::Accessibility::TextBoundary::SENTENCE:
1480     {
1481       /* not supported by default */
1482       break;
1483     }
1484     case Dali::Accessibility::TextBoundary::PARAGRAPH:
1485     {
1486       /* Paragraph is not supported by libunibreak library */
1487       break;
1488     }
1489     default:
1490       break;
1491   }
1492
1493   return range;
1494 }
1495
1496 Dali::Accessibility::Range TextEditor::TextEditorAccessible::GetRangeOfSelection(size_t selectionIndex) const
1497 {
1498   // Since DALi supports only one selection indexes higher than 0 are ignored
1499   if(selectionIndex > 0)
1500   {
1501     return {};
1502   }
1503
1504   auto        self       = Toolkit::TextEditor::DownCast(Self());
1505   auto        controller = Dali::Toolkit::GetImpl(self).GetTextController();
1506   std::string value{};
1507   controller->RetrieveSelection(value);
1508   auto indices = controller->GetSelectionIndexes();
1509
1510   return {static_cast<size_t>(indices.first), static_cast<size_t>(indices.second), value};
1511 }
1512
1513 bool TextEditor::TextEditorAccessible::RemoveSelection(size_t selectionIndex)
1514 {
1515   // Since DALi supports only one selection indexes higher than 0 are ignored
1516   if(selectionIndex > 0)
1517   {
1518     return false;
1519   }
1520
1521   auto self = Toolkit::TextEditor::DownCast(Self());
1522   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(0, 0);
1523   return true;
1524 }
1525
1526 bool TextEditor::TextEditorAccessible::SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset)
1527 {
1528   // Since DALi supports only one selection indexes higher than 0 are ignored
1529   if(selectionIndex > 0)
1530   {
1531     return false;
1532   }
1533
1534   auto self = Toolkit::TextEditor::DownCast(Self());
1535   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(startOffset, endOffset);
1536   return true;
1537 }
1538
1539 Rect<> TextEditor::TextEditorAccessible::GetRangeExtents(size_t startOffset, size_t endOffset, Accessibility::CoordinateType type)
1540 {
1541   if (endOffset <= startOffset || endOffset <= 0)
1542   {
1543     return {0, 0, 0, 0};
1544   }
1545
1546   auto self = Toolkit::TextEditor::DownCast(Self());
1547   auto rect = Dali::Toolkit::GetImpl(self).GetTextController()->GetTextBoundingRectangle(startOffset, endOffset - 1);
1548
1549   auto componentExtents = this->GetExtents(type);
1550
1551   rect.x += componentExtents.x;
1552   rect.y += componentExtents.y;
1553
1554   return rect;
1555 }
1556
1557 bool TextEditor::TextEditorAccessible::CopyText(size_t startPosition, size_t endPosition)
1558 {
1559   if(endPosition <= startPosition)
1560   {
1561     return false;
1562   }
1563
1564   auto self = Toolkit::TextEditor::DownCast(Self());
1565   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1566   Dali::Toolkit::GetImpl(self).GetTextController()->CopyStringToClipboard(text.substr(startPosition, endPosition - startPosition));
1567
1568   return true;
1569 }
1570
1571 bool TextEditor::TextEditorAccessible::CutText(size_t startPosition, size_t endPosition)
1572 {
1573   if(endPosition <= startPosition)
1574   {
1575     return false;
1576   }
1577
1578   auto self = Toolkit::TextEditor::DownCast(Self());
1579   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1580   Dali::Toolkit::GetImpl(self).GetTextController()->CopyStringToClipboard(text.substr(startPosition, endPosition - startPosition));
1581
1582   self.SetProperty(Toolkit::TextEditor::Property::TEXT, text.substr(0, startPosition) + text.substr(endPosition));
1583
1584   return true;
1585 }
1586
1587 bool TextEditor::TextEditorAccessible::DeleteText(size_t startPosition, size_t endPosition)
1588 {
1589   if(endPosition <= startPosition)
1590   {
1591     return false;
1592   }
1593
1594   auto self = Toolkit::TextEditor::DownCast(Self());
1595   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1596
1597   self.SetProperty(Toolkit::TextEditor::Property::TEXT, text.substr(0, startPosition) + text.substr(endPosition));
1598
1599   return true;
1600 }
1601
1602 Dali::Accessibility::States TextEditor::TextEditorAccessible::CalculateStates()
1603 {
1604   using namespace Dali::Accessibility;
1605
1606   auto states              = DevelControl::ControlAccessible::CalculateStates();
1607   states[State::EDITABLE]  = true;
1608   states[State::FOCUSABLE] = true;
1609
1610   Toolkit::Control focusControl = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
1611   if(Self() == focusControl)
1612   {
1613     states[State::FOCUSED] = true;
1614   }
1615
1616   return states;
1617 }
1618
1619 bool TextEditor::TextEditorAccessible::InsertText(size_t startPosition, std::string text)
1620 {
1621   auto self         = Toolkit::TextEditor::DownCast(Self());
1622   auto insertedText = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1623
1624   insertedText.insert(startPosition, text);
1625
1626   self.SetProperty(Toolkit::TextEditor::Property::TEXT, std::move(insertedText));
1627
1628   return true;
1629 }
1630
1631 bool TextEditor::TextEditorAccessible::SetTextContents(std::string newContents)
1632 {
1633   auto self = Toolkit::TextEditor::DownCast(Self());
1634   self.SetProperty(Toolkit::TextEditor::Property::TEXT, std::move(newContents));
1635   return true;
1636 }
1637
1638 int32_t TextEditor::TextEditorAccessible::GetLinkCount() const
1639 {
1640   auto self = Toolkit::TextEditor::DownCast(Self());
1641   return Dali::Toolkit::GetImpl(self).mAnchorActors.size();
1642 }
1643
1644 Accessibility::Hyperlink* TextEditor::TextEditorAccessible::GetLink(int32_t linkIndex) const
1645 {
1646   if(linkIndex < 0 || linkIndex >= GetLinkCount())
1647   {
1648     return nullptr;
1649   }
1650   auto self        = Toolkit::TextEditor::DownCast(Self());
1651   auto anchorActor = Dali::Toolkit::GetImpl(self).mAnchorActors[linkIndex];
1652   return dynamic_cast<Accessibility::Hyperlink*>(Dali::Accessibility::Accessible::Get(anchorActor));
1653 }
1654
1655 int32_t TextEditor::TextEditorAccessible::GetLinkIndex(int32_t characterOffset) const
1656 {
1657   auto self       = Toolkit::TextEditor::DownCast(Self());
1658   auto controller = Dali::Toolkit::GetImpl(self).GetTextController();
1659   return controller->GetAnchorIndex(static_cast<size_t>(characterOffset));
1660 }
1661
1662 } // namespace Internal
1663
1664 } // namespace Toolkit
1665
1666 } // namespace Dali