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