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