fix linespacing calculation in TextLabel
[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) 2022 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/devel-api/atspi-interfaces/editable-text.h>
26 #include <dali/devel-api/atspi-interfaces/hypertext.h>
27 #include <dali/devel-api/atspi-interfaces/text.h>
28 #include <dali/public-api/animation/animation.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/devel-api/controls/control-devel.h>
32 #include <dali-toolkit/devel-api/controls/scroll-bar/scroll-bar.h>
33 #include <dali-toolkit/devel-api/controls/text-controls/text-editor-devel.h>
34 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
35 #include <dali-toolkit/internal/controls/text-controls/common-text-utils.h>
36 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
37 #include <dali-toolkit/internal/text/rendering/text-renderer.h>
38 #include <dali-toolkit/internal/text/text-anchor-control-interface.h>
39 #include <dali-toolkit/internal/text/text-control-interface.h>
40 #include <dali-toolkit/internal/text/text-controller.h>
41 #include <dali-toolkit/internal/text/text-editable-control-interface.h>
42 #include <dali-toolkit/internal/text/text-selectable-control-interface.h>
43 #include <dali-toolkit/internal/text/text-vertical-scroller.h>
44 #include <dali-toolkit/public-api/controls/control-impl.h>
45 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
46
47 namespace Dali
48 {
49 namespace Toolkit
50 {
51 namespace Internal
52 {
53 /**
54  * @brief A control which renders a long text string with styles.
55  */
56 class TextEditor : public Control, public Text::ControlInterface, public Text::EditableControlInterface, public Text::SelectableControlInterface, public Text::AnchorControlInterface
57 {
58 public:
59   /**
60    * @copydoc Dali::Toollkit::TextEditor::New()
61    */
62   static Toolkit::TextEditor New();
63
64   // Properties
65
66   /**
67    * @brief Called when a property of an object of this type is set.
68    *
69    * @param[in] object The object whose property is set.
70    * @param[in] index The property index.
71    * @param[in] value The new property value.
72    */
73   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
74
75   /**
76    * @brief Called to retrieve a property of an object of this type.
77    *
78    * @param[in] object The object whose property is to be retrieved.
79    * @param[in] index The property index.
80    * @return The current value of the property.
81    */
82   static Property::Value GetProperty(BaseObject* object, Property::Index index);
83
84   /**
85    * @copydoc Dali::Toollkit::TextEditor::GetInputMethodContext()
86    */
87   InputMethodContext GetInputMethodContext();
88
89   /**
90    * @copydoc Dali::Toollkit::TextEditor::MaxLengthReachedSignal()
91    */
92   DevelTextEditor::MaxLengthReachedSignalType& MaxLengthReachedSignal();
93
94   /**
95    * @copydoc Dali::Toollkit::TextEditor::AnchorClickedSignal()
96    */
97   DevelTextEditor::AnchorClickedSignalType& AnchorClickedSignal();
98
99   /**
100    * @copydoc Dali::Toollkit::TextEditor::CursorPositionChangedSignal()
101    */
102   DevelTextEditor::CursorPositionChangedSignalType& CursorPositionChangedSignal();
103
104   /**
105    * @copydoc Dali::Toollkit::TextEditor::InputFilteredSignal()
106    */
107   DevelTextEditor::InputFilteredSignalType& InputFilteredSignal();
108
109   /**
110    * @copydoc Dali::Toollkit::TextEditor::SelectionChangedSignal()
111    */
112   DevelTextEditor::SelectionChangedSignalType& SelectionChangedSignal();
113
114   /**
115    * @copydoc Dali::Toollkit::TextEditor::SelectionClearedSignal()
116    */
117   DevelTextEditor::SelectionClearedSignalType& SelectionClearedSignal();
118
119   /**
120    * @copydoc Dali::Toollkit::TextEditor::SelectionStartedSignal()
121    */
122   DevelTextEditor::SelectionStartedSignalType& SelectionStartedSignal();
123
124   /**
125    * Connects a callback function with the object's signals.
126    * @param[in] object The object providing the signal.
127    * @param[in] tracker Used to disconnect the signal.
128    * @param[in] signalName The signal to connect to.
129    * @param[in] functor A newly allocated FunctorDelegate.
130    * @return True if the signal was connected.
131    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
132    */
133   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
134
135   /**
136    * @brief Gets text controller
137    *
138    * @return The text controller
139    */
140   Text::ControllerPtr GetTextController();
141
142   /**
143    * @copydoc TextEditor::TextChangedSignal()
144    */
145   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
146
147   /**
148    * @copydoc TextEditor::TextChangedSignal()
149    */
150   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
151
152   /**
153    * @copydoc TextEditor::ScrollStateChangedSignal()
154    */
155   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
156
157 private: // From Control
158   /**
159    * @copydoc Control::OnInitialize()
160    */
161   void OnInitialize() override;
162
163   /**
164    * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
165    */
166   DevelControl::ControlAccessible* CreateAccessibleObject() override;
167
168   /**
169    * @copydoc Control::OnStyleChange()
170    */
171   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
172
173   /**
174    * @copydoc Control::GetNaturalSize()
175    */
176   Vector3 GetNaturalSize() override;
177
178   /**
179    * @copydoc Control::GetHeightForWidth()
180    */
181   float GetHeightForWidth(float width) override;
182
183   /**
184    * @copydoc Control::OnInitialize()
185    */
186   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
187
188   /**
189    * @copydoc Control::OnKeyInputFocusGained()
190    */
191   void OnKeyInputFocusGained() override;
192
193   /**
194    * @copydoc Control::OnKeyInputFocusLost()
195    */
196   void OnKeyInputFocusLost() override;
197
198   /**
199    * @copydoc Control::OnAccessibilityActivated()
200    */
201   bool OnAccessibilityActivated() override;
202
203   /**
204    * @copydoc Control::OnTap()
205    */
206   void OnTap(const TapGesture& tap) override;
207
208   /**
209    * @copydoc Control::OnPan()
210    */
211   void OnPan(const PanGesture& gesture) override;
212
213   /**
214    * @copydoc Control::OnLongPress()
215    */
216   void OnLongPress(const LongPressGesture& gesture) override;
217
218   /**
219    * @copydoc Control::OnSceneConnection()
220    */
221   void OnSceneConnection(int depth) override;
222
223   /**
224    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
225    */
226   bool OnKeyEvent(const KeyEvent& event) override;
227
228   // From ControlInterface
229
230   /**
231    * @copydoc Text::ControlInterface::RequestTextRelayout()
232    */
233   void RequestTextRelayout() override;
234
235   // From EditableControlInterface
236
237   /**
238    * @copydoc Text::EditableControlInterface::TextChanged()
239    */
240   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
241
242   /**
243    * @copydoc Text::EditableControlInterface::TextDeleted()
244    */
245   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
246
247   /**
248    * @copydoc Text::EditableControlInterface::CursorPositionChanged()
249    */
250   void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
251
252   /**
253    * @copydoc Text::EditableControlInterface::TextChanged()
254    */
255   void TextChanged(bool immediate) override;
256
257   /**
258    * @copydoc Text::EditableControlInterface::MaxLengthReached()
259    */
260   void MaxLengthReached() override;
261
262   /**
263    * @copydoc Text::EditableControlInterface::InputStyleChanged()
264    */
265   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
266
267   /**
268    * @copydoc Text::SelectableControlInterface::SelectionChanged()
269    */
270   void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
271
272   /**
273    * @copydoc Text::EditableControlInterface::AddDecoration()
274    */
275   void AddDecoration(Actor& actor, Toolkit::Text::DecorationType type, bool needsClipping) override;
276
277   /**
278    * @copydoc Text::EditableControlInterface::InputFiltered()
279    */
280   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
281
282   /**
283    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
284    */
285   void GetControlBackgroundColor(Vector4& color) const override;
286
287   // From SelectableControlInterface
288 public:
289   /**
290    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
291    */
292   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
293
294   /**
295    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
296    */
297   Uint32Pair GetTextSelectionRange() const override;
298
299   /**
300    * @copydoc Text::SelectableControlInterface::SelectWholeText()
301    */
302   void SelectWholeText() override;
303
304   /**
305    * @copydoc Text::SelectableControlInterface::SelectNone()
306    */
307   void SelectNone() override;
308
309   /**
310    * @copydoc Text::SelectableControlInterface::SelectText()
311    */
312   void SelectText(const uint32_t start, const uint32_t end) override;
313
314   /**
315    * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
316    */
317   void ScrollBy(Vector2 Scroll);
318
319   /**
320    * @brief Get Horizontal scroll position of TextEditor.
321    *
322    * @return Horizontal scroll position (in pixels) of TextEditor.
323    */
324   float GetHorizontalScrollPosition();
325
326   /**
327    * @brief Get Vertical scroll position of TextEditor.
328    *
329    * @return Vertical scroll position (in pixels) of TextEditor.
330    */
331   float GetVerticalScrollPosition();
332
333   /**
334    * @brief Get the rendered size of a specific text range.
335    * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
336    * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
337    *
338    * @param[in] startIndex start index of the text requested to calculate size for.
339    * @param[in] endIndex end index(included) of the text requested to calculate size for.
340    * @return list of sizes of the reuested text.
341    */
342   Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
343
344   /**
345    * @brief Get the top/left rendered position of a specific text range.
346    * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
347    * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
348    *
349    * @param[in] startIndex start index of the text requested to get position to.
350    * @param[in] endIndex end index(included) of the text requested to get position to.
351    * @return list of positions of the requested text.
352    */
353   Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
354
355   /**
356    * @copydoc Text::SelectableControlInterface::GetSelectedText()
357    */
358   string GetSelectedText() const override;
359
360   /**
361    * @copydoc Text::EditableControlInterface::IsEditable()
362    */
363   bool IsEditable() const override;
364
365   /**
366    * @copydoc Text::EditableControlInterface::SetEditable()
367    */
368   void SetEditable(bool editable) override;
369
370   /**
371    * @copydoc Text::EditableControlInterface::CopyText()
372    */
373   string CopyText() override;
374
375   /**
376    * @copydoc Text::EditableControlInterface::CutText()
377    */
378   string CutText() override;
379
380   /**
381    * @copydoc Text::EditableControlInterface::PasteText()
382    */
383   void PasteText() override;
384
385   // From AnchorControlInterface
386
387   /**
388    * @copydoc Text::AnchorControlInterface::AnchorClicked()
389    */
390   void AnchorClicked(const std::string& href) override;
391
392 private: // Implementation
393   /**
394    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
395    */
396   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
397
398   /**
399    * @brief Callback when Clipboard signals an item should be pasted
400    * @param[in] clipboard handle to Clipboard Event Notifier
401    */
402   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
403
404   /**
405    * @brief Get a Property Map for the image used for the required Handle Image
406    * @param[out] value the returned image property
407    * @param[in] handleType the type of handle
408    * @param[in] handleImageType the type of image for the given handleType
409    */
410   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
411
412   /**
413    * @brief Callback when keyboard is shown/hidden.
414    *
415    * @param[in] keyboardShown True if keyboard is shown.
416    */
417   void KeyboardStatusChanged(bool keyboardShown);
418
419   /**
420    * @brief update scroll bar position
421    *
422    * If text scroll is occurred, create or update scroll bar position
423    */
424   void UpdateScrollBar();
425
426   /**
427    * @brief Callback when TextEditor is touched
428    *
429    * @param[in] actor TextEditor touched
430    * @param[in] touch Touch information
431    */
432   bool OnTouched(Actor actor, const TouchEvent& touch);
433
434   /**
435    * @brief Callbacks called on idle.
436    *
437    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
438    */
439   void OnIdleSignal();
440
441   /**
442    * @brief Emits CursorPositionChanged signal.
443    */
444   void EmitCursorPositionChangedSignal();
445
446   /**
447    * @brief Emits TextChanged signal.
448    */
449   void EmitTextChangedSignal();
450
451   /**
452    * @brief Emits SelectionChanged signal.
453    */
454   void EmitSelectionChangedSignal();
455
456   /**
457    * @brief Emits SelectionCleared signal.
458    */
459   void EmitSelectionClearedSignal();
460
461   /**
462    * @brief Emits SelectionStarted signal.
463    */
464   void EmitSelectionStartedSignal();
465
466   /**
467    * @brief set RenderActor's position with new scrollPosition
468    *
469    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
470    */
471   void ApplyScrollPosition();
472
473   /**
474    * @brief Callback function for ScrollBar indicator animation finished signal
475    *
476    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
477    */
478   void OnScrollIndicatorAnimationFinished(Animation& animation);
479
480   /**
481   * @brief Callback function for when the layout is changed.
482   * @param[in] actor The actor whose layoutDirection is changed.
483   * @param[in] type  The layoutDirection.
484   */
485   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
486
487   /**
488   * @brief Add a layer for active or cursor.
489   * @param[in] layer The actor in which to store the layer.
490   * @param[in] actor The new layer to add.
491   */
492   void AddLayer(Actor& layer, Actor& actor);
493
494   /**
495    * Construct a new TextEditor.
496    */
497   TextEditor();
498
499   /**
500    * A reference counted object may only be deleted by calling Unreference()
501    */
502   virtual ~TextEditor();
503
504   // Undefined copy constructor and assignment operators
505   TextEditor(const TextEditor&);
506   TextEditor& operator=(const TextEditor& rhs);
507
508   /**
509    * @brief Resize actor to the given size.
510    *
511    * @param[in] actor The actor to be resized.
512    * @param[in] size Size to change.
513    */
514   void ResizeActor(Actor& actor, const Vector2& size);
515
516   /**
517    * @brief Render view, create and attach actor(s) to this text editor.
518    */
519   void RenderText(Text::Controller::UpdateTextType updateTextType);
520
521   // Connection needed to re-render text, when a text editor returns to the scene.
522   void OnSceneConnect(Dali::Actor actor);
523
524   // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
525   void OnAccessibilityStatusChanged();
526
527 private: // Data
528   // Signals
529   Toolkit::TextEditor::TextChangedSignalType                mTextChangedSignal;
530   Toolkit::TextEditor::InputStyleChangedSignalType          mInputStyleChangedSignal;
531   Toolkit::TextEditor::ScrollStateChangedSignalType         mScrollStateChangedSignal;
532   Toolkit::DevelTextEditor::MaxLengthReachedSignalType      mMaxLengthReachedSignal;
533   Toolkit::DevelTextEditor::AnchorClickedSignalType         mAnchorClickedSignal;
534   Toolkit::DevelTextEditor::InputFilteredSignalType         mInputFilteredSignal;
535   Toolkit::DevelTextEditor::CursorPositionChangedSignalType mCursorPositionChangedSignal;
536   Toolkit::DevelTextEditor::SelectionChangedSignalType      mSelectionChangedSignal;
537   Toolkit::DevelTextEditor::SelectionClearedSignalType      mSelectionClearedSignal;
538   Toolkit::DevelTextEditor::SelectionStartedSignalType      mSelectionStartedSignal;
539
540   InputMethodContext               mInputMethodContext;
541   Text::ControllerPtr              mController;
542   Text::RendererPtr                mRenderer;
543   Text::DecoratorPtr               mDecorator;
544   Text::TextVerticalScrollerPtr    mTextVerticalScroller;
545   Toolkit::Control                 mStencil;
546   Toolkit::ScrollBar               mScrollBar;
547   Dali::Animation                  mAnimation; ///< Scroll indicator Show/Hide Animation.
548   Dali::TimePeriod                 mAnimationPeriod;
549   std::vector<Actor>               mClippingDecorationActors; ///< Decoration actors which need clipping.
550   std::vector<Toolkit::TextAnchor> mAnchorActors;
551   Dali::InputMethodOptions         mInputMethodOptions;
552
553   Actor         mRenderableActor;
554   Actor         mActiveLayer;
555   Actor         mCursorLayer;
556   Actor         mBackgroundActor;
557   CallbackBase* mIdleCallback;
558
559   float mAlignmentOffset;
560   float mScrollAnimationDuration;
561   float mLineSpacing;
562   int   mRenderingBackend;
563   bool  mHasBeenStaged : 1;
564   bool  mScrollAnimationEnabled : 1;
565   bool  mScrollBarEnabled : 1;
566   bool  mScrollStarted : 1;
567   bool  mTextChanged : 1;           ///< If true, emits TextChangedSignal in next OnRelayout().
568   bool  mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
569   bool  mSelectionChanged : 1;      ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
570   bool  mSelectionCleared : 1;      ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
571
572   //args for cursor PositionChanged event
573   unsigned int mOldPosition;
574
575   //args for selection changed event
576   uint32_t mOldSelectionStart;
577   uint32_t mOldSelectionEnd;
578
579   bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
580
581   struct PropertyHandler;
582
583   /**
584    * @brief This structure is to connect TextEditor with Accessible functions.
585    */
586   class TextEditorAccessible : public EditableTextControlAccessible
587   {
588   public:
589     using EditableTextControlAccessible::EditableTextControlAccessible;
590
591     /**
592      * @copydoc Dali::Accessibility::Accessible::GetName()
593      */
594     std::string GetName() const override;
595
596   protected:
597     /**
598      * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextAnchors()
599      */
600     const std::vector<Toolkit::TextAnchor>& GetTextAnchors() const override;
601
602     /**
603      * @copydoc Dali::Toolkit::Internal::TextControlAccessible::GetTextController()
604      */
605     Toolkit::Text::ControllerPtr GetTextController() const override;
606
607     /**
608      * @copydoc Dali::Toolkit::Internal::EditableTextControlAccessible::RequestTextRelayout()
609      */
610     void RequestTextRelayout() override;
611   };
612 };
613
614 } // namespace Internal
615
616 // Helpers for public-api forwarding methods
617
618 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
619 {
620   DALI_ASSERT_ALWAYS(textEditor);
621
622   Dali::RefObject& handle = textEditor.GetImplementation();
623
624   return static_cast<Toolkit::Internal::TextEditor&>(handle);
625 }
626
627 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
628 {
629   DALI_ASSERT_ALWAYS(textEditor);
630
631   const Dali::RefObject& handle = textEditor.GetImplementation();
632
633   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
634 }
635
636 } // namespace Toolkit
637
638 } // namespace Dali
639
640 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H