Merge "Set focusable to true when touched in default." into 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) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/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 private: // From Control
143   /**
144    * @copydoc Control::OnInitialize()
145    */
146   void OnInitialize() override;
147
148   /**
149    * @copydoc Control::OnStyleChange()
150    */
151   void OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change) override;
152
153   /**
154    * @copydoc Control::GetNaturalSize()
155    */
156   Vector3 GetNaturalSize() override;
157
158   /**
159    * @copydoc Control::GetHeightForWidth()
160    */
161   float GetHeightForWidth(float width) override;
162
163   /**
164    * @copydoc Control::OnInitialize()
165    */
166   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
167
168   /**
169    * @copydoc Control::OnKeyInputFocusGained()
170    */
171   void OnKeyInputFocusGained() override;
172
173   /**
174    * @copydoc Control::OnKeyInputFocusLost()
175    */
176   void OnKeyInputFocusLost() override;
177
178   /**
179    * @copydoc Control::OnAccessibilityActivated()
180    */
181   bool OnAccessibilityActivated() override;
182
183   /**
184    * @copydoc Control::OnTap()
185    */
186   void OnTap(const TapGesture& tap) override;
187
188   /**
189    * @copydoc Control::OnPan()
190    */
191   void OnPan(const PanGesture& gesture) override;
192
193   /**
194    * @copydoc Control::OnLongPress()
195    */
196   void OnLongPress(const LongPressGesture& gesture) override;
197
198   /**
199    * @copydoc Control::OnSceneConnection()
200    */
201   void OnSceneConnection(int depth) override;
202
203   /**
204    * @copydoc Dali::CustomActorImpl::OnKeyEvent(const KeyEvent&)
205    */
206   bool OnKeyEvent(const KeyEvent& event) override;
207
208   // From ControlInterface
209
210   /**
211    * @copydoc Text::ControlInterface::RequestTextRelayout()
212    */
213   void RequestTextRelayout() override;
214
215   // From EditableControlInterface
216
217   /**
218    * @copydoc Text::EditableControlInterface::TextChanged()
219    */
220   void TextInserted(unsigned int position, unsigned int length, const std::string& content) override;
221
222   /**
223    * @copydoc Text::EditableControlInterface::TextDeleted()
224    */
225   void TextDeleted(unsigned int position, unsigned int length, const std::string& content) override;
226
227   /**
228    * @copydoc Text::EditableControlInterface::CursorPositionChanged()
229    */
230   void CursorPositionChanged(unsigned int oldPosition, unsigned int newPosition) override;
231
232   /**
233    * @copydoc Text::EditableControlInterface::TextChanged()
234    */
235   void TextChanged(bool immediate) override;
236
237   /**
238    * @copydoc Text::EditableControlInterface::MaxLengthReached()
239    */
240   void MaxLengthReached() override;
241
242   /**
243    * @copydoc Text::EditableControlInterface::InputStyleChanged()
244    */
245   void InputStyleChanged(Text::InputStyle::Mask inputStyleMask) override;
246
247   /**
248    * @copydoc Text::SelectableControlInterface::SelectionChanged()
249    */
250   void SelectionChanged(uint32_t oldStart, uint32_t oldEnd, uint32_t newStart, uint32_t newEnd) override;
251
252   /**
253    * @copydoc Text::EditableControlInterface::AddDecoration()
254    */
255   void AddDecoration(Actor& actor, bool needsClipping) override;
256
257   /**
258    * @copydoc Text::EditableControlInterface::InputFiltered()
259    */
260   void InputFiltered(Toolkit::InputFilter::Property::Type type) override;
261
262   /**
263    * @copydoc Text::EditableControlInterface::GetControlBackgroundColor()
264    */
265   void GetControlBackgroundColor(Vector4& color) const override;
266
267   // From SelectableControlInterface
268 public:
269   /**
270    * @copydoc Text::SelectableControlInterface::SetTextSelectionRange()
271    */
272   void SetTextSelectionRange(const uint32_t* start, const uint32_t* end) override;
273
274   /**
275    * @copydoc Text::SelectableControlInterface::GetTextSelectionRange()
276    */
277   Uint32Pair GetTextSelectionRange() const override;
278
279   /**
280    * @copydoc Text::SelectableControlInterface::SelectWholeText()
281    */
282   void SelectWholeText() override;
283
284   /**
285    * @copydoc Text::SelectableControlInterface::SelectNone()
286    */
287   void SelectNone() override;
288
289   /**
290    * @copydoc Text::SelectableControlInterface::SelectText()
291    */
292   void SelectText(const uint32_t start, const uint32_t end) override;
293
294   /**
295    * @copydoc Text::SelectableControlInterface::GetSelectedText()
296    */
297   string GetSelectedText() const override;
298
299   /**
300    * @copydoc Text::EditableControlInterface::IsEditable()
301    */
302   bool IsEditable() const override;
303
304   /**
305    * @copydoc Text::EditableControlInterface::SetEditable()
306    */
307   void SetEditable(bool editable) override;
308
309   /**
310    * @copydoc Dali::EditableControlInterface::CopyText()
311    */
312   string CopyText() override;
313
314   /**
315    * @copydoc Dali::EditableControlInterface::CutText()
316    */
317   string CutText() override;
318
319   /**
320    * @copydoc Text::EditableControlInterface::PasteText()
321    */
322   void PasteText() override;
323
324   // From AnchorControlInterface
325
326   /**
327    * @copydoc Text::AnchorControlInterface::AnchorClicked()
328    */
329   void AnchorClicked(const std::string& href) override;
330
331   /**
332    * @brief Get the rendered size of a specific text range.
333    * if the requested text is at multilines, multiple sizes will be returned for each text located in a separate line.
334    * if a line contains characters with different directions, multiple sizes will be returned for each block of contiguous characters with the same direction.
335    *
336    * @param[in] startIndex start index of the text requested to calculate size for.
337    * @param[in] endIndex end index(included) of the text requested to calculate size for.
338    * @return list of sizes of the reuested text.
339    */
340   Vector<Vector2> GetTextSize(const uint32_t startIndex, const uint32_t endIndex) const;
341
342   /**
343    * @brief Get the top/left rendered position of a specific text range.
344    * if the requested text is at multilines, multiple positions will be returned for each text located in a separate line.
345    * if a line contains characters with different directions, multiple positions will be returned for each block of contiguous characters with the same direction.
346    *
347    * @param[in] startIndex start index of the text requested to get position to.
348    * @param[in] endIndex end index(included) of the text requested to get position to.
349    * @return list of positions of the requested text.
350    */
351   Vector<Vector2> GetTextPosition(const uint32_t startIndex, const uint32_t endIndex) const;
352
353 private: // Implementation
354   /**
355    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
356    */
357   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
358
359   /**
360    * @brief Callback when Clipboard signals an item should be pasted
361    * @param[in] clipboard handle to Clipboard Event Notifier
362    */
363   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
364
365   /**
366    * @brief Get a Property Map for the image used for the required Handle Image
367    * @param[out] value the returned image property
368    * @param[in] handleType the type of handle
369    * @param[in] handleImageType the type of image for the given handleType
370    */
371   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
372
373   /**
374    * @brief Enable or disable clipping.
375    */
376   void EnableClipping();
377
378   /**
379    * @brief Callback when keyboard is shown/hidden.
380    *
381    * @param[in] keyboardShown True if keyboard is shown.
382    */
383   void KeyboardStatusChanged(bool keyboardShown);
384
385   /**
386    * @brief Callback when TextField is touched
387    *
388    * @param[in] actor TextField touched
389    * @param[in] touch Touch information
390    */
391   bool OnTouched(Actor actor, const TouchEvent& touch);
392
393   /**
394    * @brief Callbacks called on idle.
395    *
396    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
397    */
398   void OnIdleSignal();
399
400   /**
401    * @brief Emits TextChanged signal.
402    */
403   void EmitTextChangedSignal();
404
405   /**
406    * @brief Emits CursorPositionChanged signal.
407    */
408   void EmitCursorPositionChangedSignal();
409
410   /**
411    * @brief Emits SelectionChanged signal.
412    */
413   void EmitSelectionChangedSignal();
414
415   /**
416    * @brief Emits SelectionCleared signal.
417    */
418   void EmitSelectionClearedSignal();
419
420   /**
421    * @brief Callback function for when the layout is changed.
422    * @param[in] actor The actor whose layoutDirection is changed.
423    * @param[in] type  The layoutDirection.
424    */
425   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
426
427   /**
428    * Construct a new TextField.
429    */
430   TextField();
431
432   /**
433    * A reference counted object may only be deleted by calling Unreference()
434    */
435   virtual ~TextField();
436
437   // Undefined copy constructor and assignment operators
438   TextField(const TextField&);
439   TextField& operator=(const TextField& rhs);
440
441   /**
442    * @brief Resize actor to the given size.
443    *
444    * @param[in] actor The actor to be resized.
445    * @param[in] size Size to change.
446    */
447   void ResizeActor(Actor& actor, const Vector2& size);
448
449   /**
450    * @brief Render view, create and attach actor(s) to this Text Field.
451    */
452   void RenderText(Text::Controller::UpdateTextType updateTextType);
453
454   // Connection needed to re-render text, when a Text Field returns to the scene.
455   void OnSceneConnect(Dali::Actor actor);
456
457   // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
458   void OnAccessibilityStatusChanged();
459
460 private: // Data
461   // Signals
462   Toolkit::TextField::TextChangedSignalType                mTextChangedSignal;
463   Toolkit::TextField::MaxLengthReachedSignalType           mMaxLengthReachedSignal;
464   Toolkit::TextField::InputStyleChangedSignalType          mInputStyleChangedSignal;
465   Toolkit::DevelTextField::AnchorClickedSignalType         mAnchorClickedSignal;
466   Toolkit::DevelTextField::InputFilteredSignalType         mInputFilteredSignal;
467   Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
468   Toolkit::DevelTextField::SelectionChangedSignalType      mSelectionChangedSignal;
469   Toolkit::DevelTextField::SelectionClearedSignalType      mSelectionClearedSignal;
470
471   InputMethodContext               mInputMethodContext;
472   Text::ControllerPtr              mController;
473   Text::RendererPtr                mRenderer;
474   Text::DecoratorPtr               mDecorator;
475   Toolkit::Control                 mStencil;                  ///< For EXCEED_POLICY_CLIP
476   std::vector<Actor>               mClippingDecorationActors; ///< Decoration actors which need clipping.
477   std::vector<Toolkit::TextAnchor> mAnchorActors;
478   Dali::InputMethodOptions         mInputMethodOptions;
479
480   Actor         mRenderableActor;
481   Actor         mActiveLayer;
482   Actor         mBackgroundActor;
483   CallbackBase* mIdleCallback;
484
485   float mAlignmentOffset;
486   int   mRenderingBackend;
487   int   mExceedPolicy;
488   bool  mHasBeenStaged : 1;
489   bool  mTextChanged : 1;           ///< If true, emits TextChangedSignal in next OnRelayout().
490   bool  mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
491   bool  mSelectionChanged : 1;      ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
492   bool  mSelectionCleared : 1;      ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
493
494   //args for cursor position changed event
495   unsigned int mOldPosition;
496
497   //args for selection changed event
498   uint32_t mOldSelectionStart;
499   uint32_t mOldSelectionEnd;
500
501 protected:
502   struct PropertyHandler;
503
504   /**
505    * @brief This structure is to connect TextField with Accessible functions.
506    */
507   struct AccessibleImpl : public DevelControl::ControlAccessible,
508                           public virtual Dali::Accessibility::EditableText,
509                           public virtual Dali::Accessibility::Hypertext
510   {
511     using DevelControl::ControlAccessible::ControlAccessible;
512
513     /**
514      * @copydoc Dali::Accessibility::Accessible::GetName()
515      */
516     std::string GetName() const override;
517
518     /**
519      * @copydoc Dali::Accessibility::Text::GetText()
520      */
521     std::string GetText(size_t startOffset, size_t endOffset) const override;
522
523     /**
524      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
525      */
526     size_t GetCharacterCount() const override;
527
528     /**
529      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
530      */
531     size_t GetCursorOffset() const override;
532
533     /**
534      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
535      */
536     bool SetCursorOffset(size_t offset) override;
537
538     /**
539      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
540      */
541     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) const override;
542
543     /**
544      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
545      */
546     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) const override;
547
548     /**
549      * @copydoc Dali::Accessibility::Text::RemoveSelection()
550      */
551     bool RemoveSelection(size_t selectionIndex) override;
552
553     /**
554      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
555      */
556     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
557
558     /**
559      * @copydoc Dali::Accessibility::EditableText::CopyText()
560      */
561     bool CopyText(size_t startPosition, size_t endPosition) override;
562
563     /**
564      * @copydoc Dali::Accessibility::EditableText::CutText()
565      */
566     bool CutText(size_t startPosition, size_t endPosition) override;
567
568     /**
569      * @copydoc Dali::Accessibility::Accessible::GetStates()
570      */
571     Accessibility::States CalculateStates() override;
572
573     /**
574      * @copydoc Dali::Accessibility::EditableText::InsertText()
575      */
576     bool InsertText(size_t startPosition, std::string text) override;
577
578     /**
579      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
580      */
581     bool SetTextContents(std::string newContents) override;
582
583     /**
584      * @copydoc Dali::Accessibility::EditableText::DeleteText()
585      */
586     bool DeleteText(size_t startPosition, size_t endPosition) override;
587
588     /**
589      * @copydoc Dali::Accessibility::Hypertext::GetLink()
590      */
591     Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
592
593     /**
594      * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
595      */
596     int32_t GetLinkIndex(int32_t characterOffset) const override;
597
598     /**
599      * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
600      */
601     int32_t GetLinkCount() const override;
602   };
603 };
604
605 } // namespace Internal
606
607 // Helpers for public-api forwarding methods
608
609 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
610 {
611   DALI_ASSERT_ALWAYS(textField);
612
613   Dali::RefObject& handle = textField.GetImplementation();
614
615   return static_cast<Toolkit::Internal::TextField&>(handle);
616 }
617
618 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
619 {
620   DALI_ASSERT_ALWAYS(textField);
621
622   const Dali::RefObject& handle = textField.GetImplementation();
623
624   return static_cast<const Toolkit::Internal::TextField&>(handle);
625 }
626
627 } // namespace Toolkit
628
629 } // namespace Dali
630
631 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H