Makes the LTR/RTL alignment of text follow the system language by default.
[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    * @copydoc TextEditor::TextChangedSignal()
113    */
114   Toolkit::TextEditor::TextChangedSignalType& TextChangedSignal();
115
116   /**
117    * @copydoc TextEditor::TextChangedSignal()
118    */
119   Toolkit::TextEditor::InputStyleChangedSignalType& InputStyleChangedSignal();
120
121   /**
122    * @copydoc TextEditor::ScrollStateChangedSignal()
123    */
124   Toolkit::TextEditor::ScrollStateChangedSignalType& ScrollStateChangedSignal();
125
126 private: // From Control
127   /**
128    * @copydoc Control::OnInitialize()
129    */
130   void OnInitialize() override;
131
132   /**
133    * @copydoc Control::OnStyleChange()
134    */
135   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
136
137   /**
138    * @copydoc Control::GetNaturalSize()
139    */
140   Vector3 GetNaturalSize() override;
141
142   /**
143    * @copydoc Control::GetHeightForWidth()
144    */
145   float GetHeightForWidth(float width) override;
146
147   /**
148    * @copydoc Control::OnInitialize()
149    */
150   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
151
152   /**
153    * @copydoc Control::OnKeyInputFocusGained()
154    */
155   void OnKeyInputFocusGained() override;
156
157   /**
158    * @copydoc Control::OnKeyInputFocusLost()
159    */
160   void OnKeyInputFocusLost() override;
161
162   /**
163    * @copydoc Control::OnAccessibilityActivated()
164    */
165   bool OnAccessibilityActivated() override;
166
167   /**
168    * @copydoc Control::OnTap()
169    */
170   void OnTap(const TapGesture& tap) override;
171
172   /**
173    * @copydoc Control::OnPan()
174    */
175   void OnPan(const PanGesture& gesture) override;
176
177   /**
178    * @copydoc Control::OnLongPress()
179    */
180   void OnLongPress(const LongPressGesture& gesture) override;
181
182   /**
183    * @copydoc Control::OnSceneConnection()
184    */
185   void OnSceneConnection(int depth) override;
186
187   /**
188    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
189    */
190   bool OnKeyEvent(const KeyEvent& event) override;
191
192   // From ControlInterface
193
194   /**
195    * @copydoc Text::ControlInterface::RequestTextRelayout()
196    */
197   void RequestTextRelayout() override;
198
199   // From EditableControlInterface
200
201   /**
202    * @copydoc Text::EditableControlInterface::TextChanged()
203    */
204   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
205
206   /**
207    * @copydoc Text::EditableControlInterface::TextDeleted()
208    */
209   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
210
211   /**
212    * @copydoc Text::EditableControlInterface::CaretMoved()
213    */
214   void CaretMoved(unsigned int position) override;
215
216   /**
217    * @copydoc Text::EditableControlInterface::TextChanged()
218    */
219   void TextChanged(bool immediate) override;
220
221   /**
222    * @copydoc Text::EditableControlInterface::MaxLengthReached()
223    */
224   void MaxLengthReached() override;
225
226   /**
227    * @copydoc Text::EditableControlInterface::InputStyleChanged()
228    */
229   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
230
231   /**
232    * @copydoc Text::EditableControlInterface::AddDecoration()
233    */
234   void AddDecoration(Actor& actor, bool needsClipping) override;
235
236   /**
237    * @copydoc Text::EditableControlInterface::InputFiltered()
238    */
239   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
240
241   /**
242    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
243    */
244   void GetControlBackgroundColor(Vector4& color) const override;
245
246   // From SelectableControlInterface
247 public:
248   /**
249    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
250    */
251   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
252
253   /**
254    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
255    */
256   Uint32Pair GetTextSelectionRange() const override;
257
258   /**
259    * @copydoc Text::SelectableControlInterface::SelectWholeText()
260    */
261   void SelectWholeText() override;
262
263   /**
264    * @copydoc Text::SelectableControlInterface::SelectNone()
265    */
266   void SelectNone() override;
267
268   /**
269    * @copydoc Dali::Toolkit::DevelTextEditor::ScrollBy()
270    */
271   void ScrollBy(Vector2 Scroll);
272
273   /**
274    * @brief Get Horizontal scroll position of TextEditor.
275    *
276    * @return Horizontal scroll position (in pixels) of TextEditor.
277    */
278   float GetHorizontalScrollPosition();
279
280   /**
281    * @brief Get Vertical scroll position of TextEditor.
282    *
283    * @return Vertical scroll position (in pixels) of TextEditor.
284    */
285   float GetVerticalScrollPosition();
286
287   /**
288    * @copydoc Text::SelectableControlInterface::GetSelectedText()
289    */
290   string GetSelectedText() const override;
291
292   /**
293    * @copydoc Text::EditableControlInterface::IsEditable()
294    */
295   bool IsEditable() const override;
296
297   /**
298    * @copydoc Text::EditableControlInterface::SetEditable()
299    */
300   void SetEditable(bool editable) override;
301
302   // From AnchorControlInterface
303
304   /**
305    * @copydoc Text::AnchorControlInterface::AnchorClicked()
306    */
307   void AnchorClicked(const std::string& href) override;
308
309   Text::ControllerPtr getController();
310
311 private: // Implementation
312   /**
313    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
314    */
315   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
316
317   /**
318    * @brief Callback when Clipboard signals an item should be pasted
319    * @param[in] clipboard handle to Clipboard Event Notifier
320    */
321   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
322
323   /**
324    * @brief Get a Property Map for the image used for the required Handle Image
325    * @param[out] value the returned image property
326    * @param[in] handleType the type of handle
327    * @param[in] handleImageType the type of image for the given handleType
328    */
329   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
330
331   /**
332    * @brief Callback when keyboard is shown/hidden.
333    *
334    * @param[in] keyboardShown True if keyboard is shown.
335    */
336   void KeyboardStatusChanged(bool keyboardShown);
337
338   /**
339    * @brief update scroll bar position
340    *
341    * If text scroll is occurred, create or update scroll bar position
342    */
343   void UpdateScrollBar();
344
345   /**
346    * @brief Callback when TextEditor is touched
347    *
348    * @param[in] actor TextEditor touched
349    * @param[in] touch Touch information
350    */
351   bool OnTouched(Actor actor, const TouchEvent& touch);
352
353   /**
354    * @brief Callbacks called on idle.
355    *
356    * If there are notifications of change of input style on the queue, Toolkit::TextEditor::InputStyleChangedSignal() are emitted.
357    */
358   void OnIdleSignal();
359
360   /**
361    * @brief Emits TextChanged signal.
362    */
363   void EmitTextChangedSignal();
364
365   /**
366    * @brief set RenderActor's position with new scrollPosition
367    *
368    * Apply updated scroll position or start scroll animation if VerticalScrollAnimation is enabled
369    */
370   void ApplyScrollPosition();
371
372   /**
373    * @brief Callback function for ScrollBar indicator animation finished signal
374    *
375    * Emit ScrollBarStateChanged Signal and toggle mScrollStarted flag to false
376    */
377   void OnScrollIndicatorAnimationFinished(Animation& animation);
378
379   /**
380   * @brief Callback function for when the layout is changed.
381   * @param[in] actor The actor whose layoutDirection is changed.
382   * @param[in] type  The layoutDirection.
383   */
384   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
385
386   /**
387    * Construct a new TextEditor.
388    */
389   TextEditor();
390
391   /**
392    * A reference counted object may only be deleted by calling Unreference()
393    */
394   virtual ~TextEditor();
395
396   // Undefined copy constructor and assignment operators
397   TextEditor(const TextEditor&);
398   TextEditor& operator=(const TextEditor& rhs);
399
400   /**
401    * @brief Resize actor to the given size.
402    *
403    * @param[in] actor The actor to be resized.
404    * @param[in] size Size to change.
405    */
406   void ResizeActor(Actor& actor, const Vector2& size);
407
408   /**
409    * @brief Render view, create and attach actor(s) to this text editor.
410    */
411   void RenderText(Text::Controller::UpdateTextType updateTextType);
412
413   // Connection needed to re-render text, when a text editor returns to the scene.
414   void OnSceneConnect(Dali::Actor actor);
415
416 private: // Data
417   // Signals
418   Toolkit::TextEditor::TextChangedSignalType           mTextChangedSignal;
419   Toolkit::TextEditor::InputStyleChangedSignalType     mInputStyleChangedSignal;
420   Toolkit::TextEditor::ScrollStateChangedSignalType    mScrollStateChangedSignal;
421   Toolkit::DevelTextEditor::MaxLengthReachedSignalType mMaxLengthReachedSignal;
422   Toolkit::DevelTextEditor::AnchorClickedSignalType    mAnchorClickedSignal;
423   Toolkit::DevelTextEditor::InputFilteredSignalType    mInputFilteredSignal;
424
425   InputMethodContext            mInputMethodContext;
426   Text::ControllerPtr           mController;
427   Text::RendererPtr             mRenderer;
428   Text::DecoratorPtr            mDecorator;
429   Text::TextVerticalScrollerPtr mTextVerticalScroller;
430   Toolkit::Control              mStencil;
431   Toolkit::ScrollBar            mScrollBar;
432   Dali::Animation               mAnimation; ///< Scroll indicator Show/Hide Animation.
433   Dali::TimePeriod              mAnimationPeriod;
434   std::vector<Actor>            mClippingDecorationActors; ///< Decoration actors which need clipping.
435   Dali::InputMethodOptions      mInputMethodOptions;
436
437   Actor         mRenderableActor;
438   Actor         mActiveLayer;
439   Actor         mBackgroundActor;
440   CallbackBase* mIdleCallback;
441
442   float mAlignmentOffset;
443   float mScrollAnimationDuration;
444   float mLineSpacing;
445   int   mRenderingBackend;
446   bool  mHasBeenStaged : 1;
447   bool  mScrollAnimationEnabled : 1;
448   bool  mScrollBarEnabled : 1;
449   bool  mScrollStarted : 1;
450   bool  mTextChanged : 1; ///< If true, emits TextChangedSignal in next OnRelayout().
451
452   struct AccessibleImpl : public DevelControl::AccessibleImpl,
453                           public virtual Dali::Accessibility::Text,
454                           public virtual Dali::Accessibility::EditableText
455   {
456     using DevelControl::AccessibleImpl::AccessibleImpl;
457
458     std::string           GetName() override;
459     std::string           GetText(size_t startOffset, size_t endOffset) override;
460     size_t                GetCharacterCount() override;
461     size_t                GetCaretOffset() override;
462     bool                  SetCaretOffset(size_t offset) override;
463     Accessibility::Range  GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
464     Accessibility::Range  GetSelection(size_t selectionNum) override;
465     bool                  RemoveSelection(size_t selectionNum) override;
466     bool                  SetSelection(size_t selectionNum, size_t startOffset, size_t endOffset) override;
467     bool                  CopyText(size_t startPosition, size_t endPosition) override;
468     bool                  CutText(size_t startPosition, size_t endPosition) override;
469     Accessibility::States CalculateStates() override;
470     bool                  InsertText(size_t startPosition, std::string text) override;
471     bool                  SetTextContents(std::string newContents) override;
472     bool                  DeleteText(size_t startPosition, size_t endPosition) override;
473   };
474 };
475
476 } // namespace Internal
477
478 // Helpers for public-api forwarding methods
479
480 inline Toolkit::Internal::TextEditor& GetImpl(Toolkit::TextEditor& textEditor)
481 {
482   DALI_ASSERT_ALWAYS(textEditor);
483
484   Dali::RefObject& handle = textEditor.GetImplementation();
485
486   return static_cast<Toolkit::Internal::TextEditor&>(handle);
487 }
488
489 inline const Toolkit::Internal::TextEditor& GetImpl(const Toolkit::TextEditor& textEditor)
490 {
491   DALI_ASSERT_ALWAYS(textEditor);
492
493   const Dali::RefObject& handle = textEditor.GetImplementation();
494
495   return static_cast<const Toolkit::Internal::TextEditor&>(handle);
496 }
497
498 } // namespace Toolkit
499
500 } // namespace Dali
501
502 #endif // DALI_TOOLKIT_INTERNAL_TEXT_EDITOR_H