Merge "Add ApplyCustomFragmentPrefix" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-editor-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
2 #define DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/accessibility.h>
23 #include <dali/devel-api/adaptor-framework/clipboard-event-notifier.h>
24 #include <dali/devel-api/adaptor-framework/input-method-context.h>
25 #include <dali/public-api/animation/animation.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/control-devel.h>
29 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
30 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
32 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
33 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
34 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
35 #include <dali-toolkit/internal/text/text-control-interface.h>
36 #include <dali-toolkit/internal/text/text-controller.h>
37 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
38 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
39 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
40 #include <dali-toolkit/public-api/controls/control-impl.h>
41 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
42
43 namespace Dali
44 {
45 namespace Toolkit
46 {
47 namespace Internal
48 {
49 /**
50  * @brief A control which renders a long text string with styles.
51  */
52 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
53 {
54 public:
55   /**
56    * @copydoc Dali::Toollkit::TextEditor::New()
57    */
58   static Toolkit::TextEditor New();
59
60   // Properties
61
62   /**
63    * @brief Called when a property of an object of this type is set.
64    *
65    * @param[in] object The object whose property is set.
66    * @param[in] index The property index.
67    * @param[in] value The new property value.
68    */
69   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
70
71   /**
72    * @brief Called to retrieve a property of an object of this type.
73    *
74    * @param[in] object The object whose property is to be retrieved.
75    * @param[in] index The property index.
76    * @return The current value of the property.
77    */
78   static Property::Value GetProperty(BaseObject* object, Property::Index index);
79
80   /**
81    * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
82    */
83   InputMethodContext GetInputMethodContext();
84
85   /**
86    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
87    */
88   DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
89
90   /**
91    * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
92    */
93   DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
94
95   /**
96    * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
97    */
98   DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
99
100   /**
101    * Connects a callback function with the object's signals.
102    * @param[in] object The object providing the signal.
103    * @param[in] tracker Used to disconnect the signal.
104    * @param[in] signalName The signal to connect to.
105    * @param[in] functor A newly allocated FunctorDelegate.
106    * @return True if the signal was connected.
107    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
108    */
109   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
110
111   /**
112    * @brief Gets text controller
113    *
114    * @return The text controller
115    */
116   Text::ControllerPtr GetTextController();
117
118   /**
119    * @copydoc TextEditor::TextChangedSignal()
120    */
121   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
122
123   /**
124    * @copydoc TextEditor::TextChangedSignal()
125    */
126   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
127
128   /**
129    * @copydoc TextEditor::ScrollStateChangedSignal()
130    */
131   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
132
133 private: // From Control
134   /**
135    * @copydoc Control::OnInitialize()
136    */
137   void OnInitialize() override;
138
139   /**
140    * @copydoc Control::OnStyleChange()
141    */
142   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
143
144   /**
145    * @copydoc Control::GetNaturalSize()
146    */
147   Vector3 GetNaturalSize() override;
148
149   /**
150    * @copydoc Control::GetHeightForWidth()
151    */
152   float GetHeightForWidth(float width) override;
153
154   /**
155    * @copydoc Control::OnInitialize()
156    */
157   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
158
159   /**
160    * @copydoc Control::OnKeyInputFocusGained()
161    */
162   void OnKeyInputFocusGained() override;
163
164   /**
165    * @copydoc Control::OnKeyInputFocusLost()
166    */
167   void OnKeyInputFocusLost() override;
168
169   /**
170    * @copydoc Control::OnAccessibilityActivated()
171    */
172   bool OnAccessibilityActivated() override;
173
174   /**
175    * @copydoc Control::OnTap()
176    */
177   void OnTap(const TapGesture& tap) override;
178
179   /**
180    * @copydoc Control::OnPan()
181    */
182   void OnPan(const PanGesture& gesture) override;
183
184   /**
185    * @copydoc Control::OnLongPress()
186    */
187   void OnLongPress(const LongPressGesture& gesture) override;
188
189   /**
190    * @copydoc Control::OnSceneConnection()
191    */
192   void OnSceneConnection(int depth) override;
193
194   /**
195    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
196    */
197   bool OnKeyEvent(const KeyEvent& event) override;
198
199   // From ControlInterface
200
201   /**
202    * @copydoc Text::ControlInterface::RequestTextRelayout()
203    */
204   void RequestTextRelayout() override;
205
206   // From EditableControlInterface
207
208   /**
209    * @copydoc Text::EditableControlInterface::TextChanged()
210    */
211   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
212
213   /**
214    * @copydoc Text::EditableControlInterface::TextDeleted()
215    */
216   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
217
218   /**
219    * @copydoc Text::EditableControlInterface::CursorMoved()
220    */
221   void CursorMoved(unsigned int position) override;
222
223   /**
224    * @copydoc Text::EditableControlInterface::TextChanged()
225    */
226   void TextChanged(bool immediate) override;
227
228   /**
229    * @copydoc Text::EditableControlInterface::MaxLengthReached()
230    */
231   void MaxLengthReached() override;
232
233   /**
234    * @copydoc Text::EditableControlInterface::InputStyleChanged()
235    */
236   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
237
238   /**
239    * @copydoc Text::EditableControlInterface::AddDecoration()
240    */
241   void AddDecoration(Actor& actor, bool needsClipping) override;
242
243   /**
244    * @copydoc Text::EditableControlInterface::InputFiltered()
245    */
246   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
247
248   /**
249    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
250    */
251   void GetControlBackgroundColor(Vector4& color) const override;
252
253   // From SelectableControlInterface
254 public:
255   /**
256    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
257    */
258   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
259
260   /**
261    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
262    */
263   Uint32Pair GetTextSelectionRange() const override;
264
265   /**
266    * @copydoc Text::SelectableControlInterface::SelectWholeText()
267    */
268   void SelectWholeText() override;
269
270   /**
271    * @copydoc Text::SelectableControlInterface::SelectNone()
272    */
273   void SelectNone() override;
274
275   /**
276    * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
277    */
278   void ScrollBy(Vector2 Scroll);
279
280   /**
281    * @brief Get Horizontal scroll position of TextEditor.
282    *
283    * @return Horizontal scroll position (in pixels) of TextEditor.
284    */
285   float GetHorizontalScrollPosition();
286
287   /**
288    * @brief Get Vertical scroll position of TextEditor.
289    *
290    * @return Vertical scroll position (in pixels) of TextEditor.
291    */
292   float GetVerticalScrollPosition();
293
294   /**
295    * @copydoc Text::SelectableControlInterface::GetSelectedText()
296    */
297   string GetSelectedText() const override;
298
299   /**
300    * @copydoc Text::EditableControlInterface::IsEditable()
301    */
302   bool IsEditable() const override;
303
304   /**
305    * @copydoc Text::EditableControlInterface::SetEditable()
306    */
307   void SetEditable(bool editable) override;
308
309   // From AnchorControlInterface
310
311   /**
312    * @copydoc Text::AnchorControlInterface::AnchorClicked()
313    */
314   void AnchorClicked(const std::string& href) override;
315
316 private: // Implementation
317   /**
318    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
319    */
320   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
321
322   /**
323    * @brief Callback when Clipboard signals an item should be pasted
324    * @param[in] clipboard handle to Clipboard Event Notifier
325    */
326   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
327
328   /**
329    * @brief Get a Property Map for the image used for the required Handle Image
330    * @param[out] value the returned image property
331    * @param[in] handleType the type of handle
332    * @param[in] handleImageType the type of image for the given handleType
333    */
334   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
335
336   /**
337    * @brief Callback when keyboard is shown/hidden.
338    *
339    * @param[in] keyboardShown True if keyboard is shown.
340    */
341   void KeyboardStatusChanged(bool keyboardShown);
342
343   /**
344    * @brief update scroll bar position
345    *
346    * If text scroll is occurred, create or update scroll bar position
347    */
348   void UpdateScrollBar();
349
350   /**
351    * @brief Callback when TextEditor is touched
352    *
353    * @param[in] actor TextEditor touched
354    * @param[in] touch Touch information
355    */
356   bool OnTouched(Actor actor, const TouchEvent& touch);
357
358   /**
359    * @brief Callbacks called on idle.
360    *
361    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
362    */
363   void OnIdleSignal();
364
365   /**
366    * @brief Emits TextChanged signal.
367    */
368   void EmitTextChangedSignal();
369
370   /**
371    * @brief set RenderActor's position with new scrollPosition
372    *
373    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
374    */
375   void ApplyScrollPosition();
376
377   /**
378    * @brief Callback function for ScrollBar indicator animation finished signal
379    *
380    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
381    */
382   void OnScrollIndicatorAnimationFinished(Animation& animation);
383
384   /**
385   * @brief Callback function for when the layout is changed.
386   * @param[in] actor The actor whose layoutDirection is changed.
387   * @param[in] type  The layoutDirection.
388   */
389   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
390
391   /**
392    * Construct a new TextEditor.
393    */
394   TextEditor();
395
396   /**
397    * A reference counted object may only be deleted by calling Unreference()
398    */
399   virtual ~TextEditor();
400
401   // Undefined copy constructor and assignment operators
402   TextEditor(const TextEditor&);
403   TextEditor& operator=(const TextEditor& rhs);
404
405   /**
406    * @brief Resize actor to the given size.
407    *
408    * @param[in] actor The actor to be resized.
409    * @param[in] size Size to change.
410    */
411   void ResizeActor(Actor& actor, const Vector2& size);
412
413   /**
414    * @brief Render view, create and attach actor(s) to this text editor.
415    */
416   void RenderText(Text::Controller::UpdateTextType updateTextType);
417
418   // Connection needed to re-render text, when a text editor returns to the scene.
419   void OnSceneConnect(Dali::Actor actor);
420
421 private: // Data
422   // Signals
423   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
424   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
425   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
426   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
427   Toolkit::DevelTextEditor::AnchorClickedSignalType    mAnchorClickedSignal;
428   Toolkit::DevelTextEditor::InputFilteredSignalType    mInputFilteredSignal;
429
430   InputMethodContext            mInputMethodContext;
431   Text::ControllerPtr           mController;
432   Text::RendererPtr             mRenderer;
433   Text::DecoratorPtr            mDecorator;
434   Text::TextVerticalScrollerPtr mTextVerticalScroller;
435   Toolkit::Control              mStencil;
436   Toolkit::ScrollBar            mScrollBar;
437   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
438   Dali::TimePeriod              mAnimationPeriod;
439   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
440   Dali::InputMethodOptions      mInputMethodOptions;
441
442   Actor         mRenderableActor;
443   Actor         mActiveLayer;
444   Actor         mBackgroundActor;
445   CallbackBase* mIdleCallback;
446
447   float mAlignmentOffset;
448   float mScrollAnimationDuration;
449   float mLineSpacing;
450   int   mRenderingBackend;
451   bool  mHasBeenStaged : 1;
452   bool  mScrollAnimationEnabled : 1;
453   bool  mScrollBarEnabled : 1;
454   bool  mScrollStarted : 1;
455   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
456
457   /**
458    * @brief This structure is to connect TextEditor with Accessible functions.
459    */
460   struct AccessibleImpl : public DevelControl::AccessibleImpl,
461                           public virtual Dali::Accessibility::Text,
462                           public virtual Dali::Accessibility::EditableText
463   {
464     using DevelControl::AccessibleImpl::AccessibleImpl;
465
466     /**
467      * @copydoc Dali::Accessibility::Accessible::GetName()
468      */
469     std::string GetName() override;
470
471     /**
472      * @copydoc Dali::Accessibility::Text::GetText()
473      */
474     std::string GetText(size_t startOffset, size_t endOffset) override;
475
476     /**
477      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
478      */
479     size_t GetCharacterCount() override;
480
481     /**
482      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
483      */
484     size_t GetCursorOffset() override;
485
486     /**
487      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
488      */
489     bool SetCursorOffset(size_t offset) override;
490
491     /**
492      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
493      */
494     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
495
496     /**
497      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
498      */
499     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
500
501     /**
502      * @copydoc Dali::Accessibility::Text::RemoveSelection()
503      */
504     bool RemoveSelection(size_t selectionIndex) override;
505
506     /**
507      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
508      */
509     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
510
511     /**
512      * @copydoc Dali::Accessibility::EditableText::CopyText()
513      */
514     bool CopyText(size_t startPosition, size_t endPosition) override;
515
516     /**
517      * @copydoc Dali::Accessibility::EditableText::CutText()
518      */
519     bool CutText(size_t startPosition, size_t endPosition) override;
520
521     /**
522      * @copydoc Dali::Accessibility::Accessible::GetStates()
523      */
524     Accessibility::States CalculateStates() override;
525
526     /**
527      * @copydoc Dali::Accessibility::EditableText::InsertText()
528      */
529     bool InsertText(size_t startPosition, std::string text) override;
530
531     /**
532      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
533      */
534     bool SetTextContents(std::string newContents) override;
535
536     /**
537      * @copydoc Dali::Accessibility::EditableText::DeleteText()
538      */
539     bool DeleteText(size_t startPosition, size_t endPosition) override;
540   };
541 };
542
543 } // namespace Internal
544
545 // Helpers for public-api forwarding methods
546
547 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
548 {
549   DALI_ASSERT_ALWAYS(textEditor);
550
551   Dali::RefObject& handle = textEditor.GetImplementation();
552
553   return static_cast<Toolkit::Internal::TextEditor&>(handle);
554 }
555
556 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
557 {
558   DALI_ASSERT_ALWAYS(textEditor);
559
560   const Dali::RefObject& handle = textEditor.GetImplementation();
561
562   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
563 }
564
565 } // namespace Toolkit
566
567 } // namespace Dali
568
569 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H