[AT-SPI] text: add "GetRangeExtents" interface
[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::unique_ptr<Dali::Accessibility::Accessible>(
608       new AccessibleImpl(actor, Dali::Accessibility::Role::ENTRY));
609   });
610
611   Accessibility::Bridge::EnabledSignal().Connect(this, &TextEditor::OnAccessibilityStatusChanged);
612   Accessibility::Bridge::DisabledSignal().Connect(this, &TextEditor::OnAccessibilityStatusChanged);
613 }
614
615 void TextEditor::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
616 {
617   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnStyleChange\n");
618
619   switch(change)
620   {
621     case StyleChange::DEFAULT_FONT_CHANGE:
622     {
623       DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnStyleChange DEFAULT_FONT_CHANGE\n");
624       const std::string& newFont = GetImpl(styleManager).GetDefaultFontFamily();
625       // Property system did not set the font so should update it.
626       mController->UpdateAfterFontChange(newFont);
627       RelayoutRequest();
628       break;
629     }
630
631     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
632     {
633       GetImpl(styleManager).ApplyThemeStyle(Toolkit::Control(GetOwner()));
634       RelayoutRequest();
635       break;
636     }
637     case StyleChange::THEME_CHANGE:
638     {
639       // Nothing to do, let control base class handle this
640       break;
641     }
642   }
643
644   // Up call to Control
645   Control::OnStyleChange(styleManager, change);
646 }
647
648 Vector3 TextEditor::GetNaturalSize()
649 {
650   Extents padding;
651   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
652
653   Vector3 naturalSize = mController->GetNaturalSize();
654   naturalSize.width += (padding.start + padding.end);
655   naturalSize.height += (padding.top + padding.bottom);
656
657   return naturalSize;
658 }
659
660 float TextEditor::GetHeightForWidth(float width)
661 {
662   Extents padding;
663   padding = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
664   return mController->GetHeightForWidth(width) + padding.top + padding.bottom;
665 }
666
667 void TextEditor::ResizeActor(Actor& actor, const Vector2& size)
668 {
669   if(actor.GetProperty<Vector3>(Dali::Actor::Property::SIZE).GetVectorXY() != size)
670   {
671     actor.SetProperty(Actor::Property::SIZE, size);
672   }
673 }
674
675 void TextEditor::OnRelayout(const Vector2& size, RelayoutContainer& container)
676 {
677   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor OnRelayout\n");
678
679   Actor self = Self();
680
681   Extents padding;
682   padding = self.GetProperty<Extents>(Toolkit::Control::Property::PADDING);
683
684   Vector2 contentSize(size.x - (padding.start + padding.end), size.y - (padding.top + padding.bottom));
685
686   // Support Right-To-Left of padding
687   Dali::LayoutDirection::Type layoutDirection = mController->GetLayoutDirection(self);
688
689   if(Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
690   {
691     std::swap(padding.start, padding.end);
692   }
693
694   if(mStencil)
695   {
696     mStencil.SetProperty(Actor::Property::POSITION, Vector2(padding.start, padding.top));
697     ResizeActor(mStencil, contentSize);
698   }
699   if(mActiveLayer)
700   {
701     mActiveLayer.SetProperty(Actor::Property::POSITION, Vector2(padding.start, padding.top));
702     ResizeActor(mActiveLayer, contentSize);
703   }
704   if(mCursorLayer)
705   {
706     mCursorLayer.SetProperty(Actor::Property::POSITION, Vector2(padding.start, padding.top));
707     ResizeActor(mCursorLayer, contentSize);
708   }
709
710   // If there is text changed, callback is called.
711   if(mTextChanged)
712   {
713     EmitTextChangedSignal();
714   }
715
716   const Text::Controller::UpdateTextType updateTextType = mController->Relayout(contentSize, layoutDirection);
717
718   if((Text::Controller::NONE_UPDATED != updateTextType) ||
719      !mRenderer)
720   {
721     DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnRelayout %p Displaying new contents\n", mController.Get());
722
723     if(mDecorator &&
724        (Text::Controller::NONE_UPDATED != (Text::Controller::DECORATOR_UPDATED & updateTextType)))
725     {
726       mDecorator->Relayout(contentSize);
727     }
728
729     if(!mRenderer)
730     {
731       mRenderer = Backend::Get().NewRenderer(mRenderingBackend);
732     }
733
734     RenderText(updateTextType);
735   }
736
737   if(mCursorPositionChanged)
738   {
739     EmitCursorPositionChangedSignal();
740   }
741
742   if(mSelectionStarted)
743   {
744     EmitSelectionStartedSignal();
745   }
746
747   if(mSelectionChanged)
748   {
749     EmitSelectionChangedSignal();
750   }
751
752   if(mSelectionCleared)
753   {
754     EmitSelectionClearedSignal();
755   }
756
757   // The text-editor emits signals when the input style changes. These changes of style are
758   // detected during the relayout process (size negotiation), i.e after the cursor has been moved. Signals
759   // can't be emitted during the size negotiation as the callbacks may update the UI.
760   // The text-editor adds an idle callback to the adaptor to emit the signals after the size negotiation.
761   if(!mController->IsInputStyleChangedSignalsQueueEmpty())
762   {
763     if(Adaptor::IsAvailable())
764     {
765       Adaptor& adaptor = Adaptor::Get();
766
767       if(NULL == mIdleCallback)
768       {
769         // @note: The callback manager takes the ownership of the callback object.
770         mIdleCallback = MakeCallback(this, &TextEditor::OnIdleSignal);
771         adaptor.AddIdle(mIdleCallback, false);
772       }
773     }
774   }
775 }
776
777 void TextEditor::RenderText(Text::Controller::UpdateTextType updateTextType)
778 {
779   CommonTextUtils::RenderText(Self(), mRenderer, mController, mDecorator, mAlignmentOffset, mRenderableActor, mBackgroundActor, mStencil, mClippingDecorationActors, mAnchorActors, updateTextType);
780   if(mRenderableActor)
781   {
782     ApplyScrollPosition();
783   }
784   UpdateScrollBar();
785 }
786
787 void TextEditor::OnKeyInputFocusGained()
788 {
789   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnKeyInputFocusGained %p\n", mController.Get());
790   if(mInputMethodContext && IsEditable())
791   {
792     // All input panel properties, such as layout, return key type, and input hint, should be set before input panel activates (or shows).
793     mInputMethodContext.ApplyOptions(mInputMethodOptions);
794     mInputMethodContext.NotifyTextInputMultiLine(true);
795
796     mInputMethodContext.StatusChangedSignal().Connect(this, &TextEditor::KeyboardStatusChanged);
797
798     mInputMethodContext.EventReceivedSignal().Connect(this, &TextEditor::OnInputMethodContextEvent);
799
800     // Notify that the text editing start.
801     mInputMethodContext.Activate();
802
803     // When window gain lost focus, the InputMethodContext is deactivated. Thus when window gain focus again, the InputMethodContext must be activated.
804     mInputMethodContext.SetRestoreAfterFocusLost(true);
805   }
806   ClipboardEventNotifier notifier(ClipboardEventNotifier::Get());
807
808   if(notifier)
809   {
810     notifier.ContentSelectedSignal().Connect(this, &TextEditor::OnClipboardTextSelected);
811   }
812
813   mController->KeyboardFocusGainEvent(); // Called in the case of no virtual keyboard to trigger this event
814
815   EmitKeyInputFocusSignal(true); // Calls back into the Control hence done last.
816 }
817
818 void TextEditor::OnKeyInputFocusLost()
819 {
820   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor:OnKeyInputFocusLost %p\n", mController.Get());
821   if(mInputMethodContext)
822   {
823     mInputMethodContext.StatusChangedSignal().Disconnect(this, &TextEditor::KeyboardStatusChanged);
824
825     // The text editing is finished. Therefore the InputMethodContext don't have restore activation.
826     mInputMethodContext.SetRestoreAfterFocusLost(false);
827
828     // Notify that the text editing finish.
829     mInputMethodContext.Deactivate();
830
831     mInputMethodContext.EventReceivedSignal().Disconnect(this, &TextEditor::OnInputMethodContextEvent);
832   }
833   ClipboardEventNotifier notifier(ClipboardEventNotifier::Get());
834
835   if(notifier)
836   {
837     notifier.ContentSelectedSignal().Disconnect(this, &TextEditor::OnClipboardTextSelected);
838   }
839
840   mController->KeyboardFocusLostEvent();
841
842   EmitKeyInputFocusSignal(false); // Calls back into the Control hence done last.
843 }
844
845 bool TextEditor::OnAccessibilityActivated()
846 {
847   SetKeyInputFocus();
848   return true;
849 }
850
851 void TextEditor::OnTap(const TapGesture& gesture)
852 {
853   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnTap %p\n", mController.Get());
854   if(mInputMethodContext && IsEditable())
855   {
856     mInputMethodContext.Activate();
857   }
858   // Deliver the tap before the focus event to controller; this allows us to detect when focus is gained due to tap-gestures
859   Extents padding;
860   padding                   = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
861   const Vector2& localPoint = gesture.GetLocalPoint();
862   mController->TapEvent(gesture.GetNumberOfTaps(), localPoint.x - padding.start, localPoint.y - padding.top);
863   mController->AnchorEvent(localPoint.x - padding.start, localPoint.y - padding.top);
864
865   SetKeyInputFocus();
866 }
867
868 void TextEditor::OnPan(const PanGesture& gesture)
869 {
870   mController->PanEvent(gesture.GetState(), gesture.GetDisplacement());
871 }
872
873 void TextEditor::OnLongPress(const LongPressGesture& gesture)
874 {
875   if(mInputMethodContext && IsEditable())
876   {
877     mInputMethodContext.Activate();
878   }
879   Extents padding;
880   padding                   = Self().GetProperty<Extents>(Toolkit::Control::Property::PADDING);
881   const Vector2& localPoint = gesture.GetLocalPoint();
882   mController->LongPressEvent(gesture.GetState(), localPoint.x - padding.start, localPoint.y - padding.top);
883
884   SetKeyInputFocus();
885 }
886
887 bool TextEditor::OnKeyEvent(const KeyEvent& event)
888 {
889   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnKeyEvent %p keyCode %d\n", mController.Get(), event.GetKeyCode());
890
891   if(Dali::DALI_KEY_ESCAPE == event.GetKeyCode() && mController->ShouldClearFocusOnEscape())
892   {
893     // Make sure ClearKeyInputFocus when only key is up
894     if(event.GetState() == KeyEvent::UP)
895     {
896       ClearKeyInputFocus();
897     }
898
899     return true;
900   }
901
902   return mController->KeyEvent(event);
903 }
904
905 void TextEditor::RequestTextRelayout()
906 {
907   RelayoutRequest();
908 }
909
910 void TextEditor::TextInserted(unsigned int position, unsigned int length, const std::string& content)
911 {
912   if(Accessibility::IsUp())
913   {
914     Control::Impl::GetAccessibilityObject(Self())->EmitTextInserted(position, length, content);
915   }
916 }
917
918 void TextEditor::TextDeleted(unsigned int position, unsigned int length, const std::string& content)
919 {
920   if(Accessibility::IsUp())
921   {
922     Control::Impl::GetAccessibilityObject(Self())->EmitTextDeleted(position, length, content);
923   }
924 }
925
926 void TextEditor::CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition)
927 {
928   if(Accessibility::IsUp())
929   {
930     Control::Impl::GetAccessibilityObject(Self())->EmitTextCursorMoved(newPosition);
931   }
932
933   if((oldPosition != newPosition) && !mCursorPositionChanged)
934   {
935     mCursorPositionChanged = true;
936     mOldPosition           = oldPosition;
937   }
938 }
939
940 void TextEditor::TextChanged(bool immediate)
941 {
942   if(immediate) // Emits TextChangedSignal immediately
943   {
944     EmitTextChangedSignal();
945   }
946   else
947   {
948     mTextChanged = true;
949   }
950 }
951
952 void TextEditor::EmitTextChangedSignal()
953 {
954   Dali::Toolkit::TextEditor handle(GetOwner());
955   mTextChangedSignal.Emit(handle);
956   mTextChanged = false;
957 }
958
959 void TextEditor::MaxLengthReached()
960 {
961   Dali::Toolkit::TextEditor handle(GetOwner());
962   mMaxLengthReachedSignal.Emit(handle);
963 }
964
965 void TextEditor::InputStyleChanged(Text::InputStyle::Mask inputStyleMask)
966 {
967   Dali::Toolkit::TextEditor handle(GetOwner());
968   mInputStyleChangedSignal.Emit(handle, ConvertInputStyle(inputStyleMask));
969 }
970
971 void TextEditor::AnchorClicked(const std::string& href)
972 {
973   Dali::Toolkit::TextEditor handle(GetOwner());
974   mAnchorClickedSignal.Emit(handle, href.c_str(), href.length());
975 }
976
977 void TextEditor::EmitCursorPositionChangedSignal()
978 {
979   Dali::Toolkit::TextEditor handle(GetOwner());
980   mCursorPositionChanged = false;
981   mCursorPositionChangedSignal.Emit(handle, mOldPosition);
982 }
983
984 void TextEditor::InputFiltered(Toolkit::InputFilter::Property::Type type)
985 {
986   Dali::Toolkit::TextEditor handle(GetOwner());
987   mInputFilteredSignal.Emit(handle, type);
988 }
989
990 void TextEditor::EmitSelectionChangedSignal()
991 {
992   Dali::Toolkit::TextEditor handle(GetOwner());
993   mSelectionChangedSignal.Emit(handle, mOldSelectionStart, mOldSelectionEnd);
994   mSelectionChanged = false;
995 }
996
997 void TextEditor::EmitSelectionClearedSignal()
998 {
999   Dali::Toolkit::TextEditor handle(GetOwner());
1000   mSelectionClearedSignal.Emit(handle);
1001   mSelectionCleared = false;
1002 }
1003
1004 void TextEditor::EmitSelectionStartedSignal()
1005 {
1006   Dali::Toolkit::TextEditor handle(GetOwner());
1007   mSelectionStartedSignal.Emit(handle);
1008   mSelectionStarted = false;
1009 }
1010
1011 void TextEditor::SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd)
1012 {
1013   if(((oldStart != newStart) || (oldEnd != newEnd)) && !mSelectionChanged)
1014   {
1015     if(newStart == newEnd)
1016     {
1017       mSelectionCleared = true;
1018     }
1019     else
1020     {
1021       if(oldStart == oldEnd)
1022       {
1023         mSelectionStarted = true;
1024       }
1025     }
1026
1027     mSelectionChanged  = true;
1028     mOldSelectionStart = oldStart;
1029     mOldSelectionEnd   = oldEnd;
1030
1031     if(mOldSelectionStart > mOldSelectionEnd)
1032     {
1033       //swap
1034       uint32_t temp      = mOldSelectionStart;
1035       mOldSelectionStart = mOldSelectionEnd;
1036       mOldSelectionEnd   = temp;
1037     }
1038   }
1039 }
1040
1041 void TextEditor::AddDecoration(Actor& actor, DecorationType type, bool needsClipping)
1042 {
1043   if(actor)
1044   {
1045     if(needsClipping)
1046     {
1047       mClippingDecorationActors.push_back(actor);
1048     }
1049
1050     // If the actor is a layer type, add it.
1051     if(type == DecorationType::ACTIVE_LAYER)
1052     {
1053       AddLayer(mActiveLayer, actor);
1054     }
1055     else if(type == DecorationType::CURSOR_LAYER)
1056     {
1057       AddLayer(mCursorLayer, actor);
1058     }
1059   }
1060 }
1061
1062 void TextEditor::AddLayer(Actor& layer, Actor& actor)
1063 {
1064   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
1065   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
1066   Self().Add(actor);
1067   layer = actor;
1068 }
1069
1070 void TextEditor::SetTextSelectionRange(const uint32_t* start, const uint32_t* end)
1071 {
1072   if(mController && mController->IsShowingRealText())
1073   {
1074     mController->SetTextSelectionRange(start, end);
1075     SetKeyInputFocus();
1076   }
1077 }
1078
1079 Uint32Pair TextEditor::GetTextSelectionRange() const
1080 {
1081   Uint32Pair range(0, 0);
1082   if(mController && mController->IsShowingRealText())
1083   {
1084     range = mController->GetTextSelectionRange();
1085   }
1086   return range;
1087 }
1088
1089 void TextEditor::GetControlBackgroundColor(Vector4& color) const
1090 {
1091   Property::Value propValue = Self().GetProperty(Toolkit::Control::Property::BACKGROUND);
1092   Property::Map*  resultMap = propValue.GetMap();
1093
1094   Property::Value* colorValue = nullptr;
1095   if(resultMap && (colorValue = resultMap->Find(ColorVisual::Property::MIX_COLOR)))
1096   {
1097     colorValue->Get(color);
1098   }
1099 }
1100
1101 void TextEditor::UpdateScrollBar()
1102 {
1103   using namespace Dali;
1104
1105   float scrollPosition;
1106   float controlSize;
1107   float layoutSize;
1108   bool  latestScrolled;
1109
1110   if(!mScrollBarEnabled)
1111   {
1112     return;
1113   }
1114   latestScrolled = mController->GetTextScrollInfo(scrollPosition, controlSize, layoutSize);
1115   if(!latestScrolled || controlSize > layoutSize)
1116   {
1117     return;
1118   }
1119
1120   CustomActor self = Self();
1121   if(!mScrollBar)
1122   {
1123     mScrollBar = Toolkit::ScrollBar::New(Toolkit::ScrollBar::VERTICAL);
1124     mScrollBar.SetIndicatorHeightPolicy(Toolkit::ScrollBar::VARIABLE);
1125     mScrollBar.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_RIGHT);
1126     mScrollBar.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_RIGHT);
1127     mScrollBar.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
1128     mScrollBar.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH);
1129
1130     // Register the scroll position property
1131     Property::Index propertyScrollPosition = self.RegisterProperty(SCROLL_BAR_POSITION, scrollPosition);
1132     // Register the minimum scroll position property
1133     Property::Index propertyMinScrollPosition = self.RegisterProperty(SCROLL_BAR_POSITION_MIN, 0.0f);
1134     // Register the maximum scroll position property
1135     Property::Index propertyMaxScrollPosition = self.RegisterProperty(SCROLL_BAR_POSITION_MAX, (layoutSize - controlSize));
1136     // Register the scroll content size property
1137     Property::Index propertyScrollContentSize = self.RegisterProperty(SCROLL_BAR_CONTENT_SIZE, layoutSize);
1138
1139     mScrollBar.SetScrollPropertySource(self, propertyScrollPosition, propertyMinScrollPosition, propertyMaxScrollPosition, propertyScrollContentSize);
1140
1141     // Set style name of ScrollBar for styling
1142     mScrollBar.SetStyleName("TextEditorScrollBar");
1143     Toolkit::Control scrollIndicator = Toolkit::Control::DownCast(mScrollBar.GetScrollIndicator());
1144     if(scrollIndicator)
1145     {
1146       // Set style name of ScrollBarIndicator for styling
1147       scrollIndicator.SetStyleName("TextEditorScrollBarIndicator");
1148     }
1149
1150     self.Add(mScrollBar);
1151   }
1152   else
1153   {
1154     Property::Index propertyScrollPosition    = self.GetPropertyIndex(SCROLL_BAR_POSITION);
1155     Property::Index propertyMaxScrollPosition = self.GetPropertyIndex(SCROLL_BAR_POSITION_MAX);
1156     Property::Index propertyScrollContentSize = self.GetPropertyIndex(SCROLL_BAR_CONTENT_SIZE);
1157
1158     self.SetProperty(propertyScrollPosition, scrollPosition);
1159     self.SetProperty(propertyMaxScrollPosition, (layoutSize - controlSize));
1160     self.SetProperty(propertyScrollContentSize, layoutSize);
1161   }
1162
1163   // If scrolling is not started, start scrolling and emit ScrollStateChangedSignal
1164   if(!mScrollStarted)
1165   {
1166     mScrollStarted = true;
1167     Dali::Toolkit::TextEditor handle(GetOwner());
1168     mScrollStateChangedSignal.Emit(handle, Toolkit::TextEditor::Scroll::STARTED);
1169   }
1170
1171   Actor indicator = mScrollBar.GetScrollIndicator();
1172   if(mAnimation)
1173   {
1174     mAnimation.Stop(); // Cancel any animation
1175   }
1176   else
1177   {
1178     mAnimation = Animation::New(mAnimationPeriod.durationSeconds);
1179   }
1180   indicator.SetProperty(Actor::Property::OPACITY, 1.0f);
1181   mAnimation.AnimateTo(Property(indicator, Actor::Property::COLOR_ALPHA), 0.0f, AlphaFunction::EASE_IN, mAnimationPeriod);
1182   mAnimation.Play();
1183   mAnimation.FinishedSignal().Connect(this, &TextEditor::OnScrollIndicatorAnimationFinished);
1184 }
1185
1186 void TextEditor::OnScrollIndicatorAnimationFinished(Animation& animation)
1187 {
1188   // If animation is successfully ended, then emit ScrollStateChangedSignal
1189   if(animation.GetCurrentProgress() == 0.0f)
1190   {
1191     mScrollStarted = false;
1192     Dali::Toolkit::TextEditor handle(GetOwner());
1193     mScrollStateChangedSignal.Emit(handle, Toolkit::TextEditor::Scroll::FINISHED);
1194   }
1195 }
1196
1197 void TextEditor::OnSceneConnect(Dali::Actor actor)
1198 {
1199   if(mHasBeenStaged)
1200   {
1201     RenderText(static_cast<Text::Controller::UpdateTextType>(Text::Controller::MODEL_UPDATED | Text::Controller::DECORATOR_UPDATED));
1202   }
1203   else
1204   {
1205     mHasBeenStaged = true;
1206   }
1207 }
1208
1209 InputMethodContext::CallbackData TextEditor::OnInputMethodContextEvent(Dali::InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
1210 {
1211   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::OnInputMethodContextEvent %p eventName %d\n", mController.Get(), inputMethodContextEvent.eventName);
1212   return mController->OnInputMethodContextEvent(inputMethodContext, inputMethodContextEvent);
1213 }
1214
1215 void TextEditor::GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType)
1216 {
1217   if(mDecorator)
1218   {
1219     Property::Map map;
1220     map[TextEditor::PropertyHandler::IMAGE_MAP_FILENAME_STRING] = mDecorator->GetHandleImage(handleType, handleImageType);
1221
1222     value = map;
1223   }
1224 }
1225
1226 void TextEditor::OnClipboardTextSelected(ClipboardEventNotifier& clipboard)
1227 {
1228   mController->PasteClipboardItemEvent();
1229 }
1230
1231 void TextEditor::KeyboardStatusChanged(bool keyboardShown)
1232 {
1233   DALI_LOG_INFO(gTextEditorLogFilter, Debug::Verbose, "TextEditor::KeyboardStatusChanged %p keyboardShown %d\n", mController.Get(), keyboardShown);
1234
1235   // Just hide the grab handle when keyboard is hidden.
1236   if(!keyboardShown)
1237   {
1238     mController->KeyboardFocusLostEvent();
1239   }
1240   else
1241   {
1242     mController->KeyboardFocusGainEvent(); // Initially called by OnKeyInputFocusGained
1243   }
1244 }
1245
1246 void TextEditor::OnSceneConnection(int depth)
1247 {
1248   // Sets the depth to the visuals inside the text's decorator.
1249   mDecorator->SetTextDepth(depth);
1250
1251   // The depth of the text renderer is set in the RenderText() called from OnRelayout().
1252
1253   // Call the Control::OnSceneConnection() to set the depth of the background.
1254   Control::OnSceneConnection(depth);
1255 }
1256
1257 bool TextEditor::OnTouched(Actor actor, const TouchEvent& touch)
1258 {
1259   return false;
1260 }
1261
1262 void TextEditor::OnIdleSignal()
1263 {
1264   // Emits the change of input style signals.
1265   mController->ProcessInputStyleChangedSignals();
1266
1267   // Set the pointer to null as the callback manager deletes the callback after execute it.
1268   mIdleCallback = NULL;
1269 }
1270
1271 void TextEditor::ApplyScrollPosition()
1272 {
1273   const Vector2& scrollOffset = mController->GetTextModel()->GetScrollPosition();
1274   float          scrollAmount = 0.0f;
1275
1276   if(mScrollAnimationEnabled)
1277   {
1278     scrollAmount = mController->GetScrollAmountByUserInput();
1279   }
1280   if(mTextVerticalScroller)
1281   {
1282     mTextVerticalScroller->CheckStartAnimation(mRenderableActor, scrollOffset.x + mAlignmentOffset, scrollOffset.y - scrollAmount, scrollAmount);
1283   }
1284   else if(Equals(scrollAmount, 0.0f, Math::MACHINE_EPSILON_1))
1285   {
1286     mRenderableActor.SetProperty(Actor::Property::POSITION, Vector2(scrollOffset.x + mAlignmentOffset, scrollOffset.y - scrollAmount));
1287   }
1288   else
1289   {
1290     mTextVerticalScroller = Text::TextVerticalScroller::New();
1291     if(!Equals(mScrollAnimationDuration, 0.0f, Math::MACHINE_EPSILON_1))
1292     {
1293       mTextVerticalScroller->SetDuration(mScrollAnimationDuration);
1294     }
1295     mTextVerticalScroller->CheckStartAnimation(mRenderableActor, scrollOffset.x + mAlignmentOffset, scrollOffset.y - scrollAmount, scrollAmount);
1296   }
1297 }
1298
1299 bool TextEditor::IsEditable() const
1300 {
1301   return mController->IsEditable();
1302 }
1303
1304 void TextEditor::SetEditable(bool editable)
1305 {
1306   mController->SetEditable(editable);
1307   if(mInputMethodContext && !editable)
1308   {
1309     mInputMethodContext.Deactivate();
1310   }
1311 }
1312
1313 void TextEditor::OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type)
1314 {
1315   mController->ChangedLayoutDirection();
1316 }
1317
1318 TextEditor::TextEditor()
1319 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
1320   mAnimationPeriod(0.0f, 0.0f),
1321   mIdleCallback(NULL),
1322   mAlignmentOffset(0.f),
1323   mScrollAnimationDuration(0.f),
1324   mLineSpacing(0.f),
1325   mRenderingBackend(DEFAULT_RENDERING_BACKEND),
1326   mHasBeenStaged(false),
1327   mScrollAnimationEnabled(false),
1328   mScrollBarEnabled(false),
1329   mScrollStarted(false),
1330   mTextChanged(false),
1331   mCursorPositionChanged(false),
1332   mSelectionChanged(false),
1333   mSelectionCleared(false),
1334   mOldPosition(0u),
1335   mOldSelectionStart(0u),
1336   mOldSelectionEnd(0u),
1337   mSelectionStarted(false)
1338 {
1339 }
1340
1341 TextEditor::~TextEditor()
1342 {
1343   UnparentAndReset(mStencil);
1344
1345   if((NULL != mIdleCallback) && Adaptor::IsAvailable())
1346   {
1347     // Removes the callback from the callback manager in case the text-editor is destroyed before the callback is executed.
1348     Adaptor::Get().RemoveIdle(mIdleCallback);
1349   }
1350 }
1351
1352 std::string TextEditor::AccessibleImpl::GetName() const
1353 {
1354   auto self = Toolkit::TextEditor::DownCast(Self());
1355   return self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1356 }
1357
1358 std::string TextEditor::AccessibleImpl::GetText(size_t startOffset, size_t endOffset) const
1359 {
1360   if(endOffset <= startOffset)
1361   {
1362     return {};
1363   }
1364
1365   auto self = Toolkit::TextEditor::DownCast(Self());
1366   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1367
1368   if(startOffset > text.size() || endOffset > text.size())
1369   {
1370     return {};
1371   }
1372
1373   return text.substr(startOffset, endOffset - startOffset);
1374 }
1375
1376 size_t TextEditor::AccessibleImpl::GetCharacterCount() const
1377 {
1378   auto self = Toolkit::TextEditor::DownCast(Self());
1379   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1380
1381   return text.size();
1382 }
1383
1384 size_t TextEditor::AccessibleImpl::GetCursorOffset() const
1385 {
1386   auto slf = Toolkit::TextEditor::DownCast(Self());
1387   return Dali::Toolkit::GetImpl(slf).GetTextController()->GetCursorPosition();
1388 }
1389
1390 bool TextEditor::AccessibleImpl::SetCursorOffset(size_t offset)
1391 {
1392   auto slf = Toolkit::TextEditor::DownCast(Self());
1393   auto txt = slf.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1394   if(offset > txt.size())
1395   {
1396     return false;
1397   }
1398
1399   auto& slfImpl = Dali::Toolkit::GetImpl(slf);
1400   slfImpl.GetTextController()->ResetCursorPosition(offset);
1401   slfImpl.RequestTextRelayout();
1402
1403   return true;
1404 }
1405
1406 Dali::Accessibility::Range TextEditor::AccessibleImpl::GetTextAtOffset(size_t offset, Dali::Accessibility::TextBoundary boundary) const
1407 {
1408   auto self     = Toolkit::TextEditor::DownCast(Self());
1409   auto text     = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1410   auto textSize = text.size();
1411
1412   auto range = Dali::Accessibility::Range{};
1413
1414   switch(boundary)
1415   {
1416     case Dali::Accessibility::TextBoundary::CHARACTER:
1417     {
1418       if(offset < textSize)
1419       {
1420         range.content     = text[offset];
1421         range.startOffset = offset;
1422         range.endOffset   = offset + 1;
1423       }
1424       break;
1425     }
1426     case Dali::Accessibility::TextBoundary::WORD:
1427     case Dali::Accessibility::TextBoundary::LINE:
1428     {
1429       auto textString = text.c_str();
1430       auto breaks     = std::vector<char>(textSize, 0);
1431
1432       if(boundary == Dali::Accessibility::TextBoundary::WORD)
1433       {
1434         Accessibility::Accessible::FindWordSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1435       }
1436       else
1437       {
1438         Accessibility::Accessible::FindLineSeparationsUtf8(reinterpret_cast<const utf8_t*>(textString), textSize, "", breaks.data());
1439       }
1440
1441       auto index   = 0u;
1442       auto counter = 0u;
1443       while(index < textSize && counter <= offset)
1444       {
1445         auto start = index;
1446         if(breaks[index])
1447         {
1448           while(breaks[index])
1449           {
1450             index++;
1451           }
1452           counter++;
1453         }
1454         else
1455         {
1456           if(boundary == Dali::Accessibility::TextBoundary::WORD)
1457           {
1458             index++;
1459           }
1460           if(boundary == Dali::Accessibility::TextBoundary::LINE)
1461           {
1462             counter++;
1463           }
1464         }
1465
1466         if((counter > 0) && ((counter - 1) == offset))
1467         {
1468           range.content     = text.substr(start, index - start + 1);
1469           range.startOffset = start;
1470           range.endOffset   = index + 1;
1471         }
1472
1473         if(boundary == Dali::Accessibility::TextBoundary::LINE)
1474         {
1475           index++;
1476         }
1477       }
1478       break;
1479     }
1480     case Dali::Accessibility::TextBoundary::SENTENCE:
1481     {
1482       /* not supported by default */
1483       break;
1484     }
1485     case Dali::Accessibility::TextBoundary::PARAGRAPH:
1486     {
1487       /* Paragraph is not supported by libunibreak library */
1488       break;
1489     }
1490     default:
1491       break;
1492   }
1493
1494   return range;
1495 }
1496
1497 Dali::Accessibility::Range TextEditor::AccessibleImpl::GetRangeOfSelection(size_t selectionIndex) const
1498 {
1499   // Since DALi supports only one selection indexes higher than 0 are ignored
1500   if(selectionIndex > 0)
1501   {
1502     return {};
1503   }
1504
1505   auto        self       = Toolkit::TextEditor::DownCast(Self());
1506   auto        controller = Dali::Toolkit::GetImpl(self).GetTextController();
1507   std::string value{};
1508   controller->RetrieveSelection(value);
1509   auto indices = controller->GetSelectionIndexes();
1510
1511   return {static_cast<size_t>(indices.first), static_cast<size_t>(indices.second), value};
1512 }
1513
1514 bool TextEditor::AccessibleImpl::RemoveSelection(size_t selectionIndex)
1515 {
1516   // Since DALi supports only one selection indexes higher than 0 are ignored
1517   if(selectionIndex > 0)
1518   {
1519     return false;
1520   }
1521
1522   auto self = Toolkit::TextEditor::DownCast(Self());
1523   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(0, 0);
1524   return true;
1525 }
1526
1527 bool TextEditor::AccessibleImpl::SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset)
1528 {
1529   // Since DALi supports only one selection indexes higher than 0 are ignored
1530   if(selectionIndex > 0)
1531   {
1532     return false;
1533   }
1534
1535   auto self = Toolkit::TextEditor::DownCast(Self());
1536   Dali::Toolkit::GetImpl(self).GetTextController()->SetSelection(startOffset, endOffset);
1537   return true;
1538 }
1539
1540 Rect<> TextEditor::AccessibleImpl::GetRangeExtents(size_t startOffset, size_t endOffset, Accessibility::CoordinateType type)
1541 {
1542   if (endOffset <= startOffset || endOffset <= 0)
1543   {
1544     return {0, 0, 0, 0};
1545   }
1546
1547   auto self = Toolkit::TextEditor::DownCast(Self());
1548   auto rect = Dali::Toolkit::GetImpl(self).GetTextController()->GetTextBoundingRectangle(startOffset, endOffset - 1);
1549
1550   auto componentExtents = this->GetExtents(type);
1551
1552   rect.x += componentExtents.x;
1553   rect.y += componentExtents.y;
1554
1555   return rect;
1556 }
1557
1558 bool TextEditor::AccessibleImpl::CopyText(size_t startPosition, size_t endPosition)
1559 {
1560   if(endPosition <= startPosition)
1561   {
1562     return false;
1563   }
1564
1565   auto self = Toolkit::TextEditor::DownCast(Self());
1566   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1567   Dali::Toolkit::GetImpl(self).GetTextController()->CopyStringToClipboard(text.substr(startPosition, endPosition - startPosition));
1568
1569   return true;
1570 }
1571
1572 bool TextEditor::AccessibleImpl::CutText(size_t startPosition, size_t endPosition)
1573 {
1574   if(endPosition <= startPosition)
1575   {
1576     return false;
1577   }
1578
1579   auto self = Toolkit::TextEditor::DownCast(Self());
1580   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1581   Dali::Toolkit::GetImpl(self).GetTextController()->CopyStringToClipboard(text.substr(startPosition, endPosition - startPosition));
1582
1583   self.SetProperty(Toolkit::TextEditor::Property::TEXT, text.substr(0, startPosition) + text.substr(endPosition));
1584
1585   return true;
1586 }
1587
1588 bool TextEditor::AccessibleImpl::DeleteText(size_t startPosition, size_t endPosition)
1589 {
1590   if(endPosition <= startPosition)
1591   {
1592     return false;
1593   }
1594
1595   auto self = Toolkit::TextEditor::DownCast(Self());
1596   auto text = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1597
1598   self.SetProperty(Toolkit::TextEditor::Property::TEXT, text.substr(0, startPosition) + text.substr(endPosition));
1599
1600   return true;
1601 }
1602
1603 Dali::Accessibility::States TextEditor::AccessibleImpl::CalculateStates()
1604 {
1605   using namespace Dali::Accessibility;
1606
1607   auto states              = DevelControl::ControlAccessible::CalculateStates();
1608   states[State::EDITABLE]  = true;
1609   states[State::FOCUSABLE] = true;
1610
1611   Toolkit::Control focusControl = Toolkit::KeyInputFocusManager::Get().GetCurrentFocusControl();
1612   if(Self() == focusControl)
1613   {
1614     states[State::FOCUSED] = true;
1615   }
1616
1617   return states;
1618 }
1619
1620 bool TextEditor::AccessibleImpl::InsertText(size_t startPosition, std::string text)
1621 {
1622   auto self         = Toolkit::TextEditor::DownCast(Self());
1623   auto insertedText = self.GetProperty(Toolkit::TextEditor::Property::TEXT).Get<std::string>();
1624
1625   insertedText.insert(startPosition, text);
1626
1627   self.SetProperty(Toolkit::TextEditor::Property::TEXT, std::move(insertedText));
1628
1629   return true;
1630 }
1631
1632 bool TextEditor::AccessibleImpl::SetTextContents(std::string newContents)
1633 {
1634   auto self = Toolkit::TextEditor::DownCast(Self());
1635   self.SetProperty(Toolkit::TextEditor::Property::TEXT, std::move(newContents));
1636   return true;
1637 }
1638
1639 int32_t TextEditor::AccessibleImpl::GetLinkCount() const
1640 {
1641   auto self = Toolkit::TextEditor::DownCast(Self());
1642   return Dali::Toolkit::GetImpl(self).mAnchorActors.size();
1643 }
1644
1645 Accessibility::Hyperlink* TextEditor::AccessibleImpl::GetLink(int32_t linkIndex) const
1646 {
1647   if(linkIndex < 0 || linkIndex >= GetLinkCount())
1648   {
1649     return nullptr;
1650   }
1651   auto self        = Toolkit::TextEditor::DownCast(Self());
1652   auto anchorActor = Dali::Toolkit::GetImpl(self).mAnchorActors[linkIndex];
1653   return dynamic_cast<Accessibility::Hyperlink*>(Dali::Accessibility::Accessible::Get(anchorActor));
1654 }
1655
1656 int32_t TextEditor::AccessibleImpl::GetLinkIndex(int32_t characterOffset) const
1657 {
1658   auto self       = Toolkit::TextEditor::DownCast(Self());
1659   auto controller = Dali::Toolkit::GetImpl(self).GetTextController();
1660   return controller->GetAnchorIndex(static_cast<size_t>(characterOffset));
1661 }
1662
1663 } // namespace Internal
1664
1665 } // namespace Toolkit
1666
1667 } // namespace Dali