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