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