[dali_2.1.0] Merge branch '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
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 private: // Implementation
329   /**
330    * @copydoc Dali::Toolkit::Text::Controller::(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent)
331    */
332   InputMethodContext::CallbackData OnInputMethodContextEvent(InputMethodContext& inputMethodContext, const InputMethodContext::EventData& inputMethodContextEvent);
333
334   /**
335    * @brief Callback when Clipboard signals an item should be pasted
336    * @param[in] clipboard handle to Clipboard Event Notifier
337    */
338   void OnClipboardTextSelected(ClipboardEventNotifier& clipboard);
339
340   /**
341    * @brief Get a Property Map for the image used for the required Handle Image
342    * @param[out] value the returned image property
343    * @param[in] handleType the type of handle
344    * @param[in] handleImageType the type of image for the given handleType
345    */
346   void GetHandleImagePropertyValue(Property::Value& value, Text::HandleType handleType, Text::HandleImageType handleImageType);
347
348   /**
349    * @brief Enable or disable clipping.
350    */
351   void EnableClipping();
352
353   /**
354    * @brief Callback when keyboard is shown/hidden.
355    *
356    * @param[in] keyboardShown True if keyboard is shown.
357    */
358   void KeyboardStatusChanged(bool keyboardShown);
359
360   /**
361    * @brief Callback when TextField is touched
362    *
363    * @param[in] actor TextField touched
364    * @param[in] touch Touch information
365    */
366   bool OnTouched(Actor actor, const TouchEvent& touch);
367
368   /**
369    * @brief Callbacks called on idle.
370    *
371    * If there are notifications of change of input style on the queue, Toolkit::TextField::InputStyleChangedSignal() are emitted.
372    */
373   void OnIdleSignal();
374
375   /**
376    * @brief Emits TextChanged signal.
377    */
378   void EmitTextChangedSignal();
379
380   /**
381    * @brief Emits CursorPositionChanged signal.
382    */
383   void EmitCursorPositionChangedSignal();
384
385   /**
386    * @brief Emits SelectionChanged signal.
387    */
388   void EmitSelectionChangedSignal();
389
390   /**
391    * @brief Emits SelectionCleared signal.
392    */
393   void EmitSelectionClearedSignal();
394
395   /**
396    * @brief Callback function for when the layout is changed.
397    * @param[in] actor The actor whose layoutDirection is changed.
398    * @param[in] type  The layoutDirection.
399    */
400   void OnLayoutDirectionChanged(Actor actor, LayoutDirection::Type type);
401
402   /**
403    * Construct a new TextField.
404    */
405   TextField();
406
407   /**
408    * A reference counted object may only be deleted by calling Unreference()
409    */
410   virtual ~TextField();
411
412   // Undefined copy constructor and assignment operators
413   TextField(const TextField&);
414   TextField& operator=(const TextField& rhs);
415
416   /**
417    * @brief Resize actor to the given size.
418    *
419    * @param[in] actor The actor to be resized.
420    * @param[in] size Size to change.
421    */
422   void ResizeActor(Actor& actor, const Vector2& size);
423
424   /**
425    * @brief Render view, create and attach actor(s) to this Text Field.
426    */
427   void RenderText(Text::Controller::UpdateTextType updateTextType);
428
429   // Connection needed to re-render text, when a Text Field returns to the scene.
430   void OnSceneConnect(Dali::Actor actor);
431
432   // Needed to synchronize TextAnchor actors with Anchor objects in text's logical model
433   void OnAccessibilityStatusChanged();
434
435 private: // Data
436   // Signals
437   Toolkit::TextField::TextChangedSignalType                mTextChangedSignal;
438   Toolkit::TextField::MaxLengthReachedSignalType           mMaxLengthReachedSignal;
439   Toolkit::TextField::InputStyleChangedSignalType          mInputStyleChangedSignal;
440   Toolkit::DevelTextField::AnchorClickedSignalType         mAnchorClickedSignal;
441   Toolkit::DevelTextField::InputFilteredSignalType         mInputFilteredSignal;
442   Toolkit::DevelTextField::CursorPositionChangedSignalType mCursorPositionChangedSignal;
443   Toolkit::DevelTextField::SelectionChangedSignalType      mSelectionChangedSignal;
444   Toolkit::DevelTextField::SelectionClearedSignalType      mSelectionClearedSignal;
445
446   InputMethodContext               mInputMethodContext;
447   Text::ControllerPtr              mController;
448   Text::RendererPtr                mRenderer;
449   Text::DecoratorPtr               mDecorator;
450   Toolkit::Control                 mStencil;                  ///< For EXCEED_POLICY_CLIP
451   std::vector<Actor>               mClippingDecorationActors; ///< Decoration actors which need clipping.
452   std::vector<Toolkit::TextAnchor> mAnchorActors;
453   Dali::InputMethodOptions         mInputMethodOptions;
454
455   Actor         mRenderableActor;
456   Actor         mActiveLayer;
457   Actor         mBackgroundActor;
458   CallbackBase* mIdleCallback;
459
460   float mAlignmentOffset;
461   int   mRenderingBackend;
462   int   mExceedPolicy;
463   bool  mHasBeenStaged : 1;
464   bool  mTextChanged : 1;           ///< If true, emits TextChangedSignal in next OnRelayout().
465   bool  mCursorPositionChanged : 1; ///< If true, emits CursorPositionChangedSignal at the end of OnRelayout().
466   bool  mSelectionChanged : 1;      ///< If true, emits SelectionChangedSignal at the end of OnRelayout().
467   bool  mSelectionCleared : 1;      ///< If true, emits SelectionClearedSignal at the end of OnRelayout().
468
469   //args for cursor position changed event
470   unsigned int mOldPosition;
471
472   //args for selection changed event
473   uint32_t mOldSelectionStart;
474   uint32_t mOldSelectionEnd;
475
476 protected:
477   struct PropertyHandler;
478
479   /**
480    * @brief This structure is to connect TextField with Accessible functions.
481    */
482   struct AccessibleImpl : public DevelControl::AccessibleImpl,
483                           public virtual Dali::Accessibility::Text,
484                           public virtual Dali::Accessibility::EditableText,
485                           public virtual Dali::Accessibility::Hypertext
486   {
487     using DevelControl::AccessibleImpl::AccessibleImpl;
488
489     /**
490      * @copydoc Dali::Accessibility::Accessible::GetName()
491      */
492     std::string GetName() override;
493
494     /**
495      * @copydoc Dali::Accessibility::Text::GetText()
496      */
497     std::string GetText(size_t startOffset, size_t endOffset) override;
498
499     /**
500      * @copydoc Dali::Accessibility::Text::GetCharacterCount()
501      */
502     size_t GetCharacterCount() override;
503
504     /**
505      * @copydoc Dali::Accessibility::Text::GetCursorOffset()
506      */
507     size_t GetCursorOffset() override;
508
509     /**
510      * @copydoc Dali::Accessibility::Text::SetCursorOffset()
511      */
512     bool SetCursorOffset(size_t offset) override;
513
514     /**
515      * @copydoc Dali::Accessibility::Text::GetTextAtOffset()
516      */
517     Accessibility::Range GetTextAtOffset(size_t offset, Accessibility::TextBoundary boundary) override;
518
519     /**
520      * @copydoc Dali::Accessibility::Text::GetRangeOfSelection()
521      */
522     Accessibility::Range GetRangeOfSelection(size_t selectionIndex) override;
523
524     /**
525      * @copydoc Dali::Accessibility::Text::RemoveSelection()
526      */
527     bool RemoveSelection(size_t selectionIndex) override;
528
529     /**
530      * @copydoc Dali::Accessibility::Text::SetRangeOfSelection()
531      */
532     bool SetRangeOfSelection(size_t selectionIndex, size_t startOffset, size_t endOffset) override;
533
534     /**
535      * @copydoc Dali::Accessibility::EditableText::CopyText()
536      */
537     bool CopyText(size_t startPosition, size_t endPosition) override;
538
539     /**
540      * @copydoc Dali::Accessibility::EditableText::CutText()
541      */
542     bool CutText(size_t startPosition, size_t endPosition) override;
543
544     /**
545      * @copydoc Dali::Accessibility::Accessible::GetStates()
546      */
547     Accessibility::States CalculateStates() override;
548
549     /**
550      * @copydoc Dali::Accessibility::EditableText::InsertText()
551      */
552     bool InsertText(size_t startPosition, std::string text) override;
553
554     /**
555      * @copydoc Dali::Accessibility::EditableText::SetTextContents()
556      */
557     bool SetTextContents(std::string newContents) override;
558
559     /**
560      * @copydoc Dali::Accessibility::EditableText::DeleteText()
561      */
562     bool DeleteText(size_t startPosition, size_t endPosition) override;
563
564     /**
565      * @copydoc Dali::Accessibility::Hypertext::GetLink()
566      */
567     Accessibility::Hyperlink* GetLink(int32_t linkIndex) const override;
568
569     /**
570      * @copydoc Dali::Accessibility::Hypertext::GetLinkIndex()
571      */
572     int32_t GetLinkIndex(int32_t characterOffset) const override;
573
574     /**
575      * @copydoc Dali::Accessibility::Hypertext::GetLinkCount()
576      */
577     int32_t GetLinkCount() const override;
578   };
579 };
580
581 } // namespace Internal
582
583 // Helpers for public-api forwarding methods
584
585 inline Toolkit::Internal::TextField& GetImpl(Toolkit::TextField& textField)
586 {
587   DALI_ASSERT_ALWAYS(textField);
588
589   Dali::RefObject& handle = textField.GetImplementation();
590
591   return static_cast<Toolkit::Internal::TextField&>(handle);
592 }
593
594 inline const Toolkit::Internal::TextField& GetImpl(const Toolkit::TextField& textField)
595 {
596   DALI_ASSERT_ALWAYS(textField);
597
598   const Dali::RefObject& handle = textField.GetImplementation();
599
600   return static_cast<const Toolkit::Internal::TextField&>(handle);
601 }
602
603 } // namespace Toolkit
604
605 } // namespace Dali
606
607 #endif // DALI_TOOLKIT_INTERNAL_TEXT_FIELD_H