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