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