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