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