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