51a97c7c553c9661deb82975f729b1e945591e0d
[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, 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    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
276    */
277   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
278
279   /**
280    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
281    */
282   Uint32Pair GetTextSelectionRange() const override;
283
284   /**
285    * @copydoc Text::SelectableControlInterface::SelectWholeText()
286    */
287   void SelectWholeText() override;
288
289   /**
290    * @copydoc Text::SelectableControlInterface::SelectNone()
291    */
292   void SelectNone() override;
293
294   /**
295    * @copydoc Text::SelectableControlInterface::SelectText()
296    */
297   void SelectText(const uint32_t start, const uint32_t end) override;
298
299   /**
300    * @copydoc Text::SelectableControlInterface::GetSelectedText()
301    */
302   string GetSelectedText() const override;
303
304   /**
305    * @copydoc Text::EditableControlInterface::IsEditable()
306    */
307   bool IsEditable() const override;
308
309   /**
310    * @copydoc Text::EditableControlInterface::SetEditable()
311    */
312   void SetEditable(bool editable) override;
313
314   /**
315    * @copydoc Dali::EditableControlInterface::CopyText()
316    */
317   string CopyText() override;
318
319   /**
320    * @copydoc Dali::EditableControlInterface::CutText()
321    */
322   string CutText() override;
323
324   /**
325    * @copydoc Text::EditableControlInterface::PasteText()
326    */
327   void PasteText() override;
328
329   // From AnchorControlInterface
330
331   /**
332    * @copydoc Text::AnchorControlInterface::AnchorClicked()
333    */
334   void AnchorClicked(const std::string& href) override;
335
336   /**
337    * @brief Get the rendered size of a specific text range.
338    * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
339    * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
340    *
341    * @param[in] startIndex start index of the text requested to calculate size for.
342    * @param[in] endIndex end index(included) of the text requested to calculate size for.
343    * @return list of sizes of the reuested text.
344    */
345   Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
346
347   /**
348    * @brief Get the top/left rendered position of a specific text range.
349    * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
350    * if a line contains characters with different directions, multiple positions 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 get position to.
353    * @param[in] endIndex end index(included) of the text requested to get position to.
354    * @return list of positions of the requested text.
355    */
356   Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
357
358 private: // Implementation
359   /**
360    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
361    */
362   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
363
364   /**
365    * @brief Callback when Clipboard signals an item should be pasted
366    * @param[in] clipboard handle to Clipboard Event Notifier
367    */
368   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
369
370   /**
371    * @brief Get a Property Map for the image used for the required Handle Image
372    * @param[out] value the returned image property
373    * @param[in] handleType the type of handle
374    * @param[in] handleImageType the type of image for the given handleType
375    */
376   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
377
378   /**
379    * @brief Enable or disable clipping.
380    */
381   void EnableClipping();
382
383   /**
384    * @brief Callback when keyboard is shown/hidden.
385    *
386    * @param[in] keyboardShown True if keyboard is shown.
387    */
388   void KeyboardStatusChanged(bool keyboardShown);
389
390   /**
391    * @brief Callback when TextField is touched
392    *
393    * @param[in] actor TextField touched
394    * @param[in] touch Touch information
395    */
396   bool OnTouched(Actor actor, const TouchEvent& touch);
397
398   /**
399    * @brief Callbacks called on idle.
400    *
401    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
402    */
403   void OnIdleSignal();
404
405   /**
406    * @brief Emits TextChanged signal.
407    */
408   void EmitTextChangedSignal();
409
410   /**
411    * @brief Emits CursorPositionChanged signal.
412    */
413   void EmitCursorPositionChangedSignal();
414
415   /**
416    * @brief Emits SelectionChanged signal.
417    */
418   void EmitSelectionChangedSignal();
419
420   /**
421    * @brief Emits SelectionCleared signal.
422    */
423   void EmitSelectionClearedSignal();
424
425   /**
426    * @brief Emits SelectionStarted signal.
427    */
428   void EmitSelectionStartedSignal();
429
430   /**
431    * @brief Callback function for when the layout is changed.
432    * @param[in] actor The actor whose layoutDirection is changed.
433    * @param[in] type  The layoutDirection.
434    */
435   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
436
437   /**
438    * Construct a new TextField.
439    */
440   TextField();
441
442   /**
443    * A reference counted object may only be deleted by calling Unreference()
444    */
445   virtual ~TextField();
446
447   // Undefined copy constructor and assignment operators
448   TextField(const TextField&);
449   TextField& operator=(const TextField& rhs);
450
451   /**
452    * @brief Resize actor to the given size.
453    *
454    * @param[in] actor The actor to be resized.
455    * @param[in] size Size to change.
456    */
457   void ResizeActor(Actor& actor, const Vector2& size);
458
459   /**
460    * @brief Render view, create and attach actor(s) to this Text Field.
461    */
462   void RenderText(Text::Controller::UpdateTextType updateTextType);
463
464   // Connection needed to re-render text, when a Text Field returns to the scene.
465   void OnSceneConnect(Dali::Actor actor);
466
467   // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
468   void OnAccessibilityStatusChanged();
469
470 private: // Data
471   // Signals
472   Toolkit::TextField::TextChangedSignalType                mTextChangedSignal;
473   Toolkit::TextField::MaxLengthReachedSignalType           mMaxLengthReachedSignal;
474   Toolkit::TextField::InputStyleChangedSignalType          mInputStyleChangedSignal;
475   Toolkit::DevelTextField::AnchorClickedSignalType         mAnchorClickedSignal;
476   Toolkit::DevelTextField::InputFilteredSignalType         mInputFilteredSignal;
477   Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
478   Toolkit::DevelTextField::SelectionChangedSignalType      mSelectionChangedSignal;
479   Toolkit::DevelTextField::SelectionClearedSignalType      mSelectionClearedSignal;
480   Toolkit::DevelTextField::SelectionStartedSignalType      mSelectionStartedSignal;
481
482   InputMethodContext               mInputMethodContext;
483   Text::ControllerPtr              mController;
484   Text::RendererPtr                mRenderer;
485   Text::DecoratorPtr               mDecorator;
486   Toolkit::Control                 mStencil;                  ///< For EXCEED_POLICY_CLIP
487   std::vector<Actor>               mClippingDecorationActors; ///< Decoration actors which need clipping.
488   std::vector<Toolkit::TextAnchor> mAnchorActors;
489   Dali::InputMethodOptions         mInputMethodOptions;
490
491   Actor         mRenderableActor;
492   Actor         mActiveLayer;
493   Actor         mBackgroundActor;
494   CallbackBase* mIdleCallback;
495
496   float mAlignmentOffset;
497   int   mRenderingBackend;
498   int   mExceedPolicy;
499   bool  mHasBeenStaged : 1;
500   bool  mTextChanged : 1;           ///< If true, emits TextChangedSignal in next OnRelayout().
501   bool  mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
502   bool  mSelectionChanged : 1;      ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
503   bool  mSelectionCleared : 1;      ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
504
505   //args for cursor position changed event
506   unsigned int mOldPosition;
507
508   //args for selection changed event
509   uint32_t mOldSelectionStart;
510   uint32_t mOldSelectionEnd;
511
512   bool mSelectionStarted : 1; ///< If true, emits SelectionStartedSignal at the end of OnRelayout().
513
514 protected:
515   struct PropertyHandler;
516
517   /**
518    * @brief This structure is to connect TextField with Accessible functions.
519    */
520   struct AccessibleImpl : public DevelControl::ControlAccessible,
521                           public virtual Dali::Accessibility::EditableText,
522                           public virtual Dali::Accessibility::Hypertext
523   {
524     using DevelControl::ControlAccessible::ControlAccessible;
525
526     /**
527      * @copydoc Dali::Accessibility::Accessible::GetName()
528      */
529     std::string GetName() const override;
530
531     /**
532      * @copydoc Dali::Accessibility::Text::GetText()
533      */
534     std::string GetText(size_t startOffset, size_t endOffset) const override;
535
536     /**
537      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
538      */
539     size_t GetCharacterCount() const override;
540
541     /**
542      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
543      */
544     size_t GetCursorOffset() const override;
545
546     /**
547      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
548      */
549     bool SetCursorOffset(size_t offset) override;
550
551     /**
552      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
553      */
554     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) const override;
555
556     /**
557      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
558      */
559     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) const override;
560
561     /**
562      * @copydoc Dali::Accessibility::Text::RemoveSelection()
563      */
564     bool RemoveSelection(size_t selectionIndex) override;
565
566     /**
567      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
568      */
569     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
570
571     /**
572      * @copydoc Dali::Accessibility::EditableText::CopyText()
573      */
574     bool CopyText(size_t startPosition, size_t endPosition) override;
575
576     /**
577      * @copydoc Dali::Accessibility::EditableText::CutText()
578      */
579     bool CutText(size_t startPosition, size_t endPosition) override;
580
581     /**
582      * @copydoc Dali::Accessibility::Accessible::GetStates()
583      */
584     Accessibility::States CalculateStates() override;
585
586     /**
587      * @copydoc Dali::Accessibility::EditableText::InsertText()
588      */
589     bool InsertText(size_t startPosition, std::string text) override;
590
591     /**
592      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
593      */
594     bool SetTextContents(std::string newContents) override;
595
596     /**
597      * @copydoc Dali::Accessibility::EditableText::DeleteText()
598      */
599     bool DeleteText(size_t startPosition, size_t endPosition) override;
600
601     /**
602      * @copydoc Dali::Accessibility::Hypertext::GetLink()
603      */
604     Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
605
606     /**
607      * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
608      */
609     int32_t GetLinkIndex(int32_t characterOffset) const override;
610
611     /**
612      * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
613      */
614     int32_t GetLinkCount() const override;
615   };
616 };
617
618 } // namespace Internal
619
620 // Helpers for public-api forwarding methods
621
622 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
623 {
624   DALI_ASSERT_ALWAYS(textField);
625
626   Dali::RefObject& handle = textField.GetImplementation();
627
628   return static_cast<Toolkit::Internal::TextField&>(handle);
629 }
630
631 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
632 {
633   DALI_ASSERT_ALWAYS(textField);
634
635   const Dali::RefObject& handle = textField.GetImplementation();
636
637   return static_cast<const Toolkit::Internal::TextField&>(handle);
638 }
639
640 } // namespace Toolkit
641
642 } // namespace Dali
643
644 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H